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