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