1 // SPDX-License-Identifier: GPL-2.0
6 * messaging: Benchmark for scheduler and IPC mechanisms
8 * Based on hackbench by Rusty Russell <rusty@rustcorp.com.au>
9 * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
13 #include <subcmd/parse-options.h>
16 /* Test groups of 20 processes spraying to 20 receivers */
23 #include <sys/types.h>
24 #include <sys/socket.h>
30 #include <linux/time64.h>
34 static bool use_pipes = false;
35 static unsigned int nr_loops = 100;
36 static bool thread_mode = false;
37 static unsigned int num_groups = 10;
39 struct sender_context {
46 struct receiver_context {
47 unsigned int num_packets;
53 static void fdpair(int fds[2])
59 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0)
63 err(EXIT_FAILURE, use_pipes ? "pipe()" : "socketpair()");
66 /* Block until we're ready to go */
67 static void ready(int ready_out, int wakefd)
69 struct pollfd pollfd = { .fd = wakefd, .events = POLLIN };
71 /* Tell them we're ready. */
72 if (write(ready_out, "R", 1) != 1)
73 err(EXIT_FAILURE, "CLIENT: ready write");
75 /* Wait for "GO" signal */
76 if (poll(&pollfd, 1, -1) != 1)
77 err(EXIT_FAILURE, "poll");
80 /* Sender sprays nr_loops messages down each file descriptor */
81 static void *sender(struct sender_context *ctx)
86 ready(ctx->ready_out, ctx->wakefd);
87 memset(data, 'S', sizeof(data));
89 /* Now pump to every receiver. */
90 for (i = 0; i < nr_loops; i++) {
91 for (j = 0; j < ctx->num_fds; j++) {
95 ret = write(ctx->out_fds[j], data + done,
98 err(EXIT_FAILURE, "SENDER: write");
109 /* One receiver per fd */
110 static void *receiver(struct receiver_context* ctx)
115 close(ctx->in_fds[1]);
117 /* Wait for start... */
118 ready(ctx->ready_out, ctx->wakefd);
120 /* Receive them all */
121 for (i = 0; i < ctx->num_packets; i++) {
126 ret = read(ctx->in_fds[0], data + done, DATASIZE - done);
128 err(EXIT_FAILURE, "SERVER: read");
137 static pthread_t create_worker(void *ctx, void *(*func)(void *))
145 /* Fork the receiver. */
148 err(EXIT_FAILURE, "fork()");
161 if (pthread_attr_init(&attr) != 0)
162 err(EXIT_FAILURE, "pthread_attr_init:");
165 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
166 err(EXIT_FAILURE, "pthread_attr_setstacksize");
169 ret = pthread_create(&childid, &attr, func, ctx);
171 err(EXIT_FAILURE, "pthread_create failed");
176 static void reap_worker(pthread_t id)
184 if (!WIFEXITED(proc_status))
187 pthread_join(id, &thread_status);
191 /* One group of senders and receivers */
192 static unsigned int group(pthread_t *pth,
193 unsigned int num_fds,
198 struct sender_context *snd_ctx = malloc(sizeof(struct sender_context)
199 + num_fds * sizeof(int));
202 err(EXIT_FAILURE, "malloc()");
204 for (i = 0; i < num_fds; i++) {
206 struct receiver_context *ctx = malloc(sizeof(*ctx));
209 err(EXIT_FAILURE, "malloc()");
212 /* Create the pipe between client and server */
215 ctx->num_packets = num_fds * nr_loops;
216 ctx->in_fds[0] = fds[0];
217 ctx->in_fds[1] = fds[1];
218 ctx->ready_out = ready_out;
219 ctx->wakefd = wakefd;
221 pth[i] = create_worker(ctx, (void *)receiver);
223 snd_ctx->out_fds[i] = fds[1];
228 /* Now we have all the fds, fork the senders */
229 for (i = 0; i < num_fds; i++) {
230 snd_ctx->ready_out = ready_out;
231 snd_ctx->wakefd = wakefd;
232 snd_ctx->num_fds = num_fds;
234 pth[num_fds+i] = create_worker(snd_ctx, (void *)sender);
237 /* Close the fds we have left */
239 for (i = 0; i < num_fds; i++)
240 close(snd_ctx->out_fds[i]);
242 /* Return number of children to reap */
246 static const struct option options[] = {
247 OPT_BOOLEAN('p', "pipe", &use_pipes,
248 "Use pipe() instead of socketpair()"),
249 OPT_BOOLEAN('t', "thread", &thread_mode,
250 "Be multi thread instead of multi process"),
251 OPT_UINTEGER('g', "group", &num_groups, "Specify number of groups"),
252 OPT_UINTEGER('l', "nr_loops", &nr_loops, "Specify the number of loops to run (default: 100)"),
256 static const char * const bench_sched_message_usage[] = {
257 "perf bench sched messaging <options>",
261 int bench_sched_messaging(int argc, const char **argv)
263 unsigned int i, total_children;
264 struct timeval start, stop, diff;
265 unsigned int num_fds = 20;
266 int readyfds[2], wakefds[2];
270 argc = parse_options(argc, argv, options,
271 bench_sched_message_usage, 0);
273 pth_tab = malloc(num_fds * 2 * num_groups * sizeof(pthread_t));
275 err(EXIT_FAILURE, "main:malloc()");
281 for (i = 0; i < num_groups; i++)
282 total_children += group(pth_tab+total_children, num_fds,
283 readyfds[1], wakefds[0]);
285 /* Wait for everyone to be ready */
286 for (i = 0; i < total_children; i++)
287 if (read(readyfds[0], &dummy, 1) != 1)
288 err(EXIT_FAILURE, "Reading for readyfds");
290 gettimeofday(&start, NULL);
293 if (write(wakefds[1], &dummy, 1) != 1)
294 err(EXIT_FAILURE, "Writing to start them");
297 for (i = 0; i < total_children; i++)
298 reap_worker(pth_tab[i]);
300 gettimeofday(&stop, NULL);
302 timersub(&stop, &start, &diff);
304 switch (bench_format) {
305 case BENCH_FORMAT_DEFAULT:
306 printf("# %d sender and receiver %s per group\n",
307 num_fds, thread_mode ? "threads" : "processes");
308 printf("# %d groups == %d %s run\n\n",
309 num_groups, num_groups * 2 * num_fds,
310 thread_mode ? "threads" : "processes");
311 printf(" %14s: %lu.%03lu [sec]\n", "Total time",
313 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
315 case BENCH_FORMAT_SIMPLE:
316 printf("%lu.%03lu\n", diff.tv_sec,
317 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
320 /* reaching here is something disaster */
321 fprintf(stderr, "Unknown format:%d\n", bench_format);