1 #include <AvailabilityMacros.h>
2 #include <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/select.h>
19 #include <sys/types.h>
21 #include <sys/signal.h>
23 #include "../unit_tests/tests_common.h"
25 #define MAX(A, B) ((A) < (B) ? (B) : (A))
29 mach_msg_header_t header
;
30 mach_msg_trailer_t trailer
; // subtract this when sending
31 } ipc_trivial_message
;
34 mach_msg_header_t header
;
36 mach_msg_trailer_t trailer
; // subtract this when sending
40 mach_msg_header_t header
;
42 mach_msg_ool_descriptor_t descriptor
;
43 mach_msg_trailer_t trailer
; // subtract this when sending
44 } ipc_complex_message
;
55 mach_msg_header_t
*req_msg
;
57 mach_msg_header_t
*reply_msg
;
68 static boolean_t verbose
= FALSE
;
69 static boolean_t affinity
= FALSE
;
70 static boolean_t timeshare
= FALSE
;
71 static boolean_t threaded
= FALSE
;
72 static boolean_t oneway
= FALSE
;
73 static boolean_t do_select
= FALSE
;
74 static boolean_t save_perfdata
= FALSE
;
84 char **server_port_name
;
86 void signal_handler(int sig
) {
89 void usage(const char *progname
) {
90 fprintf(stderr
, "usage: %s [options]\n", progname
);
91 fprintf(stderr
, "where options are:\n");
92 fprintf(stderr
, " -affinity\t\tthreads use affinity\n");
93 fprintf(stderr
, " -timeshare\t\tthreads use timeshare\n");
94 fprintf(stderr
, " -threaded\t\tuse (p)threads\n");
95 fprintf(stderr
, " -verbose\t\tbe verbose\n");
96 fprintf(stderr
, " -oneway\t\tdo not request return reply\n");
97 fprintf(stderr
, " -count num\t\tnumber of messages to send\n");
98 fprintf(stderr
, " -type trivial|inline|complex\ttype of messages to send\n");
99 fprintf(stderr
, " -numints num\tnumber of 32-bit ints to send in messages\n");
100 fprintf(stderr
, " -servers num\tnumber of servers threads to run\n");
101 fprintf(stderr
, " -clients num\tnumber of clients per server\n");
102 fprintf(stderr
, " -delay num\t\tmicroseconds to sleep clients between messages\n");
103 fprintf(stderr
, " -work num\t\tmicroseconds of client work\n");
104 fprintf(stderr
, " -pages num\t\tpages of memory touched by client work\n");
105 fprintf(stderr
, " -select \t\tselect prior to calling kevent().\n");
106 fprintf(stderr
, " -perf \t\tCreate perfdata files for metrics.\n");
107 fprintf(stderr
, "default values are:\n");
108 fprintf(stderr
, " . no affinity\n");
109 fprintf(stderr
, " . not timeshare\n");
110 fprintf(stderr
, " . not verbose\n");
111 fprintf(stderr
, " . not oneway\n");
112 fprintf(stderr
, " . client sends 100000 messages\n");
113 fprintf(stderr
, " . inline message type\n");
114 fprintf(stderr
, " . 64 32-bit integers in inline/complex messages\n");
115 fprintf(stderr
, " . (num_available_processors+1)%%2 servers\n");
116 fprintf(stderr
, " . 4 clients per server\n");
117 fprintf(stderr
, " . no delay\n");
121 void parse_args(int argc
, char *argv
[]) {
122 host_basic_info_data_t info
;
123 mach_msg_type_number_t count
;
124 kern_return_t result
;
126 /* Initialize defaults */
127 msg_type
= msg_type_trivial
;
133 count
= HOST_BASIC_INFO_COUNT
;
134 result
= host_info(mach_host_self(), HOST_BASIC_INFO
,
135 (host_info_t
)&info
, &count
);
136 if (result
== KERN_SUCCESS
&& info
.avail_cpus
> 1)
137 num_servers
= info
.avail_cpus
/ 2;
141 const char *progname
= argv
[0];
144 if (0 == strcmp("-verbose", argv
[0])) {
147 } else if (0 == strcmp("-affinity", argv
[0])) {
150 } else if (0 == strcmp("-timeshare", argv
[0])) {
153 } else if (0 == strcmp("-threaded", argv
[0])) {
156 } else if (0 == strcmp("-oneway", argv
[0])) {
159 } else if (0 == strcmp("-type", argv
[0])) {
162 if (0 == strcmp("trivial", argv
[1])) {
163 msg_type
= msg_type_trivial
;
164 } else if (0 == strcmp("inline", argv
[1])) {
165 msg_type
= msg_type_inline
;
166 } else if (0 == strcmp("complex", argv
[1])) {
167 msg_type
= msg_type_complex
;
170 argc
-= 2; argv
+= 2;
171 } else if (0 == strcmp("-numints", argv
[0])) {
174 num_ints
= strtoul(argv
[1], NULL
, 0);
175 argc
-= 2; argv
+= 2;
176 } else if (0 == strcmp("-count", argv
[0])) {
179 num_msgs
= strtoul(argv
[1], NULL
, 0);
180 argc
-= 2; argv
+= 2;
181 } else if (0 == strcmp("-clients", argv
[0])) {
184 num_clients
= strtoul(argv
[1], NULL
, 0);
185 argc
-= 2; argv
+= 2;
186 } else if (0 == strcmp("-servers", argv
[0])) {
189 num_servers
= strtoul(argv
[1], NULL
, 0);
190 argc
-= 2; argv
+= 2;
191 } else if (0 == strcmp("-delay", argv
[0])) {
194 client_delay
= strtoul(argv
[1], NULL
, 0);
195 argc
-= 2; argv
+= 2;
196 } else if (0 == strcmp("-spin", argv
[0])) {
199 client_spin
= strtoul(argv
[1], NULL
, 0);
200 argc
-= 2; argv
+= 2;
201 } else if (0 == strcmp("-pages", argv
[0])) {
204 client_pages
= strtoul(argv
[1], NULL
, 0);
205 argc
-= 2; argv
+= 2;
206 } else if (0 == strcmp("-select", argv
[0])) {
209 } else if (0 == strcmp("-perf", argv
[0])) {
210 save_perfdata
= TRUE
;
217 void setup_server_ports(struct port_args
*ports
)
219 kern_return_t ret
= 0;
222 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
223 sizeof(u_int32_t
) * num_ints
,
224 sizeof(ipc_complex_message
));
225 ports
->reply_size
= sizeof(ipc_trivial_message
) -
226 sizeof(mach_msg_trailer_t
);
227 ports
->req_msg
= malloc(ports
->req_size
);
228 ports
->reply_msg
= malloc(ports
->reply_size
);
230 ret
= mach_port_allocate(mach_task_self(),
231 MACH_PORT_RIGHT_RECEIVE
,
233 if (KERN_SUCCESS
!= ret
) {
234 mach_error("mach_port_allocate(): ", ret
);
238 ret
= mach_port_allocate(mach_task_self(),
239 MACH_PORT_RIGHT_PORT_SET
,
241 if (KERN_SUCCESS
!= ret
) {
242 mach_error("mach_port_allocate(): ", ret
);
246 ret
= mach_port_insert_member(mach_task_self(),
249 if (KERN_SUCCESS
!= ret
) {
250 mach_error("mach_port_insert_member(): ", ret
);
254 ret
= mach_port_insert_right(mach_task_self(),
257 MACH_MSG_TYPE_MAKE_SEND
);
258 if (KERN_SUCCESS
!= ret
) {
259 mach_error("mach_port_insert_right(): ", ret
);
263 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
264 if (KERN_SUCCESS
!= ret
) {
265 mach_error("task_get_bootstrap_port(): ", ret
);
270 printf("server waiting for IPC messages from client on port '%s'.\n",
271 server_port_name
[ports
->server_num
]);
273 ret
= bootstrap_register(bsport
,
274 server_port_name
[ports
->server_num
],
276 if (KERN_SUCCESS
!= ret
) {
277 mach_error("bootstrap_register(): ", ret
);
282 void setup_client_ports(struct port_args
*ports
)
284 kern_return_t ret
= 0;
286 case msg_type_trivial
:
287 ports
->req_size
= sizeof(ipc_trivial_message
);
289 case msg_type_inline
:
290 ports
->req_size
= sizeof(ipc_inline_message
) +
291 sizeof(u_int32_t
) * num_ints
;
293 case msg_type_complex
:
294 ports
->req_size
= sizeof(ipc_complex_message
);
297 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
298 ports
->reply_size
= sizeof(ipc_trivial_message
);
299 ports
->req_msg
= malloc(ports
->req_size
);
300 ports
->reply_msg
= malloc(ports
->reply_size
);
302 ret
= mach_port_allocate(mach_task_self(),
303 MACH_PORT_RIGHT_RECEIVE
,
305 if (KERN_SUCCESS
!= ret
) {
306 mach_error("mach_port_allocate(): ", ret
);
310 printf("Client sending %d %s IPC messages to port '%s' in %s mode.\n",
311 num_msgs
, (msg_type
== msg_type_inline
) ?
312 "inline" : ((msg_type
== msg_type_complex
) ?
313 "complex" : "trivial"),
314 server_port_name
[ports
->server_num
],
315 (oneway
? "oneway" : "rpc"));
322 thread_setup(int tag
) {
324 thread_extended_policy_data_t epolicy
;
325 thread_affinity_policy_data_t policy
;
328 epolicy
.timeshare
= FALSE
;
329 ret
= thread_policy_set(
330 mach_thread_self(), THREAD_EXTENDED_POLICY
,
331 (thread_policy_t
) &epolicy
,
332 THREAD_EXTENDED_POLICY_COUNT
);
333 if (ret
!= KERN_SUCCESS
)
334 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
338 policy
.affinity_tag
= tag
;
339 ret
= thread_policy_set(
340 mach_thread_self(), THREAD_AFFINITY_POLICY
,
341 (thread_policy_t
) &policy
,
342 THREAD_AFFINITY_POLICY_COUNT
);
343 if (ret
!= KERN_SUCCESS
)
344 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
349 server(void *serverarg
)
352 struct kevent64_s kev
[1];
355 struct port_args args
;
358 int totalmsg
= num_msgs
* num_clients
;
361 args
.server_num
= (int) (long) serverarg
;
362 setup_server_ports(&args
);
364 thread_setup(args
.server_num
+ 1);
371 EV_SET64(&kev
[0], args
.pset
, EVFILT_MACHPORT
, (EV_ADD
| EV_CLEAR
| EV_DISPATCH
),
373 MACH_RCV_MSG
|MACH_RCV_LARGE
, 0, 0, (mach_vm_address_t
)args
.req_msg
, args
.req_size
);
377 err
= kevent64(kq
, kev
, 1, NULL
, 0, 0, NULL
);
383 for (idx
= 0; idx
< totalmsg
; idx
++) {
386 printf("server awaiting message %d\n", idx
);
390 FD_SET(kq
, &readfds
);
393 printf("Calling select() prior to kevent64().\n");
395 count
= select(kq
+ 1, &readfds
, NULL
, NULL
, NULL
);
402 EV_SET64(&kev
[0], args
.pset
, EVFILT_MACHPORT
, EV_ENABLE
,
404 MACH_RCV_MSG
|MACH_RCV_LARGE
, 0, 0, (mach_vm_address_t
)args
.req_msg
, args
.req_size
);
408 err
= kevent64(kq
, kev
, 1, kev
, 1, 0, NULL
);
414 // printf("kevent64: returned zero\n");
420 if (MACH_MSG_SUCCESS
!= ret
) {
422 printf("kevent64() mach_msg_return=%d", ret
);
423 mach_error("kevent64 (msg receive): ", ret
);
427 if (kev
[0].data
!= args
.port
)
428 printf("kevent64(MACH_PORT_NULL) port name (%lld) != expected (0x%x)\n", kev
[0].data
, args
.port
);
430 args
.req_msg
->msgh_bits
= 0;
431 args
.req_msg
->msgh_size
= args
.req_size
;
432 args
.req_msg
->msgh_local_port
= args
.port
;
433 ret
= mach_msg(args
.req_msg
,
434 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
|MACH_RCV_LARGE
,
438 MACH_MSG_TIMEOUT_NONE
,
440 if (MACH_RCV_INTERRUPTED
== ret
)
442 if (MACH_MSG_SUCCESS
!= ret
) {
444 printf("mach_msg() ret=%d", ret
);
445 mach_error("mach_msg (receive): ", ret
);
450 printf("server received message %d\n", idx
);
451 if (args
.req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
452 ret
= vm_deallocate(mach_task_self(),
453 (vm_address_t
)((ipc_complex_message
*)args
.req_msg
)->descriptor
.address
,
454 ((ipc_complex_message
*)args
.req_msg
)->descriptor
.size
);
457 if (1 == args
.req_msg
->msgh_id
) {
459 printf("server sending reply %d\n", idx
);
460 args
.reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
461 MACH_MSG_TYPE_MAKE_SEND
);
462 args
.reply_msg
->msgh_size
= args
.reply_size
;
463 args
.reply_msg
->msgh_remote_port
= args
.req_msg
->msgh_remote_port
;
464 args
.reply_msg
->msgh_local_port
= args
.req_msg
->msgh_local_port
;
465 args
.reply_msg
->msgh_id
= 2;
466 ret
= mach_msg(args
.reply_msg
,
471 MACH_MSG_TIMEOUT_NONE
,
473 if (MACH_MSG_SUCCESS
!= ret
) {
474 mach_error("mach_msg (send): ", ret
);
483 client_spin_loop(unsigned count
, void (fn
)(void))
489 static long dummy_memory
;
490 static long *client_memory
= &dummy_memory
;
492 client_work_atom(void)
496 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long))
498 client_memory
[i
] = 0;
501 static int calibration_count
= 10000;
502 static int calibration_usec
;
504 calibrate_client_work(void)
507 struct timeval nowtv
;
508 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
509 struct timeval starttv
;
510 struct timeval endtv
;
513 /* Warm-up the stepper first... */
514 gettimeofday(&nowtv
, NULL
);
515 timeradd(&nowtv
, &warmuptv
, &endtv
);
517 client_spin_loop(calibration_count
, client_work_atom
);
518 gettimeofday(&nowtv
, NULL
);
519 } while (timercmp(&nowtv
, &endtv
, < ));
521 /* Now do the calibration */
523 gettimeofday(&starttv
, NULL
);
524 client_spin_loop(calibration_count
, client_work_atom
);
525 gettimeofday(&endtv
, NULL
);
526 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
527 calibration_count
/= 10;
530 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
531 if (endtv
.tv_usec
< starttv
.tv_usec
) {
532 calibration_usec
+= 1000000;
534 if (calibration_usec
< 1000) {
535 calibration_count
*= 10;
538 calibration_count
/= calibration_usec
;
542 printf("calibration_count=%d calibration_usec=%d\n",
543 calibration_count
, calibration_usec
);
553 client_spin_loop(calibration_count
*client_spin
,
558 usleep(client_delay
);
563 void *client(void *threadarg
)
565 struct port_args args
;
567 mach_msg_header_t
*req
, *reply
;
568 mach_port_t bsport
, servport
;
570 int server_num
= (int) threadarg
;
571 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
574 printf("client(%d) started, server port name %s\n",
575 server_num
, server_port_name
[server_num
]);
577 args
.server_num
= server_num
;
578 thread_setup(server_num
+ 1);
580 /* find server port */
581 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
582 if (KERN_SUCCESS
!= ret
) {
583 mach_error("task_get_bootstrap_port(): ", ret
);
586 ret
= bootstrap_look_up(bsport
,
587 server_port_name
[server_num
],
589 if (KERN_SUCCESS
!= ret
) {
590 mach_error("bootstrap_look_up(): ", ret
);
594 setup_client_ports(&args
);
596 /* Allocate and touch memory */
599 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
600 for (i
= 0; i
< client_pages
; i
++)
601 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
604 /* start message loop */
605 for (idx
= 0; idx
< num_msgs
; idx
++) {
607 reply
= args
.reply_msg
;
609 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
610 MACH_MSG_TYPE_MAKE_SEND
);
611 req
->msgh_size
= args
.req_size
;
612 req
->msgh_remote_port
= servport
;
613 req
->msgh_local_port
= args
.port
;
614 req
->msgh_id
= oneway
? 0 : 1;
615 if (msg_type
== msg_type_complex
) {
616 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
617 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
618 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
619 ((ipc_complex_message
*)req
)->descriptor
.size
=
620 num_ints
* sizeof(u_int32_t
);
621 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
622 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
623 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
626 printf("client sending message %d\n", idx
);
632 MACH_MSG_TIMEOUT_NONE
,
634 if (MACH_MSG_SUCCESS
!= ret
) {
635 mach_error("mach_msg (send): ", ret
);
636 fprintf(stderr
, "bailing after %u iterations\n", idx
);
642 printf("client awaiting reply %d\n", idx
);
643 reply
->msgh_bits
= 0;
644 reply
->msgh_size
= args
.reply_size
;
645 reply
->msgh_local_port
= args
.port
;
646 ret
= mach_msg(args
.reply_msg
,
647 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
,
651 MACH_MSG_TIMEOUT_NONE
,
653 if (MACH_MSG_SUCCESS
!= ret
) {
654 mach_error("mach_msg (receive): ", ret
);
655 fprintf(stderr
, "bailing after %u iterations\n",
660 printf("client received reply %d\n", idx
);
671 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
) {
674 ret
= pthread_create(
680 err(1, "pthread_create()");
682 printf("created pthread %p\n", thread
->tid
);
684 thread
->pid
= fork();
685 if (thread
->pid
== 0) {
687 printf("calling %p(%p)\n", fn
, arg
);
692 printf("forked pid %d\n", thread
->pid
);
697 thread_join(thread_id_t
*thread
) {
701 printf("joining thread %p\n", thread
->tid
);
702 ret
= pthread_join(thread
->tid
, NULL
);
703 if (ret
!= KERN_SUCCESS
)
704 err(1, "pthread_join(%p)", thread
->tid
);
708 printf("waiting for pid %d\n", thread
->pid
);
709 waitpid(thread
->pid
, &stat
, 0);
714 wait_for_servers(void)
717 int retry_count
= 10;
718 mach_port_t bsport
, servport
;
721 /* find server port */
722 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
723 if (KERN_SUCCESS
!= ret
) {
724 mach_error("task_get_bootstrap_port(): ", ret
);
728 while (retry_count
-- > 0) {
729 for (i
= 0; i
< num_servers
; i
++) {
730 ret
= bootstrap_look_up(bsport
,
733 if (ret
!= KERN_SUCCESS
) {
737 if (ret
== KERN_SUCCESS
)
739 usleep(100 * 1000); /* 100ms */
741 fprintf(stderr
, "Server(s) failed to register\n");
746 int main(int argc
, char *argv
[])
750 thread_id_t
*client_id
;
751 thread_id_t
*server_id
;
753 signal(SIGINT
, signal_handler
);
754 parse_args(argc
, argv
);
756 calibrate_client_work();
759 * If we're using affinity create an empty namespace now
760 * so this is shared by all our offspring.
765 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
766 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
768 printf("creating %d servers\n", num_servers
);
769 for (i
= 0; i
< num_servers
; i
++) {
770 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
771 /* PORT names include pid of main process for disambiguation */
772 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
773 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
776 int totalclients
= num_servers
* num_clients
;
777 int totalmsg
= num_msgs
* totalclients
;
778 struct timeval starttv
, endtv
, deltatv
;
781 * Wait for all servers to have registered all ports before starting
782 * the clients and the clock.
786 printf("%d server%s, %d client%s per server (%d total) %u messages...",
787 num_servers
, (num_servers
> 1)? "s" : "",
788 num_clients
, (num_clients
> 1)? "s" : "",
793 /* Call gettimeofday() once and throw away result; some implementations
794 * (like Mach's) cache some time zone info on first call.
796 gettimeofday(&starttv
, NULL
);
797 gettimeofday(&starttv
, NULL
);
799 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
801 printf("creating %d clients\n", totalclients
);
802 for (i
= 0; i
< num_servers
; i
++) {
803 for (j
= 0; j
< num_clients
; j
++) {
805 &client_id
[(i
*num_clients
) + j
],
811 /* Wait for servers to complete */
812 for (i
= 0; i
< num_servers
; i
++) {
813 thread_join(&server_id
[i
]);
816 gettimeofday(&endtv
, NULL
);
818 for (i
= 0; i
< totalclients
; i
++) {
819 thread_join(&client_id
[i
]);
823 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
824 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
825 if (endtv
.tv_usec
< starttv
.tv_usec
) {
827 deltatv
.tv_usec
+= 1000000;
830 double dsecs
= (double) deltatv
.tv_sec
+
831 1.0E-6 * (double) deltatv
.tv_usec
;
833 double time_in_sec
= (double)deltatv
.tv_sec
+ (double)deltatv
.tv_usec
/1000.0;
834 double throughput_msg_p_sec
= (double) totalmsg
/dsecs
;
835 double avg_msg_latency
= dsecs
*1.0E6
/ (double)totalmsg
;
837 printf(" in %ld.%03u seconds\n",
838 (long)deltatv
.tv_sec
, deltatv
.tv_usec
/1000);
839 printf(" throughput in messages/sec: %g\n",
840 (double)totalmsg
/ dsecs
);
841 printf(" average message latency (usec): %2.3g\n",
842 dsecs
* 1.0E6
/ (double) totalmsg
);
844 if (save_perfdata
== TRUE
) {
846 snprintf(name
, sizeof(name
), "%s_avg_msg_latency", basename(argv
[0]));
847 record_perf_data(name
, "usec", avg_msg_latency
, "Message latency measured in microseconds. Lower is better", stderr
);