1 #include <AvailabilityMacros.h>
2 #include <mach/thread_policy.h>
12 #include <mach/mach.h>
13 #include <mach/mach_error.h>
14 #include <mach/notify.h>
15 #include <servers/bootstrap.h>
16 #include <sys/event.h>
17 #include <sys/select.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
;
70 static boolean_t do_select
= 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
, " -select \t\tselect prior to calling kevent().\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("-select", argv
[0])) {
208 void setup_server_ports(struct port_args
*ports
)
210 kern_return_t ret
= 0;
213 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
214 sizeof(u_int32_t
) * num_ints
,
215 sizeof(ipc_complex_message
));
216 ports
->reply_size
= sizeof(ipc_trivial_message
) -
217 sizeof(mach_msg_trailer_t
);
218 ports
->req_msg
= malloc(ports
->req_size
);
219 ports
->reply_msg
= malloc(ports
->reply_size
);
221 ret
= mach_port_allocate(mach_task_self(),
222 MACH_PORT_RIGHT_RECEIVE
,
224 if (KERN_SUCCESS
!= ret
) {
225 mach_error("mach_port_allocate(): ", ret
);
229 ret
= mach_port_allocate(mach_task_self(),
230 MACH_PORT_RIGHT_PORT_SET
,
232 if (KERN_SUCCESS
!= ret
) {
233 mach_error("mach_port_allocate(): ", ret
);
237 ret
= mach_port_insert_member(mach_task_self(),
240 if (KERN_SUCCESS
!= ret
) {
241 mach_error("mach_port_insert_member(): ", ret
);
245 ret
= mach_port_insert_right(mach_task_self(),
248 MACH_MSG_TYPE_MAKE_SEND
);
249 if (KERN_SUCCESS
!= ret
) {
250 mach_error("mach_port_insert_right(): ", ret
);
254 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
255 if (KERN_SUCCESS
!= ret
) {
256 mach_error("task_get_bootstrap_port(): ", ret
);
261 printf("server waiting for IPC messages from client on port '%s'.\n",
262 server_port_name
[ports
->server_num
]);
264 ret
= bootstrap_register(bsport
,
265 server_port_name
[ports
->server_num
],
267 if (KERN_SUCCESS
!= ret
) {
268 mach_error("bootstrap_register(): ", ret
);
273 void setup_client_ports(struct port_args
*ports
)
275 kern_return_t ret
= 0;
277 case msg_type_trivial
:
278 ports
->req_size
= sizeof(ipc_trivial_message
);
280 case msg_type_inline
:
281 ports
->req_size
= sizeof(ipc_inline_message
) +
282 sizeof(u_int32_t
) * num_ints
;
284 case msg_type_complex
:
285 ports
->req_size
= sizeof(ipc_complex_message
);
288 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
289 ports
->reply_size
= sizeof(ipc_trivial_message
);
290 ports
->req_msg
= malloc(ports
->req_size
);
291 ports
->reply_msg
= malloc(ports
->reply_size
);
293 ret
= mach_port_allocate(mach_task_self(),
294 MACH_PORT_RIGHT_RECEIVE
,
296 if (KERN_SUCCESS
!= ret
) {
297 mach_error("mach_port_allocate(): ", ret
);
301 printf("Client sending %d %s IPC messages to port '%s' in %s mode.\n",
302 num_msgs
, (msg_type
== msg_type_inline
) ?
303 "inline" : ((msg_type
== msg_type_complex
) ?
304 "complex" : "trivial"),
305 server_port_name
[ports
->server_num
],
306 (oneway
? "oneway" : "rpc"));
313 thread_setup(int tag
) {
315 thread_extended_policy_data_t epolicy
;
316 thread_affinity_policy_data_t policy
;
319 epolicy
.timeshare
= FALSE
;
320 ret
= thread_policy_set(
321 mach_thread_self(), THREAD_EXTENDED_POLICY
,
322 (thread_policy_t
) &epolicy
,
323 THREAD_EXTENDED_POLICY_COUNT
);
324 if (ret
!= KERN_SUCCESS
)
325 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
329 policy
.affinity_tag
= tag
;
330 ret
= thread_policy_set(
331 mach_thread_self(), THREAD_AFFINITY_POLICY
,
332 (thread_policy_t
) &policy
,
333 THREAD_AFFINITY_POLICY_COUNT
);
334 if (ret
!= KERN_SUCCESS
)
335 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
340 server(void *serverarg
)
343 struct kevent64_s kev
[1];
346 struct port_args args
;
349 int totalmsg
= num_msgs
* num_clients
;
352 args
.server_num
= (int) (long) serverarg
;
353 setup_server_ports(&args
);
355 thread_setup(args
.server_num
+ 1);
362 EV_SET64(&kev
[0], args
.pset
, EVFILT_MACHPORT
, (EV_ADD
| EV_CLEAR
| EV_DISPATCH
),
364 MACH_RCV_MSG
|MACH_RCV_LARGE
, 0, 0, (mach_vm_address_t
)args
.req_msg
, args
.req_size
);
368 err
= kevent64(kq
, kev
, 1, NULL
, 0, 0, NULL
);
374 for (idx
= 0; idx
< totalmsg
; idx
++) {
377 printf("server awaiting message %d\n", idx
);
381 FD_SET(kq
, &readfds
);
384 printf("Calling select() prior to kevent64().\n");
386 count
= select(kq
+ 1, &readfds
, NULL
, NULL
, NULL
);
393 EV_SET64(&kev
[0], args
.pset
, EVFILT_MACHPORT
, EV_ENABLE
,
395 MACH_RCV_MSG
|MACH_RCV_LARGE
, 0, 0, (mach_vm_address_t
)args
.req_msg
, args
.req_size
);
399 err
= kevent64(kq
, kev
, 1, kev
, 1, 0, NULL
);
405 // printf("kevent64: returned zero\n");
411 if (MACH_MSG_SUCCESS
!= ret
) {
413 printf("kevent64() mach_msg_return=%d", ret
);
414 mach_error("kevent64 (msg receive): ", ret
);
418 if (kev
[0].data
!= args
.port
)
419 printf("kevent64(MACH_PORT_NULL) port name (%lld) != expected (0x%x)\n", kev
[0].data
, args
.port
);
421 args
.req_msg
->msgh_bits
= 0;
422 args
.req_msg
->msgh_size
= args
.req_size
;
423 args
.req_msg
->msgh_local_port
= args
.port
;
424 ret
= mach_msg(args
.req_msg
,
425 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
|MACH_RCV_LARGE
,
429 MACH_MSG_TIMEOUT_NONE
,
431 if (MACH_RCV_INTERRUPTED
== ret
)
433 if (MACH_MSG_SUCCESS
!= ret
) {
435 printf("mach_msg() ret=%d", ret
);
436 mach_error("mach_msg (receive): ", ret
);
441 printf("server received message %d\n", idx
);
442 if (args
.req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
443 ret
= vm_deallocate(mach_task_self(),
444 (vm_address_t
)((ipc_complex_message
*)args
.req_msg
)->descriptor
.address
,
445 ((ipc_complex_message
*)args
.req_msg
)->descriptor
.size
);
448 if (1 == args
.req_msg
->msgh_id
) {
450 printf("server sending reply %d\n", idx
);
451 args
.reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
452 MACH_MSG_TYPE_MAKE_SEND
);
453 args
.reply_msg
->msgh_size
= args
.reply_size
;
454 args
.reply_msg
->msgh_remote_port
= args
.req_msg
->msgh_remote_port
;
455 args
.reply_msg
->msgh_local_port
= args
.req_msg
->msgh_local_port
;
456 args
.reply_msg
->msgh_id
= 2;
457 ret
= mach_msg(args
.reply_msg
,
462 MACH_MSG_TIMEOUT_NONE
,
464 if (MACH_MSG_SUCCESS
!= ret
) {
465 mach_error("mach_msg (send): ", ret
);
474 client_spin_loop(unsigned count
, void (fn
)(void))
480 static long dummy_memory
;
481 static long *client_memory
= &dummy_memory
;
483 client_work_atom(void)
487 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long))
489 client_memory
[i
] = 0;
492 static int calibration_count
= 10000;
493 static int calibration_usec
;
495 calibrate_client_work(void)
498 struct timeval nowtv
;
499 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
500 struct timeval starttv
;
501 struct timeval endtv
;
504 /* Warm-up the stepper first... */
505 gettimeofday(&nowtv
, NULL
);
506 timeradd(&nowtv
, &warmuptv
, &endtv
);
508 client_spin_loop(calibration_count
, client_work_atom
);
509 gettimeofday(&nowtv
, NULL
);
510 } while (timercmp(&nowtv
, &endtv
, < ));
512 /* Now do the calibration */
514 gettimeofday(&starttv
, NULL
);
515 client_spin_loop(calibration_count
, client_work_atom
);
516 gettimeofday(&endtv
, NULL
);
517 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
518 calibration_count
/= 10;
521 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
522 if (endtv
.tv_usec
< starttv
.tv_usec
) {
523 calibration_usec
+= 1000000;
525 if (calibration_usec
< 1000) {
526 calibration_count
*= 10;
529 calibration_count
/= calibration_usec
;
533 printf("calibration_count=%d calibration_usec=%d\n",
534 calibration_count
, calibration_usec
);
544 client_spin_loop(calibration_count
*client_spin
,
549 usleep(client_delay
);
554 void *client(void *threadarg
)
556 struct port_args args
;
558 mach_msg_header_t
*req
, *reply
;
559 mach_port_t bsport
, servport
;
561 int server_num
= (int) threadarg
;
562 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
565 printf("client(%d) started, server port name %s\n",
566 server_num
, server_port_name
[server_num
]);
568 args
.server_num
= server_num
;
569 thread_setup(server_num
+ 1);
571 /* find server port */
572 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
573 if (KERN_SUCCESS
!= ret
) {
574 mach_error("task_get_bootstrap_port(): ", ret
);
577 ret
= bootstrap_look_up(bsport
,
578 server_port_name
[server_num
],
580 if (KERN_SUCCESS
!= ret
) {
581 mach_error("bootstrap_look_up(): ", ret
);
585 setup_client_ports(&args
);
587 /* Allocate and touch memory */
590 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
591 for (i
= 0; i
< client_pages
; i
++)
592 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
595 /* start message loop */
596 for (idx
= 0; idx
< num_msgs
; idx
++) {
598 reply
= args
.reply_msg
;
600 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
601 MACH_MSG_TYPE_MAKE_SEND
);
602 req
->msgh_size
= args
.req_size
;
603 req
->msgh_remote_port
= servport
;
604 req
->msgh_local_port
= args
.port
;
605 req
->msgh_id
= oneway
? 0 : 1;
606 if (msg_type
== msg_type_complex
) {
607 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
608 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
609 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
610 ((ipc_complex_message
*)req
)->descriptor
.size
=
611 num_ints
* sizeof(u_int32_t
);
612 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
613 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
614 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
617 printf("client sending message %d\n", idx
);
623 MACH_MSG_TIMEOUT_NONE
,
625 if (MACH_MSG_SUCCESS
!= ret
) {
626 mach_error("mach_msg (send): ", ret
);
627 fprintf(stderr
, "bailing after %u iterations\n", idx
);
633 printf("client awaiting reply %d\n", idx
);
634 reply
->msgh_bits
= 0;
635 reply
->msgh_size
= args
.reply_size
;
636 reply
->msgh_local_port
= args
.port
;
637 ret
= mach_msg(args
.reply_msg
,
638 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
,
642 MACH_MSG_TIMEOUT_NONE
,
644 if (MACH_MSG_SUCCESS
!= ret
) {
645 mach_error("mach_msg (receive): ", ret
);
646 fprintf(stderr
, "bailing after %u iterations\n",
651 printf("client received reply %d\n", idx
);
662 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
) {
665 ret
= pthread_create(
671 err(1, "pthread_create()");
673 printf("created pthread %p\n", thread
->tid
);
675 thread
->pid
= fork();
676 if (thread
->pid
== 0) {
678 printf("calling %p(%p)\n", fn
, arg
);
683 printf("forked pid %d\n", thread
->pid
);
688 thread_join(thread_id_t
*thread
) {
692 printf("joining thread %p\n", thread
->tid
);
693 ret
= pthread_join(thread
->tid
, NULL
);
694 if (ret
!= KERN_SUCCESS
)
695 err(1, "pthread_join(%p)", thread
->tid
);
699 printf("waiting for pid %d\n", thread
->pid
);
700 waitpid(thread
->pid
, &stat
, 0);
705 wait_for_servers(void)
708 int retry_count
= 10;
709 mach_port_t bsport
, servport
;
712 /* find server port */
713 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
714 if (KERN_SUCCESS
!= ret
) {
715 mach_error("task_get_bootstrap_port(): ", ret
);
719 while (retry_count
-- > 0) {
720 for (i
= 0; i
< num_servers
; i
++) {
721 ret
= bootstrap_look_up(bsport
,
724 if (ret
!= KERN_SUCCESS
) {
728 if (ret
== KERN_SUCCESS
)
730 usleep(100 * 1000); /* 100ms */
732 fprintf(stderr
, "Server(s) failed to register\n");
736 int main(int argc
, char *argv
[])
740 thread_id_t
*client_id
;
741 thread_id_t
*server_id
;
743 signal(SIGINT
, signal_handler
);
744 parse_args(argc
, argv
);
746 calibrate_client_work();
749 * If we're using affinity create an empty namespace now
750 * so this is shared by all our offspring.
755 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
756 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
758 printf("creating %d servers\n", num_servers
);
759 for (i
= 0; i
< num_servers
; i
++) {
760 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
761 /* PORT names include pid of main process for disambiguation */
762 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
763 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
766 int totalclients
= num_servers
* num_clients
;
767 int totalmsg
= num_msgs
* totalclients
;
768 struct timeval starttv
, endtv
, deltatv
;
771 * Wait for all servers to have registered all ports before starting
772 * the clients and the clock.
776 printf("%d server%s, %d client%s per server (%d total) %u messages...",
777 num_servers
, (num_servers
> 1)? "s" : "",
778 num_clients
, (num_clients
> 1)? "s" : "",
783 /* Call gettimeofday() once and throw away result; some implementations
784 * (like Mach's) cache some time zone info on first call.
786 gettimeofday(&starttv
, NULL
);
787 gettimeofday(&starttv
, NULL
);
789 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
791 printf("creating %d clients\n", totalclients
);
792 for (i
= 0; i
< num_servers
; i
++) {
793 for (j
= 0; j
< num_clients
; j
++) {
795 &client_id
[(i
*num_clients
) + j
],
801 /* Wait for servers to complete */
802 for (i
= 0; i
< num_servers
; i
++) {
803 thread_join(&server_id
[i
]);
806 gettimeofday(&endtv
, NULL
);
808 for (i
= 0; i
< totalclients
; i
++) {
809 thread_join(&client_id
[i
]);
813 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
814 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
815 if (endtv
.tv_usec
< starttv
.tv_usec
) {
817 deltatv
.tv_usec
+= 1000000;
820 double dsecs
= (double) deltatv
.tv_sec
+
821 1.0E-6 * (double) deltatv
.tv_usec
;
823 printf(" in %ld.%03u seconds\n",
824 (long)deltatv
.tv_sec
, deltatv
.tv_usec
/1000);
825 printf(" throughput in messages/sec: %g\n",
826 (double)totalmsg
/ dsecs
);
827 printf(" average message latency (usec): %2.3g\n",
828 dsecs
* 1.0E6
/ (double) totalmsg
);