1 #include <AvailabilityMacros.h>
2 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER
3 #include </System/Library/Frameworks/System.framework/PrivateHeaders/mach/thread_policy.h>
13 #include <mach/mach.h>
14 #include <mach/mach_error.h>
15 #include <mach/notify.h>
16 #include <servers/bootstrap.h>
17 #include <sys/types.h>
19 #include <sys/signal.h>
21 #define MAX(A, B) ((A) < (B) ? (B) : (A))
25 mach_msg_header_t header
;
26 mach_msg_trailer_t trailer
; // subtract this when sending
27 } ipc_trivial_message
;
30 mach_msg_header_t header
;
32 mach_msg_trailer_t trailer
; // subtract this when sending
36 mach_msg_header_t header
;
38 mach_msg_ool_descriptor_t descriptor
;
39 mach_msg_trailer_t trailer
; // subtract this when sending
40 } ipc_complex_message
;
51 mach_msg_header_t
*req_msg
;
53 mach_msg_header_t
*reply_msg
;
64 static boolean_t verbose
= FALSE
;
65 static boolean_t affinity
= FALSE
;
66 static boolean_t timeshare
= FALSE
;
67 static boolean_t threaded
= FALSE
;
68 static boolean_t oneway
= FALSE
;
69 static boolean_t useset
= FALSE
;
79 char **server_port_name
;
81 void signal_handler(int sig
) {
84 void usage(const char *progname
) {
85 fprintf(stderr
, "usage: %s [options]\n", progname
);
86 fprintf(stderr
, "where options are:\n");
87 fprintf(stderr
, " -affinity\t\tthreads use affinity\n");
88 fprintf(stderr
, " -timeshare\t\tthreads use timeshare\n");
89 fprintf(stderr
, " -threaded\t\tuse (p)threads\n");
90 fprintf(stderr
, " -verbose\t\tbe verbose\n");
91 fprintf(stderr
, " -oneway\t\tdo not request return reply\n");
92 fprintf(stderr
, " -count num\t\tnumber of messages to send\n");
93 fprintf(stderr
, " -type trivial|inline|complex\ttype of messages to send\n");
94 fprintf(stderr
, " -numints num\tnumber of 32-bit ints to send in messages\n");
95 fprintf(stderr
, " -servers num\tnumber of servers threads to run\n");
96 fprintf(stderr
, " -clients num\tnumber of clients per server\n");
97 fprintf(stderr
, " -delay num\t\tmicroseconds to sleep clients between messages\n");
98 fprintf(stderr
, " -work num\t\tmicroseconds of client work\n");
99 fprintf(stderr
, " -pages num\t\tpages of memory touched by client work\n");
100 fprintf(stderr
, " -set num\t\tuse a portset stuffed with num ports in server\n");
101 fprintf(stderr
, "default values are:\n");
102 fprintf(stderr
, " . no affinity\n");
103 fprintf(stderr
, " . not timeshare\n");
104 fprintf(stderr
, " . not verbose\n");
105 fprintf(stderr
, " . not oneway\n");
106 fprintf(stderr
, " . client sends 100000 messages\n");
107 fprintf(stderr
, " . inline message type\n");
108 fprintf(stderr
, " . 64 32-bit integers in inline/complex messages\n");
109 fprintf(stderr
, " . (num_available_processors+1)%%2 servers\n");
110 fprintf(stderr
, " . 4 clients per server\n");
111 fprintf(stderr
, " . no delay\n");
115 void parse_args(int argc
, char *argv
[]) {
116 host_basic_info_data_t info
;
117 mach_msg_type_number_t count
;
118 kern_return_t result
;
120 /* Initialize defaults */
121 msg_type
= msg_type_trivial
;
127 count
= HOST_BASIC_INFO_COUNT
;
128 result
= host_info(mach_host_self(), HOST_BASIC_INFO
,
129 (host_info_t
)&info
, &count
);
130 if (result
== KERN_SUCCESS
&& info
.avail_cpus
> 1)
131 num_servers
= info
.avail_cpus
/ 2;
135 const char *progname
= argv
[0];
138 if (0 == strcmp("-verbose", argv
[0])) {
141 } else if (0 == strcmp("-affinity", argv
[0])) {
144 } else if (0 == strcmp("-timeshare", argv
[0])) {
147 } else if (0 == strcmp("-threaded", argv
[0])) {
150 } else if (0 == strcmp("-oneway", argv
[0])) {
153 } else if (0 == strcmp("-type", argv
[0])) {
156 if (0 == strcmp("trivial", argv
[1])) {
157 msg_type
= msg_type_trivial
;
158 } else if (0 == strcmp("inline", argv
[1])) {
159 msg_type
= msg_type_inline
;
160 } else if (0 == strcmp("complex", argv
[1])) {
161 msg_type
= msg_type_complex
;
164 argc
-= 2; argv
+= 2;
165 } else if (0 == strcmp("-numints", argv
[0])) {
168 num_ints
= strtoul(argv
[1], NULL
, 0);
169 argc
-= 2; argv
+= 2;
170 } else if (0 == strcmp("-count", argv
[0])) {
173 num_msgs
= strtoul(argv
[1], NULL
, 0);
174 argc
-= 2; argv
+= 2;
175 } else if (0 == strcmp("-clients", argv
[0])) {
178 num_clients
= strtoul(argv
[1], NULL
, 0);
179 argc
-= 2; argv
+= 2;
180 } else if (0 == strcmp("-servers", argv
[0])) {
183 num_servers
= strtoul(argv
[1], NULL
, 0);
184 argc
-= 2; argv
+= 2;
185 } else if (0 == strcmp("-delay", argv
[0])) {
188 client_delay
= strtoul(argv
[1], NULL
, 0);
189 argc
-= 2; argv
+= 2;
190 } else if (0 == strcmp("-spin", argv
[0])) {
193 client_spin
= strtoul(argv
[1], NULL
, 0);
194 argc
-= 2; argv
+= 2;
195 } else if (0 == strcmp("-pages", argv
[0])) {
198 client_pages
= strtoul(argv
[1], NULL
, 0);
199 argc
-= 2; argv
+= 2;
200 } else if (0 == strcmp("-set", argv
[0])) {
203 portcount
= strtoul(argv
[1], NULL
, 0);
205 argc
-= 2; argv
+= 2;
212 void setup_server_ports(struct port_args
*ports
)
214 kern_return_t ret
= 0;
219 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
220 sizeof(u_int32_t
) * num_ints
,
221 sizeof(ipc_complex_message
));
222 ports
->reply_size
= sizeof(ipc_trivial_message
) -
223 sizeof(mach_msg_trailer_t
);
224 ports
->req_msg
= malloc(ports
->req_size
);
225 ports
->reply_msg
= malloc(ports
->reply_size
);
228 ret
= mach_port_allocate(mach_task_self(),
229 MACH_PORT_RIGHT_PORT_SET
,
231 if (KERN_SUCCESS
!= ret
) {
232 mach_error("mach_port_allocate(SET): ", ret
);
237 /* stuff the portset with ports */
238 for (i
=0; i
< portcount
; i
++) {
239 ret
= mach_port_allocate(mach_task_self(),
240 MACH_PORT_RIGHT_RECEIVE
,
242 if (KERN_SUCCESS
!= ret
) {
243 mach_error("mach_port_allocate(PORT): ", ret
);
248 ret
= mach_port_move_member(mach_task_self(),
251 if (KERN_SUCCESS
!= ret
) {
252 mach_error("mach_port_move_member(): ", ret
);
258 /* use the last one as the real port */
261 ret
= mach_port_insert_right(mach_task_self(),
264 MACH_MSG_TYPE_MAKE_SEND
);
265 if (KERN_SUCCESS
!= ret
) {
266 mach_error("mach_port_insert_right(): ", ret
);
270 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
271 if (KERN_SUCCESS
!= ret
) {
272 mach_error("task_get_bootstrap_port(): ", ret
);
277 printf("server waiting for IPC messages from client on port '%s'.\n",
278 server_port_name
[ports
->server_num
]);
280 ret
= bootstrap_register(bsport
,
281 server_port_name
[ports
->server_num
],
283 if (KERN_SUCCESS
!= ret
) {
284 mach_error("bootstrap_register(): ", ret
);
289 void setup_client_ports(struct port_args
*ports
)
291 kern_return_t ret
= 0;
293 case msg_type_trivial
:
294 ports
->req_size
= sizeof(ipc_trivial_message
);
296 case msg_type_inline
:
297 ports
->req_size
= sizeof(ipc_inline_message
) +
298 sizeof(u_int32_t
) * num_ints
;
300 case msg_type_complex
:
301 ports
->req_size
= sizeof(ipc_complex_message
);
304 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
305 ports
->reply_size
= sizeof(ipc_trivial_message
);
306 ports
->req_msg
= malloc(ports
->req_size
);
307 ports
->reply_msg
= malloc(ports
->reply_size
);
309 ret
= mach_port_allocate(mach_task_self(),
310 MACH_PORT_RIGHT_RECEIVE
,
312 if (KERN_SUCCESS
!= ret
) {
313 mach_error("mach_port_allocate(): ", ret
);
317 printf("Client sending %d %s IPC messages to port '%s' in %s mode.\n",
318 num_msgs
, (msg_type
== msg_type_inline
) ?
319 "inline" : ((msg_type
== msg_type_complex
) ?
320 "complex" : "trivial"),
321 server_port_name
[ports
->server_num
],
322 (oneway
? "oneway" : "rpc"));
329 thread_setup(int tag
) {
330 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER
332 thread_extended_policy_data_t epolicy
;
333 thread_affinity_policy_data_t policy
;
336 epolicy
.timeshare
= FALSE
;
337 ret
= thread_policy_set(
338 mach_thread_self(), THREAD_EXTENDED_POLICY
,
339 (thread_policy_t
) &epolicy
,
340 THREAD_EXTENDED_POLICY_COUNT
);
341 if (ret
!= KERN_SUCCESS
)
342 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
346 policy
.affinity_tag
= tag
;
347 ret
= thread_policy_set(
348 mach_thread_self(), THREAD_AFFINITY_POLICY
,
349 (thread_policy_t
) &policy
,
350 THREAD_AFFINITY_POLICY_COUNT
);
351 if (ret
!= KERN_SUCCESS
)
352 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
358 server(void *serverarg
)
360 struct port_args args
;
363 int totalmsg
= num_msgs
* num_clients
;
364 mach_port_t recv_port
;
366 args
.server_num
= (int) (long) serverarg
;
367 setup_server_ports(&args
);
369 thread_setup(args
.server_num
+ 1);
371 recv_port
= (useset
) ? args
.set
: args
.port
;
373 for (idx
= 0; idx
< totalmsg
; idx
++) {
375 printf("server awaiting message %d\n", idx
);
376 ret
= mach_msg(args
.req_msg
,
377 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
|MACH_RCV_LARGE
,
381 MACH_MSG_TIMEOUT_NONE
,
383 if (MACH_RCV_INTERRUPTED
== ret
)
385 if (MACH_MSG_SUCCESS
!= ret
) {
387 printf("mach_msg() ret=%d", ret
);
388 mach_error("mach_msg (receive): ", ret
);
392 printf("server received message %d\n", idx
);
393 if (args
.req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
394 ret
= vm_deallocate(mach_task_self(),
395 (vm_address_t
)((ipc_complex_message
*)args
.req_msg
)->descriptor
.address
,
396 ((ipc_complex_message
*)args
.req_msg
)->descriptor
.size
);
399 if (1 == args
.req_msg
->msgh_id
) {
401 printf("server sending reply %d\n", idx
);
402 args
.reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, 0);
403 args
.reply_msg
->msgh_size
= args
.reply_size
;
404 args
.reply_msg
->msgh_remote_port
= args
.req_msg
->msgh_remote_port
;
405 args
.reply_msg
->msgh_local_port
= MACH_PORT_NULL
;
406 args
.reply_msg
->msgh_id
= 2;
407 ret
= mach_msg(args
.reply_msg
,
412 MACH_MSG_TIMEOUT_NONE
,
414 if (MACH_MSG_SUCCESS
!= ret
) {
415 mach_error("mach_msg (send): ", ret
);
423 client_spin_loop(unsigned count
, void (fn
)(void))
429 static long dummy_memory
;
430 static long *client_memory
= &dummy_memory
;
432 client_work_atom(void)
436 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long))
438 client_memory
[i
] = 0;
441 static int calibration_count
= 10000;
442 static int calibration_usec
;
444 calibrate_client_work(void)
447 struct timeval nowtv
;
448 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
449 struct timeval starttv
;
450 struct timeval endtv
;
453 /* Warm-up the stepper first... */
454 gettimeofday(&nowtv
, NULL
);
455 timeradd(&nowtv
, &warmuptv
, &endtv
);
457 client_spin_loop(calibration_count
, client_work_atom
);
458 gettimeofday(&nowtv
, NULL
);
459 } while (timercmp(&nowtv
, &endtv
, < ));
461 /* Now do the calibration */
463 gettimeofday(&starttv
, NULL
);
464 client_spin_loop(calibration_count
, client_work_atom
);
465 gettimeofday(&endtv
, NULL
);
466 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
467 calibration_count
/= 10;
470 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
471 if (endtv
.tv_usec
< starttv
.tv_usec
) {
472 calibration_usec
+= 1000000;
474 if (calibration_usec
< 1000) {
475 calibration_count
*= 10;
478 calibration_count
/= calibration_usec
;
482 printf("calibration_count=%d calibration_usec=%d\n",
483 calibration_count
, calibration_usec
);
492 client_spin_loop(calibration_count
*client_spin
,
497 usleep(client_delay
);
501 void *client(void *threadarg
)
503 struct port_args args
;
505 mach_msg_header_t
*req
, *reply
;
506 mach_port_t bsport
, servport
;
508 long server_num
= (long) threadarg
;
509 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
512 printf("client(%d) started, server port name %s\n",
513 server_num
, server_port_name
[server_num
]);
515 args
.server_num
= server_num
;
516 thread_setup(server_num
+ 1);
518 /* find server port */
519 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
520 if (KERN_SUCCESS
!= ret
) {
521 mach_error("task_get_bootstrap_port(): ", ret
);
524 ret
= bootstrap_look_up(bsport
,
525 server_port_name
[server_num
],
527 if (KERN_SUCCESS
!= ret
) {
528 mach_error("bootstrap_look_up(): ", ret
);
532 setup_client_ports(&args
);
534 /* Allocate and touch memory */
537 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
538 for (i
= 0; i
< client_pages
; i
++)
539 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
542 /* start message loop */
543 for (idx
= 0; idx
< num_msgs
; idx
++) {
545 reply
= args
.reply_msg
;
547 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
548 MACH_MSG_TYPE_MAKE_SEND_ONCE
);
549 req
->msgh_size
= args
.req_size
;
550 req
->msgh_remote_port
= servport
;
551 req
->msgh_local_port
= args
.port
;
552 req
->msgh_id
= oneway
? 0 : 1;
553 if (msg_type
== msg_type_complex
) {
554 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
555 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
556 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
557 ((ipc_complex_message
*)req
)->descriptor
.size
=
558 num_ints
* sizeof(u_int32_t
);
559 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
560 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
561 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
564 printf("client sending message %d\n", idx
);
570 MACH_MSG_TIMEOUT_NONE
,
572 if (MACH_MSG_SUCCESS
!= ret
) {
573 mach_error("mach_msg (send): ", ret
);
574 fprintf(stderr
, "bailing after %u iterations\n", idx
);
580 printf("client awaiting reply %d\n", idx
);
581 reply
->msgh_bits
= 0;
582 reply
->msgh_size
= args
.reply_size
;
583 reply
->msgh_local_port
= args
.port
;
584 ret
= mach_msg(args
.reply_msg
,
585 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
,
589 MACH_MSG_TIMEOUT_NONE
,
591 if (MACH_MSG_SUCCESS
!= ret
) {
592 mach_error("mach_msg (receive): ", ret
);
593 fprintf(stderr
, "bailing after %u iterations\n",
598 printf("client received reply %d\n", idx
);
609 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
) {
612 ret
= pthread_create(
618 err(1, "pthread_create()");
620 printf("created pthread 0x%x\n", thread
->tid
);
622 thread
->pid
= fork();
623 if (thread
->pid
== 0) {
625 printf("calling 0x%x(0x%x)\n", fn
, arg
);
630 printf("forked pid %d\n", thread
->pid
);
635 thread_join(thread_id_t
*thread
) {
639 printf("joining thread 0x%x\n", thread
->tid
);
640 ret
= pthread_join(thread
->tid
, NULL
);
641 if (ret
!= KERN_SUCCESS
)
642 err(1, "pthread_join(0x%x)", thread
->tid
);
646 printf("waiting for pid %d\n", thread
->pid
);
647 waitpid(thread
->pid
, &stat
, 0);
652 wait_for_servers(void)
655 int retry_count
= 10;
656 mach_port_t bsport
, servport
;
659 /* find server port */
660 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
661 if (KERN_SUCCESS
!= ret
) {
662 mach_error("task_get_bootstrap_port(): ", ret
);
666 while (retry_count
-- > 0) {
667 for (i
= 0; i
< num_servers
; i
++) {
668 ret
= bootstrap_look_up(bsport
,
671 if (ret
!= KERN_SUCCESS
) {
675 if (ret
== KERN_SUCCESS
)
677 usleep(100 * 1000); /* 100ms */
679 fprintf(stderr
, "Server(s) failed to register\n");
683 int main(int argc
, char *argv
[])
687 thread_id_t
*client_id
;
688 thread_id_t
*server_id
;
690 signal(SIGINT
, signal_handler
);
691 parse_args(argc
, argv
);
693 calibrate_client_work();
696 * If we're using affinity create an empty namespace now
697 * so this is shared by all our offspring.
702 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
703 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
705 printf("creating %d servers\n", num_servers
);
706 for (i
= 0; i
< num_servers
; i
++) {
707 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
708 /* PORT names include pid of main process for disambiguation */
709 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
710 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
713 int totalclients
= num_servers
* num_clients
;
714 int totalmsg
= num_msgs
* totalclients
;
715 struct timeval starttv
, endtv
, deltatv
;
718 * Wait for all servers to have registered all ports before starting
719 * the clients and the clock.
723 printf("%d server%s, %d client%s per server (%d total) %u messages...",
724 num_servers
, (num_servers
> 1)? "s" : "",
725 num_clients
, (num_clients
> 1)? "s" : "",
730 /* Call gettimeofday() once and throw away result; some implementations
731 * (like Mach's) cache some time zone info on first call.
733 gettimeofday(&starttv
, NULL
);
734 gettimeofday(&starttv
, NULL
);
736 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
738 printf("creating %d clients\n", totalclients
);
739 for (i
= 0; i
< num_servers
; i
++) {
740 for (j
= 0; j
< num_clients
; j
++) {
742 &client_id
[(i
*num_clients
) + j
],
748 /* Wait for servers to complete */
749 for (i
= 0; i
< num_servers
; i
++) {
750 thread_join(&server_id
[i
]);
753 gettimeofday(&endtv
, NULL
);
755 for (i
= 0; i
< totalclients
; i
++) {
756 thread_join(&client_id
[i
]);
760 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
761 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
762 if (endtv
.tv_usec
< starttv
.tv_usec
) {
764 deltatv
.tv_usec
+= 1000000;
767 double dsecs
= (double) deltatv
.tv_sec
+
768 1.0E-6 * (double) deltatv
.tv_usec
;
770 printf(" in %u.%03u seconds\n",
771 deltatv
.tv_sec
, deltatv
.tv_usec
/1000);
772 printf(" throughput in messages/sec: %g\n",
773 (double)totalmsg
/ dsecs
);
774 printf(" average message latency (usec): %2.3g\n",
775 dsecs
* 1.0E6
/ (double) totalmsg
);