5 * messaging: Benchmark for scheduler and IPC mechanisms
7 * Based on hackbench by Rusty Russell <rusty@rustcorp.com.au>
8 * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
13 #include "../util/util.h"
14 #include "../util/parse-options.h"
15 #include "../builtin.h"
18 /* Test groups of 20 processes spraying to 20 receivers */
25 #include <sys/types.h>
26 #include <sys/socket.h>
35 static bool use_pipes
= false;
36 static unsigned int loops
= 100;
37 static bool thread_mode
= false;
38 static unsigned int num_groups
= 10;
40 struct sender_context
{
47 struct receiver_context
{
48 unsigned int num_packets
;
54 static void fdpair(int fds
[2])
60 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) == 0)
64 err(EXIT_FAILURE
, use_pipes
? "pipe()" : "socketpair()");
67 /* Block until we're ready to go */
68 static void ready(int ready_out
, int wakefd
)
71 struct pollfd pollfd
= { .fd
= wakefd
, .events
= POLLIN
};
73 /* Tell them we're ready. */
74 if (write(ready_out
, &dummy
, 1) != 1)
75 err(EXIT_FAILURE
, "CLIENT: ready write");
77 /* Wait for "GO" signal */
78 if (poll(&pollfd
, 1, -1) != 1)
79 err(EXIT_FAILURE
, "poll");
82 /* Sender sprays loops messages down each file descriptor */
83 static void *sender(struct sender_context
*ctx
)
88 ready(ctx
->ready_out
, ctx
->wakefd
);
90 /* Now pump to every receiver. */
91 for (i
= 0; i
< loops
; i
++) {
92 for (j
= 0; j
< ctx
->num_fds
; j
++) {
96 ret
= write(ctx
->out_fds
[j
], data
+ done
,
99 err(EXIT_FAILURE
, "SENDER: write");
110 /* One receiver per fd */
111 static void *receiver(struct receiver_context
* ctx
)
116 close(ctx
->in_fds
[1]);
118 /* Wait for start... */
119 ready(ctx
->ready_out
, ctx
->wakefd
);
121 /* Receive them all */
122 for (i
= 0; i
< ctx
->num_packets
; i
++) {
127 ret
= read(ctx
->in_fds
[0], data
+ done
, DATASIZE
- done
);
129 err(EXIT_FAILURE
, "SERVER: read");
138 static pthread_t
create_worker(void *ctx
, void *(*func
)(void *))
146 /* Fork the receiver. */
149 err(EXIT_FAILURE
, "fork()");
162 if (pthread_attr_init(&attr
) != 0)
163 err(EXIT_FAILURE
, "pthread_attr_init:");
166 if (pthread_attr_setstacksize(&attr
, PTHREAD_STACK_MIN
) != 0)
167 err(EXIT_FAILURE
, "pthread_attr_setstacksize");
170 ret
= pthread_create(&childid
, &attr
, func
, ctx
);
172 err(EXIT_FAILURE
, "pthread_create failed");
177 static void reap_worker(pthread_t id
)
185 if (!WIFEXITED(proc_status
))
188 pthread_join(id
, &thread_status
);
192 /* One group of senders and receivers */
193 static unsigned int group(pthread_t
*pth
,
194 unsigned int num_fds
,
199 struct sender_context
*snd_ctx
= malloc(sizeof(struct sender_context
)
200 + num_fds
* sizeof(int));
203 err(EXIT_FAILURE
, "malloc()");
205 for (i
= 0; i
< num_fds
; i
++) {
207 struct receiver_context
*ctx
= malloc(sizeof(*ctx
));
210 err(EXIT_FAILURE
, "malloc()");
213 /* Create the pipe between client and server */
216 ctx
->num_packets
= num_fds
* loops
;
217 ctx
->in_fds
[0] = fds
[0];
218 ctx
->in_fds
[1] = fds
[1];
219 ctx
->ready_out
= ready_out
;
220 ctx
->wakefd
= wakefd
;
222 pth
[i
] = create_worker(ctx
, (void *)receiver
);
224 snd_ctx
->out_fds
[i
] = fds
[1];
229 /* Now we have all the fds, fork the senders */
230 for (i
= 0; i
< num_fds
; i
++) {
231 snd_ctx
->ready_out
= ready_out
;
232 snd_ctx
->wakefd
= wakefd
;
233 snd_ctx
->num_fds
= num_fds
;
235 pth
[num_fds
+i
] = create_worker(snd_ctx
, (void *)sender
);
238 /* Close the fds we have left */
240 for (i
= 0; i
< num_fds
; i
++)
241 close(snd_ctx
->out_fds
[i
]);
243 /* Return number of children to reap */
247 static const struct option options
[] = {
248 OPT_BOOLEAN('p', "pipe", &use_pipes
,
249 "Use pipe() instead of socketpair()"),
250 OPT_BOOLEAN('t', "thread", &thread_mode
,
251 "Be multi thread instead of multi process"),
252 OPT_UINTEGER('g', "group", &num_groups
, "Specify number of groups"),
253 OPT_UINTEGER('l', "loop", &loops
, "Specify number of loops"),
257 static const char * const bench_sched_message_usage
[] = {
258 "perf bench sched messaging <options>",
262 int bench_sched_messaging(int argc
, const char **argv
,
263 const char *prefix __maybe_unused
)
265 unsigned int i
, total_children
;
266 struct timeval start
, stop
, diff
;
267 unsigned int num_fds
= 20;
268 int readyfds
[2], wakefds
[2];
272 argc
= parse_options(argc
, argv
, options
,
273 bench_sched_message_usage
, 0);
275 pth_tab
= malloc(num_fds
* 2 * num_groups
* sizeof(pthread_t
));
277 err(EXIT_FAILURE
, "main:malloc()");
283 for (i
= 0; i
< num_groups
; i
++)
284 total_children
+= group(pth_tab
+total_children
, num_fds
,
285 readyfds
[1], wakefds
[0]);
287 /* Wait for everyone to be ready */
288 for (i
= 0; i
< total_children
; i
++)
289 if (read(readyfds
[0], &dummy
, 1) != 1)
290 err(EXIT_FAILURE
, "Reading for readyfds");
292 gettimeofday(&start
, NULL
);
295 if (write(wakefds
[1], &dummy
, 1) != 1)
296 err(EXIT_FAILURE
, "Writing to start them");
299 for (i
= 0; i
< total_children
; i
++)
300 reap_worker(pth_tab
[i
]);
302 gettimeofday(&stop
, NULL
);
304 timersub(&stop
, &start
, &diff
);
306 switch (bench_format
) {
307 case BENCH_FORMAT_DEFAULT
:
308 printf("# %d sender and receiver %s per group\n",
309 num_fds
, thread_mode
? "threads" : "processes");
310 printf("# %d groups == %d %s run\n\n",
311 num_groups
, num_groups
* 2 * num_fds
,
312 thread_mode
? "threads" : "processes");
313 printf(" %14s: %lu.%03lu [sec]\n", "Total time",
315 (unsigned long) (diff
.tv_usec
/1000));
317 case BENCH_FORMAT_SIMPLE
:
318 printf("%lu.%03lu\n", diff
.tv_sec
,
319 (unsigned long) (diff
.tv_usec
/1000));
322 /* reaching here is something disaster */
323 fprintf(stderr
, "Unknown format:%d\n", bench_format
);