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
99 ns_to_abs(uint64_t ns
)
101 return ns
* g_timebase
.denom
/ g_timebase
.numer
;
104 static inline uint64_t
105 abs_to_ns(uint64_t abs
)
107 return abs
* g_timebase
.numer
/ g_timebase
.denom
;
112 signal_handler(int sig
)
117 usage(const char *progname
)
119 fprintf(stderr
, "usage: %s [options]\n", progname
);
120 fprintf(stderr
, "where options are:\n");
121 fprintf(stderr
, " -affinity\t\tthreads use affinity\n");
122 fprintf(stderr
, " -timeshare\t\tthreads use timeshare\n");
123 fprintf(stderr
, " -threaded\t\tuse (p)threads\n");
124 fprintf(stderr
, " -verbose\t\tbe verbose (use multiple times to increase verbosity)\n");
125 fprintf(stderr
, " -oneway\t\tdo not request return reply\n");
126 fprintf(stderr
, " -count num\t\tnumber of messages to send\n");
127 fprintf(stderr
, " -perf \t\tCreate perfdata files for metrics.\n");
128 fprintf(stderr
, " -type trivial|inline|complex\ttype of messages to send\n");
129 fprintf(stderr
, " -numints num\tnumber of 32-bit ints to send in messages\n");
130 fprintf(stderr
, " -servers num\tnumber of server threads to run\n");
131 fprintf(stderr
, " -clients num\tnumber of clients per server\n");
132 fprintf(stderr
, " -delay num\t\tmicroseconds to sleep clients between messages\n");
133 fprintf(stderr
, " -work num\t\tmicroseconds of client work\n");
134 fprintf(stderr
, " -pages num\t\tpages of memory touched by client work\n");
135 fprintf(stderr
, " -set nset num\tcreate [nset] portsets and [num] ports in each server.\n");
136 fprintf(stderr
, " \tEach port is connected to each set.\n");
137 fprintf(stderr
, " -prepost\t\tstress the prepost system (implies -threaded, requires -set X Y)\n");
138 fprintf(stderr
, "default values are:\n");
139 fprintf(stderr
, " . no affinity\n");
140 fprintf(stderr
, " . not timeshare\n");
141 fprintf(stderr
, " . not threaded\n");
142 fprintf(stderr
, " . not verbose\n");
143 fprintf(stderr
, " . not oneway\n");
144 fprintf(stderr
, " . client sends 100000 messages\n");
145 fprintf(stderr
, " . inline message type\n");
146 fprintf(stderr
, " . 64 32-bit integers in inline/complex messages\n");
147 fprintf(stderr
, " . (num_available_processors+1)%%2 servers\n");
148 fprintf(stderr
, " . 4 clients per server\n");
149 fprintf(stderr
, " . no delay\n");
150 fprintf(stderr
, " . no sets / extra ports\n");
151 fprintf(stderr
, " . no prepost stress\n");
156 parse_args(int argc
, char *argv
[])
158 host_basic_info_data_t info
;
159 mach_msg_type_number_t count
;
160 kern_return_t result
;
162 /* Initialize defaults */
163 msg_type
= msg_type_trivial
;
169 count
= HOST_BASIC_INFO_COUNT
;
170 result
= host_info(mach_host_self(), HOST_BASIC_INFO
,
171 (host_info_t
)&info
, &count
);
172 if (result
== KERN_SUCCESS
&& info
.avail_cpus
> 1) {
173 num_servers
= info
.avail_cpus
/ 2;
178 const char *progname
= argv
[0];
181 if (0 == strcmp("-verbose", argv
[0])) {
184 } else if (0 == strcmp("-affinity", argv
[0])) {
187 } else if (0 == strcmp("-timeshare", argv
[0])) {
190 } else if (0 == strcmp("-threaded", argv
[0])) {
193 } else if (0 == strcmp("-oneway", argv
[0])) {
196 } else if (0 == strcmp("-perf", argv
[0])) {
197 save_perfdata
= TRUE
;
199 } else if (0 == strcmp("-type", argv
[0])) {
203 if (0 == strcmp("trivial", argv
[1])) {
204 msg_type
= msg_type_trivial
;
205 } else if (0 == strcmp("inline", argv
[1])) {
206 msg_type
= msg_type_inline
;
207 } else if (0 == strcmp("complex", argv
[1])) {
208 msg_type
= msg_type_complex
;
212 argc
-= 2; argv
+= 2;
213 } else if (0 == strcmp("-numints", argv
[0])) {
217 num_ints
= strtoul(argv
[1], NULL
, 0);
218 argc
-= 2; argv
+= 2;
219 } else if (0 == strcmp("-count", argv
[0])) {
223 num_msgs
= strtoul(argv
[1], NULL
, 0);
224 argc
-= 2; argv
+= 2;
225 } else if (0 == strcmp("-clients", argv
[0])) {
229 num_clients
= strtoul(argv
[1], NULL
, 0);
230 argc
-= 2; argv
+= 2;
231 } else if (0 == strcmp("-servers", argv
[0])) {
235 num_servers
= strtoul(argv
[1], NULL
, 0);
236 argc
-= 2; argv
+= 2;
237 } else if (0 == strcmp("-delay", argv
[0])) {
241 client_delay
= strtoul(argv
[1], NULL
, 0);
242 argc
-= 2; argv
+= 2;
243 } else if (0 == strcmp("-spin", argv
[0])) {
247 client_spin
= strtoul(argv
[1], NULL
, 0);
248 argc
-= 2; argv
+= 2;
249 } else if (0 == strcmp("-pages", argv
[0])) {
253 client_pages
= strtoul(argv
[1], NULL
, 0);
254 argc
-= 2; argv
+= 2;
255 } else if (0 == strcmp("-set", argv
[0])) {
259 setcount
= strtoul(argv
[1], NULL
, 0);
260 portcount
= strtoul(argv
[2], NULL
, 0);
261 if (setcount
<= 0 || portcount
<= 0) {
265 argc
-= 3; argv
+= 3;
266 } else if (0 == strcmp("-prepost", argv
[0])) {
267 stress_prepost
= TRUE
;
271 fprintf(stderr
, "unknown option '%s'\n", argv
[0]);
276 if (stress_prepost
) {
278 fprintf(stderr
, "Prepost stress test _must_ be threaded\n");
281 if (portcount
< 1 || setcount
< 1) {
282 fprintf(stderr
, "Prepost stress test requires >= 1 port in >= 1 set.\n");
289 setup_server_ports(struct port_args
*ports
)
291 kern_return_t ret
= 0;
295 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
296 sizeof(u_int32_t
) * num_ints
,
297 sizeof(ipc_complex_message
));
298 ports
->reply_size
= sizeof(ipc_trivial_message
) -
299 sizeof(mach_msg_trailer_t
);
300 ports
->req_msg
= malloc(ports
->req_size
);
301 ports
->reply_msg
= malloc(ports
->reply_size
);
303 ports
->set
= (mach_port_t
*)calloc(sizeof(mach_port_t
), setcount
);
305 fprintf(stderr
, "calloc(%lu, %d) failed!\n", sizeof(mach_port_t
), setcount
);
309 if (stress_prepost
) {
310 ports
->port_list
= (mach_port_t
*)calloc(sizeof(mach_port_t
), portcount
);
311 if (!ports
->port_list
) {
312 fprintf(stderr
, "calloc(%lu, %d) failed!\n", sizeof(mach_port_t
), portcount
);
320 fprintf(stderr
, "Can't use sets with a setcount of %d\n", setcount
);
324 for (int ns
= 0; ns
< setcount
; ns
++) {
325 ret
= mach_port_allocate(mach_task_self(),
326 MACH_PORT_RIGHT_PORT_SET
,
328 if (KERN_SUCCESS
!= ret
) {
329 mach_error("mach_port_allocate(SET): ", ret
);
333 printf("SVR[%d] allocated set[%d] %#x\n",
334 ports
->server_num
, ns
, ports
->set
[ns
]);
337 set
= ports
->set
[ns
];
340 /* receive on a port set (always use the first in the chain) */
341 ports
->rcv_set
= ports
->set
[0];
344 /* stuff the portset(s) with ports */
345 for (int i
= 0; i
< portcount
; i
++) {
346 ret
= mach_port_allocate(mach_task_self(),
347 MACH_PORT_RIGHT_RECEIVE
,
349 if (KERN_SUCCESS
!= ret
) {
350 mach_error("mach_port_allocate(PORT): ", ret
);
354 if (stress_prepost
) {
355 ports
->port_list
[i
] = port
;
359 /* insert the port into _all_ allocated lowest-level sets */
360 for (int ns
= 0; ns
< setcount
; ns
++) {
362 printf("SVR[%d] moving port %#x into set %#x...\n",
363 ports
->server_num
, port
, ports
->set
[ns
]);
365 ret
= mach_port_insert_member(mach_task_self(),
366 port
, ports
->set
[ns
]);
367 if (KERN_SUCCESS
!= ret
) {
368 mach_error("mach_port_insert_member(): ", ret
);
375 /* use the last one as the server's bootstrap port */
378 if (stress_prepost
) {
379 /* insert a send right for _each_ port */
380 for (int i
= 0; i
< portcount
; i
++) {
381 ret
= mach_port_insert_right(mach_task_self(),
384 MACH_MSG_TYPE_MAKE_SEND
);
385 if (KERN_SUCCESS
!= ret
) {
386 mach_error("mach_port_insert_right(): ", ret
);
391 ret
= mach_port_insert_right(mach_task_self(),
394 MACH_MSG_TYPE_MAKE_SEND
);
395 if (KERN_SUCCESS
!= ret
) {
396 mach_error("mach_port_insert_right(): ", ret
);
401 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
402 if (KERN_SUCCESS
!= ret
) {
403 mach_error("task_get_bootstrap_port(): ", ret
);
408 printf("server waiting for IPC messages from client on port '%s' (%#x).\n",
409 server_port_name
[ports
->server_num
], ports
->port
);
411 ret
= bootstrap_register(bsport
,
412 server_port_name
[ports
->server_num
],
414 if (KERN_SUCCESS
!= ret
) {
415 mach_error("bootstrap_register(): ", ret
);
421 setup_client_ports(struct port_args
*ports
)
423 kern_return_t ret
= 0;
425 case msg_type_trivial
:
426 ports
->req_size
= sizeof(ipc_trivial_message
);
428 case msg_type_inline
:
429 ports
->req_size
= sizeof(ipc_inline_message
) +
430 sizeof(u_int32_t
) * num_ints
;
432 case msg_type_complex
:
433 ports
->req_size
= sizeof(ipc_complex_message
);
436 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
437 ports
->reply_size
= sizeof(ipc_trivial_message
);
438 ports
->req_msg
= malloc(ports
->req_size
);
439 ports
->reply_msg
= malloc(ports
->reply_size
);
441 ret
= mach_port_allocate(mach_task_self(),
442 MACH_PORT_RIGHT_RECEIVE
,
444 if (KERN_SUCCESS
!= ret
) {
445 mach_error("mach_port_allocate(): ", ret
);
449 printf("Client sending %d %s IPC messages to port '%s' in %s mode\n",
450 num_msgs
, (msg_type
== msg_type_inline
) ?
451 "inline" : ((msg_type
== msg_type_complex
) ?
452 "complex" : "trivial"),
453 server_port_name
[ports
->server_num
],
454 (oneway
? "oneway" : "rpc"));
460 thread_setup(int tag
)
463 thread_extended_policy_data_t epolicy
;
464 thread_affinity_policy_data_t policy
;
467 epolicy
.timeshare
= FALSE
;
468 ret
= thread_policy_set(
469 mach_thread_self(), THREAD_EXTENDED_POLICY
,
470 (thread_policy_t
) &epolicy
,
471 THREAD_EXTENDED_POLICY_COUNT
);
472 if (ret
!= KERN_SUCCESS
) {
473 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
478 policy
.affinity_tag
= tag
;
479 ret
= thread_policy_set(
480 mach_thread_self(), THREAD_AFFINITY_POLICY
,
481 (thread_policy_t
) &policy
,
482 THREAD_AFFINITY_POLICY_COUNT
);
483 if (ret
!= KERN_SUCCESS
) {
484 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
490 server(void *serverarg
)
494 int totalmsg
= num_msgs
* num_clients
;
495 mach_port_t recv_port
;
496 uint64_t starttm
, endtm
;
498 int svr_num
= (int)(uintptr_t)serverarg
;
499 struct port_args
*args
= &server_port_args
[svr_num
];
501 args
->server_num
= svr_num
;
502 setup_server_ports(args
);
504 thread_setup(args
->server_num
+ 1);
506 recv_port
= (useset
) ? args
->rcv_set
: args
->port
;
508 for (idx
= 0; idx
< totalmsg
; idx
++) {
510 printf("server awaiting message %d\n", idx
);
512 ret
= mach_msg(args
->req_msg
,
513 MACH_RCV_MSG
| MACH_RCV_INTERRUPT
| MACH_RCV_LARGE
,
517 MACH_MSG_TIMEOUT_NONE
,
519 if (MACH_RCV_INTERRUPTED
== ret
) {
522 if (MACH_MSG_SUCCESS
!= ret
) {
524 printf("mach_msg() ret=%d", ret
);
526 mach_error("mach_msg (receive): ", ret
);
530 printf("server received message %d\n", idx
);
532 if (args
->req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
533 ret
= vm_deallocate(mach_task_self(),
534 (vm_address_t
)((ipc_complex_message
*)args
->req_msg
)->descriptor
.address
,
535 ((ipc_complex_message
*)args
->req_msg
)->descriptor
.size
);
538 if (1 == args
->req_msg
->msgh_id
) {
540 printf("server sending reply %d\n", idx
);
542 args
->reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, 0);
543 args
->reply_msg
->msgh_size
= args
->reply_size
;
544 args
->reply_msg
->msgh_remote_port
= args
->req_msg
->msgh_remote_port
;
545 args
->reply_msg
->msgh_local_port
= MACH_PORT_NULL
;
546 args
->reply_msg
->msgh_id
= 2;
547 ret
= mach_msg(args
->reply_msg
,
552 MACH_MSG_TIMEOUT_NONE
,
554 if (MACH_MSG_SUCCESS
!= ret
) {
555 mach_error("mach_msg (send): ", ret
);
569 uint64_t deltans
= 0;
571 * If we're using multiple sets, explicitly tear them all down
572 * and measure the time.
574 for (int ns
= 0; ns
< setcount
; ns
++) {
576 printf("\tTearing down set[%d] %#x...\n", ns
, args
->set
[ns
]);
578 starttm
= mach_absolute_time();
579 ret
= mach_port_mod_refs(mach_task_self(), args
->set
[ns
], MACH_PORT_RIGHT_PORT_SET
, -1);
580 endtm
= mach_absolute_time();
581 deltans
+= abs_to_ns(endtm
- starttm
);
582 if (ret
!= KERN_SUCCESS
) {
583 mach_error("mach_port_mod_refs(): ", ret
);
588 uint64_t nlinks
= (uint64_t)setcount
* (uint64_t)portcount
;
590 printf("\tteardown of %llu links took %llu ns\n", nlinks
, deltans
);
591 printf("\t%lluns per set\n", deltans
/ (uint64_t)setcount
);
597 client_spin_loop(unsigned count
, void(fn
)(void))
604 static long dummy_memory
;
605 static long *client_memory
= &dummy_memory
;
607 client_work_atom(void)
611 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long)) {
614 client_memory
[i
] = 0;
617 static int calibration_count
= 10000;
618 static int calibration_usec
;
620 calibrate_client_work(void)
623 struct timeval nowtv
;
624 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
625 struct timeval starttv
;
626 struct timeval endtv
;
629 /* Warm-up the stepper first... */
630 gettimeofday(&nowtv
, NULL
);
631 timeradd(&nowtv
, &warmuptv
, &endtv
);
633 client_spin_loop(calibration_count
, client_work_atom
);
634 gettimeofday(&nowtv
, NULL
);
635 } while (timercmp(&nowtv
, &endtv
, < ));
637 /* Now do the calibration */
639 gettimeofday(&starttv
, NULL
);
640 client_spin_loop(calibration_count
, client_work_atom
);
641 gettimeofday(&endtv
, NULL
);
642 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
643 calibration_count
/= 10;
646 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
647 if (endtv
.tv_usec
< starttv
.tv_usec
) {
648 calibration_usec
+= 1000000;
650 if (calibration_usec
< 1000) {
651 calibration_count
*= 10;
654 calibration_count
/= calibration_usec
;
658 printf("calibration_count=%d calibration_usec=%d\n",
659 calibration_count
, calibration_usec
);
669 client_spin_loop(calibration_count
* client_spin
,
674 usleep(client_delay
);
680 client(void *threadarg
)
682 struct port_args args
;
683 struct port_args
*svr_args
= NULL
;
685 mach_msg_header_t
*req
, *reply
;
686 mach_port_t bsport
, servport
;
688 int server_num
= (int)(uintptr_t)threadarg
;
689 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
692 printf("client(%d) started, server port name %s\n",
693 server_num
, server_port_name
[server_num
]);
696 args
.server_num
= server_num
;
697 thread_setup(server_num
+ 1);
699 if (stress_prepost
) {
700 svr_args
= &server_port_args
[server_num
];
703 /* find server port */
704 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
705 if (KERN_SUCCESS
!= ret
) {
706 mach_error("task_get_bootstrap_port(): ", ret
);
709 ret
= bootstrap_look_up(bsport
,
710 server_port_name
[server_num
],
712 if (KERN_SUCCESS
!= ret
) {
713 mach_error("bootstrap_look_up(): ", ret
);
717 setup_client_ports(&args
);
719 /* Allocate and touch memory */
722 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
723 for (i
= 0; i
< client_pages
; i
++) {
724 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
728 uint64_t starttm
, endtm
;
730 /* start message loop */
731 for (idx
= 0; idx
< num_msgs
; idx
++) {
733 reply
= args
.reply_msg
;
735 req
->msgh_size
= args
.req_size
;
736 if (stress_prepost
) {
737 req
->msgh_remote_port
= svr_args
->port_list
[idx
% portcount
];
739 req
->msgh_remote_port
= servport
;
742 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
743 req
->msgh_local_port
= MACH_PORT_NULL
;
745 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
746 MACH_MSG_TYPE_MAKE_SEND_ONCE
);
747 req
->msgh_local_port
= args
.port
;
749 req
->msgh_id
= oneway
? 0 : 1;
750 if (msg_type
== msg_type_complex
) {
751 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
752 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
753 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
754 ((ipc_complex_message
*)req
)->descriptor
.size
=
755 num_ints
* sizeof(u_int32_t
);
756 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
757 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
758 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
761 printf("client sending message %d to port %#x\n",
762 idx
, req
->msgh_remote_port
);
764 starttm
= mach_absolute_time();
770 MACH_MSG_TIMEOUT_NONE
,
772 endtm
= mach_absolute_time();
773 if (MACH_MSG_SUCCESS
!= ret
) {
774 mach_error("mach_msg (send): ", ret
);
775 fprintf(stderr
, "bailing after %u iterations\n", idx
);
779 if (stress_prepost
) {
780 OSAtomicAdd64(endtm
- starttm
, &g_client_send_time
);
785 printf("client awaiting reply %d\n", idx
);
787 reply
->msgh_bits
= 0;
788 reply
->msgh_size
= args
.reply_size
;
789 reply
->msgh_local_port
= args
.port
;
790 ret
= mach_msg(args
.reply_msg
,
791 MACH_RCV_MSG
| MACH_RCV_INTERRUPT
,
795 MACH_MSG_TIMEOUT_NONE
,
797 if (MACH_MSG_SUCCESS
!= ret
) {
798 mach_error("mach_msg (receive): ", ret
);
799 fprintf(stderr
, "bailing after %u iterations\n",
804 printf("client received reply %d\n", idx
);
816 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
)
820 ret
= pthread_create(
826 err(1, "pthread_create()");
829 printf("created pthread %p\n", thread
->tid
);
832 thread
->pid
= fork();
833 if (thread
->pid
== 0) {
835 printf("calling %p(%p)\n", fn
, arg
);
841 printf("forked pid %d\n", thread
->pid
);
847 thread_join(thread_id_t
*thread
)
852 printf("joining thread %p\n", thread
->tid
);
854 ret
= pthread_join(thread
->tid
, NULL
);
855 if (ret
!= KERN_SUCCESS
) {
856 err(1, "pthread_join(%p)", thread
->tid
);
861 printf("waiting for pid %d\n", thread
->pid
);
863 waitpid(thread
->pid
, &stat
, 0);
868 wait_for_servers(void)
871 int retry_count
= 10;
872 mach_port_t bsport
, servport
;
875 /* find server port */
876 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
877 if (KERN_SUCCESS
!= ret
) {
878 mach_error("task_get_bootstrap_port(): ", ret
);
882 while (retry_count
-- > 0) {
883 for (i
= 0; i
< num_servers
; i
++) {
884 ret
= bootstrap_look_up(bsport
,
887 if (ret
!= KERN_SUCCESS
) {
891 if (ret
== KERN_SUCCESS
) {
894 usleep(100 * 1000); /* 100ms */
896 fprintf(stderr
, "Server(s) failed to register\n");
901 main(int argc
, char *argv
[])
905 thread_id_t
*client_id
;
906 thread_id_t
*server_id
;
908 signal(SIGINT
, signal_handler
);
909 parse_args(argc
, argv
);
911 if (mach_timebase_info(&g_timebase
) != KERN_SUCCESS
) {
912 fprintf(stderr
, "Can't get mach_timebase_info!\n");
916 calibrate_client_work();
919 * If we're using affinity create an empty namespace now
920 * so this is shared by all our offspring.
926 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
927 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
928 server_port_args
= (struct port_args
*)calloc(sizeof(struct port_args
), num_servers
);
929 if (!server_id
|| !server_port_name
|| !server_port_args
) {
930 fprintf(stderr
, "malloc/calloc of %d server book keeping structs failed\n", num_servers
);
935 printf("creating %d servers\n", num_servers
);
937 for (i
= 0; i
< num_servers
; i
++) {
938 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
939 /* PORT names include pid of main process for disambiguation */
940 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
941 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
944 int totalclients
= num_servers
* num_clients
;
945 int totalmsg
= num_msgs
* totalclients
;
946 struct timeval starttv
, endtv
, deltatv
;
949 * Wait for all servers to have registered all ports before starting
950 * the clients and the clock.
954 printf("%d server%s, %d client%s per server (%d total) %u messages...",
955 num_servers
, (num_servers
> 1)? "s" : "",
956 num_clients
, (num_clients
> 1)? "s" : "",
961 /* Call gettimeofday() once and throw away result; some implementations
962 * (like Mach's) cache some time zone info on first call.
964 gettimeofday(&starttv
, NULL
);
965 gettimeofday(&starttv
, NULL
);
967 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
969 printf("creating %d clients\n", totalclients
);
971 for (i
= 0; i
< num_servers
; i
++) {
972 for (j
= 0; j
< num_clients
; j
++) {
974 &client_id
[(i
* num_clients
) + j
],
980 /* Wait for servers to complete */
981 for (i
= 0; i
< num_servers
; i
++) {
982 thread_join(&server_id
[i
]);
985 gettimeofday(&endtv
, NULL
);
987 printf("all servers complete: waiting for clients...\n");
990 for (i
= 0; i
< totalclients
; i
++) {
991 thread_join(&client_id
[i
]);
995 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
996 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
997 if (endtv
.tv_usec
< starttv
.tv_usec
) {
999 deltatv
.tv_usec
+= 1000000;
1002 double dsecs
= (double) deltatv
.tv_sec
+
1003 1.0E-6 * (double) deltatv
.tv_usec
;
1005 printf(" in %lu.%03u seconds\n",
1006 deltatv
.tv_sec
, deltatv
.tv_usec
/ 1000);
1007 printf(" throughput in messages/sec: %g\n",
1008 (double)totalmsg
/ dsecs
);
1009 printf(" average message latency (usec): %2.3g\n",
1010 dsecs
* 1.0E6
/ (double) totalmsg
);
1012 double time_in_sec
= (double)deltatv
.tv_sec
+ (double)deltatv
.tv_usec
/ 1000.0;
1013 double throughput_msg_p_sec
= (double) totalmsg
/ dsecs
;
1014 double avg_msg_latency
= dsecs
* 1.0E6
/ (double)totalmsg
;
1016 if (save_perfdata
== TRUE
) {
1018 snprintf(name
, sizeof(name
), "%s_avg_msg_latency", basename(argv
[0]));
1019 record_perf_data(name
, "usec", avg_msg_latency
, "Message latency measured in microseconds. Lower is better", stderr
);
1022 if (stress_prepost
) {
1023 int64_t sendns
= abs_to_ns(g_client_send_time
);
1024 dsecs
= (double)sendns
/ (double)NSEC_PER_SEC
;
1025 printf(" total send time: %2.3gs\n", dsecs
);
1026 printf(" average send time (usec): %2.3g\n",
1027 dsecs
* 1.0E6
/ (double)totalmsg
);