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))
24 unsigned int msgt_name
: 8,
34 mach_msg_type_t msgtl_header
;
35 unsigned short msgtl_name
;
36 unsigned short msgtl_size
;
37 natural_t msgtl_number
;
38 } mach_msg_type_long_t
;
39 #define MACH_MSG_TYPE_INTEGER_32 0
43 mach_msg_header_t header
;
44 mach_msg_trailer_t trailer
; // subtract this when sending
45 } ipc_trivial_message
;
48 mach_msg_header_t header
;
51 mach_msg_trailer_t trailer
; // subtract this when sending
55 mach_msg_header_t header
;
57 mach_msg_ool_descriptor_t descriptor
;
58 mach_msg_trailer_t trailer
; // subtract this when sending
59 } ipc_complex_message
;
70 mach_msg_header_t
*req_msg
;
72 mach_msg_header_t
*reply_msg
;
82 static boolean_t verbose
= FALSE
;
83 static boolean_t affinity
= FALSE
;
84 static boolean_t timeshare
= FALSE
;
85 static boolean_t threaded
= FALSE
;
86 static boolean_t oneway
= FALSE
;
95 char **server_port_name
;
97 void signal_handler(int sig
) {
100 void usage(const char *progname
) {
101 fprintf(stderr
, "usage: %s [options]\n", progname
);
102 fprintf(stderr
, "where options are:\n");
103 fprintf(stderr
, " -affinity\t\tthreads use affinity\n");
104 fprintf(stderr
, " -timeshare\t\tthreads use timeshare\n");
105 fprintf(stderr
, " -threaded\t\tuse (p)threads\n");
106 fprintf(stderr
, " -verbose\t\tbe verbose\n");
107 fprintf(stderr
, " -oneway\t\tdo not request return reply\n");
108 fprintf(stderr
, " -count num\t\tnumber of messages to send\n");
109 fprintf(stderr
, " -type trivial|inline|complex\ttype of messages to send\n");
110 fprintf(stderr
, " -numints num\tnumber of 32-bit ints to send in messages\n");
111 fprintf(stderr
, " -servers num\tnumber of servers threads to run\n");
112 fprintf(stderr
, " -clients num\tnumber of clients per server\n");
113 fprintf(stderr
, " -delay num\t\tmicroseconds to sleep clients between messages\n");
114 fprintf(stderr
, " -work num\t\tmicroseconds of client work\n");
115 fprintf(stderr
, " -pages num\t\tpages of memory touched by client work\n");
116 fprintf(stderr
, "default values are:\n");
117 fprintf(stderr
, " . no affinity\n");
118 fprintf(stderr
, " . not timeshare\n");
119 fprintf(stderr
, " . not verbose\n");
120 fprintf(stderr
, " . not oneway\n");
121 fprintf(stderr
, " . client sends 100000 messages\n");
122 fprintf(stderr
, " . inline message type\n");
123 fprintf(stderr
, " . 64 32-bit integers in inline/complex messages\n");
124 fprintf(stderr
, " . (num_available_processors+1)%%2 servers\n");
125 fprintf(stderr
, " . 4 clients per server\n");
126 fprintf(stderr
, " . no delay\n");
130 void parse_args(int argc
, char *argv
[]) {
131 host_basic_info_data_t info
;
132 mach_msg_type_number_t count
;
133 kern_return_t result
;
135 /* Initialize defaults */
136 msg_type
= msg_type_trivial
;
142 count
= HOST_BASIC_INFO_COUNT
;
143 result
= host_info(mach_host_self(), HOST_BASIC_INFO
,
144 (host_info_t
)&info
, &count
);
145 if (result
== KERN_SUCCESS
&& info
.avail_cpus
> 1)
146 num_servers
= info
.avail_cpus
/ 2;
150 const char *progname
= argv
[0];
153 if (0 == strcmp("-verbose", argv
[0])) {
156 } else if (0 == strcmp("-affinity", argv
[0])) {
159 } else if (0 == strcmp("-timeshare", argv
[0])) {
162 } else if (0 == strcmp("-threaded", argv
[0])) {
165 } else if (0 == strcmp("-oneway", argv
[0])) {
168 } else if (0 == strcmp("-type", argv
[0])) {
171 if (0 == strcmp("trivial", argv
[1])) {
172 msg_type
= msg_type_trivial
;
173 } else if (0 == strcmp("inline", argv
[1])) {
174 msg_type
= msg_type_inline
;
175 } else if (0 == strcmp("complex", argv
[1])) {
176 msg_type
= msg_type_complex
;
179 argc
-= 2; argv
+= 2;
180 } else if (0 == strcmp("-numints", argv
[0])) {
183 num_ints
= strtoul(argv
[1], NULL
, 0);
184 argc
-= 2; argv
+= 2;
185 } else if (0 == strcmp("-count", argv
[0])) {
188 num_msgs
= strtoul(argv
[1], NULL
, 0);
189 argc
-= 2; argv
+= 2;
190 } else if (0 == strcmp("-clients", argv
[0])) {
193 num_clients
= strtoul(argv
[1], NULL
, 0);
194 argc
-= 2; argv
+= 2;
195 } else if (0 == strcmp("-servers", argv
[0])) {
198 num_servers
= strtoul(argv
[1], NULL
, 0);
199 argc
-= 2; argv
+= 2;
200 } else if (0 == strcmp("-delay", argv
[0])) {
203 client_delay
= strtoul(argv
[1], NULL
, 0);
204 argc
-= 2; argv
+= 2;
205 } else if (0 == strcmp("-spin", argv
[0])) {
208 client_spin
= strtoul(argv
[1], NULL
, 0);
209 argc
-= 2; argv
+= 2;
210 } else if (0 == strcmp("-pages", argv
[0])) {
213 client_pages
= strtoul(argv
[1], NULL
, 0);
214 argc
-= 2; argv
+= 2;
220 void setup_server_ports(struct port_args
*ports
)
222 kern_return_t ret
= 0;
225 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
226 sizeof(u_int32_t
) * num_ints
,
227 sizeof(ipc_complex_message
));
228 ports
->reply_size
= sizeof(ipc_trivial_message
) -
229 sizeof(mach_msg_trailer_t
);
230 ports
->req_msg
= malloc(ports
->req_size
);
231 ports
->reply_msg
= malloc(ports
->reply_size
);
233 ret
= mach_port_allocate(mach_task_self(),
234 MACH_PORT_RIGHT_RECEIVE
,
236 if (KERN_SUCCESS
!= ret
) {
237 mach_error("mach_port_allocate(): ", ret
);
241 ret
= mach_port_insert_right(mach_task_self(),
244 MACH_MSG_TYPE_MAKE_SEND
);
245 if (KERN_SUCCESS
!= ret
) {
246 mach_error("mach_port_insert_right(): ", ret
);
250 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
251 if (KERN_SUCCESS
!= ret
) {
252 mach_error("task_get_bootstrap_port(): ", ret
);
257 printf("server waiting for IPC messages from client on port '%s'.\n",
258 server_port_name
[ports
->server_num
]);
260 ret
= bootstrap_register(bsport
,
261 server_port_name
[ports
->server_num
],
263 if (KERN_SUCCESS
!= ret
) {
264 mach_error("bootstrap_register(): ", ret
);
269 void setup_client_ports(struct port_args
*ports
)
271 kern_return_t ret
= 0;
273 case msg_type_trivial
:
274 ports
->req_size
= sizeof(ipc_trivial_message
);
276 case msg_type_inline
:
277 ports
->req_size
= sizeof(ipc_inline_message
) +
278 sizeof(u_int32_t
) * num_ints
;
280 case msg_type_complex
:
281 ports
->req_size
= sizeof(ipc_complex_message
);
284 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
285 ports
->reply_size
= sizeof(ipc_trivial_message
);
286 ports
->req_msg
= malloc(ports
->req_size
);
287 ports
->reply_msg
= malloc(ports
->reply_size
);
289 ret
= mach_port_allocate(mach_task_self(),
290 MACH_PORT_RIGHT_RECEIVE
,
292 if (KERN_SUCCESS
!= ret
) {
293 mach_error("mach_port_allocate(): ", ret
);
297 printf("Client sending %d %s IPC messages to port '%s' in %s mode.\n",
298 num_msgs
, (msg_type
== msg_type_inline
) ?
299 "inline" : ((msg_type
== msg_type_complex
) ?
300 "complex" : "trivial"),
301 server_port_name
[ports
->server_num
],
302 (oneway
? "oneway" : "rpc"));
309 thread_setup(int tag
) {
310 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER
312 thread_extended_policy_data_t epolicy
;
313 thread_affinity_policy_data_t policy
;
316 epolicy
.timeshare
= FALSE
;
317 ret
= thread_policy_set(
318 mach_thread_self(), THREAD_EXTENDED_POLICY
,
319 (thread_policy_t
) &epolicy
,
320 THREAD_EXTENDED_POLICY_COUNT
);
321 if (ret
!= KERN_SUCCESS
)
322 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
326 policy
.affinity_tag
= tag
;
327 ret
= thread_policy_set(
328 mach_thread_self(), THREAD_AFFINITY_POLICY
,
329 (thread_policy_t
) &policy
,
330 THREAD_AFFINITY_POLICY_COUNT
);
331 if (ret
!= KERN_SUCCESS
)
332 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
338 server(void *serverarg
)
340 struct port_args args
;
343 int totalmsg
= num_msgs
* num_clients
;
345 args
.server_num
= (int) (long) serverarg
;
346 setup_server_ports(&args
);
348 thread_setup(args
.server_num
+ 1);
350 for (idx
= 0; idx
< totalmsg
; idx
++) {
352 printf("server awaiting message %d\n", idx
);
353 args
.req_msg
->msgh_bits
= 0;
354 args
.req_msg
->msgh_size
= args
.req_size
;
355 args
.req_msg
->msgh_local_port
= args
.port
;
356 ret
= mach_msg(args
.req_msg
,
357 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
|MACH_RCV_LARGE
,
361 MACH_MSG_TIMEOUT_NONE
,
363 if (MACH_RCV_INTERRUPTED
== ret
)
365 if (MACH_MSG_SUCCESS
!= ret
) {
367 printf("mach_msg() ret=%d", ret
);
368 mach_error("mach_msg (receive): ", ret
);
372 printf("server received message %d\n", idx
);
373 if (args
.req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
374 ret
= vm_deallocate(mach_task_self(),
375 (vm_address_t
)((ipc_complex_message
*)args
.req_msg
)->descriptor
.address
,
376 ((ipc_complex_message
*)args
.req_msg
)->descriptor
.size
);
379 if (1 == args
.req_msg
->msgh_id
) {
381 printf("server sending reply %d\n", idx
);
382 args
.reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
383 MACH_MSG_TYPE_MAKE_SEND
);
384 args
.reply_msg
->msgh_size
= args
.reply_size
;
385 args
.reply_msg
->msgh_remote_port
= args
.req_msg
->msgh_remote_port
;
386 args
.reply_msg
->msgh_local_port
= args
.req_msg
->msgh_local_port
;
387 args
.reply_msg
->msgh_id
= 2;
388 ret
= mach_msg(args
.reply_msg
,
393 MACH_MSG_TIMEOUT_NONE
,
395 if (MACH_MSG_SUCCESS
!= ret
) {
396 mach_error("mach_msg (send): ", ret
);
404 client_spin_loop(unsigned count
, void (fn
)(void))
410 static long dummy_memory
;
411 static long *client_memory
= &dummy_memory
;
413 client_work_atom(void)
417 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long))
419 client_memory
[i
] = 0;
422 static int calibration_count
= 10000;
423 static int calibration_usec
;
425 calibrate_client_work(void)
428 struct timeval nowtv
;
429 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
430 struct timeval starttv
;
431 struct timeval endtv
;
434 /* Warm-up the stepper first... */
435 gettimeofday(&nowtv
, NULL
);
436 timeradd(&nowtv
, &warmuptv
, &endtv
);
438 client_spin_loop(calibration_count
, client_work_atom
);
439 gettimeofday(&nowtv
, NULL
);
440 } while (timercmp(&nowtv
, &endtv
, < ));
442 /* Now do the calibration */
444 gettimeofday(&starttv
, NULL
);
445 client_spin_loop(calibration_count
, client_work_atom
);
446 gettimeofday(&endtv
, NULL
);
447 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
448 calibration_count
/= 10;
451 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
452 if (endtv
.tv_usec
< starttv
.tv_usec
) {
453 calibration_usec
+= 1000000;
455 if (calibration_usec
< 1000) {
456 calibration_count
*= 10;
459 calibration_count
/= calibration_usec
;
463 printf("calibration_count=%d calibration_usec=%d\n",
464 calibration_count
, calibration_usec
);
473 client_spin_loop(calibration_count
*client_spin
,
478 usleep(client_delay
);
482 void *client(void *threadarg
)
484 struct port_args args
;
486 mach_msg_header_t
*req
, *reply
;
487 mach_port_t bsport
, servport
;
489 long server_num
= (long) threadarg
;
490 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
493 printf("client(%d) started, server port name %s\n",
494 server_num
, server_port_name
[server_num
]);
496 args
.server_num
= server_num
;
497 thread_setup(server_num
+ 1);
499 /* find server port */
500 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
501 if (KERN_SUCCESS
!= ret
) {
502 mach_error("task_get_bootstrap_port(): ", ret
);
505 ret
= bootstrap_look_up(bsport
,
506 server_port_name
[server_num
],
508 if (KERN_SUCCESS
!= ret
) {
509 mach_error("bootstrap_look_up(): ", ret
);
513 setup_client_ports(&args
);
515 /* Allocate and touch memory */
518 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
519 for (i
= 0; i
< client_pages
; i
++)
520 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
523 /* start message loop */
524 for (idx
= 0; idx
< num_msgs
; idx
++) {
526 reply
= args
.reply_msg
;
528 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
529 MACH_MSG_TYPE_MAKE_SEND
);
530 req
->msgh_size
= args
.req_size
;
531 req
->msgh_remote_port
= servport
;
532 req
->msgh_local_port
= args
.port
;
533 req
->msgh_id
= oneway
? 0 : 1;
535 case msg_type_trivial
:
537 case msg_type_inline
:
538 ((ipc_inline_message
*)req
)->type
.msgt_name
= MACH_MSG_TYPE_INTEGER_32
;
539 ((ipc_inline_message
*)req
)->type
.msgt_size
= 32;
540 ((ipc_inline_message
*)req
)->type
.msgt_number
= num_ints
;
541 ((ipc_inline_message
*)req
)->type
.msgt_inline
= TRUE
;
542 ((ipc_inline_message
*)req
)->type
.msgt_longform
= FALSE
;
543 ((ipc_inline_message
*)req
)->type
.msgt_deallocate
= FALSE
;
544 ((ipc_inline_message
*)req
)->type
.msgt_unused
= 0;
546 case msg_type_complex
:
547 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
548 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
549 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
550 ((ipc_complex_message
*)req
)->descriptor
.size
=
551 num_ints
* sizeof(u_int32_t
);
552 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
553 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
554 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
558 printf("client sending message %d\n", idx
);
564 MACH_MSG_TIMEOUT_NONE
,
566 if (MACH_MSG_SUCCESS
!= ret
) {
567 mach_error("mach_msg (send): ", ret
);
568 fprintf(stderr
, "bailing after %u iterations\n", idx
);
574 printf("client awaiting reply %d\n", idx
);
575 reply
->msgh_bits
= 0;
576 reply
->msgh_size
= args
.reply_size
;
577 reply
->msgh_local_port
= args
.port
;
578 ret
= mach_msg(args
.reply_msg
,
579 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
,
583 MACH_MSG_TIMEOUT_NONE
,
585 if (MACH_MSG_SUCCESS
!= ret
) {
586 mach_error("mach_msg (receive): ", ret
);
587 fprintf(stderr
, "bailing after %u iterations\n",
592 printf("client received reply %d\n", idx
);
603 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
) {
606 ret
= pthread_create(
612 err(1, "pthread_create()");
614 printf("created pthread 0x%x\n", thread
->tid
);
616 thread
->pid
= fork();
617 if (thread
->pid
== 0) {
619 printf("calling 0x%x(0x%x)\n", fn
, arg
);
624 printf("forked pid %d\n", thread
->pid
);
629 thread_join(thread_id_t
*thread
) {
633 printf("joining thread 0x%x\n", thread
->tid
);
634 ret
= pthread_join(thread
->tid
, NULL
);
635 if (ret
!= KERN_SUCCESS
)
636 err(1, "pthread_join(0x%x)", thread
->tid
);
640 printf("waiting for pid %d\n", thread
->pid
);
641 waitpid(thread
->pid
, &stat
, 0);
646 wait_for_servers(void)
649 int retry_count
= 10;
650 mach_port_t bsport
, servport
;
653 /* find server port */
654 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
655 if (KERN_SUCCESS
!= ret
) {
656 mach_error("task_get_bootstrap_port(): ", ret
);
660 while (retry_count
-- > 0) {
661 for (i
= 0; i
< num_servers
; i
++) {
662 ret
= bootstrap_look_up(bsport
,
665 if (ret
!= KERN_SUCCESS
) {
669 if (ret
== KERN_SUCCESS
)
671 usleep(100 * 1000); /* 100ms */
673 fprintf(stderr
, "Server(s) failed to register\n");
677 int main(int argc
, char *argv
[])
681 thread_id_t
*client_id
;
682 thread_id_t
*server_id
;
684 signal(SIGINT
, signal_handler
);
685 parse_args(argc
, argv
);
687 calibrate_client_work();
690 * If we're using affinity create an empty namespace now
691 * so this is shared by all our offspring.
696 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
697 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
699 printf("creating %d servers\n", num_servers
);
700 for (i
= 0; i
< num_servers
; i
++) {
701 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
702 /* PORT names include pid of main process for disambiguation */
703 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
704 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
707 int totalclients
= num_servers
* num_clients
;
708 int totalmsg
= num_msgs
* totalclients
;
709 struct timeval starttv
, endtv
, deltatv
;
712 * Wait for all servers to have registered all ports before starting
713 * the clients and the clock.
717 printf("%d server%s, %d client%s per server (%d total) %u messages...",
718 num_servers
, (num_servers
> 1)? "s" : "",
719 num_clients
, (num_clients
> 1)? "s" : "",
724 /* Call gettimeofday() once and throw away result; some implementations
725 * (like Mach's) cache some time zone info on first call.
727 gettimeofday(&starttv
, NULL
);
728 gettimeofday(&starttv
, NULL
);
730 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
732 printf("creating %d clients\n", totalclients
);
733 for (i
= 0; i
< num_servers
; i
++) {
734 for (j
= 0; j
< num_clients
; j
++) {
736 &client_id
[(i
*num_clients
) + j
],
742 /* Wait for servers to complete */
743 for (i
= 0; i
< num_servers
; i
++) {
744 thread_join(&server_id
[i
]);
747 gettimeofday(&endtv
, NULL
);
749 for (i
= 0; i
< totalclients
; i
++) {
750 thread_join(&client_id
[i
]);
754 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
755 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
756 if (endtv
.tv_usec
< starttv
.tv_usec
) {
758 deltatv
.tv_usec
+= 1000000;
761 double dsecs
= (double) deltatv
.tv_sec
+
762 1.0E-6 * (double) deltatv
.tv_usec
;
764 printf(" in %u.%03u seconds\n",
765 deltatv
.tv_sec
, deltatv
.tv_usec
/1000);
766 printf(" throughput in messages/sec: %g\n",
767 (double)totalmsg
/ dsecs
);
768 printf(" average message latency (usec): %2.3g\n",
769 dsecs
* 1.0E6
/ (double) totalmsg
);