1 // SPDX-License-Identifier: GPL-2.0
6 * messaging: Benchmark for scheduler and IPC mechanisms
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/list.h>
31 #include <linux/time64.h>
35 static bool use_pipes = false;
36 static unsigned int nr_loops = 100;
37 static bool thread_mode = false;
38 static unsigned int num_groups = 10;
39 static unsigned int total_children = 0;
40 static struct list_head sender_contexts = LIST_HEAD_INIT(sender_contexts);
41 static struct list_head receiver_contexts = LIST_HEAD_INIT(receiver_contexts);
43 struct sender_context {
44 struct list_head list;
51 struct receiver_context {
52 struct list_head list;
53 unsigned int num_packets;
59 union messaging_worker {
64 static union messaging_worker *worker_tab;
66 static void fdpair(int fds[2])
72 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0)
76 err(EXIT_FAILURE, use_pipes ? "pipe()" : "socketpair()");
79 /* Block until we're ready to go */
80 static void ready(int ready_out, int wakefd)
82 struct pollfd pollfd = { .fd = wakefd, .events = POLLIN };
84 /* Tell them we're ready. */
85 if (write(ready_out, "R", 1) != 1)
86 err(EXIT_FAILURE, "CLIENT: ready write");
88 /* Wait for "GO" signal */
89 if (poll(&pollfd, 1, -1) != 1)
90 err(EXIT_FAILURE, "poll");
93 /* Sender sprays nr_loops messages down each file descriptor */
94 static void *sender(struct sender_context *ctx)
99 ready(ctx->ready_out, ctx->wakefd);
100 memset(data, 'S', sizeof(data));
102 /* Now pump to every receiver. */
103 for (i = 0; i < nr_loops; i++) {
104 for (j = 0; j < ctx->num_fds; j++) {
108 ret = write(ctx->out_fds[j], data + done,
109 sizeof(data) - done);
111 err(EXIT_FAILURE, "SENDER: write");
122 /* One receiver per fd */
123 static void *receiver(struct receiver_context* ctx)
128 close(ctx->in_fds[1]);
130 /* Wait for start... */
131 ready(ctx->ready_out, ctx->wakefd);
133 /* Receive them all */
134 for (i = 0; i < ctx->num_packets; i++) {
139 ret = read(ctx->in_fds[0], data + done, DATASIZE - done);
141 err(EXIT_FAILURE, "SERVER: read");
150 static void create_thread_worker(union messaging_worker *worker,
151 void *ctx, void *(*func)(void *))
156 if (pthread_attr_init(&attr) != 0)
157 err(EXIT_FAILURE, "pthread_attr_init:");
160 if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
161 err(EXIT_FAILURE, "pthread_attr_setstacksize");
164 ret = pthread_create(&worker->thread, &attr, func, ctx);
166 err(EXIT_FAILURE, "pthread_create failed");
168 pthread_attr_destroy(&attr);
171 static void create_process_worker(union messaging_worker *worker,
172 void *ctx, void *(*func)(void *))
174 /* Fork the receiver. */
175 worker->pid = fork();
177 if (worker->pid == -1) {
178 err(EXIT_FAILURE, "fork()");
179 } else if (worker->pid == 0) {
185 static void create_worker(union messaging_worker *worker,
186 void *ctx, void *(*func)(void *))
189 return create_process_worker(worker, ctx, func);
191 return create_thread_worker(worker, ctx, func);
194 static void reap_worker(union messaging_worker *worker)
202 if (!WIFEXITED(proc_status))
205 pthread_join(worker->thread, &thread_status);
209 /* One group of senders and receivers */
210 static unsigned int group(union messaging_worker *worker,
211 unsigned int num_fds,
216 struct sender_context *snd_ctx = malloc(sizeof(struct sender_context) +
217 num_fds * sizeof(int));
220 err(EXIT_FAILURE, "malloc()");
222 list_add(&snd_ctx->list, &sender_contexts);
223 for (i = 0; i < num_fds; i++) {
225 struct receiver_context *ctx = malloc(sizeof(*ctx));
228 err(EXIT_FAILURE, "malloc()");
230 list_add(&ctx->list, &receiver_contexts);
232 /* Create the pipe between client and server */
235 ctx->num_packets = num_fds * nr_loops;
236 ctx->in_fds[0] = fds[0];
237 ctx->in_fds[1] = fds[1];
238 ctx->ready_out = ready_out;
239 ctx->wakefd = wakefd;
241 create_worker(worker + i, ctx, (void *)receiver);
243 snd_ctx->out_fds[i] = fds[1];
248 /* Now we have all the fds, fork the senders */
249 for (i = 0; i < num_fds; i++) {
250 snd_ctx->ready_out = ready_out;
251 snd_ctx->wakefd = wakefd;
252 snd_ctx->num_fds = num_fds;
254 create_worker(worker + num_fds + i, snd_ctx, (void *)sender);
257 /* Close the fds we have left */
259 for (i = 0; i < num_fds; i++)
260 close(snd_ctx->out_fds[i]);
262 /* Return number of children to reap */
266 static void sig_handler(int sig __maybe_unused)
271 * When exit abnormally, kill all forked child processes.
273 for (i = 0; i < total_children; i++)
274 kill(worker_tab[i].pid, SIGKILL);
277 static const struct option options[] = {
278 OPT_BOOLEAN('p', "pipe", &use_pipes,
279 "Use pipe() instead of socketpair()"),
280 OPT_BOOLEAN('t', "thread", &thread_mode,
281 "Be multi thread instead of multi process"),
282 OPT_UINTEGER('g', "group", &num_groups, "Specify number of groups"),
283 OPT_UINTEGER('l', "nr_loops", &nr_loops, "Specify the number of loops to run (default: 100)"),
287 static const char * const bench_sched_message_usage[] = {
288 "perf bench sched messaging <options>",
292 int bench_sched_messaging(int argc, const char **argv)
295 struct timeval start, stop, diff;
296 unsigned int num_fds = 20;
297 int readyfds[2], wakefds[2];
299 struct sender_context *pos, *n;
301 argc = parse_options(argc, argv, options,
302 bench_sched_message_usage, 0);
304 worker_tab = malloc(num_fds * 2 * num_groups * sizeof(union messaging_worker));
306 err(EXIT_FAILURE, "main:malloc()");
312 signal(SIGINT, sig_handler);
313 signal(SIGTERM, sig_handler);
316 for (i = 0; i < num_groups; i++)
317 total_children += group(worker_tab + total_children, num_fds,
318 readyfds[1], wakefds[0]);
320 /* Wait for everyone to be ready */
321 for (i = 0; i < total_children; i++)
322 if (read(readyfds[0], &dummy, 1) != 1)
323 err(EXIT_FAILURE, "Reading for readyfds");
325 gettimeofday(&start, NULL);
328 if (write(wakefds[1], &dummy, 1) != 1)
329 err(EXIT_FAILURE, "Writing to start them");
332 for (i = 0; i < total_children; i++)
333 reap_worker(worker_tab + i);
335 gettimeofday(&stop, NULL);
337 timersub(&stop, &start, &diff);
339 switch (bench_format) {
340 case BENCH_FORMAT_DEFAULT:
341 printf("# %d sender and receiver %s per group\n",
342 num_fds, thread_mode ? "threads" : "processes");
343 printf("# %d groups == %d %s run\n\n",
344 num_groups, num_groups * 2 * num_fds,
345 thread_mode ? "threads" : "processes");
346 printf(" %14s: %lu.%03lu [sec]\n", "Total time",
347 (unsigned long) diff.tv_sec,
348 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
350 case BENCH_FORMAT_SIMPLE:
351 printf("%lu.%03lu\n", (unsigned long) diff.tv_sec,
352 (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
355 /* reaching here is something disaster */
356 fprintf(stderr, "Unknown format:%d\n", bench_format);
362 list_for_each_entry_safe(pos, n, &sender_contexts, list) {
363 list_del_init(&pos->list);
366 list_for_each_entry_safe(pos, n, &receiver_contexts, list) {
367 list_del_init(&pos->list);