]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/MPMMTest/KQMPMMtest.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / tools / tests / MPMMTest / KQMPMMtest.c
1 #include <AvailabilityMacros.h>
2 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER
3 #include </System/Library/Frameworks/System.framework/PrivateHeaders/mach/thread_policy.h>
4 #endif
5
6 #include <pthread.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <err.h>
11
12 #include <pthread.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/types.h>
19 #include <sys/time.h>
20 #include <sys/signal.h>
21
22 #define MAX(A, B) ((A) < (B) ? (B) : (A))
23
24
25 typedef struct {
26 mach_msg_header_t header;
27 mach_msg_trailer_t trailer; // subtract this when sending
28 } ipc_trivial_message;
29
30 typedef struct {
31 mach_msg_header_t header;
32 u_int32_t numbers[0];
33 mach_msg_trailer_t trailer; // subtract this when sending
34 } ipc_inline_message;
35
36 typedef struct {
37 mach_msg_header_t header;
38 mach_msg_body_t body;
39 mach_msg_ool_descriptor_t descriptor;
40 mach_msg_trailer_t trailer; // subtract this when sending
41 } ipc_complex_message;
42
43 enum {
44 msg_type_trivial = 0,
45 msg_type_inline = 1,
46 msg_type_complex = 2
47 };
48
49 struct port_args {
50 int server_num;
51 int req_size;
52 mach_msg_header_t *req_msg;
53 int reply_size;
54 mach_msg_header_t *reply_msg;
55 mach_port_t port;
56 mach_port_t pset;
57 };
58
59 typedef union {
60 pid_t pid;
61 pthread_t tid;
62 } thread_id_t;
63
64 /* Global options */
65 static boolean_t verbose = FALSE;
66 static boolean_t affinity = FALSE;
67 static boolean_t timeshare = FALSE;
68 static boolean_t threaded = FALSE;
69 static boolean_t oneway = FALSE;
70 int msg_type;
71 int num_ints;
72 int num_msgs;
73 int num_clients;
74 int num_servers;
75 int client_delay;
76 int client_spin;
77 int client_pages;
78 char **server_port_name;
79
80 void signal_handler(int sig) {
81 }
82
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, "default values are:\n");
100 fprintf(stderr, " . no affinity\n");
101 fprintf(stderr, " . not timeshare\n");
102 fprintf(stderr, " . not verbose\n");
103 fprintf(stderr, " . not oneway\n");
104 fprintf(stderr, " . client sends 100000 messages\n");
105 fprintf(stderr, " . inline message type\n");
106 fprintf(stderr, " . 64 32-bit integers in inline/complex messages\n");
107 fprintf(stderr, " . (num_available_processors+1)%%2 servers\n");
108 fprintf(stderr, " . 4 clients per server\n");
109 fprintf(stderr, " . no delay\n");
110 exit(1);
111 }
112
113 void parse_args(int argc, char *argv[]) {
114 host_basic_info_data_t info;
115 mach_msg_type_number_t count;
116 kern_return_t result;
117
118 /* Initialize defaults */
119 msg_type = msg_type_trivial;
120 num_ints = 64;
121 num_msgs = 100000;
122 client_delay = 0;
123 num_clients = 4;
124
125 count = HOST_BASIC_INFO_COUNT;
126 result = host_info(mach_host_self(), HOST_BASIC_INFO,
127 (host_info_t)&info, &count);
128 if (result == KERN_SUCCESS && info.avail_cpus > 1)
129 num_servers = info.avail_cpus / 2;
130 else
131 num_servers = 1;
132
133 const char *progname = argv[0];
134 argc--; argv++;
135 while (0 < argc) {
136 if (0 == strcmp("-verbose", argv[0])) {
137 verbose = TRUE;
138 argc--; argv++;
139 } else if (0 == strcmp("-affinity", argv[0])) {
140 affinity = TRUE;
141 argc--; argv++;
142 } else if (0 == strcmp("-timeshare", argv[0])) {
143 timeshare = TRUE;
144 argc--; argv++;
145 } else if (0 == strcmp("-threaded", argv[0])) {
146 threaded = TRUE;
147 argc--; argv++;
148 } else if (0 == strcmp("-oneway", argv[0])) {
149 oneway = TRUE;
150 argc--; argv++;
151 } else if (0 == strcmp("-type", argv[0])) {
152 if (argc < 2)
153 usage(progname);
154 if (0 == strcmp("trivial", argv[1])) {
155 msg_type = msg_type_trivial;
156 } else if (0 == strcmp("inline", argv[1])) {
157 msg_type = msg_type_inline;
158 } else if (0 == strcmp("complex", argv[1])) {
159 msg_type = msg_type_complex;
160 } else
161 usage(progname);
162 argc -= 2; argv += 2;
163 } else if (0 == strcmp("-numints", argv[0])) {
164 if (argc < 2)
165 usage(progname);
166 num_ints = strtoul(argv[1], NULL, 0);
167 argc -= 2; argv += 2;
168 } else if (0 == strcmp("-count", argv[0])) {
169 if (argc < 2)
170 usage(progname);
171 num_msgs = strtoul(argv[1], NULL, 0);
172 argc -= 2; argv += 2;
173 } else if (0 == strcmp("-clients", argv[0])) {
174 if (argc < 2)
175 usage(progname);
176 num_clients = strtoul(argv[1], NULL, 0);
177 argc -= 2; argv += 2;
178 } else if (0 == strcmp("-servers", argv[0])) {
179 if (argc < 2)
180 usage(progname);
181 num_servers = strtoul(argv[1], NULL, 0);
182 argc -= 2; argv += 2;
183 } else if (0 == strcmp("-delay", argv[0])) {
184 if (argc < 2)
185 usage(progname);
186 client_delay = strtoul(argv[1], NULL, 0);
187 argc -= 2; argv += 2;
188 } else if (0 == strcmp("-spin", argv[0])) {
189 if (argc < 2)
190 usage(progname);
191 client_spin = strtoul(argv[1], NULL, 0);
192 argc -= 2; argv += 2;
193 } else if (0 == strcmp("-pages", argv[0])) {
194 if (argc < 2)
195 usage(progname);
196 client_pages = strtoul(argv[1], NULL, 0);
197 argc -= 2; argv += 2;
198 } else
199 usage(progname);
200 }
201 }
202
203 void setup_server_ports(struct port_args *ports)
204 {
205 kern_return_t ret = 0;
206 mach_port_t bsport;
207
208 ports->req_size = MAX(sizeof(ipc_inline_message) +
209 sizeof(u_int32_t) * num_ints,
210 sizeof(ipc_complex_message));
211 ports->reply_size = sizeof(ipc_trivial_message) -
212 sizeof(mach_msg_trailer_t);
213 ports->req_msg = malloc(ports->req_size);
214 ports->reply_msg = malloc(ports->reply_size);
215
216 ret = mach_port_allocate(mach_task_self(),
217 MACH_PORT_RIGHT_RECEIVE,
218 &(ports->port));
219 if (KERN_SUCCESS != ret) {
220 mach_error("mach_port_allocate(): ", ret);
221 exit(1);
222 }
223
224 ret = mach_port_allocate(mach_task_self(),
225 MACH_PORT_RIGHT_PORT_SET,
226 &(ports->pset));
227 if (KERN_SUCCESS != ret) {
228 mach_error("mach_port_allocate(): ", ret);
229 exit(1);
230 }
231
232 ret = mach_port_insert_member(mach_task_self(),
233 ports->port,
234 ports->pset);
235 if (KERN_SUCCESS != ret) {
236 mach_error("mach_port_insert_member(): ", ret);
237 exit(1);
238 }
239
240 ret = mach_port_insert_right(mach_task_self(),
241 ports->port,
242 ports->port,
243 MACH_MSG_TYPE_MAKE_SEND);
244 if (KERN_SUCCESS != ret) {
245 mach_error("mach_port_insert_right(): ", ret);
246 exit(1);
247 }
248
249 ret = task_get_bootstrap_port(mach_task_self(), &bsport);
250 if (KERN_SUCCESS != ret) {
251 mach_error("task_get_bootstrap_port(): ", ret);
252 exit(1);
253 }
254
255 if (verbose) {
256 printf("server waiting for IPC messages from client on port '%s'.\n",
257 server_port_name[ports->server_num]);
258 }
259 ret = bootstrap_register(bsport,
260 server_port_name[ports->server_num],
261 ports->port);
262 if (KERN_SUCCESS != ret) {
263 mach_error("bootstrap_register(): ", ret);
264 exit(1);
265 }
266 }
267
268 void setup_client_ports(struct port_args *ports)
269 {
270 kern_return_t ret = 0;
271 switch(msg_type) {
272 case msg_type_trivial:
273 ports->req_size = sizeof(ipc_trivial_message);
274 break;
275 case msg_type_inline:
276 ports->req_size = sizeof(ipc_inline_message) +
277 sizeof(u_int32_t) * num_ints;
278 break;
279 case msg_type_complex:
280 ports->req_size = sizeof(ipc_complex_message);
281 break;
282 }
283 ports->req_size -= sizeof(mach_msg_trailer_t);
284 ports->reply_size = sizeof(ipc_trivial_message);
285 ports->req_msg = malloc(ports->req_size);
286 ports->reply_msg = malloc(ports->reply_size);
287
288 ret = mach_port_allocate(mach_task_self(),
289 MACH_PORT_RIGHT_RECEIVE,
290 &(ports->port));
291 if (KERN_SUCCESS != ret) {
292 mach_error("mach_port_allocate(): ", ret);
293 exit(1);
294 }
295 if (verbose) {
296 printf("Client sending %d %s IPC messages to port '%s' in %s mode.\n",
297 num_msgs, (msg_type == msg_type_inline) ?
298 "inline" : ((msg_type == msg_type_complex) ?
299 "complex" : "trivial"),
300 server_port_name[ports->server_num],
301 (oneway ? "oneway" : "rpc"));
302 }
303
304 }
305
306
307 static void
308 thread_setup(int tag) {
309 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER
310 kern_return_t ret;
311 thread_extended_policy_data_t epolicy;
312 thread_affinity_policy_data_t policy;
313
314 if (!timeshare) {
315 epolicy.timeshare = FALSE;
316 ret = thread_policy_set(
317 mach_thread_self(), THREAD_EXTENDED_POLICY,
318 (thread_policy_t) &epolicy,
319 THREAD_EXTENDED_POLICY_COUNT);
320 if (ret != KERN_SUCCESS)
321 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret);
322 }
323
324 if (affinity) {
325 policy.affinity_tag = tag;
326 ret = thread_policy_set(
327 mach_thread_self(), THREAD_AFFINITY_POLICY,
328 (thread_policy_t) &policy,
329 THREAD_AFFINITY_POLICY_COUNT);
330 if (ret != KERN_SUCCESS)
331 printf("thread_policy_set(THREAD_AFFINITY_POLICY) returned %d\n", ret);
332 }
333 #endif
334 }
335
336 void *
337 server(void *serverarg)
338 {
339 int kq;
340 struct kevent64_s kev[1];
341 int err;
342 struct port_args args;
343 int idx;
344 kern_return_t ret;
345 int totalmsg = num_msgs * num_clients;
346
347 args.server_num = (int) (long) serverarg;
348 setup_server_ports(&args);
349
350 thread_setup(args.server_num + 1);
351
352 kq = kqueue();
353 if (kq == -1) {
354 perror("kqueue");
355 exit(1);
356 }
357 EV_SET64(&kev[0], args.pset, EVFILT_MACHPORT, (EV_ADD | EV_CLEAR | EV_DISPATCH),
358 #if DIRECT_MSG_RCV
359 MACH_RCV_MSG|MACH_RCV_LARGE, 0, 0, (mach_vm_address_t)args.req_msg, args.req_size);
360 #else
361 0, 0, 0, 0, 0);
362 #endif
363 err = kevent64(kq, kev, 1, NULL, 0, 0, NULL);
364 if (err == -1) {
365 perror("kevent");
366 exit(1);
367 }
368 for (idx = 0; idx < totalmsg; idx++) {
369
370 if (verbose)
371 printf("server awaiting message %d\n", idx);
372 retry:
373 EV_SET64(&kev[0], args.pset, EVFILT_MACHPORT, EV_ENABLE,
374 #if DIRECT_MSG_RCV
375 MACH_RCV_MSG|MACH_RCV_LARGE, 0, 0, (mach_vm_address_t)args.req_msg, args.req_size);
376 #else
377 0, 0, 0, 0, 0);
378 #endif
379 err = kevent64(kq, kev, 1, kev, 1, 0, NULL);
380 if (err == -1) {
381 perror("kevent64");
382 exit(1);
383 }
384 if (err == 0) {
385 // printf("kevent64: returned zero\n");
386 goto retry;
387 }
388
389 #if DIRECT_MSG_RCV
390 ret = kev[0].fflags;
391 if (MACH_MSG_SUCCESS != ret) {
392 if (verbose)
393 printf("kevent64() mach_msg_return=%d", ret);
394 mach_error("kevent64 (msg receive): ", ret);
395 exit(1);
396 }
397 #else
398 if (kev[0].data != args.port)
399 printf("kevent64(MACH_PORT_NULL) port name (0x%x) != expected (0x%x)\n", kev[0].data, args.port);
400
401 args.req_msg->msgh_bits = 0;
402 args.req_msg->msgh_size = args.req_size;
403 args.req_msg->msgh_local_port = args.port;
404 ret = mach_msg(args.req_msg,
405 MACH_RCV_MSG|MACH_RCV_INTERRUPT|MACH_RCV_LARGE,
406 0,
407 args.req_size,
408 args.pset,
409 MACH_MSG_TIMEOUT_NONE,
410 MACH_PORT_NULL);
411 if (MACH_RCV_INTERRUPTED == ret)
412 break;
413 if (MACH_MSG_SUCCESS != ret) {
414 if (verbose)
415 printf("mach_msg() ret=%d", ret);
416 mach_error("mach_msg (receive): ", ret);
417 exit(1);
418 }
419 #endif
420 if (verbose)
421 printf("server received message %d\n", idx);
422 if (args.req_msg->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
423 ret = vm_deallocate(mach_task_self(),
424 (vm_address_t)((ipc_complex_message *)args.req_msg)->descriptor.address,
425 ((ipc_complex_message *)args.req_msg)->descriptor.size);
426 }
427
428 if (1 == args.req_msg->msgh_id) {
429 if (verbose)
430 printf("server sending reply %d\n", idx);
431 args.reply_msg->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND,
432 MACH_MSG_TYPE_MAKE_SEND);
433 args.reply_msg->msgh_size = args.reply_size;
434 args.reply_msg->msgh_remote_port = args.req_msg->msgh_remote_port;
435 args.reply_msg->msgh_local_port = args.req_msg->msgh_local_port;
436 args.reply_msg->msgh_id = 2;
437 ret = mach_msg(args.reply_msg,
438 MACH_SEND_MSG,
439 args.reply_size,
440 0,
441 MACH_PORT_NULL,
442 MACH_MSG_TIMEOUT_NONE,
443 MACH_PORT_NULL);
444 if (MACH_MSG_SUCCESS != ret) {
445 mach_error("mach_msg (send): ", ret);
446 exit(1);
447 }
448 }
449 }
450 }
451
452 static inline void
453 client_spin_loop(unsigned count, void (fn)(void))
454 {
455 while (count--)
456 fn();
457 }
458
459 static long dummy_memory;
460 static long *client_memory = &dummy_memory;
461 static void
462 client_work_atom(void)
463 {
464 static int i;
465
466 if (++i > client_pages * PAGE_SIZE / sizeof(long))
467 i = 0;
468 client_memory[i] = 0;
469 }
470
471 static int calibration_count = 10000;
472 static int calibration_usec;
473 static void *
474 calibrate_client_work(void)
475 {
476 long dummy;
477 struct timeval nowtv;
478 struct timeval warmuptv = { 0, 100 * 1000 }; /* 100ms */
479 struct timeval starttv;
480 struct timeval endtv;
481
482 if (client_spin) {
483 /* Warm-up the stepper first... */
484 gettimeofday(&nowtv, NULL);
485 timeradd(&nowtv, &warmuptv, &endtv);
486 do {
487 client_spin_loop(calibration_count, client_work_atom);
488 gettimeofday(&nowtv, NULL);
489 } while (timercmp(&nowtv, &endtv, < ));
490
491 /* Now do the calibration */
492 while (TRUE) {
493 gettimeofday(&starttv, NULL);
494 client_spin_loop(calibration_count, client_work_atom);
495 gettimeofday(&endtv, NULL);
496 if (endtv.tv_sec - starttv.tv_sec > 1) {
497 calibration_count /= 10;
498 continue;
499 }
500 calibration_usec = endtv.tv_usec - starttv.tv_usec;
501 if (endtv.tv_usec < starttv.tv_usec) {
502 calibration_usec += 1000000;
503 }
504 if (calibration_usec < 1000) {
505 calibration_count *= 10;
506 continue;
507 }
508 calibration_count /= calibration_usec;
509 break;
510 }
511 if (verbose)
512 printf("calibration_count=%d calibration_usec=%d\n",
513 calibration_count, calibration_usec);
514 }
515 }
516
517 static void *
518 client_work(void)
519 {
520
521 if (client_spin) {
522 client_spin_loop(calibration_count*client_spin,
523 client_work_atom);
524 }
525
526 if (client_delay) {
527 usleep(client_delay);
528 }
529 }
530
531 void *client(void *threadarg)
532 {
533 struct port_args args;
534 int idx;
535 mach_msg_header_t *req, *reply;
536 mach_port_t bsport, servport;
537 kern_return_t ret;
538 long server_num = (long) threadarg;
539 void *ints = malloc(sizeof(u_int32_t) * num_ints);
540
541 if (verbose)
542 printf("client(%d) started, server port name %s\n",
543 server_num, server_port_name[server_num]);
544
545 args.server_num = server_num;
546 thread_setup(server_num + 1);
547
548 /* find server port */
549 ret = task_get_bootstrap_port(mach_task_self(), &bsport);
550 if (KERN_SUCCESS != ret) {
551 mach_error("task_get_bootstrap_port(): ", ret);
552 exit(1);
553 }
554 ret = bootstrap_look_up(bsport,
555 server_port_name[server_num],
556 &servport);
557 if (KERN_SUCCESS != ret) {
558 mach_error("bootstrap_look_up(): ", ret);
559 exit(1);
560 }
561
562 setup_client_ports(&args);
563
564 /* Allocate and touch memory */
565 if (client_pages) {
566 unsigned i;
567 client_memory = (long *) malloc(client_pages * PAGE_SIZE);
568 for (i = 0; i < client_pages; i++)
569 client_memory[i * PAGE_SIZE / sizeof(long)] = 0;
570 }
571
572 /* start message loop */
573 for (idx = 0; idx < num_msgs; idx++) {
574 req = args.req_msg;
575 reply = args.reply_msg;
576
577 req->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND,
578 MACH_MSG_TYPE_MAKE_SEND);
579 req->msgh_size = args.req_size;
580 req->msgh_remote_port = servport;
581 req->msgh_local_port = args.port;
582 req->msgh_id = oneway ? 0 : 1;
583 if (msg_type == msg_type_complex) {
584 (req)->msgh_bits |= MACH_MSGH_BITS_COMPLEX;
585 ((ipc_complex_message *)req)->body.msgh_descriptor_count = 1;
586 ((ipc_complex_message *)req)->descriptor.address = ints;
587 ((ipc_complex_message *)req)->descriptor.size =
588 num_ints * sizeof(u_int32_t);
589 ((ipc_complex_message *)req)->descriptor.deallocate = FALSE;
590 ((ipc_complex_message *)req)->descriptor.copy = MACH_MSG_VIRTUAL_COPY;
591 ((ipc_complex_message *)req)->descriptor.type = MACH_MSG_OOL_DESCRIPTOR;
592 }
593 if (verbose)
594 printf("client sending message %d\n", idx);
595 ret = mach_msg(req,
596 MACH_SEND_MSG,
597 args.req_size,
598 0,
599 MACH_PORT_NULL,
600 MACH_MSG_TIMEOUT_NONE,
601 MACH_PORT_NULL);
602 if (MACH_MSG_SUCCESS != ret) {
603 mach_error("mach_msg (send): ", ret);
604 fprintf(stderr, "bailing after %u iterations\n", idx);
605 exit(1);
606 break;
607 }
608 if (!oneway) {
609 if (verbose)
610 printf("client awaiting reply %d\n", idx);
611 reply->msgh_bits = 0;
612 reply->msgh_size = args.reply_size;
613 reply->msgh_local_port = args.port;
614 ret = mach_msg(args.reply_msg,
615 MACH_RCV_MSG|MACH_RCV_INTERRUPT,
616 0,
617 args.reply_size,
618 args.port,
619 MACH_MSG_TIMEOUT_NONE,
620 MACH_PORT_NULL);
621 if (MACH_MSG_SUCCESS != ret) {
622 mach_error("mach_msg (receive): ", ret);
623 fprintf(stderr, "bailing after %u iterations\n",
624 idx);
625 exit(1);
626 }
627 if (verbose)
628 printf("client received reply %d\n", idx);
629 }
630
631 client_work();
632 }
633
634 free(ints);
635 return;
636 }
637
638 static void
639 thread_spawn(thread_id_t *thread, void *(fn)(void *), void *arg) {
640 if (threaded) {
641 kern_return_t ret;
642 ret = pthread_create(
643 &thread->tid,
644 NULL,
645 fn,
646 arg);
647 if (ret != 0)
648 err(1, "pthread_create()");
649 if (verbose)
650 printf("created pthread 0x%x\n", thread->tid);
651 } else {
652 thread->pid = fork();
653 if (thread->pid == 0) {
654 if (verbose)
655 printf("calling 0x%x(0x%x)\n", fn, arg);
656 fn(arg);
657 exit(0);
658 }
659 if (verbose)
660 printf("forked pid %d\n", thread->pid);
661 }
662 }
663
664 static void
665 thread_join(thread_id_t *thread) {
666 if (threaded) {
667 kern_return_t ret;
668 if (verbose)
669 printf("joining thread 0x%x\n", thread->tid);
670 ret = pthread_join(thread->tid, NULL);
671 if (ret != KERN_SUCCESS)
672 err(1, "pthread_join(0x%x)", thread->tid);
673 } else {
674 int stat;
675 if (verbose)
676 printf("waiting for pid %d\n", thread->pid);
677 waitpid(thread->pid, &stat, 0);
678 }
679 }
680
681 static void
682 wait_for_servers(void)
683 {
684 int i;
685 int retry_count = 10;
686 mach_port_t bsport, servport;
687 kern_return_t ret;
688
689 /* find server port */
690 ret = task_get_bootstrap_port(mach_task_self(), &bsport);
691 if (KERN_SUCCESS != ret) {
692 mach_error("task_get_bootstrap_port(): ", ret);
693 exit(1);
694 }
695
696 while (retry_count-- > 0) {
697 for (i = 0; i < num_servers; i++) {
698 ret = bootstrap_look_up(bsport,
699 server_port_name[i],
700 &servport);
701 if (ret != KERN_SUCCESS) {
702 break;
703 }
704 }
705 if (ret == KERN_SUCCESS)
706 return;
707 usleep(100 * 1000); /* 100ms */
708 }
709 fprintf(stderr, "Server(s) failed to register\n");
710 exit(1);
711 }
712
713 int main(int argc, char *argv[])
714 {
715 int i;
716 int j;
717 thread_id_t *client_id;
718 thread_id_t *server_id;
719
720 signal(SIGINT, signal_handler);
721 parse_args(argc, argv);
722
723 calibrate_client_work();
724
725 /*
726 * If we're using affinity create an empty namespace now
727 * so this is shared by all our offspring.
728 */
729 if (affinity)
730 thread_setup(0);
731
732 server_id = (thread_id_t *) malloc(num_servers * sizeof(thread_id_t));
733 server_port_name = (char **) malloc(num_servers * sizeof(char *));
734 if (verbose)
735 printf("creating %d servers\n", num_servers);
736 for (i = 0; i < num_servers; i++) {
737 server_port_name[i] = (char *) malloc(sizeof("PORT.pppppp.xx"));
738 /* PORT names include pid of main process for disambiguation */
739 sprintf(server_port_name[i], "PORT.%06d.%02d", getpid(), i);
740 thread_spawn(&server_id[i], server, (void *) (long) i);
741 }
742
743 int totalclients = num_servers * num_clients;
744 int totalmsg = num_msgs * totalclients;
745 struct timeval starttv, endtv, deltatv;
746
747 /*
748 * Wait for all servers to have registered all ports before starting
749 * the clients and the clock.
750 */
751 wait_for_servers();
752
753 printf("%d server%s, %d client%s per server (%d total) %u messages...",
754 num_servers, (num_servers > 1)? "s" : "",
755 num_clients, (num_clients > 1)? "s" : "",
756 totalclients,
757 totalmsg);
758 fflush(stdout);
759
760 /* Call gettimeofday() once and throw away result; some implementations
761 * (like Mach's) cache some time zone info on first call.
762 */
763 gettimeofday(&starttv, NULL);
764 gettimeofday(&starttv, NULL);
765
766 client_id = (thread_id_t *) malloc(totalclients * sizeof(thread_id_t));
767 if (verbose)
768 printf("creating %d clients\n", totalclients);
769 for (i = 0; i < num_servers; i++) {
770 for (j = 0; j < num_clients; j++) {
771 thread_spawn(
772 &client_id[(i*num_clients) + j],
773 client,
774 (void *) (long) i);
775 }
776 }
777
778 /* Wait for servers to complete */
779 for (i = 0; i < num_servers; i++) {
780 thread_join(&server_id[i]);
781 }
782
783 gettimeofday(&endtv, NULL);
784
785 for (i = 0; i < totalclients; i++) {
786 thread_join(&client_id[i]);
787 }
788
789 /* report results */
790 deltatv.tv_sec = endtv.tv_sec - starttv.tv_sec;
791 deltatv.tv_usec = endtv.tv_usec - starttv.tv_usec;
792 if (endtv.tv_usec < starttv.tv_usec) {
793 deltatv.tv_sec--;
794 deltatv.tv_usec += 1000000;
795 }
796
797 double dsecs = (double) deltatv.tv_sec +
798 1.0E-6 * (double) deltatv.tv_usec;
799
800 printf(" in %u.%03u seconds\n",
801 deltatv.tv_sec, deltatv.tv_usec/1000);
802 printf(" throughput in messages/sec: %g\n",
803 (double)totalmsg / dsecs);
804 printf(" average message latency (usec): %2.3g\n",
805 dsecs * 1.0E6 / (double) totalmsg);
806
807 return (0);
808
809 }