]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/MPMMTest/MPMMtest.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tools / tests / MPMMTest / MPMMtest.c
CommitLineData
2d21ac55 1#include <AvailabilityMacros.h>
fe8ab488 2#include <mach/thread_policy.h>
2d21ac55
A
3
4#include <pthread.h>
5#include <stdio.h>
6#include <stdlib.h>
490019cf 7#include <libgen.h>
2d21ac55
A
8#include <string.h>
9#include <err.h>
316670eb 10#include <unistd.h>
2d21ac55
A
11
12#include <pthread.h>
13#include <mach/mach.h>
14#include <mach/mach_error.h>
3e170ce0 15#include <mach/mach_time.h>
2d21ac55
A
16#include <mach/notify.h>
17#include <servers/bootstrap.h>
18#include <sys/types.h>
19#include <sys/time.h>
20#include <sys/signal.h>
4bd07ac2
A
21#include <errno.h>
22#include "../unit_tests/tests_common.h" /* for record_perf_data() */
2d21ac55 23
3e170ce0
A
24#include <libkern/OSAtomic.h>
25
2d21ac55
A
26#define MAX(A, B) ((A) < (B) ? (B) : (A))
27
2d21ac55
A
28
29typedef struct {
0a7de745
A
30 mach_msg_header_t header;
31 mach_msg_trailer_t trailer; // subtract this when sending
2d21ac55
A
32} ipc_trivial_message;
33
34typedef struct {
0a7de745
A
35 mach_msg_header_t header;
36 u_int32_t numbers[0];
37 mach_msg_trailer_t trailer; // subtract this when sending
2d21ac55
A
38} ipc_inline_message;
39
40typedef struct {
0a7de745
A
41 mach_msg_header_t header;
42 mach_msg_body_t body;
43 mach_msg_ool_descriptor_t descriptor;
44 mach_msg_trailer_t trailer; // subtract this when sending
2d21ac55
A
45} ipc_complex_message;
46
47enum {
48 msg_type_trivial = 0,
49 msg_type_inline = 1,
50 msg_type_complex = 2
51};
52
53struct port_args {
54 int server_num;
55 int req_size;
56 mach_msg_header_t *req_msg;
57 int reply_size;
58 mach_msg_header_t *reply_msg;
59 mach_port_t port;
3e170ce0
A
60 mach_port_t rcv_set;
61
62 mach_port_t *set;
63 mach_port_t *port_list;
2d21ac55
A
64};
65
66typedef union {
0a7de745
A
67 pid_t pid;
68 pthread_t tid;
2d21ac55
A
69} thread_id_t;
70
71/* Global options */
0a7de745
A
72static int verbose = 0;
73static boolean_t affinity = FALSE;
74static boolean_t timeshare = FALSE;
75static boolean_t threaded = FALSE;
76static boolean_t oneway = FALSE;
77static boolean_t useset = FALSE;
78static boolean_t save_perfdata = FALSE;
79int msg_type;
80int num_ints;
81int num_msgs;
82int num_clients;
83int num_servers;
84int client_delay;
85int client_spin;
86int client_pages;
87int portcount = 1;
88int setcount = 0;
89boolean_t stress_prepost = FALSE;
90char **server_port_name;
91
92struct port_args *server_port_args;
3e170ce0
A
93
94/* global data */
95mach_timebase_info_data_t g_timebase;
96int64_t g_client_send_time = 0;
97
0a7de745
A
98static inline uint64_t
99ns_to_abs(uint64_t ns)
3e170ce0
A
100{
101 return ns * g_timebase.denom / g_timebase.numer;
102}
103
0a7de745
A
104static inline uint64_t
105abs_to_ns(uint64_t abs)
3e170ce0
A
106{
107 return abs * g_timebase.numer / g_timebase.denom;
108}
109
110
0a7de745
A
111void
112signal_handler(int sig)
113{
2d21ac55
A
114}
115
0a7de745
A
116void
117usage(const char *progname)
118{
2d21ac55
A
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");
3e170ce0 124 fprintf(stderr, " -verbose\t\tbe verbose (use multiple times to increase verbosity)\n");
2d21ac55
A
125 fprintf(stderr, " -oneway\t\tdo not request return reply\n");
126 fprintf(stderr, " -count num\t\tnumber of messages to send\n");
4bd07ac2 127 fprintf(stderr, " -perf \t\tCreate perfdata files for metrics.\n");
2d21ac55
A
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");
3e170ce0 130 fprintf(stderr, " -servers num\tnumber of server threads to run\n");
2d21ac55
A
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");
3e170ce0
A
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");
2d21ac55
A
138 fprintf(stderr, "default values are:\n");
139 fprintf(stderr, " . no affinity\n");
140 fprintf(stderr, " . not timeshare\n");
3e170ce0 141 fprintf(stderr, " . not threaded\n");
2d21ac55
A
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");
3e170ce0
A
150 fprintf(stderr, " . no sets / extra ports\n");
151 fprintf(stderr, " . no prepost stress\n");
2d21ac55
A
152 exit(1);
153}
154
0a7de745
A
155void
156parse_args(int argc, char *argv[])
157{
158 host_basic_info_data_t info;
159 mach_msg_type_number_t count;
160 kern_return_t result;
2d21ac55
A
161
162 /* Initialize defaults */
163 msg_type = msg_type_trivial;
164 num_ints = 64;
165 num_msgs = 100000;
166 client_delay = 0;
167 num_clients = 4;
168
169 count = HOST_BASIC_INFO_COUNT;
0a7de745
A
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) {
2d21ac55 173 num_servers = info.avail_cpus / 2;
0a7de745 174 } else {
2d21ac55 175 num_servers = 1;
0a7de745 176 }
2d21ac55
A
177
178 const char *progname = argv[0];
179 argc--; argv++;
180 while (0 < argc) {
181 if (0 == strcmp("-verbose", argv[0])) {
3e170ce0 182 verbose++;
2d21ac55
A
183 argc--; argv++;
184 } else if (0 == strcmp("-affinity", argv[0])) {
185 affinity = TRUE;
186 argc--; argv++;
187 } else if (0 == strcmp("-timeshare", argv[0])) {
188 timeshare = TRUE;
189 argc--; argv++;
190 } else if (0 == strcmp("-threaded", argv[0])) {
191 threaded = TRUE;
192 argc--; argv++;
193 } else if (0 == strcmp("-oneway", argv[0])) {
194 oneway = TRUE;
195 argc--; argv++;
4bd07ac2
A
196 } else if (0 == strcmp("-perf", argv[0])) {
197 save_perfdata = TRUE;
198 argc--; argv++;
2d21ac55 199 } else if (0 == strcmp("-type", argv[0])) {
0a7de745 200 if (argc < 2) {
2d21ac55 201 usage(progname);
0a7de745 202 }
2d21ac55
A
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;
0a7de745 209 } else {
2d21ac55 210 usage(progname);
0a7de745 211 }
2d21ac55
A
212 argc -= 2; argv += 2;
213 } else if (0 == strcmp("-numints", argv[0])) {
0a7de745 214 if (argc < 2) {
2d21ac55 215 usage(progname);
0a7de745 216 }
2d21ac55
A
217 num_ints = strtoul(argv[1], NULL, 0);
218 argc -= 2; argv += 2;
219 } else if (0 == strcmp("-count", argv[0])) {
0a7de745 220 if (argc < 2) {
2d21ac55 221 usage(progname);
0a7de745 222 }
2d21ac55
A
223 num_msgs = strtoul(argv[1], NULL, 0);
224 argc -= 2; argv += 2;
0a7de745
A
225 } else if (0 == strcmp("-clients", argv[0])) {
226 if (argc < 2) {
2d21ac55 227 usage(progname);
0a7de745 228 }
2d21ac55
A
229 num_clients = strtoul(argv[1], NULL, 0);
230 argc -= 2; argv += 2;
0a7de745
A
231 } else if (0 == strcmp("-servers", argv[0])) {
232 if (argc < 2) {
2d21ac55 233 usage(progname);
0a7de745 234 }
2d21ac55
A
235 num_servers = strtoul(argv[1], NULL, 0);
236 argc -= 2; argv += 2;
237 } else if (0 == strcmp("-delay", argv[0])) {
0a7de745 238 if (argc < 2) {
2d21ac55 239 usage(progname);
0a7de745 240 }
2d21ac55
A
241 client_delay = strtoul(argv[1], NULL, 0);
242 argc -= 2; argv += 2;
243 } else if (0 == strcmp("-spin", argv[0])) {
0a7de745 244 if (argc < 2) {
2d21ac55 245 usage(progname);
0a7de745 246 }
2d21ac55
A
247 client_spin = strtoul(argv[1], NULL, 0);
248 argc -= 2; argv += 2;
249 } else if (0 == strcmp("-pages", argv[0])) {
0a7de745 250 if (argc < 2) {
2d21ac55 251 usage(progname);
0a7de745 252 }
2d21ac55
A
253 client_pages = strtoul(argv[1], NULL, 0);
254 argc -= 2; argv += 2;
b0d623f7 255 } else if (0 == strcmp("-set", argv[0])) {
0a7de745 256 if (argc < 3) {
3e170ce0 257 usage(progname);
0a7de745 258 }
3e170ce0
A
259 setcount = strtoul(argv[1], NULL, 0);
260 portcount = strtoul(argv[2], NULL, 0);
0a7de745 261 if (setcount <= 0 || portcount <= 0) {
b0d623f7 262 usage(progname);
0a7de745 263 }
b0d623f7 264 useset = TRUE;
3e170ce0
A
265 argc -= 3; argv += 3;
266 } else if (0 == strcmp("-prepost", argv[0])) {
267 stress_prepost = TRUE;
268 threaded = TRUE;
b0d623f7 269 argc--; argv++;
3e170ce0
A
270 } else {
271 fprintf(stderr, "unknown option '%s'\n", argv[0]);
2d21ac55 272 usage(progname);
3e170ce0
A
273 }
274 }
275
276 if (stress_prepost) {
277 if (!threaded) {
278 fprintf(stderr, "Prepost stress test _must_ be threaded\n");
279 exit(1);
280 }
281 if (portcount < 1 || setcount < 1) {
282 fprintf(stderr, "Prepost stress test requires >= 1 port in >= 1 set.\n");
283 exit(1);
284 }
2d21ac55
A
285 }
286}
287
0a7de745
A
288void
289setup_server_ports(struct port_args *ports)
2d21ac55
A
290{
291 kern_return_t ret = 0;
292 mach_port_t bsport;
b0d623f7 293 mach_port_t port;
2d21ac55 294
0a7de745
A
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);
2d21ac55
A
300 ports->req_msg = malloc(ports->req_size);
301 ports->reply_msg = malloc(ports->reply_size);
3e170ce0
A
302 if (setcount > 0) {
303 ports->set = (mach_port_t *)calloc(sizeof(mach_port_t), setcount);
304 if (!ports->set) {
305 fprintf(stderr, "calloc(%lu, %d) failed!\n", sizeof(mach_port_t), setcount);
306 exit(1);
307 }
308 }
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);
313 exit(1);
314 }
315 }
2d21ac55 316
b0d623f7 317 if (useset) {
3e170ce0
A
318 mach_port_t set;
319 if (setcount < 1) {
320 fprintf(stderr, "Can't use sets with a setcount of %d\n", setcount);
b0d623f7
A
321 exit(1);
322 }
3e170ce0
A
323
324 for (int ns = 0; ns < setcount; ns++) {
325 ret = mach_port_allocate(mach_task_self(),
0a7de745
A
326 MACH_PORT_RIGHT_PORT_SET,
327 &ports->set[ns]);
3e170ce0
A
328 if (KERN_SUCCESS != ret) {
329 mach_error("mach_port_allocate(SET): ", ret);
330 exit(1);
331 }
0a7de745 332 if (verbose > 1) {
3e170ce0 333 printf("SVR[%d] allocated set[%d] %#x\n",
0a7de745
A
334 ports->server_num, ns, ports->set[ns]);
335 }
3e170ce0
A
336
337 set = ports->set[ns];
338 }
339
340 /* receive on a port set (always use the first in the chain) */
341 ports->rcv_set = ports->set[0];
b0d623f7
A
342 }
343
3e170ce0
A
344 /* stuff the portset(s) with ports */
345 for (int i = 0; i < portcount; i++) {
0a7de745
A
346 ret = mach_port_allocate(mach_task_self(),
347 MACH_PORT_RIGHT_RECEIVE,
348 &port);
b0d623f7
A
349 if (KERN_SUCCESS != ret) {
350 mach_error("mach_port_allocate(PORT): ", ret);
351 exit(1);
352 }
353
0a7de745 354 if (stress_prepost) {
3e170ce0 355 ports->port_list[i] = port;
0a7de745 356 }
3e170ce0 357
b0d623f7 358 if (useset) {
3e170ce0
A
359 /* insert the port into _all_ allocated lowest-level sets */
360 for (int ns = 0; ns < setcount; ns++) {
0a7de745 361 if (verbose > 1) {
3e170ce0 362 printf("SVR[%d] moving port %#x into set %#x...\n",
0a7de745
A
363 ports->server_num, port, ports->set[ns]);
364 }
3e170ce0 365 ret = mach_port_insert_member(mach_task_self(),
0a7de745 366 port, ports->set[ns]);
3e170ce0
A
367 if (KERN_SUCCESS != ret) {
368 mach_error("mach_port_insert_member(): ", ret);
369 exit(1);
370 }
b0d623f7
A
371 }
372 }
2d21ac55
A
373 }
374
3e170ce0 375 /* use the last one as the server's bootstrap port */
b0d623f7
A
376 ports->port = port;
377
3e170ce0
A
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(),
0a7de745
A
382 ports->port_list[i],
383 ports->port_list[i],
384 MACH_MSG_TYPE_MAKE_SEND);
3e170ce0
A
385 if (KERN_SUCCESS != ret) {
386 mach_error("mach_port_insert_right(): ", ret);
387 exit(1);
388 }
389 }
390 } else {
391 ret = mach_port_insert_right(mach_task_self(),
0a7de745
A
392 ports->port,
393 ports->port,
394 MACH_MSG_TYPE_MAKE_SEND);
3e170ce0
A
395 if (KERN_SUCCESS != ret) {
396 mach_error("mach_port_insert_right(): ", ret);
397 exit(1);
398 }
2d21ac55
A
399 }
400
401 ret = task_get_bootstrap_port(mach_task_self(), &bsport);
402 if (KERN_SUCCESS != ret) {
403 mach_error("task_get_bootstrap_port(): ", ret);
404 exit(1);
405 }
406
407 if (verbose) {
3e170ce0 408 printf("server waiting for IPC messages from client on port '%s' (%#x).\n",
0a7de745 409 server_port_name[ports->server_num], ports->port);
2d21ac55
A
410 }
411 ret = bootstrap_register(bsport,
0a7de745
A
412 server_port_name[ports->server_num],
413 ports->port);
2d21ac55
A
414 if (KERN_SUCCESS != ret) {
415 mach_error("bootstrap_register(): ", ret);
416 exit(1);
417 }
418}
419
0a7de745
A
420void
421setup_client_ports(struct port_args *ports)
2d21ac55
A
422{
423 kern_return_t ret = 0;
0a7de745
A
424 switch (msg_type) {
425 case msg_type_trivial:
426 ports->req_size = sizeof(ipc_trivial_message);
427 break;
428 case msg_type_inline:
429 ports->req_size = sizeof(ipc_inline_message) +
430 sizeof(u_int32_t) * num_ints;
431 break;
432 case msg_type_complex:
433 ports->req_size = sizeof(ipc_complex_message);
434 break;
2d21ac55
A
435 }
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);
440
0a7de745
A
441 ret = mach_port_allocate(mach_task_self(),
442 MACH_PORT_RIGHT_RECEIVE,
443 &(ports->port));
2d21ac55
A
444 if (KERN_SUCCESS != ret) {
445 mach_error("mach_port_allocate(): ", ret);
446 exit(1);
447 }
448 if (verbose) {
3e170ce0 449 printf("Client sending %d %s IPC messages to port '%s' in %s mode\n",
0a7de745
A
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"));
2d21ac55 455 }
2d21ac55
A
456}
457
458
459static void
0a7de745
A
460thread_setup(int tag)
461{
462 kern_return_t ret;
463 thread_extended_policy_data_t epolicy;
464 thread_affinity_policy_data_t policy;
2d21ac55
A
465
466 if (!timeshare) {
467 epolicy.timeshare = FALSE;
468 ret = thread_policy_set(
0a7de745
A
469 mach_thread_self(), THREAD_EXTENDED_POLICY,
470 (thread_policy_t) &epolicy,
471 THREAD_EXTENDED_POLICY_COUNT);
472 if (ret != KERN_SUCCESS) {
2d21ac55 473 printf("thread_policy_set(THREAD_EXTENDED_POLICY) returned %d\n", ret);
0a7de745 474 }
2d21ac55
A
475 }
476
0a7de745
A
477 if (affinity) {
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);
485 }
486 }
2d21ac55
A
487}
488
489void *
3e170ce0 490server(void *serverarg)
2d21ac55 491{
2d21ac55
A
492 int idx;
493 kern_return_t ret;
494 int totalmsg = num_msgs * num_clients;
b0d623f7 495 mach_port_t recv_port;
3e170ce0
A
496 uint64_t starttm, endtm;
497
498 int svr_num = (int)(uintptr_t)serverarg;
499 struct port_args *args = &server_port_args[svr_num];
2d21ac55 500
3e170ce0
A
501 args->server_num = svr_num;
502 setup_server_ports(args);
2d21ac55 503
3e170ce0 504 thread_setup(args->server_num + 1);
2d21ac55 505
3e170ce0 506 recv_port = (useset) ? args->rcv_set : args->port;
b0d623f7 507
2d21ac55 508 for (idx = 0; idx < totalmsg; idx++) {
0a7de745 509 if (verbose > 2) {
2d21ac55 510 printf("server awaiting message %d\n", idx);
0a7de745 511 }
3e170ce0 512 ret = mach_msg(args->req_msg,
0a7de745
A
513 MACH_RCV_MSG | MACH_RCV_INTERRUPT | MACH_RCV_LARGE,
514 0,
515 args->req_size,
516 recv_port,
517 MACH_MSG_TIMEOUT_NONE,
518 MACH_PORT_NULL);
519 if (MACH_RCV_INTERRUPTED == ret) {
2d21ac55 520 break;
0a7de745 521 }
2d21ac55 522 if (MACH_MSG_SUCCESS != ret) {
0a7de745 523 if (verbose) {
2d21ac55 524 printf("mach_msg() ret=%d", ret);
0a7de745 525 }
2d21ac55
A
526 mach_error("mach_msg (receive): ", ret);
527 exit(1);
528 }
0a7de745 529 if (verbose > 2) {
2d21ac55 530 printf("server received message %d\n", idx);
0a7de745 531 }
3e170ce0 532 if (args->req_msg->msgh_bits & MACH_MSGH_BITS_COMPLEX) {
0a7de745
A
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);
2d21ac55
A
536 }
537
3e170ce0 538 if (1 == args->req_msg->msgh_id) {
0a7de745 539 if (verbose > 2) {
2d21ac55 540 printf("server sending reply %d\n", idx);
0a7de745 541 }
3e170ce0
A
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,
0a7de745
A
548 MACH_SEND_MSG,
549 args->reply_size,
550 0,
551 MACH_PORT_NULL,
552 MACH_MSG_TIMEOUT_NONE,
553 MACH_PORT_NULL);
2d21ac55
A
554 if (MACH_MSG_SUCCESS != ret) {
555 mach_error("mach_msg (send): ", ret);
556 exit(1);
557 }
558 }
559 }
3e170ce0 560
0a7de745 561 if (!useset) {
3e170ce0 562 return NULL;
0a7de745 563 }
3e170ce0 564
0a7de745 565 if (verbose < 1) {
3e170ce0 566 return NULL;
0a7de745 567 }
3e170ce0
A
568
569 uint64_t deltans = 0;
570 /*
571 * If we're using multiple sets, explicitly tear them all down
572 * and measure the time.
573 */
574 for (int ns = 0; ns < setcount; ns++) {
0a7de745 575 if (verbose > 1) {
3e170ce0 576 printf("\tTearing down set[%d] %#x...\n", ns, args->set[ns]);
0a7de745 577 }
3e170ce0
A
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);
584 exit(1);
585 }
586 }
587
588 uint64_t nlinks = (uint64_t)setcount * (uint64_t)portcount;
589
590 printf("\tteardown of %llu links took %llu ns\n", nlinks, deltans);
591 printf("\t%lluns per set\n", deltans / (uint64_t)setcount);
592
316670eb 593 return NULL;
2d21ac55
A
594}
595
596static inline void
0a7de745 597client_spin_loop(unsigned count, void(fn)(void))
2d21ac55 598{
0a7de745 599 while (count--) {
2d21ac55 600 fn();
0a7de745 601 }
2d21ac55
A
602}
603
0a7de745
A
604static long dummy_memory;
605static long *client_memory = &dummy_memory;
2d21ac55
A
606static void
607client_work_atom(void)
608{
0a7de745 609 static int i;
2d21ac55 610
0a7de745 611 if (++i > client_pages * PAGE_SIZE / sizeof(long)) {
2d21ac55 612 i = 0;
0a7de745 613 }
2d21ac55
A
614 client_memory[i] = 0;
615}
616
0a7de745
A
617static int calibration_count = 10000;
618static int calibration_usec;
2d21ac55
A
619static void *
620calibrate_client_work(void)
621{
0a7de745
A
622 long dummy;
623 struct timeval nowtv;
624 struct timeval warmuptv = { 0, 100 * 1000 }; /* 100ms */
625 struct timeval starttv;
626 struct timeval endtv;
2d21ac55
A
627
628 if (client_spin) {
629 /* Warm-up the stepper first... */
630 gettimeofday(&nowtv, NULL);
631 timeradd(&nowtv, &warmuptv, &endtv);
632 do {
633 client_spin_loop(calibration_count, client_work_atom);
634 gettimeofday(&nowtv, NULL);
0a7de745
A
635 } while (timercmp(&nowtv, &endtv, < ));
636
2d21ac55
A
637 /* Now do the calibration */
638 while (TRUE) {
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;
644 continue;
645 }
646 calibration_usec = endtv.tv_usec - starttv.tv_usec;
647 if (endtv.tv_usec < starttv.tv_usec) {
648 calibration_usec += 1000000;
649 }
650 if (calibration_usec < 1000) {
651 calibration_count *= 10;
652 continue;
653 }
654 calibration_count /= calibration_usec;
655 break;
656 }
0a7de745 657 if (verbose > 1) {
2d21ac55 658 printf("calibration_count=%d calibration_usec=%d\n",
0a7de745
A
659 calibration_count, calibration_usec);
660 }
2d21ac55 661 }
316670eb 662 return NULL;
2d21ac55
A
663}
664
665static void *
666client_work(void)
667{
2d21ac55 668 if (client_spin) {
0a7de745
A
669 client_spin_loop(calibration_count * client_spin,
670 client_work_atom);
2d21ac55 671 }
0a7de745 672
2d21ac55
A
673 if (client_delay) {
674 usleep(client_delay);
675 }
316670eb 676 return NULL;
2d21ac55
A
677}
678
0a7de745
A
679void *
680client(void *threadarg)
2d21ac55
A
681{
682 struct port_args args;
3e170ce0 683 struct port_args *svr_args = NULL;
2d21ac55 684 int idx;
0a7de745 685 mach_msg_header_t *req, *reply;
2d21ac55
A
686 mach_port_t bsport, servport;
687 kern_return_t ret;
3e170ce0 688 int server_num = (int)(uintptr_t)threadarg;
2d21ac55
A
689 void *ints = malloc(sizeof(u_int32_t) * num_ints);
690
0a7de745 691 if (verbose) {
2d21ac55 692 printf("client(%d) started, server port name %s\n",
0a7de745
A
693 server_num, server_port_name[server_num]);
694 }
2d21ac55
A
695
696 args.server_num = server_num;
697 thread_setup(server_num + 1);
698
0a7de745 699 if (stress_prepost) {
3e170ce0 700 svr_args = &server_port_args[server_num];
0a7de745 701 }
3e170ce0 702
2d21ac55
A
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);
707 exit(1);
708 }
709 ret = bootstrap_look_up(bsport,
0a7de745
A
710 server_port_name[server_num],
711 &servport);
2d21ac55
A
712 if (KERN_SUCCESS != ret) {
713 mach_error("bootstrap_look_up(): ", ret);
714 exit(1);
715 }
716
717 setup_client_ports(&args);
718
719 /* Allocate and touch memory */
720 if (client_pages) {
0a7de745 721 unsigned i;
2d21ac55 722 client_memory = (long *) malloc(client_pages * PAGE_SIZE);
0a7de745 723 for (i = 0; i < client_pages; i++) {
2d21ac55 724 client_memory[i * PAGE_SIZE / sizeof(long)] = 0;
0a7de745 725 }
2d21ac55 726 }
3e170ce0
A
727
728 uint64_t starttm, endtm;
0a7de745 729
2d21ac55
A
730 /* start message loop */
731 for (idx = 0; idx < num_msgs; idx++) {
732 req = args.req_msg;
733 reply = args.reply_msg;
734
2d21ac55 735 req->msgh_size = args.req_size;
3e170ce0
A
736 if (stress_prepost) {
737 req->msgh_remote_port = svr_args->port_list[idx % portcount];
738 } else {
739 req->msgh_remote_port = servport;
740 }
741 if (oneway) {
742 req->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
743 req->msgh_local_port = MACH_PORT_NULL;
744 } else {
745 req->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND,
0a7de745 746 MACH_MSG_TYPE_MAKE_SEND_ONCE);
3e170ce0
A
747 req->msgh_local_port = args.port;
748 }
2d21ac55 749 req->msgh_id = oneway ? 0 : 1;
b0d623f7
A
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;
0a7de745
A
754 ((ipc_complex_message *)req)->descriptor.size =
755 num_ints * sizeof(u_int32_t);
b0d623f7
A
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;
2d21ac55 759 }
0a7de745 760 if (verbose > 2) {
3e170ce0 761 printf("client sending message %d to port %#x\n",
0a7de745
A
762 idx, req->msgh_remote_port);
763 }
3e170ce0 764 starttm = mach_absolute_time();
0a7de745
A
765 ret = mach_msg(req,
766 MACH_SEND_MSG,
767 args.req_size,
768 0,
769 MACH_PORT_NULL,
770 MACH_MSG_TIMEOUT_NONE,
771 MACH_PORT_NULL);
3e170ce0 772 endtm = mach_absolute_time();
2d21ac55
A
773 if (MACH_MSG_SUCCESS != ret) {
774 mach_error("mach_msg (send): ", ret);
775 fprintf(stderr, "bailing after %u iterations\n", idx);
776 exit(1);
777 break;
778 }
0a7de745 779 if (stress_prepost) {
3e170ce0 780 OSAtomicAdd64(endtm - starttm, &g_client_send_time);
0a7de745 781 }
3e170ce0 782
2d21ac55 783 if (!oneway) {
0a7de745 784 if (verbose > 2) {
2d21ac55 785 printf("client awaiting reply %d\n", idx);
0a7de745 786 }
2d21ac55
A
787 reply->msgh_bits = 0;
788 reply->msgh_size = args.reply_size;
789 reply->msgh_local_port = args.port;
0a7de745
A
790 ret = mach_msg(args.reply_msg,
791 MACH_RCV_MSG | MACH_RCV_INTERRUPT,
792 0,
793 args.reply_size,
794 args.port,
795 MACH_MSG_TIMEOUT_NONE,
796 MACH_PORT_NULL);
2d21ac55
A
797 if (MACH_MSG_SUCCESS != ret) {
798 mach_error("mach_msg (receive): ", ret);
799 fprintf(stderr, "bailing after %u iterations\n",
0a7de745 800 idx);
2d21ac55
A
801 exit(1);
802 }
0a7de745 803 if (verbose > 2) {
2d21ac55 804 printf("client received reply %d\n", idx);
0a7de745 805 }
2d21ac55
A
806 }
807
808 client_work();
809 }
810
811 free(ints);
316670eb 812 return NULL;
2d21ac55
A
813}
814
815static void
0a7de745
A
816thread_spawn(thread_id_t *thread, void *(fn)(void *), void *arg)
817{
2d21ac55 818 if (threaded) {
0a7de745 819 kern_return_t ret;
2d21ac55 820 ret = pthread_create(
0a7de745
A
821 &thread->tid,
822 NULL,
823 fn,
824 arg);
825 if (ret != 0) {
2d21ac55 826 err(1, "pthread_create()");
0a7de745
A
827 }
828 if (verbose > 1) {
316670eb 829 printf("created pthread %p\n", thread->tid);
0a7de745 830 }
2d21ac55
A
831 } else {
832 thread->pid = fork();
833 if (thread->pid == 0) {
0a7de745 834 if (verbose > 1) {
316670eb 835 printf("calling %p(%p)\n", fn, arg);
0a7de745 836 }
2d21ac55
A
837 fn(arg);
838 exit(0);
839 }
0a7de745 840 if (verbose > 1) {
2d21ac55 841 printf("forked pid %d\n", thread->pid);
0a7de745 842 }
2d21ac55
A
843 }
844}
845
846static void
0a7de745
A
847thread_join(thread_id_t *thread)
848{
2d21ac55 849 if (threaded) {
0a7de745
A
850 kern_return_t ret;
851 if (verbose > 1) {
316670eb 852 printf("joining thread %p\n", thread->tid);
0a7de745 853 }
2d21ac55 854 ret = pthread_join(thread->tid, NULL);
0a7de745 855 if (ret != KERN_SUCCESS) {
316670eb 856 err(1, "pthread_join(%p)", thread->tid);
0a7de745 857 }
2d21ac55 858 } else {
0a7de745
A
859 int stat;
860 if (verbose > 1) {
2d21ac55 861 printf("waiting for pid %d\n", thread->pid);
0a7de745 862 }
2d21ac55
A
863 waitpid(thread->pid, &stat, 0);
864 }
865}
866
867static void
868wait_for_servers(void)
869{
0a7de745
A
870 int i;
871 int retry_count = 10;
872 mach_port_t bsport, servport;
873 kern_return_t ret;
2d21ac55
A
874
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);
879 exit(1);
880 }
881
882 while (retry_count-- > 0) {
883 for (i = 0; i < num_servers; i++) {
884 ret = bootstrap_look_up(bsport,
0a7de745
A
885 server_port_name[i],
886 &servport);
2d21ac55
A
887 if (ret != KERN_SUCCESS) {
888 break;
889 }
890 }
0a7de745 891 if (ret == KERN_SUCCESS) {
2d21ac55 892 return;
0a7de745
A
893 }
894 usleep(100 * 1000); /* 100ms */
2d21ac55
A
895 }
896 fprintf(stderr, "Server(s) failed to register\n");
897 exit(1);
898}
899
0a7de745
A
900int
901main(int argc, char *argv[])
2d21ac55 902{
0a7de745
A
903 int i;
904 int j;
905 thread_id_t *client_id;
906 thread_id_t *server_id;
2d21ac55
A
907
908 signal(SIGINT, signal_handler);
909 parse_args(argc, argv);
910
3e170ce0
A
911 if (mach_timebase_info(&g_timebase) != KERN_SUCCESS) {
912 fprintf(stderr, "Can't get mach_timebase_info!\n");
913 exit(1);
914 }
915
2d21ac55
A
916 calibrate_client_work();
917
918 /*
919 * If we're using affinity create an empty namespace now
920 * so this is shared by all our offspring.
921 */
0a7de745 922 if (affinity) {
2d21ac55 923 thread_setup(0);
0a7de745 924 }
2d21ac55
A
925
926 server_id = (thread_id_t *) malloc(num_servers * sizeof(thread_id_t));
927 server_port_name = (char **) malloc(num_servers * sizeof(char *));
3e170ce0
A
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);
931 exit(1);
932 }
933
0a7de745 934 if (verbose) {
2d21ac55 935 printf("creating %d servers\n", num_servers);
0a7de745 936 }
2d21ac55
A
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);
942 }
943
944 int totalclients = num_servers * num_clients;
945 int totalmsg = num_msgs * totalclients;
946 struct timeval starttv, endtv, deltatv;
947
948 /*
949 * Wait for all servers to have registered all ports before starting
950 * the clients and the clock.
951 */
952 wait_for_servers();
0a7de745
A
953
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" : "",
957 totalclients,
958 totalmsg);
2d21ac55
A
959 fflush(stdout);
960
961 /* Call gettimeofday() once and throw away result; some implementations
962 * (like Mach's) cache some time zone info on first call.
963 */
964 gettimeofday(&starttv, NULL);
965 gettimeofday(&starttv, NULL);
966
967 client_id = (thread_id_t *) malloc(totalclients * sizeof(thread_id_t));
0a7de745 968 if (verbose) {
2d21ac55 969 printf("creating %d clients\n", totalclients);
0a7de745 970 }
2d21ac55
A
971 for (i = 0; i < num_servers; i++) {
972 for (j = 0; j < num_clients; j++) {
973 thread_spawn(
0a7de745 974 &client_id[(i * num_clients) + j],
2d21ac55
A
975 client,
976 (void *) (long) i);
977 }
978 }
979
980 /* Wait for servers to complete */
981 for (i = 0; i < num_servers; i++) {
982 thread_join(&server_id[i]);
983 }
984
985 gettimeofday(&endtv, NULL);
0a7de745 986 if (verbose) {
3e170ce0 987 printf("all servers complete: waiting for clients...\n");
0a7de745 988 }
2d21ac55
A
989
990 for (i = 0; i < totalclients; i++) {
991 thread_join(&client_id[i]);
992 }
993
994 /* report results */
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) {
998 deltatv.tv_sec--;
999 deltatv.tv_usec += 1000000;
1000 }
1001
0a7de745
A
1002 double dsecs = (double) deltatv.tv_sec +
1003 1.0E-6 * (double) deltatv.tv_usec;
2d21ac55 1004
3e170ce0 1005 printf(" in %lu.%03u seconds\n",
0a7de745 1006 deltatv.tv_sec, deltatv.tv_usec / 1000);
2d21ac55 1007 printf(" throughput in messages/sec: %g\n",
0a7de745
A
1008 (double)totalmsg / dsecs);
1009 printf(" average message latency (usec): %2.3g\n",
1010 dsecs * 1.0E6 / (double) totalmsg);
2d21ac55 1011
0a7de745
A
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;
4bd07ac2
A
1015
1016 if (save_perfdata == TRUE) {
490019cf
A
1017 char name[256];
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);
4bd07ac2
A
1020 }
1021
3e170ce0
A
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",
0a7de745 1027 dsecs * 1.0E6 / (double)totalmsg);
3e170ce0
A
1028 }
1029
0a7de745 1030 return 0;
2d21ac55 1031}