Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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) #ifdef HAVE_EVENTFD_SUPPORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2018 Davidlohr Bueso.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * This program benchmarks concurrent epoll_wait(2) monitoring multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * file descriptors under one or two load balancing models. The first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * and default, is the single/combined queueing (which refers to a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * epoll instance for N worker threads):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *                          |---> [worker A]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *                          |---> [worker B]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *        [combined queue]  .---> [worker C]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *                          |---> [worker D]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *                          |---> [worker E]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * While the second model, enabled via --multiq option, uses multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * queueing (which refers to one epoll instance per worker). For example,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * short lived tcp connections in a high throughput httpd server will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * ditribute the accept()'ing  connections across CPUs. In this case each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * worker does a limited  amount of processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *             [queue A]  ---> [worker]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *             [queue B]  ---> [worker]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *             [queue C]  ---> [worker]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *             [queue D]  ---> [worker]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *             [queue E]  ---> [worker]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * Naturally, the single queue will enforce more concurrency on the epoll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * instance, and can therefore scale poorly compared to multiple queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * However, this is a benchmark raw data and must be taken with a grain of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * salt when choosing how to make use of sys_epoll.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * Each thread has a number of private, nonblocking file descriptors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * referred to as fdmap. A writer thread will constantly be writing to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * the fdmaps of all threads, minimizing each threads's chances of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * epoll_wait not finding any ready read events and blocking as this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * is not what we want to stress. The size of the fdmap can be adjusted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * by the user; enlarging the value will increase the chances of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * epoll_wait(2) blocking as the lineal writer thread will take "longer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * at least at a high level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * Note that because fds are private to each thread, this workload does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * not stress scenarios where multiple tasks are awoken per ready IO; ie:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * EPOLLEXCLUSIVE semantics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * The end result/metric is throughput: number of ops/second where an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * operation consists of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  *   epoll_wait(2) + [others]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  *        ... where [others] is the cost of re-adding the fd (EPOLLET),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  *            or rearming it (EPOLLONESHOT).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * The purpose of this is program is that it be useful for measuring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * kernel related changes to the sys_epoll, and not comparing different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * IO polling methods, for example. Hence everything is very adhoc and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * outputs raw microbenchmark numbers. Also this uses eventfd, similar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * tools tend to use pipes or sockets, but the result is the same.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) /* For the CLR_() macros */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #include <sys/epoll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #include <sys/eventfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #include <internal/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #include <perf/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #include "../util/stat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #include <subcmd/parse-options.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #include "bench.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #include <err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) #define printinfo(fmt, arg...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	do { if (__verbose) { printf(fmt, ## arg); fflush(stdout); } } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static unsigned int nthreads = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static unsigned int nsecs    = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static bool wdone, done, __verbose, randomize, nonblocking;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * epoll related shared variables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) /* Maximum number of nesting allowed inside epoll sets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define EPOLL_MAXNESTS 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int epollfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int *epollfdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static bool noaffinity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static unsigned int nested = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static bool et; /* edge-trigger */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static bool oneshot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static bool multiq; /* use an epoll instance per thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* amount of fds to monitor, per thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static unsigned int nfds = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static pthread_mutex_t thread_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static unsigned int threads_starting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static struct stats throughput_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static pthread_cond_t thread_parent, thread_worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct worker {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	int tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int epollfd; /* for --multiq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	pthread_t thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	unsigned long ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	int *fdmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static const struct option options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	/* general benchmark options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	OPT_UINTEGER('f', "nfds",    &nfds,  "Specify amount of file descriptors to monitor for each thread"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	OPT_BOOLEAN( 'n', "noaffinity",  &noaffinity,   "Disables CPU affinity"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	OPT_BOOLEAN('R', "randomize", &randomize,   "Enable random write behaviour (default is lineal)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	OPT_BOOLEAN( 'v', "verbose", &__verbose, "Verbose mode"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	/* epoll specific options */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	OPT_BOOLEAN( 'm', "multiq",  &multiq,   "Use multiple epoll instances (one per thread)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	OPT_BOOLEAN( 'B', "nonblocking", &nonblocking, "Nonblocking epoll_wait(2) behaviour"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	OPT_UINTEGER( 'N', "nested",  &nested,   "Nesting level epoll hierarchy (default is 0, no nesting)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	OPT_BOOLEAN( 'S', "oneshot",  &oneshot,   "Use EPOLLONESHOT semantics"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	OPT_BOOLEAN( 'E', "edge",  &et,   "Use Edge-triggered interface (default is LT)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static const char * const bench_epoll_wait_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	"perf bench epoll wait <options>",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * Arrange the N elements of ARRAY in random order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * Only effective if N is much smaller than RAND_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * if this may not be the case, use a better random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * number generator. -- Ben Pfaff.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void shuffle(void *array, size_t n, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	char *carray = array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	void *aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (n <= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	aux = calloc(1, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (!aux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		err(EXIT_FAILURE, "calloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	for (i = 1; i < n; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		size_t j =   i + rand() / (RAND_MAX / (n - i) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		j *= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		memcpy(aux, &carray[j], size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		memcpy(&carray[j], &carray[i*size], size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		memcpy(&carray[i*size], aux, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	free(aux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static void *workerfn(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	int fd, ret, r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct worker *w = (struct worker *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	unsigned long ops = w->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct epoll_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	uint64_t val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	int to = nonblocking? 0 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	int efd = multiq ? w->epollfd : epollfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	pthread_mutex_lock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	threads_starting--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (!threads_starting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		pthread_cond_signal(&thread_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	pthread_cond_wait(&thread_worker, &thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	pthread_mutex_unlock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		 * Block undefinitely waiting for the IN event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		 * In order to stress the epoll_wait(2) syscall,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		 * call it event per event, instead of a larger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		 * batch (max)limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			ret = epoll_wait(efd, &ev, 1, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		} while (ret < 0 && errno == EINTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			err(EXIT_FAILURE, "epoll_wait");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		fd = ev.data.fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			r = read(fd, &val, sizeof(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		} while (!done && (r < 0 && errno == EAGAIN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if (et) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			ev.events = EPOLLIN | EPOLLET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			ret = epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		if (oneshot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			/* rearm the file descriptor with a new event mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			ev.events |= EPOLLIN | EPOLLONESHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			ret = epoll_ctl(efd, EPOLL_CTL_MOD, fd, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		ops++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}  while (!done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (multiq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		close(w->epollfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	w->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static void nest_epollfd(struct worker *w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct epoll_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	int efd = multiq ? w->epollfd : epollfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (nested > EPOLL_MAXNESTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		nested = EPOLL_MAXNESTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	epollfdp = calloc(nested, sizeof(*epollfdp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (!epollfdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		err(EXIT_FAILURE, "calloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	for (i = 0; i < nested; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		epollfdp[i] = epoll_create(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (epollfdp[i] < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			err(EXIT_FAILURE, "epoll_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	ev.events = EPOLLHUP; /* anything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	ev.data.u64 = i; /* any number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	for (i = nested - 1; i; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		if (epoll_ctl(epollfdp[i - 1], EPOLL_CTL_ADD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			      epollfdp[i], &ev) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			err(EXIT_FAILURE, "epoll_ctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (epoll_ctl(efd, EPOLL_CTL_ADD, *epollfdp, &ev) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		err(EXIT_FAILURE, "epoll_ctl");
^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) static void toggle_done(int sig __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			siginfo_t *info __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			void *uc __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	/* inform all threads that we're done for the day */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	gettimeofday(&bench__end, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	timersub(&bench__end, &bench__start, &bench__runtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static void print_summary(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	unsigned long avg = avg_stats(&throughput_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	double stddev = stddev_stats(&throughput_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	printf("\nAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	       avg, rel_stddev_stats(stddev, avg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	       (int)bench__runtime.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static int do_threads(struct worker *worker, struct perf_cpu_map *cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	pthread_attr_t thread_attr, *attrp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	cpu_set_t cpuset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	unsigned int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	int ret = 0, events = EPOLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (oneshot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		events |= EPOLLONESHOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (et)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		events |= EPOLLET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	printinfo("starting worker/consumer %sthreads%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		  noaffinity ?  "":"CPU affinity ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		  nonblocking ? " (nonblocking)":"");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (!noaffinity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		pthread_attr_init(&thread_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	for (i = 0; i < nthreads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		struct worker *w = &worker[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (multiq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			w->epollfd = epoll_create(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			if (w->epollfd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 				err(EXIT_FAILURE, "epoll_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			if (nested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 				nest_epollfd(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		w->tid = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		w->fdmap = calloc(nfds, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		if (!w->fdmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		for (j = 0; j < nfds; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			int efd = multiq ? w->epollfd : epollfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			struct epoll_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			w->fdmap[j] = eventfd(0, EFD_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			if (w->fdmap[j] < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 				err(EXIT_FAILURE, "eventfd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			ev.data.fd = w->fdmap[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			ev.events = events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			ret = epoll_ctl(efd, EPOLL_CTL_ADD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 					w->fdmap[j], &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 				err(EXIT_FAILURE, "epoll_ctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		if (!noaffinity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			CPU_ZERO(&cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			CPU_SET(cpu->map[i % cpu->nr], &cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 				err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			attrp = &thread_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		ret = pthread_create(&w->thread, attrp, workerfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				     (void *)(struct worker *) w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			err(EXIT_FAILURE, "pthread_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (!noaffinity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		pthread_attr_destroy(&thread_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static void *writerfn(void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct worker *worker = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	size_t i, j, iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	const uint64_t val = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	ssize_t sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	struct timespec ts = { .tv_sec = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			       .tv_nsec = 500 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	printinfo("starting writer-thread: doing %s writes ...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		  randomize? "random":"lineal");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	for (iter = 0; !wdone; iter++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		if (randomize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			shuffle((void *)worker, nthreads, sizeof(*worker));
^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) 		for (i = 0; i < nthreads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 			struct worker *w = &worker[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			if (randomize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 				shuffle((void *)w->fdmap, nfds, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			for (j = 0; j < nfds; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 				do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 					sz = write(w->fdmap[j], &val, sizeof(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 				} while (!wdone && (sz < 0 && errno == EAGAIN));
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		nanosleep(&ts, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	printinfo("exiting writer-thread (total full-loops: %zd)\n", iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static int cmpworker(const void *p1, const void *p2)
^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) 	struct worker *w1 = (struct worker *) p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	struct worker *w2 = (struct worker *) p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return w1->tid > w2->tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) int bench_epoll_wait(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	struct sigaction act;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	struct worker *worker = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	struct perf_cpu_map *cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	pthread_t wthread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	struct rlimit rl, prevrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	argc = parse_options(argc, argv, options, bench_epoll_wait_usage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		usage_with_options(bench_epoll_wait_usage, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	memset(&act, 0, sizeof(act));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	sigfillset(&act.sa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	act.sa_sigaction = toggle_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	sigaction(SIGINT, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	cpu = perf_cpu_map__new(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (!cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		goto errmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	/* a single, main epoll instance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if (!multiq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		epollfd = epoll_create(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		if (epollfd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			err(EXIT_FAILURE, "epoll_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		 * Deal with nested epolls, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		if (nested)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			nest_epollfd(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	printinfo("Using %s queue model\n", multiq ? "multi" : "single");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	printinfo("Nesting level(s): %d\n", nested);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	/* default to the number of CPUs and leave one for the writer pthread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	if (!nthreads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		nthreads = cpu->nr - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	worker = calloc(nthreads, sizeof(*worker));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (!worker) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		goto errmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (getrlimit(RLIMIT_NOFILE, &prevrl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		err(EXIT_FAILURE, "getrlimit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	rl.rlim_cur = rl.rlim_max = nfds * nthreads * 2 + 50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	printinfo("Setting RLIMIT_NOFILE rlimit from %" PRIu64 " to: %" PRIu64 "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		  (uint64_t)prevrl.rlim_max, (uint64_t)rl.rlim_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		err(EXIT_FAILURE, "setrlimit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	printf("Run summary [PID %d]: %d threads monitoring%s on "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	       "%d file-descriptors for %d secs.\n\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	       getpid(), nthreads, oneshot ? " (EPOLLONESHOT semantics)": "", nfds, nsecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	init_stats(&throughput_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	pthread_mutex_init(&thread_lock, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	pthread_cond_init(&thread_parent, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	pthread_cond_init(&thread_worker, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	threads_starting = nthreads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	gettimeofday(&bench__start, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	do_threads(worker, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	pthread_mutex_lock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	while (threads_starting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		pthread_cond_wait(&thread_parent, &thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	pthread_cond_broadcast(&thread_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	pthread_mutex_unlock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	 * At this point the workers should be blocked waiting for read events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	 * to become ready. Launch the writer which will constantly be writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	 * to each thread's fdmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	ret = pthread_create(&wthread, NULL, writerfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			     (void *)(struct worker *) worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		err(EXIT_FAILURE, "pthread_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	sleep(nsecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	toggle_done(0, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	printinfo("main thread: toggling done\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	sleep(1); /* meh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	wdone = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	ret = pthread_join(wthread, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		err(EXIT_FAILURE, "pthread_join");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	/* cleanup & report results */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	pthread_cond_destroy(&thread_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	pthread_cond_destroy(&thread_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	pthread_mutex_destroy(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	/* sort the array back before reporting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (randomize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		qsort(worker, nthreads, sizeof(struct worker), cmpworker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	for (i = 0; i < nthreads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		unsigned long t = bench__runtime.tv_sec > 0 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 			worker[i].ops / bench__runtime.tv_sec : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		update_stats(&throughput_stats, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		if (nfds == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			printf("[thread %2d] fdmap: %p [ %04ld ops/sec ]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			       worker[i].tid, &worker[i].fdmap[0], t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 			printf("[thread %2d] fdmap: %p ... %p [ %04ld ops/sec ]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			       worker[i].tid, &worker[i].fdmap[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			       &worker[i].fdmap[nfds-1], t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	print_summary();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	close(epollfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) errmem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	err(EXIT_FAILURE, "calloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) #endif // HAVE_EVENTFD_SUPPORT