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