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