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 #include "../unit_tests/tests_common.h"
24 #define MAX(A, B) ((A) < (B) ? (B) : (A))
28 mach_msg_header_t header
;
29 mach_msg_trailer_t trailer
; // subtract this when sending
30 } ipc_trivial_message
;
33 mach_msg_header_t header
;
35 mach_msg_trailer_t trailer
; // subtract this when sending
39 mach_msg_header_t header
;
41 mach_msg_ool_descriptor_t descriptor
;
42 mach_msg_trailer_t trailer
; // subtract this when sending
43 } ipc_complex_message
;
54 mach_msg_header_t
*req_msg
;
56 mach_msg_header_t
*reply_msg
;
67 static boolean_t verbose
= FALSE
;
68 static boolean_t affinity
= FALSE
;
69 static boolean_t timeshare
= FALSE
;
70 static boolean_t threaded
= FALSE
;
71 static boolean_t oneway
= FALSE
;
72 static boolean_t do_select
= FALSE
;
73 static boolean_t save_perfdata
= FALSE
;
83 char **server_port_name
;
85 void signal_handler(int sig
) {
88 void usage(const char *progname
) {
89 fprintf(stderr
, "usage: %s [options]\n", progname
);
90 fprintf(stderr
, "where options are:\n");
91 fprintf(stderr
, " -affinity\t\tthreads use affinity\n");
92 fprintf(stderr
, " -timeshare\t\tthreads use timeshare\n");
93 fprintf(stderr
, " -threaded\t\tuse (p)threads\n");
94 fprintf(stderr
, " -verbose\t\tbe verbose\n");
95 fprintf(stderr
, " -oneway\t\tdo not request return reply\n");
96 fprintf(stderr
, " -count num\t\tnumber of messages to send\n");
97 fprintf(stderr
, " -type trivial|inline|complex\ttype of messages to send\n");
98 fprintf(stderr
, " -numints num\tnumber of 32-bit ints to send in messages\n");
99 fprintf(stderr
, " -servers num\tnumber of servers threads to run\n");
100 fprintf(stderr
, " -clients num\tnumber of clients per server\n");
101 fprintf(stderr
, " -delay num\t\tmicroseconds to sleep clients between messages\n");
102 fprintf(stderr
, " -work num\t\tmicroseconds of client work\n");
103 fprintf(stderr
, " -pages num\t\tpages of memory touched by client work\n");
104 fprintf(stderr
, " -select \t\tselect prior to calling kevent().\n");
105 fprintf(stderr
, " -perf \t\tCreate perfdata files for metrics.\n");
106 fprintf(stderr
, "default values are:\n");
107 fprintf(stderr
, " . no affinity\n");
108 fprintf(stderr
, " . not timeshare\n");
109 fprintf(stderr
, " . not verbose\n");
110 fprintf(stderr
, " . not oneway\n");
111 fprintf(stderr
, " . client sends 100000 messages\n");
112 fprintf(stderr
, " . inline message type\n");
113 fprintf(stderr
, " . 64 32-bit integers in inline/complex messages\n");
114 fprintf(stderr
, " . (num_available_processors+1)%%2 servers\n");
115 fprintf(stderr
, " . 4 clients per server\n");
116 fprintf(stderr
, " . no delay\n");
120 void parse_args(int argc
, char *argv
[]) {
121 host_basic_info_data_t info
;
122 mach_msg_type_number_t count
;
123 kern_return_t result
;
125 /* Initialize defaults */
126 msg_type
= msg_type_trivial
;
132 count
= HOST_BASIC_INFO_COUNT
;
133 result
= host_info(mach_host_self(), HOST_BASIC_INFO
,
134 (host_info_t
)&info
, &count
);
135 if (result
== KERN_SUCCESS
&& info
.avail_cpus
> 1)
136 num_servers
= info
.avail_cpus
/ 2;
140 const char *progname
= argv
[0];
143 if (0 == strcmp("-verbose", argv
[0])) {
146 } else if (0 == strcmp("-affinity", argv
[0])) {
149 } else if (0 == strcmp("-timeshare", argv
[0])) {
152 } else if (0 == strcmp("-threaded", argv
[0])) {
155 } else if (0 == strcmp("-oneway", argv
[0])) {
158 } else if (0 == strcmp("-type", argv
[0])) {
161 if (0 == strcmp("trivial", argv
[1])) {
162 msg_type
= msg_type_trivial
;
163 } else if (0 == strcmp("inline", argv
[1])) {
164 msg_type
= msg_type_inline
;
165 } else if (0 == strcmp("complex", argv
[1])) {
166 msg_type
= msg_type_complex
;
169 argc
-= 2; argv
+= 2;
170 } else if (0 == strcmp("-numints", argv
[0])) {
173 num_ints
= strtoul(argv
[1], NULL
, 0);
174 argc
-= 2; argv
+= 2;
175 } else if (0 == strcmp("-count", argv
[0])) {
178 num_msgs
= strtoul(argv
[1], NULL
, 0);
179 argc
-= 2; argv
+= 2;
180 } else if (0 == strcmp("-clients", argv
[0])) {
183 num_clients
= strtoul(argv
[1], NULL
, 0);
184 argc
-= 2; argv
+= 2;
185 } else if (0 == strcmp("-servers", argv
[0])) {
188 num_servers
= strtoul(argv
[1], NULL
, 0);
189 argc
-= 2; argv
+= 2;
190 } else if (0 == strcmp("-delay", argv
[0])) {
193 client_delay
= strtoul(argv
[1], NULL
, 0);
194 argc
-= 2; argv
+= 2;
195 } else if (0 == strcmp("-spin", argv
[0])) {
198 client_spin
= strtoul(argv
[1], NULL
, 0);
199 argc
-= 2; argv
+= 2;
200 } else if (0 == strcmp("-pages", argv
[0])) {
203 client_pages
= strtoul(argv
[1], NULL
, 0);
204 argc
-= 2; argv
+= 2;
205 } else if (0 == strcmp("-select", argv
[0])) {
208 } else if (0 == strcmp("-perf", argv
[0])) {
209 save_perfdata
= TRUE
;
216 void setup_server_ports(struct port_args
*ports
)
218 kern_return_t ret
= 0;
221 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
222 sizeof(u_int32_t
) * num_ints
,
223 sizeof(ipc_complex_message
));
224 ports
->reply_size
= sizeof(ipc_trivial_message
) -
225 sizeof(mach_msg_trailer_t
);
226 ports
->req_msg
= malloc(ports
->req_size
);
227 ports
->reply_msg
= malloc(ports
->reply_size
);
229 ret
= mach_port_allocate(mach_task_self(),
230 MACH_PORT_RIGHT_RECEIVE
,
232 if (KERN_SUCCESS
!= ret
) {
233 mach_error("mach_port_allocate(): ", ret
);
237 ret
= mach_port_allocate(mach_task_self(),
238 MACH_PORT_RIGHT_PORT_SET
,
240 if (KERN_SUCCESS
!= ret
) {
241 mach_error("mach_port_allocate(): ", ret
);
245 ret
= mach_port_insert_member(mach_task_self(),
248 if (KERN_SUCCESS
!= ret
) {
249 mach_error("mach_port_insert_member(): ", ret
);
253 ret
= mach_port_insert_right(mach_task_self(),
256 MACH_MSG_TYPE_MAKE_SEND
);
257 if (KERN_SUCCESS
!= ret
) {
258 mach_error("mach_port_insert_right(): ", ret
);
262 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
263 if (KERN_SUCCESS
!= ret
) {
264 mach_error("task_get_bootstrap_port(): ", ret
);
269 printf("server waiting for IPC messages from client on port '%s'.\n",
270 server_port_name
[ports
->server_num
]);
272 ret
= bootstrap_register(bsport
,
273 server_port_name
[ports
->server_num
],
275 if (KERN_SUCCESS
!= ret
) {
276 mach_error("bootstrap_register(): ", ret
);
281 void setup_client_ports(struct port_args
*ports
)
283 kern_return_t ret
= 0;
285 case msg_type_trivial
:
286 ports
->req_size
= sizeof(ipc_trivial_message
);
288 case msg_type_inline
:
289 ports
->req_size
= sizeof(ipc_inline_message
) +
290 sizeof(u_int32_t
) * num_ints
;
292 case msg_type_complex
:
293 ports
->req_size
= sizeof(ipc_complex_message
);
296 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
297 ports
->reply_size
= sizeof(ipc_trivial_message
);
298 ports
->req_msg
= malloc(ports
->req_size
);
299 ports
->reply_msg
= malloc(ports
->reply_size
);
301 ret
= mach_port_allocate(mach_task_self(),
302 MACH_PORT_RIGHT_RECEIVE
,
304 if (KERN_SUCCESS
!= ret
) {
305 mach_error("mach_port_allocate(): ", ret
);
309 printf("Client sending %d %s IPC messages to port '%s' in %s mode.\n",
310 num_msgs
, (msg_type
== msg_type_inline
) ?
311 "inline" : ((msg_type
== msg_type_complex
) ?
312 "complex" : "trivial"),
313 server_port_name
[ports
->server_num
],
314 (oneway
? "oneway" : "rpc"));
321 thread_setup(int tag
) {
323 thread_extended_policy_data_t epolicy
;
324 thread_affinity_policy_data_t policy
;
327 epolicy
.timeshare
= FALSE
;
328 ret
= thread_policy_set(
329 mach_thread_self(), THREAD_EXTENDED_POLICY
,
330 (thread_policy_t
) &epolicy
,
331 THREAD_EXTENDED_POLICY_COUNT
);
332 if (ret
!= KERN_SUCCESS
)
333 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
337 policy
.affinity_tag
= tag
;
338 ret
= thread_policy_set(
339 mach_thread_self(), THREAD_AFFINITY_POLICY
,
340 (thread_policy_t
) &policy
,
341 THREAD_AFFINITY_POLICY_COUNT
);
342 if (ret
!= KERN_SUCCESS
)
343 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
348 server(void *serverarg
)
351 struct kevent64_s kev
[1];
354 struct port_args args
;
357 int totalmsg
= num_msgs
* num_clients
;
360 args
.server_num
= (int) (long) serverarg
;
361 setup_server_ports(&args
);
363 thread_setup(args
.server_num
+ 1);
370 EV_SET64(&kev
[0], args
.pset
, EVFILT_MACHPORT
, (EV_ADD
| EV_CLEAR
| EV_DISPATCH
),
372 MACH_RCV_MSG
|MACH_RCV_LARGE
, 0, 0, (mach_vm_address_t
)args
.req_msg
, args
.req_size
);
376 err
= kevent64(kq
, kev
, 1, NULL
, 0, 0, NULL
);
382 for (idx
= 0; idx
< totalmsg
; idx
++) {
385 printf("server awaiting message %d\n", idx
);
389 FD_SET(kq
, &readfds
);
392 printf("Calling select() prior to kevent64().\n");
394 count
= select(kq
+ 1, &readfds
, NULL
, NULL
, NULL
);
401 EV_SET64(&kev
[0], args
.pset
, EVFILT_MACHPORT
, EV_ENABLE
,
403 MACH_RCV_MSG
|MACH_RCV_LARGE
, 0, 0, (mach_vm_address_t
)args
.req_msg
, args
.req_size
);
407 err
= kevent64(kq
, kev
, 1, kev
, 1, 0, NULL
);
413 // printf("kevent64: returned zero\n");
419 if (MACH_MSG_SUCCESS
!= ret
) {
421 printf("kevent64() mach_msg_return=%d", ret
);
422 mach_error("kevent64 (msg receive): ", ret
);
426 if (kev
[0].data
!= args
.port
)
427 printf("kevent64(MACH_PORT_NULL) port name (%lld) != expected (0x%x)\n", kev
[0].data
, args
.port
);
429 args
.req_msg
->msgh_bits
= 0;
430 args
.req_msg
->msgh_size
= args
.req_size
;
431 args
.req_msg
->msgh_local_port
= args
.port
;
432 ret
= mach_msg(args
.req_msg
,
433 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
|MACH_RCV_LARGE
,
437 MACH_MSG_TIMEOUT_NONE
,
439 if (MACH_RCV_INTERRUPTED
== ret
)
441 if (MACH_MSG_SUCCESS
!= ret
) {
443 printf("mach_msg() ret=%d", ret
);
444 mach_error("mach_msg (receive): ", ret
);
449 printf("server received message %d\n", idx
);
450 if (args
.req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
451 ret
= vm_deallocate(mach_task_self(),
452 (vm_address_t
)((ipc_complex_message
*)args
.req_msg
)->descriptor
.address
,
453 ((ipc_complex_message
*)args
.req_msg
)->descriptor
.size
);
456 if (1 == args
.req_msg
->msgh_id
) {
458 printf("server sending reply %d\n", idx
);
459 args
.reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
460 MACH_MSG_TYPE_MAKE_SEND
);
461 args
.reply_msg
->msgh_size
= args
.reply_size
;
462 args
.reply_msg
->msgh_remote_port
= args
.req_msg
->msgh_remote_port
;
463 args
.reply_msg
->msgh_local_port
= args
.req_msg
->msgh_local_port
;
464 args
.reply_msg
->msgh_id
= 2;
465 ret
= mach_msg(args
.reply_msg
,
470 MACH_MSG_TIMEOUT_NONE
,
472 if (MACH_MSG_SUCCESS
!= ret
) {
473 mach_error("mach_msg (send): ", ret
);
482 client_spin_loop(unsigned count
, void (fn
)(void))
488 static long dummy_memory
;
489 static long *client_memory
= &dummy_memory
;
491 client_work_atom(void)
495 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long))
497 client_memory
[i
] = 0;
500 static int calibration_count
= 10000;
501 static int calibration_usec
;
503 calibrate_client_work(void)
506 struct timeval nowtv
;
507 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
508 struct timeval starttv
;
509 struct timeval endtv
;
512 /* Warm-up the stepper first... */
513 gettimeofday(&nowtv
, NULL
);
514 timeradd(&nowtv
, &warmuptv
, &endtv
);
516 client_spin_loop(calibration_count
, client_work_atom
);
517 gettimeofday(&nowtv
, NULL
);
518 } while (timercmp(&nowtv
, &endtv
, < ));
520 /* Now do the calibration */
522 gettimeofday(&starttv
, NULL
);
523 client_spin_loop(calibration_count
, client_work_atom
);
524 gettimeofday(&endtv
, NULL
);
525 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
526 calibration_count
/= 10;
529 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
530 if (endtv
.tv_usec
< starttv
.tv_usec
) {
531 calibration_usec
+= 1000000;
533 if (calibration_usec
< 1000) {
534 calibration_count
*= 10;
537 calibration_count
/= calibration_usec
;
541 printf("calibration_count=%d calibration_usec=%d\n",
542 calibration_count
, calibration_usec
);
552 client_spin_loop(calibration_count
*client_spin
,
557 usleep(client_delay
);
562 void *client(void *threadarg
)
564 struct port_args args
;
566 mach_msg_header_t
*req
, *reply
;
567 mach_port_t bsport
, servport
;
569 int server_num
= (int) threadarg
;
570 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
573 printf("client(%d) started, server port name %s\n",
574 server_num
, server_port_name
[server_num
]);
576 args
.server_num
= server_num
;
577 thread_setup(server_num
+ 1);
579 /* find server port */
580 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
581 if (KERN_SUCCESS
!= ret
) {
582 mach_error("task_get_bootstrap_port(): ", ret
);
585 ret
= bootstrap_look_up(bsport
,
586 server_port_name
[server_num
],
588 if (KERN_SUCCESS
!= ret
) {
589 mach_error("bootstrap_look_up(): ", ret
);
593 setup_client_ports(&args
);
595 /* Allocate and touch memory */
598 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
599 for (i
= 0; i
< client_pages
; i
++)
600 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
603 /* start message loop */
604 for (idx
= 0; idx
< num_msgs
; idx
++) {
606 reply
= args
.reply_msg
;
608 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
609 MACH_MSG_TYPE_MAKE_SEND
);
610 req
->msgh_size
= args
.req_size
;
611 req
->msgh_remote_port
= servport
;
612 req
->msgh_local_port
= args
.port
;
613 req
->msgh_id
= oneway
? 0 : 1;
614 if (msg_type
== msg_type_complex
) {
615 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
616 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
617 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
618 ((ipc_complex_message
*)req
)->descriptor
.size
=
619 num_ints
* sizeof(u_int32_t
);
620 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
621 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
622 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
625 printf("client sending message %d\n", idx
);
631 MACH_MSG_TIMEOUT_NONE
,
633 if (MACH_MSG_SUCCESS
!= ret
) {
634 mach_error("mach_msg (send): ", ret
);
635 fprintf(stderr
, "bailing after %u iterations\n", idx
);
641 printf("client awaiting reply %d\n", idx
);
642 reply
->msgh_bits
= 0;
643 reply
->msgh_size
= args
.reply_size
;
644 reply
->msgh_local_port
= args
.port
;
645 ret
= mach_msg(args
.reply_msg
,
646 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
,
650 MACH_MSG_TIMEOUT_NONE
,
652 if (MACH_MSG_SUCCESS
!= ret
) {
653 mach_error("mach_msg (receive): ", ret
);
654 fprintf(stderr
, "bailing after %u iterations\n",
659 printf("client received reply %d\n", idx
);
670 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
) {
673 ret
= pthread_create(
679 err(1, "pthread_create()");
681 printf("created pthread %p\n", thread
->tid
);
683 thread
->pid
= fork();
684 if (thread
->pid
== 0) {
686 printf("calling %p(%p)\n", fn
, arg
);
691 printf("forked pid %d\n", thread
->pid
);
696 thread_join(thread_id_t
*thread
) {
700 printf("joining thread %p\n", thread
->tid
);
701 ret
= pthread_join(thread
->tid
, NULL
);
702 if (ret
!= KERN_SUCCESS
)
703 err(1, "pthread_join(%p)", thread
->tid
);
707 printf("waiting for pid %d\n", thread
->pid
);
708 waitpid(thread
->pid
, &stat
, 0);
713 wait_for_servers(void)
716 int retry_count
= 10;
717 mach_port_t bsport
, servport
;
720 /* find server port */
721 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
722 if (KERN_SUCCESS
!= ret
) {
723 mach_error("task_get_bootstrap_port(): ", ret
);
727 while (retry_count
-- > 0) {
728 for (i
= 0; i
< num_servers
; i
++) {
729 ret
= bootstrap_look_up(bsport
,
732 if (ret
!= KERN_SUCCESS
) {
736 if (ret
== KERN_SUCCESS
)
738 usleep(100 * 1000); /* 100ms */
740 fprintf(stderr
, "Server(s) failed to register\n");
745 int main(int argc
, char *argv
[])
749 thread_id_t
*client_id
;
750 thread_id_t
*server_id
;
752 signal(SIGINT
, signal_handler
);
753 parse_args(argc
, argv
);
755 calibrate_client_work();
758 * If we're using affinity create an empty namespace now
759 * so this is shared by all our offspring.
764 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
765 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
767 printf("creating %d servers\n", num_servers
);
768 for (i
= 0; i
< num_servers
; i
++) {
769 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
770 /* PORT names include pid of main process for disambiguation */
771 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
772 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
775 int totalclients
= num_servers
* num_clients
;
776 int totalmsg
= num_msgs
* totalclients
;
777 struct timeval starttv
, endtv
, deltatv
;
780 * Wait for all servers to have registered all ports before starting
781 * the clients and the clock.
785 printf("%d server%s, %d client%s per server (%d total) %u messages...",
786 num_servers
, (num_servers
> 1)? "s" : "",
787 num_clients
, (num_clients
> 1)? "s" : "",
792 /* Call gettimeofday() once and throw away result; some implementations
793 * (like Mach's) cache some time zone info on first call.
795 gettimeofday(&starttv
, NULL
);
796 gettimeofday(&starttv
, NULL
);
798 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
800 printf("creating %d clients\n", totalclients
);
801 for (i
= 0; i
< num_servers
; i
++) {
802 for (j
= 0; j
< num_clients
; j
++) {
804 &client_id
[(i
*num_clients
) + j
],
810 /* Wait for servers to complete */
811 for (i
= 0; i
< num_servers
; i
++) {
812 thread_join(&server_id
[i
]);
815 gettimeofday(&endtv
, NULL
);
817 for (i
= 0; i
< totalclients
; i
++) {
818 thread_join(&client_id
[i
]);
822 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
823 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
824 if (endtv
.tv_usec
< starttv
.tv_usec
) {
826 deltatv
.tv_usec
+= 1000000;
829 double dsecs
= (double) deltatv
.tv_sec
+
830 1.0E-6 * (double) deltatv
.tv_usec
;
832 double time_in_sec
= (double)deltatv
.tv_sec
+ (double)deltatv
.tv_usec
/1000.0;
833 double throughput_msg_p_sec
= (double) totalmsg
/dsecs
;
834 double avg_msg_latency
= dsecs
*1.0E6
/ (double)totalmsg
;
836 printf(" in %ld.%03u seconds\n",
837 (long)deltatv
.tv_sec
, deltatv
.tv_usec
/1000);
838 printf(" throughput in messages/sec: %g\n",
839 (double)totalmsg
/ dsecs
);
840 printf(" average message latency (usec): %2.3g\n",
841 dsecs
* 1.0E6
/ (double) totalmsg
);
843 if (save_perfdata
== TRUE
) {
844 record_perf_data("kqmpmm_avg_msg_latency", "usec", avg_msg_latency
, "Message latency measured in microseconds. Lower is better", stderr
);