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