^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2018 Davidlohr Bueso.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Benchmark the various operations allowed for epoll_ctl(2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * The idea is to concurrently stress a single epoll instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #ifdef HAVE_EVENTFD_SUPPORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /* For the CLR_() macros */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <sys/epoll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <sys/eventfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <internal/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <perf/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "../util/stat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <subcmd/parse-options.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "bench.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define printinfo(fmt, arg...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) do { if (__verbose) printf(fmt, ## arg); } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static unsigned int nthreads = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static unsigned int nsecs = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static bool done, __verbose, randomize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * epoll related shared variables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Maximum number of nesting allowed inside epoll sets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define EPOLL_MAXNESTS 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) OP_EPOLL_ADD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) OP_EPOLL_MOD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) OP_EPOLL_DEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) EPOLL_NR_OPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static int epollfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int *epollfdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static bool noaffinity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static unsigned int nested = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* amount of fds to monitor, per thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static unsigned int nfds = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static pthread_mutex_t thread_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static unsigned int threads_starting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static struct stats all_stats[EPOLL_NR_OPS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static pthread_cond_t thread_parent, thread_worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct worker {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) pthread_t thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned long ops[EPOLL_NR_OPS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int *fdmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static const struct option options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) OPT_UINTEGER('f', "nfds", &nfds, "Specify amount of file descriptors to monitor for each thread"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) OPT_BOOLEAN( 'n', "noaffinity", &noaffinity, "Disables CPU affinity"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) OPT_UINTEGER( 'N', "nested", &nested, "Nesting level epoll hierarchy (default is 0, no nesting)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) OPT_BOOLEAN( 'R', "randomize", &randomize, "Perform random operations on random fds"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) OPT_BOOLEAN( 'v', "verbose", &__verbose, "Verbose mode"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static const char * const bench_epoll_ctl_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) "perf bench epoll ctl <options>",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static void toggle_done(int sig __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) siginfo_t *info __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) void *uc __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* inform all threads that we're done for the day */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) gettimeofday(&bench__end, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) timersub(&bench__end, &bench__start, &bench__runtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static void nest_epollfd(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct epoll_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (nested > EPOLL_MAXNESTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) nested = EPOLL_MAXNESTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) printinfo("Nesting level(s): %d\n", nested);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) epollfdp = calloc(nested, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!epollfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) err(EXIT_FAILURE, "calloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) for (i = 0; i < nested; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) epollfdp[i] = epoll_create(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (epollfd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) err(EXIT_FAILURE, "epoll_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ev.events = EPOLLHUP; /* anything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ev.data.u64 = i; /* any number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) for (i = nested - 1; i; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (epoll_ctl(epollfdp[i - 1], EPOLL_CTL_ADD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) epollfdp[i], &ev) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) err(EXIT_FAILURE, "epoll_ctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (epoll_ctl(epollfd, EPOLL_CTL_ADD, *epollfdp, &ev) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) err(EXIT_FAILURE, "epoll_ctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static inline void do_epoll_op(struct worker *w, int op, int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct epoll_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ev.events = EPOLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ev.data.u64 = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) switch (op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) case OP_EPOLL_ADD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) error = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) case OP_EPOLL_MOD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ev.events = EPOLLOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) error = epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) case OP_EPOLL_DEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) error = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) error = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) w->ops[op]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static inline void do_random_epoll_op(struct worker *w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) unsigned long rnd1 = random(), rnd2 = random();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int op, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) fd = w->fdmap[rnd1 % nfds];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) op = rnd2 % EPOLL_NR_OPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) do_epoll_op(w, op, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void *workerfn(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct worker *w = (struct worker *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct timespec ts = { .tv_sec = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .tv_nsec = 250 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pthread_mutex_lock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) threads_starting--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!threads_starting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) pthread_cond_signal(&thread_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) pthread_cond_wait(&thread_worker, &thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) pthread_mutex_unlock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /* Let 'em loose */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /* random */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (randomize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) do_random_epoll_op(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) for (i = 0; i < nfds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) do_epoll_op(w, OP_EPOLL_ADD, w->fdmap[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) do_epoll_op(w, OP_EPOLL_MOD, w->fdmap[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) do_epoll_op(w, OP_EPOLL_DEL, w->fdmap[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) nanosleep(&ts, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) } while (!done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static void init_fdmaps(struct worker *w, int pct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) int inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct epoll_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (!pct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) inc = 100/pct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) for (i = 0; i < nfds; i+=inc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ev.data.fd = w->fdmap[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ev.events = EPOLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (epoll_ctl(epollfd, EPOLL_CTL_ADD, w->fdmap[i], &ev) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) err(EXIT_FAILURE, "epoll_ct");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int do_threads(struct worker *worker, struct perf_cpu_map *cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) pthread_attr_t thread_attr, *attrp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) cpu_set_t cpuset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) unsigned int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!noaffinity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) pthread_attr_init(&thread_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) for (i = 0; i < nthreads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct worker *w = &worker[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) w->tid = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) w->fdmap = calloc(nfds, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!w->fdmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) for (j = 0; j < nfds; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) w->fdmap[j] = eventfd(0, EFD_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (w->fdmap[j] < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) err(EXIT_FAILURE, "eventfd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * Lets add 50% of the fdmap to the epoll instance, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * do it before any threads are started; otherwise there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * an initial bias of the call failing (mod and del ops).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (randomize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) init_fdmaps(w, 50);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (!noaffinity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) CPU_ZERO(&cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) CPU_SET(cpu->map[i % cpu->nr], &cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) attrp = &thread_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ret = pthread_create(&w->thread, attrp, workerfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) (void *)(struct worker *) w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) err(EXIT_FAILURE, "pthread_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!noaffinity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) pthread_attr_destroy(&thread_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static void print_summary(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) unsigned long avg[EPOLL_NR_OPS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) double stddev[EPOLL_NR_OPS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) for (i = 0; i < EPOLL_NR_OPS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) avg[i] = avg_stats(&all_stats[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) stddev[i] = stddev_stats(&all_stats[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) printf("\nAveraged %ld ADD operations (+- %.2f%%)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) avg[OP_EPOLL_ADD], rel_stddev_stats(stddev[OP_EPOLL_ADD],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) avg[OP_EPOLL_ADD]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) printf("Averaged %ld MOD operations (+- %.2f%%)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) avg[OP_EPOLL_MOD], rel_stddev_stats(stddev[OP_EPOLL_MOD],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) avg[OP_EPOLL_MOD]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) printf("Averaged %ld DEL operations (+- %.2f%%)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) avg[OP_EPOLL_DEL], rel_stddev_stats(stddev[OP_EPOLL_DEL],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) avg[OP_EPOLL_DEL]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int bench_epoll_ctl(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) int j, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct sigaction act;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct worker *worker = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct perf_cpu_map *cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct rlimit rl, prevrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) argc = parse_options(argc, argv, options, bench_epoll_ctl_usage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) usage_with_options(bench_epoll_ctl_usage, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) memset(&act, 0, sizeof(act));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) sigfillset(&act.sa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) act.sa_sigaction = toggle_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) sigaction(SIGINT, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) cpu = perf_cpu_map__new(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (!cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) goto errmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /* a single, main epoll instance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) epollfd = epoll_create(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (epollfd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) err(EXIT_FAILURE, "epoll_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * Deal with nested epolls, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (nested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) nest_epollfd();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /* default to the number of CPUs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (!nthreads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) nthreads = cpu->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) worker = calloc(nthreads, sizeof(*worker));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (!worker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) goto errmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (getrlimit(RLIMIT_NOFILE, &prevrl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) err(EXIT_FAILURE, "getrlimit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) rl.rlim_cur = rl.rlim_max = nfds * nthreads * 2 + 50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) printinfo("Setting RLIMIT_NOFILE rlimit from %" PRIu64 " to: %" PRIu64 "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) (uint64_t)prevrl.rlim_max, (uint64_t)rl.rlim_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) err(EXIT_FAILURE, "setrlimit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) printf("Run summary [PID %d]: %d threads doing epoll_ctl ops "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) "%d file-descriptors for %d secs.\n\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) getpid(), nthreads, nfds, nsecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) for (i = 0; i < EPOLL_NR_OPS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) init_stats(&all_stats[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) pthread_mutex_init(&thread_lock, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) pthread_cond_init(&thread_parent, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) pthread_cond_init(&thread_worker, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) threads_starting = nthreads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) gettimeofday(&bench__start, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) do_threads(worker, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) pthread_mutex_lock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) while (threads_starting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) pthread_cond_wait(&thread_parent, &thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) pthread_cond_broadcast(&thread_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) pthread_mutex_unlock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) sleep(nsecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) toggle_done(0, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) printinfo("main thread: toggling done\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) for (i = 0; i < nthreads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) ret = pthread_join(worker[i].thread, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) err(EXIT_FAILURE, "pthread_join");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /* cleanup & report results */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) pthread_cond_destroy(&thread_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) pthread_cond_destroy(&thread_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) pthread_mutex_destroy(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) for (i = 0; i < nthreads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) unsigned long t[EPOLL_NR_OPS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) for (j = 0; j < EPOLL_NR_OPS; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) t[j] = worker[i].ops[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) update_stats(&all_stats[j], t[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (nfds == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) printf("[thread %2d] fdmap: %p [ add: %04ld; mod: %04ld; del: %04lds ops ]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) worker[i].tid, &worker[i].fdmap[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) t[OP_EPOLL_ADD], t[OP_EPOLL_MOD], t[OP_EPOLL_DEL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) printf("[thread %2d] fdmap: %p ... %p [ add: %04ld ops; mod: %04ld ops; del: %04ld ops ]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) worker[i].tid, &worker[i].fdmap[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) &worker[i].fdmap[nfds-1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) t[OP_EPOLL_ADD], t[OP_EPOLL_MOD], t[OP_EPOLL_DEL]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) print_summary();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) close(epollfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) errmem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) err(EXIT_FAILURE, "calloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) #endif // HAVE_EVENTFD_SUPPORT