3 * builtin-bench-messaging.c
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>
34 static int use_pipes = 0;
35 static unsigned int loops = 100;
36 static unsigned int thread_mode = 0;
37 static unsigned int num_groups = 10;
38 static int simple = 0;
40 struct sender_context {
47 struct receiver_context {
48 unsigned int num_packets;
54 static void barf(const char *msg)
56 fprintf(stderr, "%s (error: %s)\n", msg, strerror(errno));
60 static void fdpair(int fds[2])
66 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0)
70 barf(use_pipes ? "pipe()" : "socketpair()");
73 /* Block until we're ready to go */
74 static void ready(int ready_out, int wakefd)
77 struct pollfd pollfd = { .fd = wakefd, .events = POLLIN };
79 /* Tell them we're ready. */
80 if (write(ready_out, &dummy, 1) != 1)
81 barf("CLIENT: ready write");
83 /* Wait for "GO" signal */
84 if (poll(&pollfd, 1, -1) != 1)
88 /* Sender sprays loops messages down each file descriptor */
89 static void *sender(struct sender_context *ctx)
94 ready(ctx->ready_out, ctx->wakefd);
96 /* Now pump to every receiver. */
97 for (i = 0; i < loops; i++) {
98 for (j = 0; j < ctx->num_fds; j++) {
102 ret = write(ctx->out_fds[j], data + done,
105 barf("SENDER: write");
116 /* One receiver per fd */
117 static void *receiver(struct receiver_context* ctx)
122 close(ctx->in_fds[1]);
124 /* Wait for start... */
125 ready(ctx->ready_out, ctx->wakefd);
127 /* Receive them all */
128 for (i = 0; i < ctx->num_packets; i++) {
133 ret = read(ctx->in_fds[0], data + done, DATASIZE - done);
135 barf("SERVER: read");
144 static pthread_t create_worker(void *ctx, void *(*func)(void *))
152 /* Fork the receiver. */
168 if (pthread_attr_init(&attr) != 0)
169 barf("pthread_attr_init:");
172 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
173 barf("pthread_attr_setstacksize");
176 err = pthread_create(&childid, &attr, func, ctx);
178 fprintf(stderr, "pthread_create failed: %s (%d)\n",
185 static void reap_worker(pthread_t id)
193 if (!WIFEXITED(proc_status))
196 pthread_join(id, &thread_status);
200 /* One group of senders and receivers */
201 static unsigned int group(pthread_t *pth,
202 unsigned int num_fds,
207 struct sender_context *snd_ctx = malloc(sizeof(struct sender_context)
208 + num_fds * sizeof(int));
213 for (i = 0; i < num_fds; i++) {
215 struct receiver_context *ctx = malloc(sizeof(*ctx));
221 /* Create the pipe between client and server */
224 ctx->num_packets = num_fds * loops;
225 ctx->in_fds[0] = fds[0];
226 ctx->in_fds[1] = fds[1];
227 ctx->ready_out = ready_out;
228 ctx->wakefd = wakefd;
230 pth[i] = create_worker(ctx, (void *)receiver);
232 snd_ctx->out_fds[i] = fds[1];
237 /* Now we have all the fds, fork the senders */
238 for (i = 0; i < num_fds; i++) {
239 snd_ctx->ready_out = ready_out;
240 snd_ctx->wakefd = wakefd;
241 snd_ctx->num_fds = num_fds;
243 pth[num_fds+i] = create_worker(snd_ctx, (void *)sender);
246 /* Close the fds we have left */
248 for (i = 0; i < num_fds; i++)
249 close(snd_ctx->out_fds[i]);
251 /* Return number of children to reap */
255 static const struct option options[] = {
256 OPT_BOOLEAN('p', "pipe", &use_pipes,
257 "Use pipe() instead of socketpair()"),
258 OPT_BOOLEAN('t', "thread", &thread_mode,
259 "Be multi thread instead of multi process"),
260 OPT_INTEGER('g', "group", &num_groups,
261 "Specify number of groups"),
262 OPT_INTEGER('l', "loop", &loops,
263 "Specify number of loops"),
264 OPT_BOOLEAN('s', "simple-output", &simple,
265 "Do simple output (this maybe useful for"
266 "processing by scripts or graph tools like gnuplot)"),
270 static const char * const bench_sched_message_usage[] = {
271 "perf bench sched messaging <options>",
275 int bench_sched_messaging(int argc, const char **argv,
276 const char *prefix __used)
278 unsigned int i, total_children;
279 struct timeval start, stop, diff;
280 unsigned int num_fds = 20;
281 int readyfds[2], wakefds[2];
285 argc = parse_options(argc, argv, options,
286 bench_sched_message_usage, 0);
288 pth_tab = malloc(num_fds * 2 * num_groups * sizeof(pthread_t));
290 barf("main:malloc()");
296 for (i = 0; i < num_groups; i++)
297 total_children += group(pth_tab+total_children, num_fds,
298 readyfds[1], wakefds[0]);
300 /* Wait for everyone to be ready */
301 for (i = 0; i < total_children; i++)
302 if (read(readyfds[0], &dummy, 1) != 1)
303 barf("Reading for readyfds");
305 gettimeofday(&start, NULL);
308 if (write(wakefds[1], &dummy, 1) != 1)
309 barf("Writing to start them");
312 for (i = 0; i < total_children; i++)
313 reap_worker(pth_tab[i]);
315 gettimeofday(&stop, NULL);
317 timersub(&stop, &start, &diff);
320 printf("%lu.%03lu\n", diff.tv_sec, diff.tv_usec/1000);
322 printf("(%d sender and receiver %s per group)\n",
323 num_fds, thread_mode ? "threads" : "processes");
324 printf("(%d groups == %d %s run)\n\n",
325 num_groups, num_groups * 2 * num_fds,
326 thread_mode ? "threads" : "processes");
327 printf("\tTotal time:%lu.%03lu sec\n",
328 diff.tv_sec, diff.tv_usec/1000);