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/types.h>
18 #include <sys/signal.h>
20 #define MAX(A, B) ((A) < (B) ? (B) : (A))
24 mach_msg_header_t header
;
25 mach_msg_trailer_t trailer
; // subtract this when sending
26 } ipc_trivial_message
;
29 mach_msg_header_t header
;
31 mach_msg_trailer_t trailer
; // subtract this when sending
35 mach_msg_header_t header
;
37 mach_msg_ool_descriptor_t descriptor
;
38 mach_msg_trailer_t trailer
; // subtract this when sending
39 } ipc_complex_message
;
50 mach_msg_header_t
*req_msg
;
52 mach_msg_header_t
*reply_msg
;
63 static boolean_t verbose
= FALSE
;
64 static boolean_t affinity
= FALSE
;
65 static boolean_t timeshare
= FALSE
;
66 static boolean_t threaded
= FALSE
;
67 static boolean_t oneway
= FALSE
;
68 static boolean_t useset
= 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
, " -set num\t\tuse a portset stuffed with num ports in server\n");
100 fprintf(stderr
, "default values are:\n");
101 fprintf(stderr
, " . no affinity\n");
102 fprintf(stderr
, " . not timeshare\n");
103 fprintf(stderr
, " . not verbose\n");
104 fprintf(stderr
, " . not oneway\n");
105 fprintf(stderr
, " . client sends 100000 messages\n");
106 fprintf(stderr
, " . inline message type\n");
107 fprintf(stderr
, " . 64 32-bit integers in inline/complex messages\n");
108 fprintf(stderr
, " . (num_available_processors+1)%%2 servers\n");
109 fprintf(stderr
, " . 4 clients per server\n");
110 fprintf(stderr
, " . no delay\n");
114 void parse_args(int argc
, char *argv
[]) {
115 host_basic_info_data_t info
;
116 mach_msg_type_number_t count
;
117 kern_return_t result
;
119 /* Initialize defaults */
120 msg_type
= msg_type_trivial
;
126 count
= HOST_BASIC_INFO_COUNT
;
127 result
= host_info(mach_host_self(), HOST_BASIC_INFO
,
128 (host_info_t
)&info
, &count
);
129 if (result
== KERN_SUCCESS
&& info
.avail_cpus
> 1)
130 num_servers
= info
.avail_cpus
/ 2;
134 const char *progname
= argv
[0];
137 if (0 == strcmp("-verbose", argv
[0])) {
140 } else if (0 == strcmp("-affinity", argv
[0])) {
143 } else if (0 == strcmp("-timeshare", argv
[0])) {
146 } else if (0 == strcmp("-threaded", argv
[0])) {
149 } else if (0 == strcmp("-oneway", argv
[0])) {
152 } else if (0 == strcmp("-type", argv
[0])) {
155 if (0 == strcmp("trivial", argv
[1])) {
156 msg_type
= msg_type_trivial
;
157 } else if (0 == strcmp("inline", argv
[1])) {
158 msg_type
= msg_type_inline
;
159 } else if (0 == strcmp("complex", argv
[1])) {
160 msg_type
= msg_type_complex
;
163 argc
-= 2; argv
+= 2;
164 } else if (0 == strcmp("-numints", argv
[0])) {
167 num_ints
= strtoul(argv
[1], NULL
, 0);
168 argc
-= 2; argv
+= 2;
169 } else if (0 == strcmp("-count", argv
[0])) {
172 num_msgs
= strtoul(argv
[1], NULL
, 0);
173 argc
-= 2; argv
+= 2;
174 } else if (0 == strcmp("-clients", argv
[0])) {
177 num_clients
= strtoul(argv
[1], NULL
, 0);
178 argc
-= 2; argv
+= 2;
179 } else if (0 == strcmp("-servers", argv
[0])) {
182 num_servers
= strtoul(argv
[1], NULL
, 0);
183 argc
-= 2; argv
+= 2;
184 } else if (0 == strcmp("-delay", argv
[0])) {
187 client_delay
= strtoul(argv
[1], NULL
, 0);
188 argc
-= 2; argv
+= 2;
189 } else if (0 == strcmp("-spin", argv
[0])) {
192 client_spin
= strtoul(argv
[1], NULL
, 0);
193 argc
-= 2; argv
+= 2;
194 } else if (0 == strcmp("-pages", argv
[0])) {
197 client_pages
= strtoul(argv
[1], NULL
, 0);
198 argc
-= 2; argv
+= 2;
199 } else if (0 == strcmp("-set", argv
[0])) {
202 portcount
= strtoul(argv
[1], NULL
, 0);
204 argc
-= 2; argv
+= 2;
211 void setup_server_ports(struct port_args
*ports
)
213 kern_return_t ret
= 0;
218 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
219 sizeof(u_int32_t
) * num_ints
,
220 sizeof(ipc_complex_message
));
221 ports
->reply_size
= sizeof(ipc_trivial_message
) -
222 sizeof(mach_msg_trailer_t
);
223 ports
->req_msg
= malloc(ports
->req_size
);
224 ports
->reply_msg
= malloc(ports
->reply_size
);
227 ret
= mach_port_allocate(mach_task_self(),
228 MACH_PORT_RIGHT_PORT_SET
,
230 if (KERN_SUCCESS
!= ret
) {
231 mach_error("mach_port_allocate(SET): ", ret
);
236 /* stuff the portset with ports */
237 for (i
=0; i
< portcount
; i
++) {
238 ret
= mach_port_allocate(mach_task_self(),
239 MACH_PORT_RIGHT_RECEIVE
,
241 if (KERN_SUCCESS
!= ret
) {
242 mach_error("mach_port_allocate(PORT): ", ret
);
247 ret
= mach_port_move_member(mach_task_self(),
250 if (KERN_SUCCESS
!= ret
) {
251 mach_error("mach_port_move_member(): ", ret
);
257 /* use the last one as the real port */
260 ret
= mach_port_insert_right(mach_task_self(),
263 MACH_MSG_TYPE_MAKE_SEND
);
264 if (KERN_SUCCESS
!= ret
) {
265 mach_error("mach_port_insert_right(): ", ret
);
269 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
270 if (KERN_SUCCESS
!= ret
) {
271 mach_error("task_get_bootstrap_port(): ", ret
);
276 printf("server waiting for IPC messages from client on port '%s'.\n",
277 server_port_name
[ports
->server_num
]);
279 ret
= bootstrap_register(bsport
,
280 server_port_name
[ports
->server_num
],
282 if (KERN_SUCCESS
!= ret
) {
283 mach_error("bootstrap_register(): ", ret
);
288 void setup_client_ports(struct port_args
*ports
)
290 kern_return_t ret
= 0;
292 case msg_type_trivial
:
293 ports
->req_size
= sizeof(ipc_trivial_message
);
295 case msg_type_inline
:
296 ports
->req_size
= sizeof(ipc_inline_message
) +
297 sizeof(u_int32_t
) * num_ints
;
299 case msg_type_complex
:
300 ports
->req_size
= sizeof(ipc_complex_message
);
303 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
304 ports
->reply_size
= sizeof(ipc_trivial_message
);
305 ports
->req_msg
= malloc(ports
->req_size
);
306 ports
->reply_msg
= malloc(ports
->reply_size
);
308 ret
= mach_port_allocate(mach_task_self(),
309 MACH_PORT_RIGHT_RECEIVE
,
311 if (KERN_SUCCESS
!= ret
) {
312 mach_error("mach_port_allocate(): ", ret
);
316 printf("Client sending %d %s IPC messages to port '%s' in %s mode.\n",
317 num_msgs
, (msg_type
== msg_type_inline
) ?
318 "inline" : ((msg_type
== msg_type_complex
) ?
319 "complex" : "trivial"),
320 server_port_name
[ports
->server_num
],
321 (oneway
? "oneway" : "rpc"));
328 thread_setup(int tag
) {
330 thread_extended_policy_data_t epolicy
;
331 thread_affinity_policy_data_t policy
;
334 epolicy
.timeshare
= FALSE
;
335 ret
= thread_policy_set(
336 mach_thread_self(), THREAD_EXTENDED_POLICY
,
337 (thread_policy_t
) &epolicy
,
338 THREAD_EXTENDED_POLICY_COUNT
);
339 if (ret
!= KERN_SUCCESS
)
340 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
344 policy
.affinity_tag
= tag
;
345 ret
= thread_policy_set(
346 mach_thread_self(), THREAD_AFFINITY_POLICY
,
347 (thread_policy_t
) &policy
,
348 THREAD_AFFINITY_POLICY_COUNT
);
349 if (ret
!= KERN_SUCCESS
)
350 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
355 server(void *serverarg
)
357 struct port_args args
;
360 int totalmsg
= num_msgs
* num_clients
;
361 mach_port_t recv_port
;
363 args
.server_num
= (int) (long) serverarg
;
364 setup_server_ports(&args
);
366 thread_setup(args
.server_num
+ 1);
368 recv_port
= (useset
) ? args
.set
: args
.port
;
370 for (idx
= 0; idx
< totalmsg
; idx
++) {
372 printf("server awaiting message %d\n", idx
);
373 ret
= mach_msg(args
.req_msg
,
374 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
|MACH_RCV_LARGE
,
378 MACH_MSG_TIMEOUT_NONE
,
380 if (MACH_RCV_INTERRUPTED
== ret
)
382 if (MACH_MSG_SUCCESS
!= ret
) {
384 printf("mach_msg() ret=%d", ret
);
385 mach_error("mach_msg (receive): ", ret
);
389 printf("server received message %d\n", idx
);
390 if (args
.req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
391 ret
= vm_deallocate(mach_task_self(),
392 (vm_address_t
)((ipc_complex_message
*)args
.req_msg
)->descriptor
.address
,
393 ((ipc_complex_message
*)args
.req_msg
)->descriptor
.size
);
396 if (1 == args
.req_msg
->msgh_id
) {
398 printf("server sending reply %d\n", idx
);
399 args
.reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, 0);
400 args
.reply_msg
->msgh_size
= args
.reply_size
;
401 args
.reply_msg
->msgh_remote_port
= args
.req_msg
->msgh_remote_port
;
402 args
.reply_msg
->msgh_local_port
= MACH_PORT_NULL
;
403 args
.reply_msg
->msgh_id
= 2;
404 ret
= mach_msg(args
.reply_msg
,
409 MACH_MSG_TIMEOUT_NONE
,
411 if (MACH_MSG_SUCCESS
!= ret
) {
412 mach_error("mach_msg (send): ", ret
);
421 client_spin_loop(unsigned count
, void (fn
)(void))
427 static long dummy_memory
;
428 static long *client_memory
= &dummy_memory
;
430 client_work_atom(void)
434 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long))
436 client_memory
[i
] = 0;
439 static int calibration_count
= 10000;
440 static int calibration_usec
;
442 calibrate_client_work(void)
445 struct timeval nowtv
;
446 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
447 struct timeval starttv
;
448 struct timeval endtv
;
451 /* Warm-up the stepper first... */
452 gettimeofday(&nowtv
, NULL
);
453 timeradd(&nowtv
, &warmuptv
, &endtv
);
455 client_spin_loop(calibration_count
, client_work_atom
);
456 gettimeofday(&nowtv
, NULL
);
457 } while (timercmp(&nowtv
, &endtv
, < ));
459 /* Now do the calibration */
461 gettimeofday(&starttv
, NULL
);
462 client_spin_loop(calibration_count
, client_work_atom
);
463 gettimeofday(&endtv
, NULL
);
464 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
465 calibration_count
/= 10;
468 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
469 if (endtv
.tv_usec
< starttv
.tv_usec
) {
470 calibration_usec
+= 1000000;
472 if (calibration_usec
< 1000) {
473 calibration_count
*= 10;
476 calibration_count
/= calibration_usec
;
480 printf("calibration_count=%d calibration_usec=%d\n",
481 calibration_count
, calibration_usec
);
491 client_spin_loop(calibration_count
*client_spin
,
496 usleep(client_delay
);
501 void *client(void *threadarg
)
503 struct port_args args
;
505 mach_msg_header_t
*req
, *reply
;
506 mach_port_t bsport
, servport
;
508 int server_num
= (int) threadarg
;
509 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
512 printf("client(%d) started, server port name %s\n",
513 server_num
, server_port_name
[server_num
]);
515 args
.server_num
= server_num
;
516 thread_setup(server_num
+ 1);
518 /* find server port */
519 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
520 if (KERN_SUCCESS
!= ret
) {
521 mach_error("task_get_bootstrap_port(): ", ret
);
524 ret
= bootstrap_look_up(bsport
,
525 server_port_name
[server_num
],
527 if (KERN_SUCCESS
!= ret
) {
528 mach_error("bootstrap_look_up(): ", ret
);
532 setup_client_ports(&args
);
534 /* Allocate and touch memory */
537 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
538 for (i
= 0; i
< client_pages
; i
++)
539 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
542 /* start message loop */
543 for (idx
= 0; idx
< num_msgs
; idx
++) {
545 reply
= args
.reply_msg
;
547 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
548 MACH_MSG_TYPE_MAKE_SEND_ONCE
);
549 req
->msgh_size
= args
.req_size
;
550 req
->msgh_remote_port
= servport
;
551 req
->msgh_local_port
= args
.port
;
552 req
->msgh_id
= oneway
? 0 : 1;
553 if (msg_type
== msg_type_complex
) {
554 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
555 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
556 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
557 ((ipc_complex_message
*)req
)->descriptor
.size
=
558 num_ints
* sizeof(u_int32_t
);
559 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
560 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
561 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
564 printf("client sending message %d\n", idx
);
570 MACH_MSG_TIMEOUT_NONE
,
572 if (MACH_MSG_SUCCESS
!= ret
) {
573 mach_error("mach_msg (send): ", ret
);
574 fprintf(stderr
, "bailing after %u iterations\n", idx
);
580 printf("client awaiting reply %d\n", idx
);
581 reply
->msgh_bits
= 0;
582 reply
->msgh_size
= args
.reply_size
;
583 reply
->msgh_local_port
= args
.port
;
584 ret
= mach_msg(args
.reply_msg
,
585 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
,
589 MACH_MSG_TIMEOUT_NONE
,
591 if (MACH_MSG_SUCCESS
!= ret
) {
592 mach_error("mach_msg (receive): ", ret
);
593 fprintf(stderr
, "bailing after %u iterations\n",
598 printf("client received reply %d\n", idx
);
609 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
) {
612 ret
= pthread_create(
618 err(1, "pthread_create()");
620 printf("created pthread %p\n", thread
->tid
);
622 thread
->pid
= fork();
623 if (thread
->pid
== 0) {
625 printf("calling %p(%p)\n", fn
, arg
);
630 printf("forked pid %d\n", thread
->pid
);
635 thread_join(thread_id_t
*thread
) {
639 printf("joining thread %p\n", thread
->tid
);
640 ret
= pthread_join(thread
->tid
, NULL
);
641 if (ret
!= KERN_SUCCESS
)
642 err(1, "pthread_join(%p)", thread
->tid
);
646 printf("waiting for pid %d\n", thread
->pid
);
647 waitpid(thread
->pid
, &stat
, 0);
652 wait_for_servers(void)
655 int retry_count
= 10;
656 mach_port_t bsport
, servport
;
659 /* find server port */
660 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
661 if (KERN_SUCCESS
!= ret
) {
662 mach_error("task_get_bootstrap_port(): ", ret
);
666 while (retry_count
-- > 0) {
667 for (i
= 0; i
< num_servers
; i
++) {
668 ret
= bootstrap_look_up(bsport
,
671 if (ret
!= KERN_SUCCESS
) {
675 if (ret
== KERN_SUCCESS
)
677 usleep(100 * 1000); /* 100ms */
679 fprintf(stderr
, "Server(s) failed to register\n");
683 int main(int argc
, char *argv
[])
687 thread_id_t
*client_id
;
688 thread_id_t
*server_id
;
690 signal(SIGINT
, signal_handler
);
691 parse_args(argc
, argv
);
693 calibrate_client_work();
696 * If we're using affinity create an empty namespace now
697 * so this is shared by all our offspring.
702 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
703 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
705 printf("creating %d servers\n", num_servers
);
706 for (i
= 0; i
< num_servers
; i
++) {
707 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
708 /* PORT names include pid of main process for disambiguation */
709 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
710 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
713 int totalclients
= num_servers
* num_clients
;
714 int totalmsg
= num_msgs
* totalclients
;
715 struct timeval starttv
, endtv
, deltatv
;
718 * Wait for all servers to have registered all ports before starting
719 * the clients and the clock.
723 printf("%d server%s, %d client%s per server (%d total) %u messages...",
724 num_servers
, (num_servers
> 1)? "s" : "",
725 num_clients
, (num_clients
> 1)? "s" : "",
730 /* Call gettimeofday() once and throw away result; some implementations
731 * (like Mach's) cache some time zone info on first call.
733 gettimeofday(&starttv
, NULL
);
734 gettimeofday(&starttv
, NULL
);
736 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
738 printf("creating %d clients\n", totalclients
);
739 for (i
= 0; i
< num_servers
; i
++) {
740 for (j
= 0; j
< num_clients
; j
++) {
742 &client_id
[(i
*num_clients
) + j
],
748 /* Wait for servers to complete */
749 for (i
= 0; i
< num_servers
; i
++) {
750 thread_join(&server_id
[i
]);
753 gettimeofday(&endtv
, NULL
);
755 for (i
= 0; i
< totalclients
; i
++) {
756 thread_join(&client_id
[i
]);
760 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
761 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
762 if (endtv
.tv_usec
< starttv
.tv_usec
) {
764 deltatv
.tv_usec
+= 1000000;
767 double dsecs
= (double) deltatv
.tv_sec
+
768 1.0E-6 * (double) deltatv
.tv_usec
;
770 printf(" in %u.%03u seconds\n",
771 deltatv
.tv_sec
, deltatv
.tv_usec
/1000);
772 printf(" throughput in messages/sec: %g\n",
773 (double)totalmsg
/ dsecs
);
774 printf(" average message latency (usec): %2.3g\n",
775 dsecs
* 1.0E6
/ (double) totalmsg
);