1 #include <AvailabilityMacros.h>
2 #include <mach/thread_policy.h>
12 #include <mach/mach.h>
13 #include <mach/mach_error.h>
14 #include <mach/mach_time.h>
15 #include <mach/notify.h>
16 #include <servers/bootstrap.h>
17 #include <sys/types.h>
19 #include <sys/signal.h>
21 #include <libkern/OSAtomic.h>
23 #define MAX(A, B) ((A) < (B) ? (B) : (A))
27 mach_msg_header_t header
;
28 mach_msg_trailer_t trailer
; // subtract this when sending
29 } ipc_trivial_message
;
32 mach_msg_header_t header
;
34 mach_msg_trailer_t trailer
; // subtract this when sending
38 mach_msg_header_t header
;
40 mach_msg_ool_descriptor_t descriptor
;
41 mach_msg_trailer_t trailer
; // subtract this when sending
42 } ipc_complex_message
;
53 mach_msg_header_t
*req_msg
;
55 mach_msg_header_t
*reply_msg
;
60 mach_port_t
*port_list
;
69 static int verbose
= 0;
70 static boolean_t affinity
= FALSE
;
71 static boolean_t timeshare
= FALSE
;
72 static boolean_t threaded
= FALSE
;
73 static boolean_t oneway
= FALSE
;
74 static boolean_t useset
= FALSE
;
85 boolean_t stress_prepost
= FALSE
;
86 char **server_port_name
;
88 struct port_args
*server_port_args
;
91 mach_timebase_info_data_t g_timebase
;
92 int64_t g_client_send_time
= 0;
94 static inline uint64_t ns_to_abs(uint64_t ns
)
96 return ns
* g_timebase
.denom
/ g_timebase
.numer
;
99 static inline uint64_t abs_to_ns(uint64_t abs
)
101 return abs
* g_timebase
.numer
/ g_timebase
.denom
;
105 void signal_handler(int sig
) {
108 void usage(const char *progname
) {
109 fprintf(stderr
, "usage: %s [options]\n", progname
);
110 fprintf(stderr
, "where options are:\n");
111 fprintf(stderr
, " -affinity\t\tthreads use affinity\n");
112 fprintf(stderr
, " -timeshare\t\tthreads use timeshare\n");
113 fprintf(stderr
, " -threaded\t\tuse (p)threads\n");
114 fprintf(stderr
, " -verbose\t\tbe verbose (use multiple times to increase verbosity)\n");
115 fprintf(stderr
, " -oneway\t\tdo not request return reply\n");
116 fprintf(stderr
, " -count num\t\tnumber of messages to send\n");
117 fprintf(stderr
, " -type trivial|inline|complex\ttype of messages to send\n");
118 fprintf(stderr
, " -numints num\tnumber of 32-bit ints to send in messages\n");
119 fprintf(stderr
, " -servers num\tnumber of server threads to run\n");
120 fprintf(stderr
, " -clients num\tnumber of clients per server\n");
121 fprintf(stderr
, " -delay num\t\tmicroseconds to sleep clients between messages\n");
122 fprintf(stderr
, " -work num\t\tmicroseconds of client work\n");
123 fprintf(stderr
, " -pages num\t\tpages of memory touched by client work\n");
124 fprintf(stderr
, " -set nset num\tcreate [nset] portsets and [num] ports in each server.\n");
125 fprintf(stderr
, " \tEach port is connected to each set.\n");
126 fprintf(stderr
, " -prepost\t\tstress the prepost system (implies -threaded, requires -set X Y)\n");
127 fprintf(stderr
, "default values are:\n");
128 fprintf(stderr
, " . no affinity\n");
129 fprintf(stderr
, " . not timeshare\n");
130 fprintf(stderr
, " . not threaded\n");
131 fprintf(stderr
, " . not verbose\n");
132 fprintf(stderr
, " . not oneway\n");
133 fprintf(stderr
, " . client sends 100000 messages\n");
134 fprintf(stderr
, " . inline message type\n");
135 fprintf(stderr
, " . 64 32-bit integers in inline/complex messages\n");
136 fprintf(stderr
, " . (num_available_processors+1)%%2 servers\n");
137 fprintf(stderr
, " . 4 clients per server\n");
138 fprintf(stderr
, " . no delay\n");
139 fprintf(stderr
, " . no sets / extra ports\n");
140 fprintf(stderr
, " . no prepost stress\n");
144 void parse_args(int argc
, char *argv
[]) {
145 host_basic_info_data_t info
;
146 mach_msg_type_number_t count
;
147 kern_return_t result
;
149 /* Initialize defaults */
150 msg_type
= msg_type_trivial
;
156 count
= HOST_BASIC_INFO_COUNT
;
157 result
= host_info(mach_host_self(), HOST_BASIC_INFO
,
158 (host_info_t
)&info
, &count
);
159 if (result
== KERN_SUCCESS
&& info
.avail_cpus
> 1)
160 num_servers
= info
.avail_cpus
/ 2;
164 const char *progname
= argv
[0];
167 if (0 == strcmp("-verbose", argv
[0])) {
170 } else if (0 == strcmp("-affinity", argv
[0])) {
173 } else if (0 == strcmp("-timeshare", argv
[0])) {
176 } else if (0 == strcmp("-threaded", argv
[0])) {
179 } else if (0 == strcmp("-oneway", argv
[0])) {
182 } else if (0 == strcmp("-type", argv
[0])) {
185 if (0 == strcmp("trivial", argv
[1])) {
186 msg_type
= msg_type_trivial
;
187 } else if (0 == strcmp("inline", argv
[1])) {
188 msg_type
= msg_type_inline
;
189 } else if (0 == strcmp("complex", argv
[1])) {
190 msg_type
= msg_type_complex
;
193 argc
-= 2; argv
+= 2;
194 } else if (0 == strcmp("-numints", argv
[0])) {
197 num_ints
= strtoul(argv
[1], NULL
, 0);
198 argc
-= 2; argv
+= 2;
199 } else if (0 == strcmp("-count", argv
[0])) {
202 num_msgs
= strtoul(argv
[1], NULL
, 0);
203 argc
-= 2; argv
+= 2;
204 } else if (0 == strcmp("-clients", argv
[0])) {
207 num_clients
= strtoul(argv
[1], NULL
, 0);
208 argc
-= 2; argv
+= 2;
209 } else if (0 == strcmp("-servers", argv
[0])) {
212 num_servers
= strtoul(argv
[1], NULL
, 0);
213 argc
-= 2; argv
+= 2;
214 } else if (0 == strcmp("-delay", argv
[0])) {
217 client_delay
= strtoul(argv
[1], NULL
, 0);
218 argc
-= 2; argv
+= 2;
219 } else if (0 == strcmp("-spin", argv
[0])) {
222 client_spin
= strtoul(argv
[1], NULL
, 0);
223 argc
-= 2; argv
+= 2;
224 } else if (0 == strcmp("-pages", argv
[0])) {
227 client_pages
= strtoul(argv
[1], NULL
, 0);
228 argc
-= 2; argv
+= 2;
229 } else if (0 == strcmp("-set", argv
[0])) {
232 setcount
= strtoul(argv
[1], NULL
, 0);
233 portcount
= strtoul(argv
[2], NULL
, 0);
234 if (setcount
<= 0 || portcount
<= 0)
237 argc
-= 3; argv
+= 3;
238 } else if (0 == strcmp("-prepost", argv
[0])) {
239 stress_prepost
= TRUE
;
243 fprintf(stderr
, "unknown option '%s'\n", argv
[0]);
248 if (stress_prepost
) {
250 fprintf(stderr
, "Prepost stress test _must_ be threaded\n");
253 if (portcount
< 1 || setcount
< 1) {
254 fprintf(stderr
, "Prepost stress test requires >= 1 port in >= 1 set.\n");
260 void setup_server_ports(struct port_args
*ports
)
262 kern_return_t ret
= 0;
266 ports
->req_size
= MAX(sizeof(ipc_inline_message
) +
267 sizeof(u_int32_t
) * num_ints
,
268 sizeof(ipc_complex_message
));
269 ports
->reply_size
= sizeof(ipc_trivial_message
) -
270 sizeof(mach_msg_trailer_t
);
271 ports
->req_msg
= malloc(ports
->req_size
);
272 ports
->reply_msg
= malloc(ports
->reply_size
);
274 ports
->set
= (mach_port_t
*)calloc(sizeof(mach_port_t
), setcount
);
276 fprintf(stderr
, "calloc(%lu, %d) failed!\n", sizeof(mach_port_t
), setcount
);
280 if (stress_prepost
) {
281 ports
->port_list
= (mach_port_t
*)calloc(sizeof(mach_port_t
), portcount
);
282 if (!ports
->port_list
) {
283 fprintf(stderr
, "calloc(%lu, %d) failed!\n", sizeof(mach_port_t
), portcount
);
291 fprintf(stderr
, "Can't use sets with a setcount of %d\n", setcount
);
295 for (int ns
= 0; ns
< setcount
; ns
++) {
296 ret
= mach_port_allocate(mach_task_self(),
297 MACH_PORT_RIGHT_PORT_SET
,
299 if (KERN_SUCCESS
!= ret
) {
300 mach_error("mach_port_allocate(SET): ", ret
);
304 printf("SVR[%d] allocated set[%d] %#x\n",
305 ports
->server_num
, ns
, ports
->set
[ns
]);
307 set
= ports
->set
[ns
];
310 /* receive on a port set (always use the first in the chain) */
311 ports
->rcv_set
= ports
->set
[0];
314 /* stuff the portset(s) with ports */
315 for (int i
= 0; i
< portcount
; i
++) {
316 ret
= mach_port_allocate(mach_task_self(),
317 MACH_PORT_RIGHT_RECEIVE
,
319 if (KERN_SUCCESS
!= ret
) {
320 mach_error("mach_port_allocate(PORT): ", ret
);
325 ports
->port_list
[i
] = port
;
328 /* insert the port into _all_ allocated lowest-level sets */
329 for (int ns
= 0; ns
< setcount
; ns
++) {
331 printf("SVR[%d] moving port %#x into set %#x...\n",
332 ports
->server_num
, port
, ports
->set
[ns
]);
333 ret
= mach_port_insert_member(mach_task_self(),
334 port
, ports
->set
[ns
]);
335 if (KERN_SUCCESS
!= ret
) {
336 mach_error("mach_port_insert_member(): ", ret
);
343 /* use the last one as the server's bootstrap port */
346 if (stress_prepost
) {
347 /* insert a send right for _each_ port */
348 for (int i
= 0; i
< portcount
; i
++) {
349 ret
= mach_port_insert_right(mach_task_self(),
352 MACH_MSG_TYPE_MAKE_SEND
);
353 if (KERN_SUCCESS
!= ret
) {
354 mach_error("mach_port_insert_right(): ", ret
);
359 ret
= mach_port_insert_right(mach_task_self(),
362 MACH_MSG_TYPE_MAKE_SEND
);
363 if (KERN_SUCCESS
!= ret
) {
364 mach_error("mach_port_insert_right(): ", ret
);
369 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
370 if (KERN_SUCCESS
!= ret
) {
371 mach_error("task_get_bootstrap_port(): ", ret
);
376 printf("server waiting for IPC messages from client on port '%s' (%#x).\n",
377 server_port_name
[ports
->server_num
], ports
->port
);
379 ret
= bootstrap_register(bsport
,
380 server_port_name
[ports
->server_num
],
382 if (KERN_SUCCESS
!= ret
) {
383 mach_error("bootstrap_register(): ", ret
);
388 void setup_client_ports(struct port_args
*ports
)
390 kern_return_t ret
= 0;
392 case msg_type_trivial
:
393 ports
->req_size
= sizeof(ipc_trivial_message
);
395 case msg_type_inline
:
396 ports
->req_size
= sizeof(ipc_inline_message
) +
397 sizeof(u_int32_t
) * num_ints
;
399 case msg_type_complex
:
400 ports
->req_size
= sizeof(ipc_complex_message
);
403 ports
->req_size
-= sizeof(mach_msg_trailer_t
);
404 ports
->reply_size
= sizeof(ipc_trivial_message
);
405 ports
->req_msg
= malloc(ports
->req_size
);
406 ports
->reply_msg
= malloc(ports
->reply_size
);
408 ret
= mach_port_allocate(mach_task_self(),
409 MACH_PORT_RIGHT_RECEIVE
,
411 if (KERN_SUCCESS
!= ret
) {
412 mach_error("mach_port_allocate(): ", ret
);
416 printf("Client sending %d %s IPC messages to port '%s' in %s mode\n",
417 num_msgs
, (msg_type
== msg_type_inline
) ?
418 "inline" : ((msg_type
== msg_type_complex
) ?
419 "complex" : "trivial"),
420 server_port_name
[ports
->server_num
],
421 (oneway
? "oneway" : "rpc"));
427 thread_setup(int tag
) {
429 thread_extended_policy_data_t epolicy
;
430 thread_affinity_policy_data_t policy
;
433 epolicy
.timeshare
= FALSE
;
434 ret
= thread_policy_set(
435 mach_thread_self(), THREAD_EXTENDED_POLICY
,
436 (thread_policy_t
) &epolicy
,
437 THREAD_EXTENDED_POLICY_COUNT
);
438 if (ret
!= KERN_SUCCESS
)
439 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret
);
443 policy
.affinity_tag
= tag
;
444 ret
= thread_policy_set(
445 mach_thread_self(), THREAD_AFFINITY_POLICY
,
446 (thread_policy_t
) &policy
,
447 THREAD_AFFINITY_POLICY_COUNT
);
448 if (ret
!= KERN_SUCCESS
)
449 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret
);
454 server(void *serverarg
)
458 int totalmsg
= num_msgs
* num_clients
;
459 mach_port_t recv_port
;
460 uint64_t starttm
, endtm
;
462 int svr_num
= (int)(uintptr_t)serverarg
;
463 struct port_args
*args
= &server_port_args
[svr_num
];
465 args
->server_num
= svr_num
;
466 setup_server_ports(args
);
468 thread_setup(args
->server_num
+ 1);
470 recv_port
= (useset
) ? args
->rcv_set
: args
->port
;
472 for (idx
= 0; idx
< totalmsg
; idx
++) {
474 printf("server awaiting message %d\n", idx
);
475 ret
= mach_msg(args
->req_msg
,
476 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
|MACH_RCV_LARGE
,
480 MACH_MSG_TIMEOUT_NONE
,
482 if (MACH_RCV_INTERRUPTED
== ret
)
484 if (MACH_MSG_SUCCESS
!= ret
) {
486 printf("mach_msg() ret=%d", ret
);
487 mach_error("mach_msg (receive): ", ret
);
491 printf("server received message %d\n", idx
);
492 if (args
->req_msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
493 ret
= vm_deallocate(mach_task_self(),
494 (vm_address_t
)((ipc_complex_message
*)args
->req_msg
)->descriptor
.address
,
495 ((ipc_complex_message
*)args
->req_msg
)->descriptor
.size
);
498 if (1 == args
->req_msg
->msgh_id
) {
500 printf("server sending reply %d\n", idx
);
501 args
->reply_msg
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE
, 0);
502 args
->reply_msg
->msgh_size
= args
->reply_size
;
503 args
->reply_msg
->msgh_remote_port
= args
->req_msg
->msgh_remote_port
;
504 args
->reply_msg
->msgh_local_port
= MACH_PORT_NULL
;
505 args
->reply_msg
->msgh_id
= 2;
506 ret
= mach_msg(args
->reply_msg
,
511 MACH_MSG_TIMEOUT_NONE
,
513 if (MACH_MSG_SUCCESS
!= ret
) {
514 mach_error("mach_msg (send): ", ret
);
526 uint64_t deltans
= 0;
528 * If we're using multiple sets, explicitly tear them all down
529 * and measure the time.
531 for (int ns
= 0; ns
< setcount
; ns
++) {
533 printf("\tTearing down set[%d] %#x...\n", ns
, args
->set
[ns
]);
534 starttm
= mach_absolute_time();
535 ret
= mach_port_mod_refs(mach_task_self(), args
->set
[ns
], MACH_PORT_RIGHT_PORT_SET
, -1);
536 endtm
= mach_absolute_time();
537 deltans
+= abs_to_ns(endtm
- starttm
);
538 if (ret
!= KERN_SUCCESS
) {
539 mach_error("mach_port_mod_refs(): ", ret
);
544 uint64_t nlinks
= (uint64_t)setcount
* (uint64_t)portcount
;
546 printf("\tteardown of %llu links took %llu ns\n", nlinks
, deltans
);
547 printf("\t%lluns per set\n", deltans
/ (uint64_t)setcount
);
553 client_spin_loop(unsigned count
, void (fn
)(void))
559 static long dummy_memory
;
560 static long *client_memory
= &dummy_memory
;
562 client_work_atom(void)
566 if (++i
> client_pages
* PAGE_SIZE
/ sizeof(long))
568 client_memory
[i
] = 0;
571 static int calibration_count
= 10000;
572 static int calibration_usec
;
574 calibrate_client_work(void)
577 struct timeval nowtv
;
578 struct timeval warmuptv
= { 0, 100 * 1000 }; /* 100ms */
579 struct timeval starttv
;
580 struct timeval endtv
;
583 /* Warm-up the stepper first... */
584 gettimeofday(&nowtv
, NULL
);
585 timeradd(&nowtv
, &warmuptv
, &endtv
);
587 client_spin_loop(calibration_count
, client_work_atom
);
588 gettimeofday(&nowtv
, NULL
);
589 } while (timercmp(&nowtv
, &endtv
, < ));
591 /* Now do the calibration */
593 gettimeofday(&starttv
, NULL
);
594 client_spin_loop(calibration_count
, client_work_atom
);
595 gettimeofday(&endtv
, NULL
);
596 if (endtv
.tv_sec
- starttv
.tv_sec
> 1) {
597 calibration_count
/= 10;
600 calibration_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
601 if (endtv
.tv_usec
< starttv
.tv_usec
) {
602 calibration_usec
+= 1000000;
604 if (calibration_usec
< 1000) {
605 calibration_count
*= 10;
608 calibration_count
/= calibration_usec
;
612 printf("calibration_count=%d calibration_usec=%d\n",
613 calibration_count
, calibration_usec
);
623 client_spin_loop(calibration_count
*client_spin
,
628 usleep(client_delay
);
633 void *client(void *threadarg
)
635 struct port_args args
;
636 struct port_args
*svr_args
= NULL
;
638 mach_msg_header_t
*req
, *reply
;
639 mach_port_t bsport
, servport
;
641 int server_num
= (int)(uintptr_t)threadarg
;
642 void *ints
= malloc(sizeof(u_int32_t
) * num_ints
);
645 printf("client(%d) started, server port name %s\n",
646 server_num
, server_port_name
[server_num
]);
648 args
.server_num
= server_num
;
649 thread_setup(server_num
+ 1);
652 svr_args
= &server_port_args
[server_num
];
654 /* find server port */
655 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
656 if (KERN_SUCCESS
!= ret
) {
657 mach_error("task_get_bootstrap_port(): ", ret
);
660 ret
= bootstrap_look_up(bsport
,
661 server_port_name
[server_num
],
663 if (KERN_SUCCESS
!= ret
) {
664 mach_error("bootstrap_look_up(): ", ret
);
668 setup_client_ports(&args
);
670 /* Allocate and touch memory */
673 client_memory
= (long *) malloc(client_pages
* PAGE_SIZE
);
674 for (i
= 0; i
< client_pages
; i
++)
675 client_memory
[i
* PAGE_SIZE
/ sizeof(long)] = 0;
678 uint64_t starttm
, endtm
;
680 /* start message loop */
681 for (idx
= 0; idx
< num_msgs
; idx
++) {
683 reply
= args
.reply_msg
;
685 req
->msgh_size
= args
.req_size
;
686 if (stress_prepost
) {
687 req
->msgh_remote_port
= svr_args
->port_list
[idx
% portcount
];
689 req
->msgh_remote_port
= servport
;
692 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
693 req
->msgh_local_port
= MACH_PORT_NULL
;
695 req
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
,
696 MACH_MSG_TYPE_MAKE_SEND_ONCE
);
697 req
->msgh_local_port
= args
.port
;
699 req
->msgh_id
= oneway
? 0 : 1;
700 if (msg_type
== msg_type_complex
) {
701 (req
)->msgh_bits
|= MACH_MSGH_BITS_COMPLEX
;
702 ((ipc_complex_message
*)req
)->body
.msgh_descriptor_count
= 1;
703 ((ipc_complex_message
*)req
)->descriptor
.address
= ints
;
704 ((ipc_complex_message
*)req
)->descriptor
.size
=
705 num_ints
* sizeof(u_int32_t
);
706 ((ipc_complex_message
*)req
)->descriptor
.deallocate
= FALSE
;
707 ((ipc_complex_message
*)req
)->descriptor
.copy
= MACH_MSG_VIRTUAL_COPY
;
708 ((ipc_complex_message
*)req
)->descriptor
.type
= MACH_MSG_OOL_DESCRIPTOR
;
711 printf("client sending message %d to port %#x\n",
712 idx
, req
->msgh_remote_port
);
713 starttm
= mach_absolute_time();
719 MACH_MSG_TIMEOUT_NONE
,
721 endtm
= mach_absolute_time();
722 if (MACH_MSG_SUCCESS
!= ret
) {
723 mach_error("mach_msg (send): ", ret
);
724 fprintf(stderr
, "bailing after %u iterations\n", idx
);
729 OSAtomicAdd64(endtm
- starttm
, &g_client_send_time
);
733 printf("client awaiting reply %d\n", idx
);
734 reply
->msgh_bits
= 0;
735 reply
->msgh_size
= args
.reply_size
;
736 reply
->msgh_local_port
= args
.port
;
737 ret
= mach_msg(args
.reply_msg
,
738 MACH_RCV_MSG
|MACH_RCV_INTERRUPT
,
742 MACH_MSG_TIMEOUT_NONE
,
744 if (MACH_MSG_SUCCESS
!= ret
) {
745 mach_error("mach_msg (receive): ", ret
);
746 fprintf(stderr
, "bailing after %u iterations\n",
751 printf("client received reply %d\n", idx
);
762 thread_spawn(thread_id_t
*thread
, void *(fn
)(void *), void *arg
) {
765 ret
= pthread_create(
771 err(1, "pthread_create()");
773 printf("created pthread %p\n", thread
->tid
);
775 thread
->pid
= fork();
776 if (thread
->pid
== 0) {
778 printf("calling %p(%p)\n", fn
, arg
);
783 printf("forked pid %d\n", thread
->pid
);
788 thread_join(thread_id_t
*thread
) {
792 printf("joining thread %p\n", thread
->tid
);
793 ret
= pthread_join(thread
->tid
, NULL
);
794 if (ret
!= KERN_SUCCESS
)
795 err(1, "pthread_join(%p)", thread
->tid
);
799 printf("waiting for pid %d\n", thread
->pid
);
800 waitpid(thread
->pid
, &stat
, 0);
805 wait_for_servers(void)
808 int retry_count
= 10;
809 mach_port_t bsport
, servport
;
812 /* find server port */
813 ret
= task_get_bootstrap_port(mach_task_self(), &bsport
);
814 if (KERN_SUCCESS
!= ret
) {
815 mach_error("task_get_bootstrap_port(): ", ret
);
819 while (retry_count
-- > 0) {
820 for (i
= 0; i
< num_servers
; i
++) {
821 ret
= bootstrap_look_up(bsport
,
824 if (ret
!= KERN_SUCCESS
) {
828 if (ret
== KERN_SUCCESS
)
830 usleep(100 * 1000); /* 100ms */
832 fprintf(stderr
, "Server(s) failed to register\n");
836 int main(int argc
, char *argv
[])
840 thread_id_t
*client_id
;
841 thread_id_t
*server_id
;
843 signal(SIGINT
, signal_handler
);
844 parse_args(argc
, argv
);
846 if (mach_timebase_info(&g_timebase
) != KERN_SUCCESS
) {
847 fprintf(stderr
, "Can't get mach_timebase_info!\n");
851 calibrate_client_work();
854 * If we're using affinity create an empty namespace now
855 * so this is shared by all our offspring.
860 server_id
= (thread_id_t
*) malloc(num_servers
* sizeof(thread_id_t
));
861 server_port_name
= (char **) malloc(num_servers
* sizeof(char *));
862 server_port_args
= (struct port_args
*)calloc(sizeof(struct port_args
), num_servers
);
863 if (!server_id
|| !server_port_name
|| !server_port_args
) {
864 fprintf(stderr
, "malloc/calloc of %d server book keeping structs failed\n", num_servers
);
869 printf("creating %d servers\n", num_servers
);
870 for (i
= 0; i
< num_servers
; i
++) {
871 server_port_name
[i
] = (char *) malloc(sizeof("PORT.pppppp.xx"));
872 /* PORT names include pid of main process for disambiguation */
873 sprintf(server_port_name
[i
], "PORT.%06d.%02d", getpid(), i
);
874 thread_spawn(&server_id
[i
], server
, (void *) (long) i
);
877 int totalclients
= num_servers
* num_clients
;
878 int totalmsg
= num_msgs
* totalclients
;
879 struct timeval starttv
, endtv
, deltatv
;
882 * Wait for all servers to have registered all ports before starting
883 * the clients and the clock.
887 printf("%d server%s, %d client%s per server (%d total) %u messages...",
888 num_servers
, (num_servers
> 1)? "s" : "",
889 num_clients
, (num_clients
> 1)? "s" : "",
894 /* Call gettimeofday() once and throw away result; some implementations
895 * (like Mach's) cache some time zone info on first call.
897 gettimeofday(&starttv
, NULL
);
898 gettimeofday(&starttv
, NULL
);
900 client_id
= (thread_id_t
*) malloc(totalclients
* sizeof(thread_id_t
));
902 printf("creating %d clients\n", totalclients
);
903 for (i
= 0; i
< num_servers
; i
++) {
904 for (j
= 0; j
< num_clients
; j
++) {
906 &client_id
[(i
*num_clients
) + j
],
912 /* Wait for servers to complete */
913 for (i
= 0; i
< num_servers
; i
++) {
914 thread_join(&server_id
[i
]);
917 gettimeofday(&endtv
, NULL
);
919 printf("all servers complete: waiting for clients...\n");
921 for (i
= 0; i
< totalclients
; i
++) {
922 thread_join(&client_id
[i
]);
926 deltatv
.tv_sec
= endtv
.tv_sec
- starttv
.tv_sec
;
927 deltatv
.tv_usec
= endtv
.tv_usec
- starttv
.tv_usec
;
928 if (endtv
.tv_usec
< starttv
.tv_usec
) {
930 deltatv
.tv_usec
+= 1000000;
933 double dsecs
= (double) deltatv
.tv_sec
+
934 1.0E-6 * (double) deltatv
.tv_usec
;
936 printf(" in %lu.%03u seconds\n",
937 deltatv
.tv_sec
, deltatv
.tv_usec
/1000);
938 printf(" throughput in messages/sec: %g\n",
939 (double)totalmsg
/ dsecs
);
940 printf(" average message latency (usec): %2.3g\n",
941 dsecs
* 1.0E6
/ (double) totalmsg
);
943 if (stress_prepost
) {
944 int64_t sendns
= abs_to_ns(g_client_send_time
);
945 dsecs
= (double)sendns
/ (double)NSEC_PER_SEC
;
946 printf(" total send time: %2.3gs\n", dsecs
);
947 printf(" average send time (usec): %2.3g\n",
948 dsecs
* 1.0E6
/ (double)totalmsg
);