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
;
87 signal_handler(int sig
)
92 usage(const char *progname
)
94 fprintf(stderr
, "usage: %s [options]\n", progname
);
95 fprintf(stderr
, "where options are:\n");
96 fprintf(stderr
, " -affinity\t\tthreads use affinity\n");
97 fprintf(stderr
, " -timeshare\t\tthreads use timeshare\n");
98 fprintf(stderr
, " -threaded\t\tuse (p)threads\n");
99 fprintf(stderr
, " -verbose\t\tbe verbose\n");
100 fprintf(stderr
, " -oneway\t\tdo not request return reply\n");
101 fprintf(stderr
, " -count num\t\tnumber of messages to send\n");
102 fprintf(stderr
, " -type trivial|inline|complex\ttype of messages to send\n");
103 fprintf(stderr
, " -numints num\tnumber of 32-bit ints to send in messages\n");
104 fprintf(stderr
, " -servers num\tnumber of servers threads to run\n");
105 fprintf(stderr
, " -clients num\tnumber of clients per server\n");
106 fprintf(stderr
, " -delay num\t\tmicroseconds to sleep clients between messages\n");
107 fprintf(stderr
, " -work num\t\tmicroseconds of client work\n");
108 fprintf(stderr
, " -pages num\t\tpages of memory touched by client work\n");
109 fprintf(stderr
, " -select \t\tselect prior to calling kevent().\n");
110 fprintf(stderr
, " -perf \t\tCreate perfdata files for metrics.\n");
111 fprintf(stderr
, "default values are:\n");
112 fprintf(stderr
, " . no affinity\n");
113 fprintf(stderr
, " . not timeshare\n");
114 fprintf(stderr
, " . not verbose\n");
115 fprintf(stderr
, " . not oneway\n");
116 fprintf(stderr
, " . client sends 100000 messages\n");
117 fprintf(stderr
, " . inline message type\n");
118 fprintf(stderr
, " . 64 32-bit integers in inline/complex messages\n");
119 fprintf(stderr
, " . (num_available_processors+1)%%2 servers\n");
120 fprintf(stderr
, " . 4 clients per server\n");
121 fprintf(stderr
, " . no delay\n");
126 parse_args(int argc
, char *argv
[])
128 host_basic_info_data_t info
;
129 mach_msg_type_number_t count
;
130 kern_return_t result
;
132 /* Initialize defaults */
133 msg_type
= msg_type_trivial
;
139 count
= HOST_BASIC_INFO_COUNT
;
140 result
= host_info(mach_host_self(), HOST_BASIC_INFO
,
141 (host_info_t
)&info
, &count
);
142 if (result
== KERN_SUCCESS
&& info
.avail_cpus
> 1) {
143 num_servers
= info
.avail_cpus
/ 2;
148 const char *progname
= argv
[0];
151 if (0 == strcmp("-verbose", argv
[0])) {
154 } else if (0 == strcmp("-affinity", argv
[0])) {
157 } else if (0 == strcmp("-timeshare", argv
[0])) {
160 } else if (0 == strcmp("-threaded", argv
[0])) {
163 } else if (0 == strcmp("-oneway", argv
[0])) {
166 } else if (0 == strcmp("-type", argv
[0])) {
170 if (0 == strcmp("trivial", argv
[1])) {
171 msg_type
= msg_type_trivial
;
172 } else if (0 == strcmp("inline", argv
[1])) {
173 msg_type
= msg_type_inline
;
174 } else if (0 == strcmp("complex", argv
[1])) {
175 msg_type
= msg_type_complex
;
179 argc
-= 2; argv
+= 2;
180 } else if (0 == strcmp("-numints", argv
[0])) {
184 num_ints
= strtoul(argv
[1], NULL
, 0);
185 argc
-= 2; argv
+= 2;
186 } else if (0 == strcmp("-count", argv
[0])) {
190 num_msgs
= strtoul(argv
[1], NULL
, 0);
191 argc
-= 2; argv
+= 2;
192 } else if (0 == strcmp("-clients", argv
[0])) {
196 num_clients
= strtoul(argv
[1], NULL
, 0);
197 argc
-= 2; argv
+= 2;
198 } else if (0 == strcmp("-servers", argv
[0])) {
202 num_servers
= strtoul(argv
[1], NULL
, 0);
203 argc
-= 2; argv
+= 2;
204 } else if (0 == strcmp("-delay", argv
[0])) {
208 client_delay
= strtoul(argv
[1], NULL
, 0);
209 argc
-= 2; argv
+= 2;
210 } else if (0 == strcmp("-spin", argv
[0])) {
214 client_spin
= strtoul(argv
[1], NULL
, 0);
215 argc
-= 2; argv
+= 2;
216 } else if (0 == strcmp("-pages", argv
[0])) {
220 client_pages
= strtoul(argv
[1], NULL
, 0);
221 argc
-= 2; argv
+= 2;
222 } else if (0 == strcmp("-select", argv
[0])) {
225 } else if (0 == strcmp("-perf", argv
[0])) {
226 save_perfdata
= TRUE
;
235 setup_server_ports(struct port_args
*ports
)
237 kern_return_t ret
= 0;
240 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
241 sizeof(u_int32_t
) * num_ints
,
242 sizeof(ipc_complex_message
));
243 ports
->reply_size
= sizeof(ipc_trivial_message
) -
244 sizeof(mach_msg_trailer_t
);
245 ports
->req_msg
= malloc(ports
->req_size
);
246 ports
->reply_msg
= malloc(ports
->reply_size
);
248 ret
= mach_port_allocate(mach_task_self(),
249 MACH_PORT_RIGHT_RECEIVE
,
251 if (KERN_SUCCESS
!= ret
) {
252 mach_error("mach_port_allocate(): ", ret
);
256 ret
= mach_port_allocate(mach_task_self(),
257 MACH_PORT_RIGHT_PORT_SET
,
259 if (KERN_SUCCESS
!= ret
) {
260 mach_error("mach_port_allocate(): ", ret
);
264 ret
= mach_port_insert_member(mach_task_self(),
267 if (KERN_SUCCESS
!= ret
) {
268 mach_error("mach_port_insert_member(): ", ret
);
272 ret
= mach_port_insert_right(mach_task_self(),
275 MACH_MSG_TYPE_MAKE_SEND
);
276 if (KERN_SUCCESS
!= ret
) {
277 mach_error("mach_port_insert_right(): ", ret
);
281 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
282 if (KERN_SUCCESS
!= ret
) {
283 mach_error("task_get_bootstrap_port(): ", ret
);
288 printf("server waiting for IPC messages from client on port '%s'.\n",
289 server_port_name
[ports
->server_num
]);
291 ret
= bootstrap_register(bsport
,
292 server_port_name
[ports
->server_num
],
294 if (KERN_SUCCESS
!= ret
) {
295 mach_error("bootstrap_register(): ", ret
);
301 setup_client_ports(struct port_args
*ports
)
303 kern_return_t ret
= 0;
305 case msg_type_trivial
:
306 ports
->req_size
= sizeof(ipc_trivial_message
);
308 case msg_type_inline
:
309 ports
->req_size
= sizeof(ipc_inline_message
) +
310 sizeof(u_int32_t
) * num_ints
;
312 case msg_type_complex
:
313 ports
->req_size
= sizeof(ipc_complex_message
);
316 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
317 ports
->reply_size
= sizeof(ipc_trivial_message
);
318 ports
->req_msg
= malloc(ports
->req_size
);
319 ports
->reply_msg
= malloc(ports
->reply_size
);
321 ret
= mach_port_allocate(mach_task_self(),
322 MACH_PORT_RIGHT_RECEIVE
,
324 if (KERN_SUCCESS
!= ret
) {
325 mach_error("mach_port_allocate(): ", ret
);
329 printf("Client sending %d %s IPC messages to port '%s' in %s mode.\n",
330 num_msgs
, (msg_type
== msg_type_inline
) ?
331 "inline" : ((msg_type
== msg_type_complex
) ?
332 "complex" : "trivial"),
333 server_port_name
[ports
->server_num
],
334 (oneway
? "oneway" : "rpc"));
340 thread_setup(int tag
)
343 thread_extended_policy_data_t epolicy
;
344 thread_affinity_policy_data_t policy
;
347 epolicy
.timeshare
= FALSE
;
348 ret
= thread_policy_set(
349 mach_thread_self(), THREAD_EXTENDED_POLICY
,
350 (thread_policy_t
) &epolicy
,
351 THREAD_EXTENDED_POLICY_COUNT
);
352 if (ret
!= KERN_SUCCESS
) {
353 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
358 policy
.affinity_tag
= tag
;
359 ret
= thread_policy_set(
360 mach_thread_self(), THREAD_AFFINITY_POLICY
,
361 (thread_policy_t
) &policy
,
362 THREAD_AFFINITY_POLICY_COUNT
);
363 if (ret
!= KERN_SUCCESS
) {
364 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
370 server(void *serverarg
)
373 struct kevent64_s kev
[1];
376 struct port_args args
;
379 int totalmsg
= num_msgs
* num_clients
;
382 args
.server_num
= (int) (long) serverarg
;
383 setup_server_ports(&args
);
385 thread_setup(args
.server_num
+ 1);
392 EV_SET64(&kev
[0], args
.pset
, EVFILT_MACHPORT
, (EV_ADD
| EV_CLEAR
| EV_DISPATCH
),
394 MACH_RCV_MSG
| MACH_RCV_LARGE
, 0, 0, (mach_vm_address_t
)args
.req_msg
, args
.req_size
);
398 err
= kevent64(kq
, kev
, 1, NULL
, 0, 0, NULL
);
404 for (idx
= 0; idx
< totalmsg
; idx
++) {
406 printf("server awaiting message %d\n", idx
);
411 FD_SET(kq
, &readfds
);
414 printf("Calling select() prior to kevent64().\n");
417 count
= select(kq
+ 1, &readfds
, NULL
, NULL
, NULL
);
424 EV_SET64(&kev
[0], args
.pset
, EVFILT_MACHPORT
, EV_ENABLE
,
426 MACH_RCV_MSG
| MACH_RCV_LARGE
, 0, 0, (mach_vm_address_t
)args
.req_msg
, args
.req_size
);
430 err
= kevent64(kq
, kev
, 1, kev
, 1, 0, NULL
);
436 // printf("kevent64: returned zero\n");
442 if (MACH_MSG_SUCCESS
!= ret
) {
444 printf("kevent64() mach_msg_return=%d", ret
);
446 mach_error("kevent64 (msg receive): ", ret
);
450 if (kev
[0].data
!= args
.port
) {
451 printf("kevent64(MACH_PORT_NULL) port name (%lld) != expected (0x%x)\n", kev
[0].data
, args
.port
);
454 args
.req_msg
->msgh_bits
= 0;
455 args
.req_msg
->msgh_size
= args
.req_size
;
456 args
.req_msg
->msgh_local_port
= args
.port
;
457 ret
= mach_msg(args
.req_msg
,
458 MACH_RCV_MSG
| MACH_RCV_INTERRUPT
| MACH_RCV_LARGE
,
462 MACH_MSG_TIMEOUT_NONE
,
464 if (MACH_RCV_INTERRUPTED
== ret
) {
467 if (MACH_MSG_SUCCESS
!= ret
) {
469 printf("mach_msg() ret=%d", ret
);
471 mach_error("mach_msg (receive): ", ret
);
476 printf("server received message %d\n", idx
);
478 if (args
.req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
479 ret
= vm_deallocate(mach_task_self(),
480 (vm_address_t
)((ipc_complex_message
*)args
.req_msg
)->descriptor
.address
,
481 ((ipc_complex_message
*)args
.req_msg
)->descriptor
.size
);
484 if (1 == args
.req_msg
->msgh_id
) {
486 printf("server sending reply %d\n", idx
);
488 args
.reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
489 MACH_MSG_TYPE_MAKE_SEND
);
490 args
.reply_msg
->msgh_size
= args
.reply_size
;
491 args
.reply_msg
->msgh_remote_port
= args
.req_msg
->msgh_remote_port
;
492 args
.reply_msg
->msgh_local_port
= args
.req_msg
->msgh_local_port
;
493 args
.reply_msg
->msgh_id
= 2;
494 ret
= mach_msg(args
.reply_msg
,
499 MACH_MSG_TIMEOUT_NONE
,
501 if (MACH_MSG_SUCCESS
!= ret
) {
502 mach_error("mach_msg (send): ", ret
);
511 client_spin_loop(unsigned count
, void(fn
)(void))
518 static long dummy_memory
;
519 static long *client_memory
= &dummy_memory
;
521 client_work_atom(void)
525 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long)) {
528 client_memory
[i
] = 0;
531 static int calibration_count
= 10000;
532 static int calibration_usec
;
534 calibrate_client_work(void)
537 struct timeval nowtv
;
538 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
539 struct timeval starttv
;
540 struct timeval endtv
;
543 /* Warm-up the stepper first... */
544 gettimeofday(&nowtv
, NULL
);
545 timeradd(&nowtv
, &warmuptv
, &endtv
);
547 client_spin_loop(calibration_count
, client_work_atom
);
548 gettimeofday(&nowtv
, NULL
);
549 } while (timercmp(&nowtv
, &endtv
, < ));
551 /* Now do the calibration */
553 gettimeofday(&starttv
, NULL
);
554 client_spin_loop(calibration_count
, client_work_atom
);
555 gettimeofday(&endtv
, NULL
);
556 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
557 calibration_count
/= 10;
560 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
561 if (endtv
.tv_usec
< starttv
.tv_usec
) {
562 calibration_usec
+= 1000000;
564 if (calibration_usec
< 1000) {
565 calibration_count
*= 10;
568 calibration_count
/= calibration_usec
;
572 printf("calibration_count=%d calibration_usec=%d\n",
573 calibration_count
, calibration_usec
);
583 client_spin_loop(calibration_count
* client_spin
,
588 usleep(client_delay
);
594 client(void *threadarg
)
596 struct port_args args
;
598 mach_msg_header_t
*req
, *reply
;
599 mach_port_t bsport
, servport
;
601 int server_num
= (int) threadarg
;
602 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
605 printf("client(%d) started, server port name %s\n",
606 server_num
, server_port_name
[server_num
]);
609 args
.server_num
= server_num
;
610 thread_setup(server_num
+ 1);
612 /* find server port */
613 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
614 if (KERN_SUCCESS
!= ret
) {
615 mach_error("task_get_bootstrap_port(): ", ret
);
618 ret
= bootstrap_look_up(bsport
,
619 server_port_name
[server_num
],
621 if (KERN_SUCCESS
!= ret
) {
622 mach_error("bootstrap_look_up(): ", ret
);
626 setup_client_ports(&args
);
628 /* Allocate and touch memory */
631 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
632 for (i
= 0; i
< client_pages
; i
++) {
633 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
637 /* start message loop */
638 for (idx
= 0; idx
< num_msgs
; idx
++) {
640 reply
= args
.reply_msg
;
642 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
643 MACH_MSG_TYPE_MAKE_SEND
);
644 req
->msgh_size
= args
.req_size
;
645 req
->msgh_remote_port
= servport
;
646 req
->msgh_local_port
= args
.port
;
647 req
->msgh_id
= oneway
? 0 : 1;
648 if (msg_type
== msg_type_complex
) {
649 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
650 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
651 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
652 ((ipc_complex_message
*)req
)->descriptor
.size
=
653 num_ints
* sizeof(u_int32_t
);
654 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
655 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
656 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
659 printf("client sending message %d\n", idx
);
666 MACH_MSG_TIMEOUT_NONE
,
668 if (MACH_MSG_SUCCESS
!= ret
) {
669 mach_error("mach_msg (send): ", ret
);
670 fprintf(stderr
, "bailing after %u iterations\n", idx
);
676 printf("client awaiting reply %d\n", idx
);
678 reply
->msgh_bits
= 0;
679 reply
->msgh_size
= args
.reply_size
;
680 reply
->msgh_local_port
= args
.port
;
681 ret
= mach_msg(args
.reply_msg
,
682 MACH_RCV_MSG
| MACH_RCV_INTERRUPT
,
686 MACH_MSG_TIMEOUT_NONE
,
688 if (MACH_MSG_SUCCESS
!= ret
) {
689 mach_error("mach_msg (receive): ", ret
);
690 fprintf(stderr
, "bailing after %u iterations\n",
695 printf("client received reply %d\n", idx
);
707 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
)
711 ret
= pthread_create(
717 err(1, "pthread_create()");
720 printf("created pthread %p\n", thread
->tid
);
723 thread
->pid
= fork();
724 if (thread
->pid
== 0) {
726 printf("calling %p(%p)\n", fn
, arg
);
732 printf("forked pid %d\n", thread
->pid
);
738 thread_join(thread_id_t
*thread
)
743 printf("joining thread %p\n", thread
->tid
);
745 ret
= pthread_join(thread
->tid
, NULL
);
746 if (ret
!= KERN_SUCCESS
) {
747 err(1, "pthread_join(%p)", thread
->tid
);
752 printf("waiting for pid %d\n", thread
->pid
);
754 waitpid(thread
->pid
, &stat
, 0);
759 wait_for_servers(void)
762 int retry_count
= 10;
763 mach_port_t bsport
, servport
;
766 /* find server port */
767 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
768 if (KERN_SUCCESS
!= ret
) {
769 mach_error("task_get_bootstrap_port(): ", ret
);
773 while (retry_count
-- > 0) {
774 for (i
= 0; i
< num_servers
; i
++) {
775 ret
= bootstrap_look_up(bsport
,
778 if (ret
!= KERN_SUCCESS
) {
782 if (ret
== KERN_SUCCESS
) {
785 usleep(100 * 1000); /* 100ms */
787 fprintf(stderr
, "Server(s) failed to register\n");
793 main(int argc
, char *argv
[])
797 thread_id_t
*client_id
;
798 thread_id_t
*server_id
;
800 signal(SIGINT
, signal_handler
);
801 parse_args(argc
, argv
);
803 calibrate_client_work();
806 * If we're using affinity create an empty namespace now
807 * so this is shared by all our offspring.
813 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
814 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
816 printf("creating %d servers\n", num_servers
);
818 for (i
= 0; i
< num_servers
; i
++) {
819 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
820 /* PORT names include pid of main process for disambiguation */
821 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
822 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
825 int totalclients
= num_servers
* num_clients
;
826 int totalmsg
= num_msgs
* totalclients
;
827 struct timeval starttv
, endtv
, deltatv
;
830 * Wait for all servers to have registered all ports before starting
831 * the clients and the clock.
835 printf("%d server%s, %d client%s per server (%d total) %u messages...",
836 num_servers
, (num_servers
> 1)? "s" : "",
837 num_clients
, (num_clients
> 1)? "s" : "",
842 /* Call gettimeofday() once and throw away result; some implementations
843 * (like Mach's) cache some time zone info on first call.
845 gettimeofday(&starttv
, NULL
);
846 gettimeofday(&starttv
, NULL
);
848 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
850 printf("creating %d clients\n", totalclients
);
852 for (i
= 0; i
< num_servers
; i
++) {
853 for (j
= 0; j
< num_clients
; j
++) {
855 &client_id
[(i
* num_clients
) + j
],
861 /* Wait for servers to complete */
862 for (i
= 0; i
< num_servers
; i
++) {
863 thread_join(&server_id
[i
]);
866 gettimeofday(&endtv
, NULL
);
868 for (i
= 0; i
< totalclients
; i
++) {
869 thread_join(&client_id
[i
]);
873 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
874 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
875 if (endtv
.tv_usec
< starttv
.tv_usec
) {
877 deltatv
.tv_usec
+= 1000000;
880 double dsecs
= (double) deltatv
.tv_sec
+
881 1.0E-6 * (double) deltatv
.tv_usec
;
883 double time_in_sec
= (double)deltatv
.tv_sec
+ (double)deltatv
.tv_usec
/ 1000.0;
884 double throughput_msg_p_sec
= (double) totalmsg
/ dsecs
;
885 double avg_msg_latency
= dsecs
* 1.0E6
/ (double)totalmsg
;
887 printf(" in %ld.%03u seconds\n",
888 (long)deltatv
.tv_sec
, deltatv
.tv_usec
/ 1000);
889 printf(" throughput in messages/sec: %g\n",
890 (double)totalmsg
/ dsecs
);
891 printf(" average message latency (usec): %2.3g\n",
892 dsecs
* 1.0E6
/ (double) totalmsg
);
894 if (save_perfdata
== TRUE
) {
896 snprintf(name
, sizeof(name
), "%s_avg_msg_latency", basename(argv
[0]));
897 record_perf_data(name
, "usec", avg_msg_latency
, "Message latency measured in microseconds. Lower is better", stderr
);