1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Davidlohr Bueso.
5 * Benchmark the various operations allowed for epoll_ctl(2).
6 * The idea is to concurrently stress a single epoll instance
9 /* For the CLR_() macros */
17 #include <linux/compiler.h>
18 #include <linux/kernel.h>
20 #include <sys/resource.h>
21 #include <sys/epoll.h>
22 #include <sys/eventfd.h>
24 #include "../util/stat.h"
25 #include <subcmd/parse-options.h>
31 #define printinfo(fmt, arg...) \
32 do { if (__verbose) printf(fmt, ## arg); } while (0)
34 static unsigned int nthreads = 0;
35 static unsigned int nsecs = 8;
36 struct timeval start, end, runtime;
37 static bool done, __verbose, randomize;
40 * epoll related shared variables.
43 /* Maximum number of nesting allowed inside epoll sets */
44 #define EPOLL_MAXNESTS 4
55 static bool noaffinity;
56 static unsigned int nested = 0;
58 /* amount of fds to monitor, per thread */
59 static unsigned int nfds = 64;
61 static pthread_mutex_t thread_lock;
62 static unsigned int threads_starting;
63 static struct stats all_stats[EPOLL_NR_OPS];
64 static pthread_cond_t thread_parent, thread_worker;
69 unsigned long ops[EPOLL_NR_OPS];
73 static const struct option options[] = {
74 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
75 OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
76 OPT_UINTEGER('f', "nfds", &nfds, "Specify amount of file descriptors to monitor for each thread"),
77 OPT_BOOLEAN( 'n', "noaffinity", &noaffinity, "Disables CPU affinity"),
78 OPT_UINTEGER( 'N', "nested", &nested, "Nesting level epoll hierarchy (default is 0, no nesting)"),
79 OPT_BOOLEAN( 'R', "randomize", &randomize, "Perform random operations on random fds"),
80 OPT_BOOLEAN( 'v', "verbose", &__verbose, "Verbose mode"),
84 static const char * const bench_epoll_ctl_usage[] = {
85 "perf bench epoll ctl <options>",
89 static void toggle_done(int sig __maybe_unused,
90 siginfo_t *info __maybe_unused,
91 void *uc __maybe_unused)
93 /* inform all threads that we're done for the day */
95 gettimeofday(&end, NULL);
96 timersub(&end, &start, &runtime);
99 static void nest_epollfd(void)
102 struct epoll_event ev;
104 if (nested > EPOLL_MAXNESTS)
105 nested = EPOLL_MAXNESTS;
106 printinfo("Nesting level(s): %d\n", nested);
108 epollfdp = calloc(nested, sizeof(int));
110 err(EXIT_FAILURE, "calloc");
112 for (i = 0; i < nested; i++) {
113 epollfdp[i] = epoll_create(1);
115 err(EXIT_FAILURE, "epoll_create");
118 ev.events = EPOLLHUP; /* anything */
119 ev.data.u64 = i; /* any number */
121 for (i = nested - 1; i; i--) {
122 if (epoll_ctl(epollfdp[i - 1], EPOLL_CTL_ADD,
123 epollfdp[i], &ev) < 0)
124 err(EXIT_FAILURE, "epoll_ctl");
127 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, *epollfdp, &ev) < 0)
128 err(EXIT_FAILURE, "epoll_ctl");
131 static inline void do_epoll_op(struct worker *w, int op, int fd)
134 struct epoll_event ev;
141 error = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
144 ev.events = EPOLLOUT;
145 error = epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &ev);
148 error = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);
159 static inline void do_random_epoll_op(struct worker *w)
161 unsigned long rnd1 = random(), rnd2 = random();
164 fd = w->fdmap[rnd1 % nfds];
165 op = rnd2 % EPOLL_NR_OPS;
167 do_epoll_op(w, op, fd);
170 static void *workerfn(void *arg)
173 struct worker *w = (struct worker *) arg;
174 struct timespec ts = { .tv_sec = 0,
177 pthread_mutex_lock(&thread_lock);
179 if (!threads_starting)
180 pthread_cond_signal(&thread_parent);
181 pthread_cond_wait(&thread_worker, &thread_lock);
182 pthread_mutex_unlock(&thread_lock);
188 do_random_epoll_op(w);
190 for (i = 0; i < nfds; i++) {
191 do_epoll_op(w, OP_EPOLL_ADD, w->fdmap[i]);
192 do_epoll_op(w, OP_EPOLL_MOD, w->fdmap[i]);
193 do_epoll_op(w, OP_EPOLL_DEL, w->fdmap[i]);
197 nanosleep(&ts, NULL);
203 static void init_fdmaps(struct worker *w, int pct)
207 struct epoll_event ev;
213 for (i = 0; i < nfds; i+=inc) {
214 ev.data.fd = w->fdmap[i];
217 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, w->fdmap[i], &ev) < 0)
218 err(EXIT_FAILURE, "epoll_ct");
222 static int do_threads(struct worker *worker, struct cpu_map *cpu)
224 pthread_attr_t thread_attr, *attrp = NULL;
230 pthread_attr_init(&thread_attr);
232 for (i = 0; i < nthreads; i++) {
233 struct worker *w = &worker[i];
236 w->fdmap = calloc(nfds, sizeof(int));
240 for (j = 0; j < nfds; j++) {
241 w->fdmap[j] = eventfd(0, EFD_NONBLOCK);
243 err(EXIT_FAILURE, "eventfd");
247 * Lets add 50% of the fdmap to the epoll instance, and
248 * do it before any threads are started; otherwise there is
249 * an initial bias of the call failing (mod and del ops).
256 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
258 ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset);
260 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
262 attrp = &thread_attr;
265 ret = pthread_create(&w->thread, attrp, workerfn,
266 (void *)(struct worker *) w);
268 err(EXIT_FAILURE, "pthread_create");
272 pthread_attr_destroy(&thread_attr);
277 static void print_summary(void)
280 unsigned long avg[EPOLL_NR_OPS];
281 double stddev[EPOLL_NR_OPS];
283 for (i = 0; i < EPOLL_NR_OPS; i++) {
284 avg[i] = avg_stats(&all_stats[i]);
285 stddev[i] = stddev_stats(&all_stats[i]);
288 printf("\nAveraged %ld ADD operations (+- %.2f%%)\n",
289 avg[OP_EPOLL_ADD], rel_stddev_stats(stddev[OP_EPOLL_ADD],
291 printf("Averaged %ld MOD operations (+- %.2f%%)\n",
292 avg[OP_EPOLL_MOD], rel_stddev_stats(stddev[OP_EPOLL_MOD],
294 printf("Averaged %ld DEL operations (+- %.2f%%)\n",
295 avg[OP_EPOLL_DEL], rel_stddev_stats(stddev[OP_EPOLL_DEL],
299 int bench_epoll_ctl(int argc, const char **argv)
302 struct sigaction act;
303 struct worker *worker = NULL;
305 struct rlimit rl, prevrl;
308 argc = parse_options(argc, argv, options, bench_epoll_ctl_usage, 0);
310 usage_with_options(bench_epoll_ctl_usage, options);
314 sigfillset(&act.sa_mask);
315 act.sa_sigaction = toggle_done;
316 sigaction(SIGINT, &act, NULL);
318 cpu = cpu_map__new(NULL);
322 /* a single, main epoll instance */
323 epollfd = epoll_create(1);
325 err(EXIT_FAILURE, "epoll_create");
328 * Deal with nested epolls, if any.
333 /* default to the number of CPUs */
337 worker = calloc(nthreads, sizeof(*worker));
341 if (getrlimit(RLIMIT_NOFILE, &prevrl))
342 err(EXIT_FAILURE, "getrlimit");
343 rl.rlim_cur = rl.rlim_max = nfds * nthreads * 2 + 50;
344 printinfo("Setting RLIMIT_NOFILE rlimit from %" PRIu64 " to: %" PRIu64 "\n",
345 (uint64_t)prevrl.rlim_max, (uint64_t)rl.rlim_max);
346 if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
347 err(EXIT_FAILURE, "setrlimit");
349 printf("Run summary [PID %d]: %d threads doing epoll_ctl ops "
350 "%d file-descriptors for %d secs.\n\n",
351 getpid(), nthreads, nfds, nsecs);
353 for (i = 0; i < EPOLL_NR_OPS; i++)
354 init_stats(&all_stats[i]);
356 pthread_mutex_init(&thread_lock, NULL);
357 pthread_cond_init(&thread_parent, NULL);
358 pthread_cond_init(&thread_worker, NULL);
360 threads_starting = nthreads;
362 gettimeofday(&start, NULL);
364 do_threads(worker, cpu);
366 pthread_mutex_lock(&thread_lock);
367 while (threads_starting)
368 pthread_cond_wait(&thread_parent, &thread_lock);
369 pthread_cond_broadcast(&thread_worker);
370 pthread_mutex_unlock(&thread_lock);
373 toggle_done(0, NULL, NULL);
374 printinfo("main thread: toggling done\n");
376 for (i = 0; i < nthreads; i++) {
377 ret = pthread_join(worker[i].thread, NULL);
379 err(EXIT_FAILURE, "pthread_join");
382 /* cleanup & report results */
383 pthread_cond_destroy(&thread_parent);
384 pthread_cond_destroy(&thread_worker);
385 pthread_mutex_destroy(&thread_lock);
387 for (i = 0; i < nthreads; i++) {
388 unsigned long t[EPOLL_NR_OPS];
390 for (j = 0; j < EPOLL_NR_OPS; j++) {
391 t[j] = worker[i].ops[j];
392 update_stats(&all_stats[j], t[j]);
396 printf("[thread %2d] fdmap: %p [ add: %04ld; mod: %04ld; del: %04lds ops ]\n",
397 worker[i].tid, &worker[i].fdmap[0],
398 t[OP_EPOLL_ADD], t[OP_EPOLL_MOD], t[OP_EPOLL_DEL]);
400 printf("[thread %2d] fdmap: %p ... %p [ add: %04ld ops; mod: %04ld ops; del: %04ld ops ]\n",
401 worker[i].tid, &worker[i].fdmap[0],
402 &worker[i].fdmap[nfds-1],
403 t[OP_EPOLL_ADD], t[OP_EPOLL_MOD], t[OP_EPOLL_DEL]);
411 err(EXIT_FAILURE, "calloc");
413 #endif // HAVE_EVENTFD