1 #include <AvailabilityMacros.h>
2 #include <mach/thread_policy.h>
13 #include <mach/mach.h>
14 #include <mach/mach_error.h>
15 #include <mach/mach_time.h>
16 #include <mach/notify.h>
17 #include <servers/bootstrap.h>
18 #include <sys/types.h>
20 #include <sys/signal.h>
22 #include "../unit_tests/tests_common.h" /* for record_perf_data() */
24 #include <libkern/OSAtomic.h>
26 #define MAX(A, B) ((A) < (B) ? (B) : (A))
30 mach_msg_header_t header
;
31 mach_msg_trailer_t trailer
; // subtract this when sending
32 } ipc_trivial_message
;
35 mach_msg_header_t header
;
37 mach_msg_trailer_t trailer
; // subtract this when sending
41 mach_msg_header_t header
;
43 mach_msg_ool_descriptor_t descriptor
;
44 mach_msg_trailer_t trailer
; // subtract this when sending
45 } ipc_complex_message
;
56 mach_msg_header_t
*req_msg
;
58 mach_msg_header_t
*reply_msg
;
63 mach_port_t
*port_list
;
72 static int verbose
= 0;
73 static boolean_t affinity
= FALSE
;
74 static boolean_t timeshare
= FALSE
;
75 static boolean_t threaded
= FALSE
;
76 static boolean_t oneway
= FALSE
;
77 static boolean_t useset
= FALSE
;
78 static boolean_t save_perfdata
= FALSE
;
89 boolean_t stress_prepost
= FALSE
;
90 char **server_port_name
;
92 struct port_args
*server_port_args
;
95 mach_timebase_info_data_t g_timebase
;
96 int64_t g_client_send_time
= 0;
98 static inline uint64_t ns_to_abs(uint64_t ns
)
100 return ns
* g_timebase
.denom
/ g_timebase
.numer
;
103 static inline uint64_t abs_to_ns(uint64_t abs
)
105 return abs
* g_timebase
.numer
/ g_timebase
.denom
;
109 void signal_handler(int sig
) {
112 void usage(const char *progname
) {
113 fprintf(stderr
, "usage: %s [options]\n", progname
);
114 fprintf(stderr
, "where options are:\n");
115 fprintf(stderr
, " -affinity\t\tthreads use affinity\n");
116 fprintf(stderr
, " -timeshare\t\tthreads use timeshare\n");
117 fprintf(stderr
, " -threaded\t\tuse (p)threads\n");
118 fprintf(stderr
, " -verbose\t\tbe verbose (use multiple times to increase verbosity)\n");
119 fprintf(stderr
, " -oneway\t\tdo not request return reply\n");
120 fprintf(stderr
, " -count num\t\tnumber of messages to send\n");
121 fprintf(stderr
, " -perf \t\tCreate perfdata files for metrics.\n");
122 fprintf(stderr
, " -type trivial|inline|complex\ttype of messages to send\n");
123 fprintf(stderr
, " -numints num\tnumber of 32-bit ints to send in messages\n");
124 fprintf(stderr
, " -servers num\tnumber of server threads to run\n");
125 fprintf(stderr
, " -clients num\tnumber of clients per server\n");
126 fprintf(stderr
, " -delay num\t\tmicroseconds to sleep clients between messages\n");
127 fprintf(stderr
, " -work num\t\tmicroseconds of client work\n");
128 fprintf(stderr
, " -pages num\t\tpages of memory touched by client work\n");
129 fprintf(stderr
, " -set nset num\tcreate [nset] portsets and [num] ports in each server.\n");
130 fprintf(stderr
, " \tEach port is connected to each set.\n");
131 fprintf(stderr
, " -prepost\t\tstress the prepost system (implies -threaded, requires -set X Y)\n");
132 fprintf(stderr
, "default values are:\n");
133 fprintf(stderr
, " . no affinity\n");
134 fprintf(stderr
, " . not timeshare\n");
135 fprintf(stderr
, " . not threaded\n");
136 fprintf(stderr
, " . not verbose\n");
137 fprintf(stderr
, " . not oneway\n");
138 fprintf(stderr
, " . client sends 100000 messages\n");
139 fprintf(stderr
, " . inline message type\n");
140 fprintf(stderr
, " . 64 32-bit integers in inline/complex messages\n");
141 fprintf(stderr
, " . (num_available_processors+1)%%2 servers\n");
142 fprintf(stderr
, " . 4 clients per server\n");
143 fprintf(stderr
, " . no delay\n");
144 fprintf(stderr
, " . no sets / extra ports\n");
145 fprintf(stderr
, " . no prepost stress\n");
149 void parse_args(int argc
, char *argv
[]) {
150 host_basic_info_data_t info
;
151 mach_msg_type_number_t count
;
152 kern_return_t result
;
154 /* Initialize defaults */
155 msg_type
= msg_type_trivial
;
161 count
= HOST_BASIC_INFO_COUNT
;
162 result
= host_info(mach_host_self(), HOST_BASIC_INFO
,
163 (host_info_t
)&info
, &count
);
164 if (result
== KERN_SUCCESS
&& info
.avail_cpus
> 1)
165 num_servers
= info
.avail_cpus
/ 2;
169 const char *progname
= argv
[0];
172 if (0 == strcmp("-verbose", argv
[0])) {
175 } else if (0 == strcmp("-affinity", argv
[0])) {
178 } else if (0 == strcmp("-timeshare", argv
[0])) {
181 } else if (0 == strcmp("-threaded", argv
[0])) {
184 } else if (0 == strcmp("-oneway", argv
[0])) {
187 } else if (0 == strcmp("-perf", argv
[0])) {
188 save_perfdata
= TRUE
;
190 } else if (0 == strcmp("-type", argv
[0])) {
193 if (0 == strcmp("trivial", argv
[1])) {
194 msg_type
= msg_type_trivial
;
195 } else if (0 == strcmp("inline", argv
[1])) {
196 msg_type
= msg_type_inline
;
197 } else if (0 == strcmp("complex", argv
[1])) {
198 msg_type
= msg_type_complex
;
201 argc
-= 2; argv
+= 2;
202 } else if (0 == strcmp("-numints", argv
[0])) {
205 num_ints
= strtoul(argv
[1], NULL
, 0);
206 argc
-= 2; argv
+= 2;
207 } else if (0 == strcmp("-count", argv
[0])) {
210 num_msgs
= strtoul(argv
[1], NULL
, 0);
211 argc
-= 2; argv
+= 2;
212 } else if (0 == strcmp("-clients", argv
[0])) {
215 num_clients
= strtoul(argv
[1], NULL
, 0);
216 argc
-= 2; argv
+= 2;
217 } else if (0 == strcmp("-servers", argv
[0])) {
220 num_servers
= strtoul(argv
[1], NULL
, 0);
221 argc
-= 2; argv
+= 2;
222 } else if (0 == strcmp("-delay", argv
[0])) {
225 client_delay
= strtoul(argv
[1], NULL
, 0);
226 argc
-= 2; argv
+= 2;
227 } else if (0 == strcmp("-spin", argv
[0])) {
230 client_spin
= strtoul(argv
[1], NULL
, 0);
231 argc
-= 2; argv
+= 2;
232 } else if (0 == strcmp("-pages", argv
[0])) {
235 client_pages
= strtoul(argv
[1], NULL
, 0);
236 argc
-= 2; argv
+= 2;
237 } else if (0 == strcmp("-set", argv
[0])) {
240 setcount
= strtoul(argv
[1], NULL
, 0);
241 portcount
= strtoul(argv
[2], NULL
, 0);
242 if (setcount
<= 0 || portcount
<= 0)
245 argc
-= 3; argv
+= 3;
246 } else if (0 == strcmp("-prepost", argv
[0])) {
247 stress_prepost
= TRUE
;
251 fprintf(stderr
, "unknown option '%s'\n", argv
[0]);
256 if (stress_prepost
) {
258 fprintf(stderr
, "Prepost stress test _must_ be threaded\n");
261 if (portcount
< 1 || setcount
< 1) {
262 fprintf(stderr
, "Prepost stress test requires >= 1 port in >= 1 set.\n");
268 void setup_server_ports(struct port_args
*ports
)
270 kern_return_t ret
= 0;
274 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
275 sizeof(u_int32_t
) * num_ints
,
276 sizeof(ipc_complex_message
));
277 ports
->reply_size
= sizeof(ipc_trivial_message
) -
278 sizeof(mach_msg_trailer_t
);
279 ports
->req_msg
= malloc(ports
->req_size
);
280 ports
->reply_msg
= malloc(ports
->reply_size
);
282 ports
->set
= (mach_port_t
*)calloc(sizeof(mach_port_t
), setcount
);
284 fprintf(stderr
, "calloc(%lu, %d) failed!\n", sizeof(mach_port_t
), setcount
);
288 if (stress_prepost
) {
289 ports
->port_list
= (mach_port_t
*)calloc(sizeof(mach_port_t
), portcount
);
290 if (!ports
->port_list
) {
291 fprintf(stderr
, "calloc(%lu, %d) failed!\n", sizeof(mach_port_t
), portcount
);
299 fprintf(stderr
, "Can't use sets with a setcount of %d\n", setcount
);
303 for (int ns
= 0; ns
< setcount
; ns
++) {
304 ret
= mach_port_allocate(mach_task_self(),
305 MACH_PORT_RIGHT_PORT_SET
,
307 if (KERN_SUCCESS
!= ret
) {
308 mach_error("mach_port_allocate(SET): ", ret
);
312 printf("SVR[%d] allocated set[%d] %#x\n",
313 ports
->server_num
, ns
, ports
->set
[ns
]);
315 set
= ports
->set
[ns
];
318 /* receive on a port set (always use the first in the chain) */
319 ports
->rcv_set
= ports
->set
[0];
322 /* stuff the portset(s) with ports */
323 for (int i
= 0; i
< portcount
; i
++) {
324 ret
= mach_port_allocate(mach_task_self(),
325 MACH_PORT_RIGHT_RECEIVE
,
327 if (KERN_SUCCESS
!= ret
) {
328 mach_error("mach_port_allocate(PORT): ", ret
);
333 ports
->port_list
[i
] = port
;
336 /* insert the port into _all_ allocated lowest-level sets */
337 for (int ns
= 0; ns
< setcount
; ns
++) {
339 printf("SVR[%d] moving port %#x into set %#x...\n",
340 ports
->server_num
, port
, ports
->set
[ns
]);
341 ret
= mach_port_insert_member(mach_task_self(),
342 port
, ports
->set
[ns
]);
343 if (KERN_SUCCESS
!= ret
) {
344 mach_error("mach_port_insert_member(): ", ret
);
351 /* use the last one as the server's bootstrap port */
354 if (stress_prepost
) {
355 /* insert a send right for _each_ port */
356 for (int i
= 0; i
< portcount
; i
++) {
357 ret
= mach_port_insert_right(mach_task_self(),
360 MACH_MSG_TYPE_MAKE_SEND
);
361 if (KERN_SUCCESS
!= ret
) {
362 mach_error("mach_port_insert_right(): ", ret
);
367 ret
= mach_port_insert_right(mach_task_self(),
370 MACH_MSG_TYPE_MAKE_SEND
);
371 if (KERN_SUCCESS
!= ret
) {
372 mach_error("mach_port_insert_right(): ", ret
);
377 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
378 if (KERN_SUCCESS
!= ret
) {
379 mach_error("task_get_bootstrap_port(): ", ret
);
384 printf("server waiting for IPC messages from client on port '%s' (%#x).\n",
385 server_port_name
[ports
->server_num
], ports
->port
);
387 ret
= bootstrap_register(bsport
,
388 server_port_name
[ports
->server_num
],
390 if (KERN_SUCCESS
!= ret
) {
391 mach_error("bootstrap_register(): ", ret
);
396 void setup_client_ports(struct port_args
*ports
)
398 kern_return_t ret
= 0;
400 case msg_type_trivial
:
401 ports
->req_size
= sizeof(ipc_trivial_message
);
403 case msg_type_inline
:
404 ports
->req_size
= sizeof(ipc_inline_message
) +
405 sizeof(u_int32_t
) * num_ints
;
407 case msg_type_complex
:
408 ports
->req_size
= sizeof(ipc_complex_message
);
411 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
412 ports
->reply_size
= sizeof(ipc_trivial_message
);
413 ports
->req_msg
= malloc(ports
->req_size
);
414 ports
->reply_msg
= malloc(ports
->reply_size
);
416 ret
= mach_port_allocate(mach_task_self(),
417 MACH_PORT_RIGHT_RECEIVE
,
419 if (KERN_SUCCESS
!= ret
) {
420 mach_error("mach_port_allocate(): ", ret
);
424 printf("Client sending %d %s IPC messages to port '%s' in %s mode\n",
425 num_msgs
, (msg_type
== msg_type_inline
) ?
426 "inline" : ((msg_type
== msg_type_complex
) ?
427 "complex" : "trivial"),
428 server_port_name
[ports
->server_num
],
429 (oneway
? "oneway" : "rpc"));
435 thread_setup(int tag
) {
437 thread_extended_policy_data_t epolicy
;
438 thread_affinity_policy_data_t policy
;
441 epolicy
.timeshare
= FALSE
;
442 ret
= thread_policy_set(
443 mach_thread_self(), THREAD_EXTENDED_POLICY
,
444 (thread_policy_t
) &epolicy
,
445 THREAD_EXTENDED_POLICY_COUNT
);
446 if (ret
!= KERN_SUCCESS
)
447 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
451 policy
.affinity_tag
= tag
;
452 ret
= thread_policy_set(
453 mach_thread_self(), THREAD_AFFINITY_POLICY
,
454 (thread_policy_t
) &policy
,
455 THREAD_AFFINITY_POLICY_COUNT
);
456 if (ret
!= KERN_SUCCESS
)
457 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
462 server(void *serverarg
)
466 int totalmsg
= num_msgs
* num_clients
;
467 mach_port_t recv_port
;
468 uint64_t starttm
, endtm
;
470 int svr_num
= (int)(uintptr_t)serverarg
;
471 struct port_args
*args
= &server_port_args
[svr_num
];
473 args
->server_num
= svr_num
;
474 setup_server_ports(args
);
476 thread_setup(args
->server_num
+ 1);
478 recv_port
= (useset
) ? args
->rcv_set
: args
->port
;
480 for (idx
= 0; idx
< totalmsg
; idx
++) {
482 printf("server awaiting message %d\n", idx
);
483 ret
= mach_msg(args
->req_msg
,
484 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
|MACH_RCV_LARGE
,
488 MACH_MSG_TIMEOUT_NONE
,
490 if (MACH_RCV_INTERRUPTED
== ret
)
492 if (MACH_MSG_SUCCESS
!= ret
) {
494 printf("mach_msg() ret=%d", ret
);
495 mach_error("mach_msg (receive): ", ret
);
499 printf("server received message %d\n", idx
);
500 if (args
->req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
501 ret
= vm_deallocate(mach_task_self(),
502 (vm_address_t
)((ipc_complex_message
*)args
->req_msg
)->descriptor
.address
,
503 ((ipc_complex_message
*)args
->req_msg
)->descriptor
.size
);
506 if (1 == args
->req_msg
->msgh_id
) {
508 printf("server sending reply %d\n", idx
);
509 args
->reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, 0);
510 args
->reply_msg
->msgh_size
= args
->reply_size
;
511 args
->reply_msg
->msgh_remote_port
= args
->req_msg
->msgh_remote_port
;
512 args
->reply_msg
->msgh_local_port
= MACH_PORT_NULL
;
513 args
->reply_msg
->msgh_id
= 2;
514 ret
= mach_msg(args
->reply_msg
,
519 MACH_MSG_TIMEOUT_NONE
,
521 if (MACH_MSG_SUCCESS
!= ret
) {
522 mach_error("mach_msg (send): ", ret
);
534 uint64_t deltans
= 0;
536 * If we're using multiple sets, explicitly tear them all down
537 * and measure the time.
539 for (int ns
= 0; ns
< setcount
; ns
++) {
541 printf("\tTearing down set[%d] %#x...\n", ns
, args
->set
[ns
]);
542 starttm
= mach_absolute_time();
543 ret
= mach_port_mod_refs(mach_task_self(), args
->set
[ns
], MACH_PORT_RIGHT_PORT_SET
, -1);
544 endtm
= mach_absolute_time();
545 deltans
+= abs_to_ns(endtm
- starttm
);
546 if (ret
!= KERN_SUCCESS
) {
547 mach_error("mach_port_mod_refs(): ", ret
);
552 uint64_t nlinks
= (uint64_t)setcount
* (uint64_t)portcount
;
554 printf("\tteardown of %llu links took %llu ns\n", nlinks
, deltans
);
555 printf("\t%lluns per set\n", deltans
/ (uint64_t)setcount
);
561 client_spin_loop(unsigned count
, void (fn
)(void))
567 static long dummy_memory
;
568 static long *client_memory
= &dummy_memory
;
570 client_work_atom(void)
574 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long))
576 client_memory
[i
] = 0;
579 static int calibration_count
= 10000;
580 static int calibration_usec
;
582 calibrate_client_work(void)
585 struct timeval nowtv
;
586 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
587 struct timeval starttv
;
588 struct timeval endtv
;
591 /* Warm-up the stepper first... */
592 gettimeofday(&nowtv
, NULL
);
593 timeradd(&nowtv
, &warmuptv
, &endtv
);
595 client_spin_loop(calibration_count
, client_work_atom
);
596 gettimeofday(&nowtv
, NULL
);
597 } while (timercmp(&nowtv
, &endtv
, < ));
599 /* Now do the calibration */
601 gettimeofday(&starttv
, NULL
);
602 client_spin_loop(calibration_count
, client_work_atom
);
603 gettimeofday(&endtv
, NULL
);
604 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
605 calibration_count
/= 10;
608 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
609 if (endtv
.tv_usec
< starttv
.tv_usec
) {
610 calibration_usec
+= 1000000;
612 if (calibration_usec
< 1000) {
613 calibration_count
*= 10;
616 calibration_count
/= calibration_usec
;
620 printf("calibration_count=%d calibration_usec=%d\n",
621 calibration_count
, calibration_usec
);
631 client_spin_loop(calibration_count
*client_spin
,
636 usleep(client_delay
);
641 void *client(void *threadarg
)
643 struct port_args args
;
644 struct port_args
*svr_args
= NULL
;
646 mach_msg_header_t
*req
, *reply
;
647 mach_port_t bsport
, servport
;
649 int server_num
= (int)(uintptr_t)threadarg
;
650 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
653 printf("client(%d) started, server port name %s\n",
654 server_num
, server_port_name
[server_num
]);
656 args
.server_num
= server_num
;
657 thread_setup(server_num
+ 1);
660 svr_args
= &server_port_args
[server_num
];
662 /* find server port */
663 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
664 if (KERN_SUCCESS
!= ret
) {
665 mach_error("task_get_bootstrap_port(): ", ret
);
668 ret
= bootstrap_look_up(bsport
,
669 server_port_name
[server_num
],
671 if (KERN_SUCCESS
!= ret
) {
672 mach_error("bootstrap_look_up(): ", ret
);
676 setup_client_ports(&args
);
678 /* Allocate and touch memory */
681 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
682 for (i
= 0; i
< client_pages
; i
++)
683 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
686 uint64_t starttm
, endtm
;
688 /* start message loop */
689 for (idx
= 0; idx
< num_msgs
; idx
++) {
691 reply
= args
.reply_msg
;
693 req
->msgh_size
= args
.req_size
;
694 if (stress_prepost
) {
695 req
->msgh_remote_port
= svr_args
->port_list
[idx
% portcount
];
697 req
->msgh_remote_port
= servport
;
700 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
701 req
->msgh_local_port
= MACH_PORT_NULL
;
703 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
704 MACH_MSG_TYPE_MAKE_SEND_ONCE
);
705 req
->msgh_local_port
= args
.port
;
707 req
->msgh_id
= oneway
? 0 : 1;
708 if (msg_type
== msg_type_complex
) {
709 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
710 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
711 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
712 ((ipc_complex_message
*)req
)->descriptor
.size
=
713 num_ints
* sizeof(u_int32_t
);
714 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
715 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
716 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
719 printf("client sending message %d to port %#x\n",
720 idx
, req
->msgh_remote_port
);
721 starttm
= mach_absolute_time();
727 MACH_MSG_TIMEOUT_NONE
,
729 endtm
= mach_absolute_time();
730 if (MACH_MSG_SUCCESS
!= ret
) {
731 mach_error("mach_msg (send): ", ret
);
732 fprintf(stderr
, "bailing after %u iterations\n", idx
);
737 OSAtomicAdd64(endtm
- starttm
, &g_client_send_time
);
741 printf("client awaiting reply %d\n", idx
);
742 reply
->msgh_bits
= 0;
743 reply
->msgh_size
= args
.reply_size
;
744 reply
->msgh_local_port
= args
.port
;
745 ret
= mach_msg(args
.reply_msg
,
746 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
,
750 MACH_MSG_TIMEOUT_NONE
,
752 if (MACH_MSG_SUCCESS
!= ret
) {
753 mach_error("mach_msg (receive): ", ret
);
754 fprintf(stderr
, "bailing after %u iterations\n",
759 printf("client received reply %d\n", idx
);
770 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
) {
773 ret
= pthread_create(
779 err(1, "pthread_create()");
781 printf("created pthread %p\n", thread
->tid
);
783 thread
->pid
= fork();
784 if (thread
->pid
== 0) {
786 printf("calling %p(%p)\n", fn
, arg
);
791 printf("forked pid %d\n", thread
->pid
);
796 thread_join(thread_id_t
*thread
) {
800 printf("joining thread %p\n", thread
->tid
);
801 ret
= pthread_join(thread
->tid
, NULL
);
802 if (ret
!= KERN_SUCCESS
)
803 err(1, "pthread_join(%p)", thread
->tid
);
807 printf("waiting for pid %d\n", thread
->pid
);
808 waitpid(thread
->pid
, &stat
, 0);
813 wait_for_servers(void)
816 int retry_count
= 10;
817 mach_port_t bsport
, servport
;
820 /* find server port */
821 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
822 if (KERN_SUCCESS
!= ret
) {
823 mach_error("task_get_bootstrap_port(): ", ret
);
827 while (retry_count
-- > 0) {
828 for (i
= 0; i
< num_servers
; i
++) {
829 ret
= bootstrap_look_up(bsport
,
832 if (ret
!= KERN_SUCCESS
) {
836 if (ret
== KERN_SUCCESS
)
838 usleep(100 * 1000); /* 100ms */
840 fprintf(stderr
, "Server(s) failed to register\n");
844 int main(int argc
, char *argv
[])
848 thread_id_t
*client_id
;
849 thread_id_t
*server_id
;
851 signal(SIGINT
, signal_handler
);
852 parse_args(argc
, argv
);
854 if (mach_timebase_info(&g_timebase
) != KERN_SUCCESS
) {
855 fprintf(stderr
, "Can't get mach_timebase_info!\n");
859 calibrate_client_work();
862 * If we're using affinity create an empty namespace now
863 * so this is shared by all our offspring.
868 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
869 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
870 server_port_args
= (struct port_args
*)calloc(sizeof(struct port_args
), num_servers
);
871 if (!server_id
|| !server_port_name
|| !server_port_args
) {
872 fprintf(stderr
, "malloc/calloc of %d server book keeping structs failed\n", num_servers
);
877 printf("creating %d servers\n", num_servers
);
878 for (i
= 0; i
< num_servers
; i
++) {
879 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
880 /* PORT names include pid of main process for disambiguation */
881 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
882 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
885 int totalclients
= num_servers
* num_clients
;
886 int totalmsg
= num_msgs
* totalclients
;
887 struct timeval starttv
, endtv
, deltatv
;
890 * Wait for all servers to have registered all ports before starting
891 * the clients and the clock.
895 printf("%d server%s, %d client%s per server (%d total) %u messages...",
896 num_servers
, (num_servers
> 1)? "s" : "",
897 num_clients
, (num_clients
> 1)? "s" : "",
902 /* Call gettimeofday() once and throw away result; some implementations
903 * (like Mach's) cache some time zone info on first call.
905 gettimeofday(&starttv
, NULL
);
906 gettimeofday(&starttv
, NULL
);
908 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
910 printf("creating %d clients\n", totalclients
);
911 for (i
= 0; i
< num_servers
; i
++) {
912 for (j
= 0; j
< num_clients
; j
++) {
914 &client_id
[(i
*num_clients
) + j
],
920 /* Wait for servers to complete */
921 for (i
= 0; i
< num_servers
; i
++) {
922 thread_join(&server_id
[i
]);
925 gettimeofday(&endtv
, NULL
);
927 printf("all servers complete: waiting for clients...\n");
929 for (i
= 0; i
< totalclients
; i
++) {
930 thread_join(&client_id
[i
]);
934 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
935 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
936 if (endtv
.tv_usec
< starttv
.tv_usec
) {
938 deltatv
.tv_usec
+= 1000000;
941 double dsecs
= (double) deltatv
.tv_sec
+
942 1.0E-6 * (double) deltatv
.tv_usec
;
944 printf(" in %lu.%03u seconds\n",
945 deltatv
.tv_sec
, deltatv
.tv_usec
/1000);
946 printf(" throughput in messages/sec: %g\n",
947 (double)totalmsg
/ dsecs
);
948 printf(" average message latency (usec): %2.3g\n",
949 dsecs
* 1.0E6
/ (double) totalmsg
);
951 double time_in_sec
= (double)deltatv
.tv_sec
+ (double)deltatv
.tv_usec
/1000.0;
952 double throughput_msg_p_sec
= (double) totalmsg
/dsecs
;
953 double avg_msg_latency
= dsecs
*1.0E6
/ (double)totalmsg
;
955 if (save_perfdata
== TRUE
) {
957 snprintf(name
, sizeof(name
), "%s_avg_msg_latency", basename(argv
[0]));
958 record_perf_data(name
, "usec", avg_msg_latency
, "Message latency measured in microseconds. Lower is better", stderr
);
961 if (stress_prepost
) {
962 int64_t sendns
= abs_to_ns(g_client_send_time
);
963 dsecs
= (double)sendns
/ (double)NSEC_PER_SEC
;
964 printf(" total send time: %2.3gs\n", dsecs
);
965 printf(" average send time (usec): %2.3g\n",
966 dsecs
* 1.0E6
/ (double)totalmsg
);