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/event.h>
18 #include <sys/types.h>
20 #include <sys/signal.h>
22 #define MAX(A, B) ((A) < (B) ? (B) : (A))
26 mach_msg_header_t header
;
27 mach_msg_trailer_t trailer
; // subtract this when sending
28 } ipc_trivial_message
;
31 mach_msg_header_t header
;
33 mach_msg_trailer_t trailer
; // subtract this when sending
37 mach_msg_header_t header
;
39 mach_msg_ool_descriptor_t descriptor
;
40 mach_msg_trailer_t trailer
; // subtract this when sending
41 } ipc_complex_message
;
52 mach_msg_header_t
*req_msg
;
54 mach_msg_header_t
*reply_msg
;
65 static boolean_t verbose
= FALSE
;
66 static boolean_t affinity
= FALSE
;
67 static boolean_t timeshare
= FALSE
;
68 static boolean_t threaded
= FALSE
;
69 static boolean_t oneway
= FALSE
;
78 char **server_port_name
;
80 void signal_handler(int sig
) {
83 void usage(const char *progname
) {
84 fprintf(stderr
, "usage: %s [options]\n", progname
);
85 fprintf(stderr
, "where options are:\n");
86 fprintf(stderr
, " -affinity\t\tthreads use affinity\n");
87 fprintf(stderr
, " -timeshare\t\tthreads use timeshare\n");
88 fprintf(stderr
, " -threaded\t\tuse (p)threads\n");
89 fprintf(stderr
, " -verbose\t\tbe verbose\n");
90 fprintf(stderr
, " -oneway\t\tdo not request return reply\n");
91 fprintf(stderr
, " -count num\t\tnumber of messages to send\n");
92 fprintf(stderr
, " -type trivial|inline|complex\ttype of messages to send\n");
93 fprintf(stderr
, " -numints num\tnumber of 32-bit ints to send in messages\n");
94 fprintf(stderr
, " -servers num\tnumber of servers threads to run\n");
95 fprintf(stderr
, " -clients num\tnumber of clients per server\n");
96 fprintf(stderr
, " -delay num\t\tmicroseconds to sleep clients between messages\n");
97 fprintf(stderr
, " -work num\t\tmicroseconds of client work\n");
98 fprintf(stderr
, " -pages num\t\tpages of memory touched by client work\n");
99 fprintf(stderr
, "default values are:\n");
100 fprintf(stderr
, " . no affinity\n");
101 fprintf(stderr
, " . not timeshare\n");
102 fprintf(stderr
, " . not verbose\n");
103 fprintf(stderr
, " . not oneway\n");
104 fprintf(stderr
, " . client sends 100000 messages\n");
105 fprintf(stderr
, " . inline message type\n");
106 fprintf(stderr
, " . 64 32-bit integers in inline/complex messages\n");
107 fprintf(stderr
, " . (num_available_processors+1)%%2 servers\n");
108 fprintf(stderr
, " . 4 clients per server\n");
109 fprintf(stderr
, " . no delay\n");
113 void parse_args(int argc
, char *argv
[]) {
114 host_basic_info_data_t info
;
115 mach_msg_type_number_t count
;
116 kern_return_t result
;
118 /* Initialize defaults */
119 msg_type
= msg_type_trivial
;
125 count
= HOST_BASIC_INFO_COUNT
;
126 result
= host_info(mach_host_self(), HOST_BASIC_INFO
,
127 (host_info_t
)&info
, &count
);
128 if (result
== KERN_SUCCESS
&& info
.avail_cpus
> 1)
129 num_servers
= info
.avail_cpus
/ 2;
133 const char *progname
= argv
[0];
136 if (0 == strcmp("-verbose", argv
[0])) {
139 } else if (0 == strcmp("-affinity", argv
[0])) {
142 } else if (0 == strcmp("-timeshare", argv
[0])) {
145 } else if (0 == strcmp("-threaded", argv
[0])) {
148 } else if (0 == strcmp("-oneway", argv
[0])) {
151 } else if (0 == strcmp("-type", argv
[0])) {
154 if (0 == strcmp("trivial", argv
[1])) {
155 msg_type
= msg_type_trivial
;
156 } else if (0 == strcmp("inline", argv
[1])) {
157 msg_type
= msg_type_inline
;
158 } else if (0 == strcmp("complex", argv
[1])) {
159 msg_type
= msg_type_complex
;
162 argc
-= 2; argv
+= 2;
163 } else if (0 == strcmp("-numints", argv
[0])) {
166 num_ints
= strtoul(argv
[1], NULL
, 0);
167 argc
-= 2; argv
+= 2;
168 } else if (0 == strcmp("-count", argv
[0])) {
171 num_msgs
= strtoul(argv
[1], NULL
, 0);
172 argc
-= 2; argv
+= 2;
173 } else if (0 == strcmp("-clients", argv
[0])) {
176 num_clients
= strtoul(argv
[1], NULL
, 0);
177 argc
-= 2; argv
+= 2;
178 } else if (0 == strcmp("-servers", argv
[0])) {
181 num_servers
= strtoul(argv
[1], NULL
, 0);
182 argc
-= 2; argv
+= 2;
183 } else if (0 == strcmp("-delay", argv
[0])) {
186 client_delay
= strtoul(argv
[1], NULL
, 0);
187 argc
-= 2; argv
+= 2;
188 } else if (0 == strcmp("-spin", argv
[0])) {
191 client_spin
= strtoul(argv
[1], NULL
, 0);
192 argc
-= 2; argv
+= 2;
193 } else if (0 == strcmp("-pages", argv
[0])) {
196 client_pages
= strtoul(argv
[1], NULL
, 0);
197 argc
-= 2; argv
+= 2;
203 void setup_server_ports(struct port_args
*ports
)
205 kern_return_t ret
= 0;
208 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
209 sizeof(u_int32_t
) * num_ints
,
210 sizeof(ipc_complex_message
));
211 ports
->reply_size
= sizeof(ipc_trivial_message
) -
212 sizeof(mach_msg_trailer_t
);
213 ports
->req_msg
= malloc(ports
->req_size
);
214 ports
->reply_msg
= malloc(ports
->reply_size
);
216 ret
= mach_port_allocate(mach_task_self(),
217 MACH_PORT_RIGHT_RECEIVE
,
219 if (KERN_SUCCESS
!= ret
) {
220 mach_error("mach_port_allocate(): ", ret
);
224 ret
= mach_port_allocate(mach_task_self(),
225 MACH_PORT_RIGHT_PORT_SET
,
227 if (KERN_SUCCESS
!= ret
) {
228 mach_error("mach_port_allocate(): ", ret
);
232 ret
= mach_port_insert_member(mach_task_self(),
235 if (KERN_SUCCESS
!= ret
) {
236 mach_error("mach_port_insert_member(): ", ret
);
240 ret
= mach_port_insert_right(mach_task_self(),
243 MACH_MSG_TYPE_MAKE_SEND
);
244 if (KERN_SUCCESS
!= ret
) {
245 mach_error("mach_port_insert_right(): ", ret
);
249 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
250 if (KERN_SUCCESS
!= ret
) {
251 mach_error("task_get_bootstrap_port(): ", ret
);
256 printf("server waiting for IPC messages from client on port '%s'.\n",
257 server_port_name
[ports
->server_num
]);
259 ret
= bootstrap_register(bsport
,
260 server_port_name
[ports
->server_num
],
262 if (KERN_SUCCESS
!= ret
) {
263 mach_error("bootstrap_register(): ", ret
);
268 void setup_client_ports(struct port_args
*ports
)
270 kern_return_t ret
= 0;
272 case msg_type_trivial
:
273 ports
->req_size
= sizeof(ipc_trivial_message
);
275 case msg_type_inline
:
276 ports
->req_size
= sizeof(ipc_inline_message
) +
277 sizeof(u_int32_t
) * num_ints
;
279 case msg_type_complex
:
280 ports
->req_size
= sizeof(ipc_complex_message
);
283 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
284 ports
->reply_size
= sizeof(ipc_trivial_message
);
285 ports
->req_msg
= malloc(ports
->req_size
);
286 ports
->reply_msg
= malloc(ports
->reply_size
);
288 ret
= mach_port_allocate(mach_task_self(),
289 MACH_PORT_RIGHT_RECEIVE
,
291 if (KERN_SUCCESS
!= ret
) {
292 mach_error("mach_port_allocate(): ", ret
);
296 printf("Client sending %d %s IPC messages to port '%s' in %s mode.\n",
297 num_msgs
, (msg_type
== msg_type_inline
) ?
298 "inline" : ((msg_type
== msg_type_complex
) ?
299 "complex" : "trivial"),
300 server_port_name
[ports
->server_num
],
301 (oneway
? "oneway" : "rpc"));
308 thread_setup(int tag
) {
309 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER
311 thread_extended_policy_data_t epolicy
;
312 thread_affinity_policy_data_t policy
;
315 epolicy
.timeshare
= FALSE
;
316 ret
= thread_policy_set(
317 mach_thread_self(), THREAD_EXTENDED_POLICY
,
318 (thread_policy_t
) &epolicy
,
319 THREAD_EXTENDED_POLICY_COUNT
);
320 if (ret
!= KERN_SUCCESS
)
321 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
325 policy
.affinity_tag
= tag
;
326 ret
= thread_policy_set(
327 mach_thread_self(), THREAD_AFFINITY_POLICY
,
328 (thread_policy_t
) &policy
,
329 THREAD_AFFINITY_POLICY_COUNT
);
330 if (ret
!= KERN_SUCCESS
)
331 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
337 server(void *serverarg
)
340 struct kevent64_s kev
[1];
342 struct port_args args
;
345 int totalmsg
= num_msgs
* num_clients
;
347 args
.server_num
= (int) (long) serverarg
;
348 setup_server_ports(&args
);
350 thread_setup(args
.server_num
+ 1);
357 EV_SET64(&kev
[0], args
.pset
, EVFILT_MACHPORT
, (EV_ADD
| EV_CLEAR
| EV_DISPATCH
),
359 MACH_RCV_MSG
|MACH_RCV_LARGE
, 0, 0, (mach_vm_address_t
)args
.req_msg
, args
.req_size
);
363 err
= kevent64(kq
, kev
, 1, NULL
, 0, 0, NULL
);
368 for (idx
= 0; idx
< totalmsg
; idx
++) {
371 printf("server awaiting message %d\n", idx
);
373 EV_SET64(&kev
[0], args
.pset
, EVFILT_MACHPORT
, EV_ENABLE
,
375 MACH_RCV_MSG
|MACH_RCV_LARGE
, 0, 0, (mach_vm_address_t
)args
.req_msg
, args
.req_size
);
379 err
= kevent64(kq
, kev
, 1, kev
, 1, 0, NULL
);
385 // printf("kevent64: returned zero\n");
391 if (MACH_MSG_SUCCESS
!= ret
) {
393 printf("kevent64() mach_msg_return=%d", ret
);
394 mach_error("kevent64 (msg receive): ", ret
);
398 if (kev
[0].data
!= args
.port
)
399 printf("kevent64(MACH_PORT_NULL) port name (0x%x) != expected (0x%x)\n", kev
[0].data
, args
.port
);
401 args
.req_msg
->msgh_bits
= 0;
402 args
.req_msg
->msgh_size
= args
.req_size
;
403 args
.req_msg
->msgh_local_port
= args
.port
;
404 ret
= mach_msg(args
.req_msg
,
405 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
|MACH_RCV_LARGE
,
409 MACH_MSG_TIMEOUT_NONE
,
411 if (MACH_RCV_INTERRUPTED
== ret
)
413 if (MACH_MSG_SUCCESS
!= ret
) {
415 printf("mach_msg() ret=%d", ret
);
416 mach_error("mach_msg (receive): ", ret
);
421 printf("server received message %d\n", idx
);
422 if (args
.req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
423 ret
= vm_deallocate(mach_task_self(),
424 (vm_address_t
)((ipc_complex_message
*)args
.req_msg
)->descriptor
.address
,
425 ((ipc_complex_message
*)args
.req_msg
)->descriptor
.size
);
428 if (1 == args
.req_msg
->msgh_id
) {
430 printf("server sending reply %d\n", idx
);
431 args
.reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
432 MACH_MSG_TYPE_MAKE_SEND
);
433 args
.reply_msg
->msgh_size
= args
.reply_size
;
434 args
.reply_msg
->msgh_remote_port
= args
.req_msg
->msgh_remote_port
;
435 args
.reply_msg
->msgh_local_port
= args
.req_msg
->msgh_local_port
;
436 args
.reply_msg
->msgh_id
= 2;
437 ret
= mach_msg(args
.reply_msg
,
442 MACH_MSG_TIMEOUT_NONE
,
444 if (MACH_MSG_SUCCESS
!= ret
) {
445 mach_error("mach_msg (send): ", ret
);
453 client_spin_loop(unsigned count
, void (fn
)(void))
459 static long dummy_memory
;
460 static long *client_memory
= &dummy_memory
;
462 client_work_atom(void)
466 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long))
468 client_memory
[i
] = 0;
471 static int calibration_count
= 10000;
472 static int calibration_usec
;
474 calibrate_client_work(void)
477 struct timeval nowtv
;
478 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
479 struct timeval starttv
;
480 struct timeval endtv
;
483 /* Warm-up the stepper first... */
484 gettimeofday(&nowtv
, NULL
);
485 timeradd(&nowtv
, &warmuptv
, &endtv
);
487 client_spin_loop(calibration_count
, client_work_atom
);
488 gettimeofday(&nowtv
, NULL
);
489 } while (timercmp(&nowtv
, &endtv
, < ));
491 /* Now do the calibration */
493 gettimeofday(&starttv
, NULL
);
494 client_spin_loop(calibration_count
, client_work_atom
);
495 gettimeofday(&endtv
, NULL
);
496 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
497 calibration_count
/= 10;
500 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
501 if (endtv
.tv_usec
< starttv
.tv_usec
) {
502 calibration_usec
+= 1000000;
504 if (calibration_usec
< 1000) {
505 calibration_count
*= 10;
508 calibration_count
/= calibration_usec
;
512 printf("calibration_count=%d calibration_usec=%d\n",
513 calibration_count
, calibration_usec
);
522 client_spin_loop(calibration_count
*client_spin
,
527 usleep(client_delay
);
531 void *client(void *threadarg
)
533 struct port_args args
;
535 mach_msg_header_t
*req
, *reply
;
536 mach_port_t bsport
, servport
;
538 long server_num
= (long) threadarg
;
539 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
542 printf("client(%d) started, server port name %s\n",
543 server_num
, server_port_name
[server_num
]);
545 args
.server_num
= server_num
;
546 thread_setup(server_num
+ 1);
548 /* find server port */
549 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
550 if (KERN_SUCCESS
!= ret
) {
551 mach_error("task_get_bootstrap_port(): ", ret
);
554 ret
= bootstrap_look_up(bsport
,
555 server_port_name
[server_num
],
557 if (KERN_SUCCESS
!= ret
) {
558 mach_error("bootstrap_look_up(): ", ret
);
562 setup_client_ports(&args
);
564 /* Allocate and touch memory */
567 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
568 for (i
= 0; i
< client_pages
; i
++)
569 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
572 /* start message loop */
573 for (idx
= 0; idx
< num_msgs
; idx
++) {
575 reply
= args
.reply_msg
;
577 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
578 MACH_MSG_TYPE_MAKE_SEND
);
579 req
->msgh_size
= args
.req_size
;
580 req
->msgh_remote_port
= servport
;
581 req
->msgh_local_port
= args
.port
;
582 req
->msgh_id
= oneway
? 0 : 1;
583 if (msg_type
== msg_type_complex
) {
584 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
585 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
586 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
587 ((ipc_complex_message
*)req
)->descriptor
.size
=
588 num_ints
* sizeof(u_int32_t
);
589 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
590 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
591 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
594 printf("client sending message %d\n", idx
);
600 MACH_MSG_TIMEOUT_NONE
,
602 if (MACH_MSG_SUCCESS
!= ret
) {
603 mach_error("mach_msg (send): ", ret
);
604 fprintf(stderr
, "bailing after %u iterations\n", idx
);
610 printf("client awaiting reply %d\n", idx
);
611 reply
->msgh_bits
= 0;
612 reply
->msgh_size
= args
.reply_size
;
613 reply
->msgh_local_port
= args
.port
;
614 ret
= mach_msg(args
.reply_msg
,
615 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
,
619 MACH_MSG_TIMEOUT_NONE
,
621 if (MACH_MSG_SUCCESS
!= ret
) {
622 mach_error("mach_msg (receive): ", ret
);
623 fprintf(stderr
, "bailing after %u iterations\n",
628 printf("client received reply %d\n", idx
);
639 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
) {
642 ret
= pthread_create(
648 err(1, "pthread_create()");
650 printf("created pthread 0x%x\n", thread
->tid
);
652 thread
->pid
= fork();
653 if (thread
->pid
== 0) {
655 printf("calling 0x%x(0x%x)\n", fn
, arg
);
660 printf("forked pid %d\n", thread
->pid
);
665 thread_join(thread_id_t
*thread
) {
669 printf("joining thread 0x%x\n", thread
->tid
);
670 ret
= pthread_join(thread
->tid
, NULL
);
671 if (ret
!= KERN_SUCCESS
)
672 err(1, "pthread_join(0x%x)", thread
->tid
);
676 printf("waiting for pid %d\n", thread
->pid
);
677 waitpid(thread
->pid
, &stat
, 0);
682 wait_for_servers(void)
685 int retry_count
= 10;
686 mach_port_t bsport
, servport
;
689 /* find server port */
690 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
691 if (KERN_SUCCESS
!= ret
) {
692 mach_error("task_get_bootstrap_port(): ", ret
);
696 while (retry_count
-- > 0) {
697 for (i
= 0; i
< num_servers
; i
++) {
698 ret
= bootstrap_look_up(bsport
,
701 if (ret
!= KERN_SUCCESS
) {
705 if (ret
== KERN_SUCCESS
)
707 usleep(100 * 1000); /* 100ms */
709 fprintf(stderr
, "Server(s) failed to register\n");
713 int main(int argc
, char *argv
[])
717 thread_id_t
*client_id
;
718 thread_id_t
*server_id
;
720 signal(SIGINT
, signal_handler
);
721 parse_args(argc
, argv
);
723 calibrate_client_work();
726 * If we're using affinity create an empty namespace now
727 * so this is shared by all our offspring.
732 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
733 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
735 printf("creating %d servers\n", num_servers
);
736 for (i
= 0; i
< num_servers
; i
++) {
737 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
738 /* PORT names include pid of main process for disambiguation */
739 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
740 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
743 int totalclients
= num_servers
* num_clients
;
744 int totalmsg
= num_msgs
* totalclients
;
745 struct timeval starttv
, endtv
, deltatv
;
748 * Wait for all servers to have registered all ports before starting
749 * the clients and the clock.
753 printf("%d server%s, %d client%s per server (%d total) %u messages...",
754 num_servers
, (num_servers
> 1)? "s" : "",
755 num_clients
, (num_clients
> 1)? "s" : "",
760 /* Call gettimeofday() once and throw away result; some implementations
761 * (like Mach's) cache some time zone info on first call.
763 gettimeofday(&starttv
, NULL
);
764 gettimeofday(&starttv
, NULL
);
766 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
768 printf("creating %d clients\n", totalclients
);
769 for (i
= 0; i
< num_servers
; i
++) {
770 for (j
= 0; j
< num_clients
; j
++) {
772 &client_id
[(i
*num_clients
) + j
],
778 /* Wait for servers to complete */
779 for (i
= 0; i
< num_servers
; i
++) {
780 thread_join(&server_id
[i
]);
783 gettimeofday(&endtv
, NULL
);
785 for (i
= 0; i
< totalclients
; i
++) {
786 thread_join(&client_id
[i
]);
790 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
791 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
792 if (endtv
.tv_usec
< starttv
.tv_usec
) {
794 deltatv
.tv_usec
+= 1000000;
797 double dsecs
= (double) deltatv
.tv_sec
+
798 1.0E-6 * (double) deltatv
.tv_usec
;
800 printf(" in %u.%03u seconds\n",
801 deltatv
.tv_sec
, deltatv
.tv_usec
/1000);
802 printf(" throughput in messages/sec: %g\n",
803 (double)totalmsg
/ dsecs
);
804 printf(" average message latency (usec): %2.3g\n",
805 dsecs
* 1.0E6
/ (double) totalmsg
);