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 */
18 #include <linux/compiler.h>
19 #include <linux/kernel.h>
21 #include <sys/resource.h>
22 #include <sys/epoll.h>
23 #include <sys/eventfd.h>
24 #include <internal/cpumap.h>
25 #include <perf/cpumap.h>
27 #include "../util/stat.h"
28 #include <subcmd/parse-options.h>
33 #define printinfo(fmt, arg...) \
34 do { if (__verbose) printf(fmt, ## arg); } while (0)
36 static unsigned int nthreads
= 0;
37 static unsigned int nsecs
= 8;
38 struct timeval start
, end
, runtime
;
39 static bool done
, __verbose
, randomize
;
42 * epoll related shared variables.
45 /* Maximum number of nesting allowed inside epoll sets */
46 #define EPOLL_MAXNESTS 4
57 static bool noaffinity
;
58 static unsigned int nested
= 0;
60 /* amount of fds to monitor, per thread */
61 static unsigned int nfds
= 64;
63 static pthread_mutex_t thread_lock
;
64 static unsigned int threads_starting
;
65 static struct stats all_stats
[EPOLL_NR_OPS
];
66 static pthread_cond_t thread_parent
, thread_worker
;
71 unsigned long ops
[EPOLL_NR_OPS
];
75 static const struct option options
[] = {
76 OPT_UINTEGER('t', "threads", &nthreads
, "Specify amount of threads"),
77 OPT_UINTEGER('r', "runtime", &nsecs
, "Specify runtime (in seconds)"),
78 OPT_UINTEGER('f', "nfds", &nfds
, "Specify amount of file descriptors to monitor for each thread"),
79 OPT_BOOLEAN( 'n', "noaffinity", &noaffinity
, "Disables CPU affinity"),
80 OPT_UINTEGER( 'N', "nested", &nested
, "Nesting level epoll hierarchy (default is 0, no nesting)"),
81 OPT_BOOLEAN( 'R', "randomize", &randomize
, "Perform random operations on random fds"),
82 OPT_BOOLEAN( 'v', "verbose", &__verbose
, "Verbose mode"),
86 static const char * const bench_epoll_ctl_usage
[] = {
87 "perf bench epoll ctl <options>",
91 static void toggle_done(int sig __maybe_unused
,
92 siginfo_t
*info __maybe_unused
,
93 void *uc __maybe_unused
)
95 /* inform all threads that we're done for the day */
97 gettimeofday(&end
, NULL
);
98 timersub(&end
, &start
, &runtime
);
101 static void nest_epollfd(void)
104 struct epoll_event ev
;
106 if (nested
> EPOLL_MAXNESTS
)
107 nested
= EPOLL_MAXNESTS
;
108 printinfo("Nesting level(s): %d\n", nested
);
110 epollfdp
= calloc(nested
, sizeof(int));
112 err(EXIT_FAILURE
, "calloc");
114 for (i
= 0; i
< nested
; i
++) {
115 epollfdp
[i
] = epoll_create(1);
117 err(EXIT_FAILURE
, "epoll_create");
120 ev
.events
= EPOLLHUP
; /* anything */
121 ev
.data
.u64
= i
; /* any number */
123 for (i
= nested
- 1; i
; i
--) {
124 if (epoll_ctl(epollfdp
[i
- 1], EPOLL_CTL_ADD
,
125 epollfdp
[i
], &ev
) < 0)
126 err(EXIT_FAILURE
, "epoll_ctl");
129 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, *epollfdp
, &ev
) < 0)
130 err(EXIT_FAILURE
, "epoll_ctl");
133 static inline void do_epoll_op(struct worker
*w
, int op
, int fd
)
136 struct epoll_event ev
;
143 error
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, fd
, &ev
);
146 ev
.events
= EPOLLOUT
;
147 error
= epoll_ctl(epollfd
, EPOLL_CTL_MOD
, fd
, &ev
);
150 error
= epoll_ctl(epollfd
, EPOLL_CTL_DEL
, fd
, NULL
);
161 static inline void do_random_epoll_op(struct worker
*w
)
163 unsigned long rnd1
= random(), rnd2
= random();
166 fd
= w
->fdmap
[rnd1
% nfds
];
167 op
= rnd2
% EPOLL_NR_OPS
;
169 do_epoll_op(w
, op
, fd
);
172 static void *workerfn(void *arg
)
175 struct worker
*w
= (struct worker
*) arg
;
176 struct timespec ts
= { .tv_sec
= 0,
179 pthread_mutex_lock(&thread_lock
);
181 if (!threads_starting
)
182 pthread_cond_signal(&thread_parent
);
183 pthread_cond_wait(&thread_worker
, &thread_lock
);
184 pthread_mutex_unlock(&thread_lock
);
190 do_random_epoll_op(w
);
192 for (i
= 0; i
< nfds
; i
++) {
193 do_epoll_op(w
, OP_EPOLL_ADD
, w
->fdmap
[i
]);
194 do_epoll_op(w
, OP_EPOLL_MOD
, w
->fdmap
[i
]);
195 do_epoll_op(w
, OP_EPOLL_DEL
, w
->fdmap
[i
]);
199 nanosleep(&ts
, NULL
);
205 static void init_fdmaps(struct worker
*w
, int pct
)
209 struct epoll_event ev
;
215 for (i
= 0; i
< nfds
; i
+=inc
) {
216 ev
.data
.fd
= w
->fdmap
[i
];
219 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, w
->fdmap
[i
], &ev
) < 0)
220 err(EXIT_FAILURE
, "epoll_ct");
224 static int do_threads(struct worker
*worker
, struct perf_cpu_map
*cpu
)
226 pthread_attr_t thread_attr
, *attrp
= NULL
;
232 pthread_attr_init(&thread_attr
);
234 for (i
= 0; i
< nthreads
; i
++) {
235 struct worker
*w
= &worker
[i
];
238 w
->fdmap
= calloc(nfds
, sizeof(int));
242 for (j
= 0; j
< nfds
; j
++) {
243 w
->fdmap
[j
] = eventfd(0, EFD_NONBLOCK
);
245 err(EXIT_FAILURE
, "eventfd");
249 * Lets add 50% of the fdmap to the epoll instance, and
250 * do it before any threads are started; otherwise there is
251 * an initial bias of the call failing (mod and del ops).
258 CPU_SET(cpu
->map
[i
% cpu
->nr
], &cpuset
);
260 ret
= pthread_attr_setaffinity_np(&thread_attr
, sizeof(cpu_set_t
), &cpuset
);
262 err(EXIT_FAILURE
, "pthread_attr_setaffinity_np");
264 attrp
= &thread_attr
;
267 ret
= pthread_create(&w
->thread
, attrp
, workerfn
,
268 (void *)(struct worker
*) w
);
270 err(EXIT_FAILURE
, "pthread_create");
274 pthread_attr_destroy(&thread_attr
);
279 static void print_summary(void)
282 unsigned long avg
[EPOLL_NR_OPS
];
283 double stddev
[EPOLL_NR_OPS
];
285 for (i
= 0; i
< EPOLL_NR_OPS
; i
++) {
286 avg
[i
] = avg_stats(&all_stats
[i
]);
287 stddev
[i
] = stddev_stats(&all_stats
[i
]);
290 printf("\nAveraged %ld ADD operations (+- %.2f%%)\n",
291 avg
[OP_EPOLL_ADD
], rel_stddev_stats(stddev
[OP_EPOLL_ADD
],
293 printf("Averaged %ld MOD operations (+- %.2f%%)\n",
294 avg
[OP_EPOLL_MOD
], rel_stddev_stats(stddev
[OP_EPOLL_MOD
],
296 printf("Averaged %ld DEL operations (+- %.2f%%)\n",
297 avg
[OP_EPOLL_DEL
], rel_stddev_stats(stddev
[OP_EPOLL_DEL
],
301 int bench_epoll_ctl(int argc
, const char **argv
)
304 struct sigaction act
;
305 struct worker
*worker
= NULL
;
306 struct perf_cpu_map
*cpu
;
307 struct rlimit rl
, prevrl
;
310 argc
= parse_options(argc
, argv
, options
, bench_epoll_ctl_usage
, 0);
312 usage_with_options(bench_epoll_ctl_usage
, options
);
316 sigfillset(&act
.sa_mask
);
317 act
.sa_sigaction
= toggle_done
;
318 sigaction(SIGINT
, &act
, NULL
);
320 cpu
= perf_cpu_map__new(NULL
);
324 /* a single, main epoll instance */
325 epollfd
= epoll_create(1);
327 err(EXIT_FAILURE
, "epoll_create");
330 * Deal with nested epolls, if any.
335 /* default to the number of CPUs */
339 worker
= calloc(nthreads
, sizeof(*worker
));
343 if (getrlimit(RLIMIT_NOFILE
, &prevrl
))
344 err(EXIT_FAILURE
, "getrlimit");
345 rl
.rlim_cur
= rl
.rlim_max
= nfds
* nthreads
* 2 + 50;
346 printinfo("Setting RLIMIT_NOFILE rlimit from %" PRIu64
" to: %" PRIu64
"\n",
347 (uint64_t)prevrl
.rlim_max
, (uint64_t)rl
.rlim_max
);
348 if (setrlimit(RLIMIT_NOFILE
, &rl
) < 0)
349 err(EXIT_FAILURE
, "setrlimit");
351 printf("Run summary [PID %d]: %d threads doing epoll_ctl ops "
352 "%d file-descriptors for %d secs.\n\n",
353 getpid(), nthreads
, nfds
, nsecs
);
355 for (i
= 0; i
< EPOLL_NR_OPS
; i
++)
356 init_stats(&all_stats
[i
]);
358 pthread_mutex_init(&thread_lock
, NULL
);
359 pthread_cond_init(&thread_parent
, NULL
);
360 pthread_cond_init(&thread_worker
, NULL
);
362 threads_starting
= nthreads
;
364 gettimeofday(&start
, NULL
);
366 do_threads(worker
, cpu
);
368 pthread_mutex_lock(&thread_lock
);
369 while (threads_starting
)
370 pthread_cond_wait(&thread_parent
, &thread_lock
);
371 pthread_cond_broadcast(&thread_worker
);
372 pthread_mutex_unlock(&thread_lock
);
375 toggle_done(0, NULL
, NULL
);
376 printinfo("main thread: toggling done\n");
378 for (i
= 0; i
< nthreads
; i
++) {
379 ret
= pthread_join(worker
[i
].thread
, NULL
);
381 err(EXIT_FAILURE
, "pthread_join");
384 /* cleanup & report results */
385 pthread_cond_destroy(&thread_parent
);
386 pthread_cond_destroy(&thread_worker
);
387 pthread_mutex_destroy(&thread_lock
);
389 for (i
= 0; i
< nthreads
; i
++) {
390 unsigned long t
[EPOLL_NR_OPS
];
392 for (j
= 0; j
< EPOLL_NR_OPS
; j
++) {
393 t
[j
] = worker
[i
].ops
[j
];
394 update_stats(&all_stats
[j
], t
[j
]);
398 printf("[thread %2d] fdmap: %p [ add: %04ld; mod: %04ld; del: %04lds ops ]\n",
399 worker
[i
].tid
, &worker
[i
].fdmap
[0],
400 t
[OP_EPOLL_ADD
], t
[OP_EPOLL_MOD
], t
[OP_EPOLL_DEL
]);
402 printf("[thread %2d] fdmap: %p ... %p [ add: %04ld ops; mod: %04ld ops; del: %04ld ops ]\n",
403 worker
[i
].tid
, &worker
[i
].fdmap
[0],
404 &worker
[i
].fdmap
[nfds
-1],
405 t
[OP_EPOLL_ADD
], t
[OP_EPOLL_MOD
], t
[OP_EPOLL_DEL
]);
413 err(EXIT_FAILURE
, "calloc");
415 #endif // HAVE_EVENTFD