18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2018 Davidlohr Bueso.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Benchmark the various operations allowed for epoll_ctl(2).
68c2ecf20Sopenharmony_ci * The idea is to concurrently stress a single epoll instance
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#ifdef HAVE_EVENTFD_SUPPORT
98c2ecf20Sopenharmony_ci/* For the CLR_() macros */
108c2ecf20Sopenharmony_ci#include <string.h>
118c2ecf20Sopenharmony_ci#include <pthread.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <errno.h>
148c2ecf20Sopenharmony_ci#include <inttypes.h>
158c2ecf20Sopenharmony_ci#include <signal.h>
168c2ecf20Sopenharmony_ci#include <stdlib.h>
178c2ecf20Sopenharmony_ci#include <unistd.h>
188c2ecf20Sopenharmony_ci#include <linux/compiler.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <sys/time.h>
218c2ecf20Sopenharmony_ci#include <sys/resource.h>
228c2ecf20Sopenharmony_ci#include <sys/epoll.h>
238c2ecf20Sopenharmony_ci#include <sys/eventfd.h>
248c2ecf20Sopenharmony_ci#include <internal/cpumap.h>
258c2ecf20Sopenharmony_ci#include <perf/cpumap.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include "../util/stat.h"
288c2ecf20Sopenharmony_ci#include <subcmd/parse-options.h>
298c2ecf20Sopenharmony_ci#include "bench.h"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <err.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define printinfo(fmt, arg...) \
348c2ecf20Sopenharmony_ci	do { if (__verbose) printf(fmt, ## arg); } while (0)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic unsigned int nthreads = 0;
378c2ecf20Sopenharmony_cistatic unsigned int nsecs    = 8;
388c2ecf20Sopenharmony_cistatic bool done, __verbose, randomize;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * epoll related shared variables.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* Maximum number of nesting allowed inside epoll sets */
458c2ecf20Sopenharmony_ci#define EPOLL_MAXNESTS 4
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cienum {
488c2ecf20Sopenharmony_ci	OP_EPOLL_ADD,
498c2ecf20Sopenharmony_ci	OP_EPOLL_MOD,
508c2ecf20Sopenharmony_ci	OP_EPOLL_DEL,
518c2ecf20Sopenharmony_ci	EPOLL_NR_OPS,
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int epollfd;
558c2ecf20Sopenharmony_cistatic int *epollfdp;
568c2ecf20Sopenharmony_cistatic bool noaffinity;
578c2ecf20Sopenharmony_cistatic unsigned int nested = 0;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* amount of fds to monitor, per thread */
608c2ecf20Sopenharmony_cistatic unsigned int nfds = 64;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic pthread_mutex_t thread_lock;
638c2ecf20Sopenharmony_cistatic unsigned int threads_starting;
648c2ecf20Sopenharmony_cistatic struct stats all_stats[EPOLL_NR_OPS];
658c2ecf20Sopenharmony_cistatic pthread_cond_t thread_parent, thread_worker;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistruct worker {
688c2ecf20Sopenharmony_ci	int tid;
698c2ecf20Sopenharmony_ci	pthread_t thread;
708c2ecf20Sopenharmony_ci	unsigned long ops[EPOLL_NR_OPS];
718c2ecf20Sopenharmony_ci	int *fdmap;
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic const struct option options[] = {
758c2ecf20Sopenharmony_ci	OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
768c2ecf20Sopenharmony_ci	OPT_UINTEGER('r', "runtime", &nsecs,    "Specify runtime (in seconds)"),
778c2ecf20Sopenharmony_ci	OPT_UINTEGER('f', "nfds", &nfds, "Specify amount of file descriptors to monitor for each thread"),
788c2ecf20Sopenharmony_ci	OPT_BOOLEAN( 'n', "noaffinity",  &noaffinity,   "Disables CPU affinity"),
798c2ecf20Sopenharmony_ci	OPT_UINTEGER( 'N', "nested",  &nested,   "Nesting level epoll hierarchy (default is 0, no nesting)"),
808c2ecf20Sopenharmony_ci	OPT_BOOLEAN( 'R', "randomize", &randomize,   "Perform random operations on random fds"),
818c2ecf20Sopenharmony_ci	OPT_BOOLEAN( 'v', "verbose",  &__verbose,   "Verbose mode"),
828c2ecf20Sopenharmony_ci	OPT_END()
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic const char * const bench_epoll_ctl_usage[] = {
868c2ecf20Sopenharmony_ci	"perf bench epoll ctl <options>",
878c2ecf20Sopenharmony_ci	NULL
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic void toggle_done(int sig __maybe_unused,
918c2ecf20Sopenharmony_ci			siginfo_t *info __maybe_unused,
928c2ecf20Sopenharmony_ci			void *uc __maybe_unused)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	/* inform all threads that we're done for the day */
958c2ecf20Sopenharmony_ci	done = true;
968c2ecf20Sopenharmony_ci	gettimeofday(&bench__end, NULL);
978c2ecf20Sopenharmony_ci	timersub(&bench__end, &bench__start, &bench__runtime);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic void nest_epollfd(void)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	unsigned int i;
1038c2ecf20Sopenharmony_ci	struct epoll_event ev;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (nested > EPOLL_MAXNESTS)
1068c2ecf20Sopenharmony_ci		nested = EPOLL_MAXNESTS;
1078c2ecf20Sopenharmony_ci	printinfo("Nesting level(s): %d\n", nested);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	epollfdp = calloc(nested, sizeof(int));
1108c2ecf20Sopenharmony_ci	if (!epollfd)
1118c2ecf20Sopenharmony_ci		err(EXIT_FAILURE, "calloc");
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	for (i = 0; i < nested; i++) {
1148c2ecf20Sopenharmony_ci		epollfdp[i] = epoll_create(1);
1158c2ecf20Sopenharmony_ci		if (epollfd < 0)
1168c2ecf20Sopenharmony_ci			err(EXIT_FAILURE, "epoll_create");
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	ev.events = EPOLLHUP; /* anything */
1208c2ecf20Sopenharmony_ci	ev.data.u64 = i; /* any number */
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	for (i = nested - 1; i; i--) {
1238c2ecf20Sopenharmony_ci		if (epoll_ctl(epollfdp[i - 1], EPOLL_CTL_ADD,
1248c2ecf20Sopenharmony_ci			      epollfdp[i], &ev) < 0)
1258c2ecf20Sopenharmony_ci			err(EXIT_FAILURE, "epoll_ctl");
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, *epollfdp, &ev) < 0)
1298c2ecf20Sopenharmony_ci		err(EXIT_FAILURE, "epoll_ctl");
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic inline void do_epoll_op(struct worker *w, int op, int fd)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	int error;
1358c2ecf20Sopenharmony_ci	struct epoll_event ev;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	ev.events = EPOLLIN;
1388c2ecf20Sopenharmony_ci	ev.data.u64 = fd;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	switch (op) {
1418c2ecf20Sopenharmony_ci	case OP_EPOLL_ADD:
1428c2ecf20Sopenharmony_ci		error = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
1438c2ecf20Sopenharmony_ci		break;
1448c2ecf20Sopenharmony_ci	case OP_EPOLL_MOD:
1458c2ecf20Sopenharmony_ci		ev.events = EPOLLOUT;
1468c2ecf20Sopenharmony_ci		error = epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &ev);
1478c2ecf20Sopenharmony_ci		break;
1488c2ecf20Sopenharmony_ci	case OP_EPOLL_DEL:
1498c2ecf20Sopenharmony_ci		error = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);
1508c2ecf20Sopenharmony_ci		break;
1518c2ecf20Sopenharmony_ci	default:
1528c2ecf20Sopenharmony_ci		error = 1;
1538c2ecf20Sopenharmony_ci		break;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (!error)
1578c2ecf20Sopenharmony_ci		w->ops[op]++;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic inline void do_random_epoll_op(struct worker *w)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	unsigned long rnd1 = random(), rnd2 = random();
1638c2ecf20Sopenharmony_ci	int op, fd;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	fd = w->fdmap[rnd1 % nfds];
1668c2ecf20Sopenharmony_ci	op = rnd2 % EPOLL_NR_OPS;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	do_epoll_op(w, op, fd);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic void *workerfn(void *arg)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	unsigned int i;
1748c2ecf20Sopenharmony_ci	struct worker *w = (struct worker *) arg;
1758c2ecf20Sopenharmony_ci	struct timespec ts = { .tv_sec = 0,
1768c2ecf20Sopenharmony_ci			       .tv_nsec = 250 };
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	pthread_mutex_lock(&thread_lock);
1798c2ecf20Sopenharmony_ci	threads_starting--;
1808c2ecf20Sopenharmony_ci	if (!threads_starting)
1818c2ecf20Sopenharmony_ci		pthread_cond_signal(&thread_parent);
1828c2ecf20Sopenharmony_ci	pthread_cond_wait(&thread_worker, &thread_lock);
1838c2ecf20Sopenharmony_ci	pthread_mutex_unlock(&thread_lock);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* Let 'em loose */
1868c2ecf20Sopenharmony_ci	do {
1878c2ecf20Sopenharmony_ci		/* random */
1888c2ecf20Sopenharmony_ci		if (randomize) {
1898c2ecf20Sopenharmony_ci			do_random_epoll_op(w);
1908c2ecf20Sopenharmony_ci		} else {
1918c2ecf20Sopenharmony_ci			for (i = 0; i < nfds; i++) {
1928c2ecf20Sopenharmony_ci				do_epoll_op(w, OP_EPOLL_ADD, w->fdmap[i]);
1938c2ecf20Sopenharmony_ci				do_epoll_op(w, OP_EPOLL_MOD, w->fdmap[i]);
1948c2ecf20Sopenharmony_ci				do_epoll_op(w, OP_EPOLL_DEL, w->fdmap[i]);
1958c2ecf20Sopenharmony_ci			}
1968c2ecf20Sopenharmony_ci		}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci		nanosleep(&ts, NULL);
1998c2ecf20Sopenharmony_ci	}  while (!done);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	return NULL;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic void init_fdmaps(struct worker *w, int pct)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	unsigned int i;
2078c2ecf20Sopenharmony_ci	int inc;
2088c2ecf20Sopenharmony_ci	struct epoll_event ev;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	if (!pct)
2118c2ecf20Sopenharmony_ci		return;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	inc = 100/pct;
2148c2ecf20Sopenharmony_ci	for (i = 0; i < nfds; i+=inc) {
2158c2ecf20Sopenharmony_ci		ev.data.fd = w->fdmap[i];
2168c2ecf20Sopenharmony_ci		ev.events = EPOLLIN;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci		if (epoll_ctl(epollfd, EPOLL_CTL_ADD, w->fdmap[i], &ev) < 0)
2198c2ecf20Sopenharmony_ci			err(EXIT_FAILURE, "epoll_ct");
2208c2ecf20Sopenharmony_ci	}
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic int do_threads(struct worker *worker, struct perf_cpu_map *cpu)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	pthread_attr_t thread_attr, *attrp = NULL;
2268c2ecf20Sopenharmony_ci	cpu_set_t cpuset;
2278c2ecf20Sopenharmony_ci	unsigned int i, j;
2288c2ecf20Sopenharmony_ci	int ret = 0;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	if (!noaffinity)
2318c2ecf20Sopenharmony_ci		pthread_attr_init(&thread_attr);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	for (i = 0; i < nthreads; i++) {
2348c2ecf20Sopenharmony_ci		struct worker *w = &worker[i];
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci		w->tid = i;
2378c2ecf20Sopenharmony_ci		w->fdmap = calloc(nfds, sizeof(int));
2388c2ecf20Sopenharmony_ci		if (!w->fdmap)
2398c2ecf20Sopenharmony_ci			return 1;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		for (j = 0; j < nfds; j++) {
2428c2ecf20Sopenharmony_ci			w->fdmap[j] = eventfd(0, EFD_NONBLOCK);
2438c2ecf20Sopenharmony_ci			if (w->fdmap[j] < 0)
2448c2ecf20Sopenharmony_ci				err(EXIT_FAILURE, "eventfd");
2458c2ecf20Sopenharmony_ci		}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci		/*
2488c2ecf20Sopenharmony_ci		 * Lets add 50% of the fdmap to the epoll instance, and
2498c2ecf20Sopenharmony_ci		 * do it before any threads are started; otherwise there is
2508c2ecf20Sopenharmony_ci		 * an initial bias of the call failing  (mod and del ops).
2518c2ecf20Sopenharmony_ci		 */
2528c2ecf20Sopenharmony_ci		if (randomize)
2538c2ecf20Sopenharmony_ci			init_fdmaps(w, 50);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci		if (!noaffinity) {
2568c2ecf20Sopenharmony_ci			CPU_ZERO(&cpuset);
2578c2ecf20Sopenharmony_ci			CPU_SET(cpu->map[i % cpu->nr], &cpuset);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci			ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset);
2608c2ecf20Sopenharmony_ci			if (ret)
2618c2ecf20Sopenharmony_ci				err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci			attrp = &thread_attr;
2648c2ecf20Sopenharmony_ci		}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci		ret = pthread_create(&w->thread, attrp, workerfn,
2678c2ecf20Sopenharmony_ci				     (void *)(struct worker *) w);
2688c2ecf20Sopenharmony_ci		if (ret)
2698c2ecf20Sopenharmony_ci			err(EXIT_FAILURE, "pthread_create");
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	if (!noaffinity)
2738c2ecf20Sopenharmony_ci		pthread_attr_destroy(&thread_attr);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	return ret;
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic void print_summary(void)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	int i;
2818c2ecf20Sopenharmony_ci	unsigned long avg[EPOLL_NR_OPS];
2828c2ecf20Sopenharmony_ci	double stddev[EPOLL_NR_OPS];
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	for (i = 0; i < EPOLL_NR_OPS; i++) {
2858c2ecf20Sopenharmony_ci		avg[i] = avg_stats(&all_stats[i]);
2868c2ecf20Sopenharmony_ci		stddev[i] = stddev_stats(&all_stats[i]);
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	printf("\nAveraged %ld ADD operations (+- %.2f%%)\n",
2908c2ecf20Sopenharmony_ci	       avg[OP_EPOLL_ADD], rel_stddev_stats(stddev[OP_EPOLL_ADD],
2918c2ecf20Sopenharmony_ci						   avg[OP_EPOLL_ADD]));
2928c2ecf20Sopenharmony_ci	printf("Averaged %ld MOD operations (+- %.2f%%)\n",
2938c2ecf20Sopenharmony_ci	       avg[OP_EPOLL_MOD], rel_stddev_stats(stddev[OP_EPOLL_MOD],
2948c2ecf20Sopenharmony_ci						   avg[OP_EPOLL_MOD]));
2958c2ecf20Sopenharmony_ci	printf("Averaged %ld DEL operations (+- %.2f%%)\n",
2968c2ecf20Sopenharmony_ci	       avg[OP_EPOLL_DEL], rel_stddev_stats(stddev[OP_EPOLL_DEL],
2978c2ecf20Sopenharmony_ci						   avg[OP_EPOLL_DEL]));
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ciint bench_epoll_ctl(int argc, const char **argv)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	int j, ret = 0;
3038c2ecf20Sopenharmony_ci	struct sigaction act;
3048c2ecf20Sopenharmony_ci	struct worker *worker = NULL;
3058c2ecf20Sopenharmony_ci	struct perf_cpu_map *cpu;
3068c2ecf20Sopenharmony_ci	struct rlimit rl, prevrl;
3078c2ecf20Sopenharmony_ci	unsigned int i;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	argc = parse_options(argc, argv, options, bench_epoll_ctl_usage, 0);
3108c2ecf20Sopenharmony_ci	if (argc) {
3118c2ecf20Sopenharmony_ci		usage_with_options(bench_epoll_ctl_usage, options);
3128c2ecf20Sopenharmony_ci		exit(EXIT_FAILURE);
3138c2ecf20Sopenharmony_ci	}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	memset(&act, 0, sizeof(act));
3168c2ecf20Sopenharmony_ci	sigfillset(&act.sa_mask);
3178c2ecf20Sopenharmony_ci	act.sa_sigaction = toggle_done;
3188c2ecf20Sopenharmony_ci	sigaction(SIGINT, &act, NULL);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	cpu = perf_cpu_map__new(NULL);
3218c2ecf20Sopenharmony_ci	if (!cpu)
3228c2ecf20Sopenharmony_ci		goto errmem;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	/* a single, main epoll instance */
3258c2ecf20Sopenharmony_ci	epollfd = epoll_create(1);
3268c2ecf20Sopenharmony_ci	if (epollfd < 0)
3278c2ecf20Sopenharmony_ci		err(EXIT_FAILURE, "epoll_create");
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	/*
3308c2ecf20Sopenharmony_ci	 * Deal with nested epolls, if any.
3318c2ecf20Sopenharmony_ci	 */
3328c2ecf20Sopenharmony_ci	if (nested)
3338c2ecf20Sopenharmony_ci		nest_epollfd();
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/* default to the number of CPUs */
3368c2ecf20Sopenharmony_ci	if (!nthreads)
3378c2ecf20Sopenharmony_ci		nthreads = cpu->nr;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	worker = calloc(nthreads, sizeof(*worker));
3408c2ecf20Sopenharmony_ci	if (!worker)
3418c2ecf20Sopenharmony_ci		goto errmem;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	if (getrlimit(RLIMIT_NOFILE, &prevrl))
3448c2ecf20Sopenharmony_ci	    err(EXIT_FAILURE, "getrlimit");
3458c2ecf20Sopenharmony_ci	rl.rlim_cur = rl.rlim_max = nfds * nthreads * 2 + 50;
3468c2ecf20Sopenharmony_ci	printinfo("Setting RLIMIT_NOFILE rlimit from %" PRIu64 " to: %" PRIu64 "\n",
3478c2ecf20Sopenharmony_ci		  (uint64_t)prevrl.rlim_max, (uint64_t)rl.rlim_max);
3488c2ecf20Sopenharmony_ci	if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
3498c2ecf20Sopenharmony_ci		err(EXIT_FAILURE, "setrlimit");
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	printf("Run summary [PID %d]: %d threads doing epoll_ctl ops "
3528c2ecf20Sopenharmony_ci	       "%d file-descriptors for %d secs.\n\n",
3538c2ecf20Sopenharmony_ci	       getpid(), nthreads, nfds, nsecs);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	for (i = 0; i < EPOLL_NR_OPS; i++)
3568c2ecf20Sopenharmony_ci		init_stats(&all_stats[i]);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	pthread_mutex_init(&thread_lock, NULL);
3598c2ecf20Sopenharmony_ci	pthread_cond_init(&thread_parent, NULL);
3608c2ecf20Sopenharmony_ci	pthread_cond_init(&thread_worker, NULL);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	threads_starting = nthreads;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	gettimeofday(&bench__start, NULL);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	do_threads(worker, cpu);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	pthread_mutex_lock(&thread_lock);
3698c2ecf20Sopenharmony_ci	while (threads_starting)
3708c2ecf20Sopenharmony_ci		pthread_cond_wait(&thread_parent, &thread_lock);
3718c2ecf20Sopenharmony_ci	pthread_cond_broadcast(&thread_worker);
3728c2ecf20Sopenharmony_ci	pthread_mutex_unlock(&thread_lock);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	sleep(nsecs);
3758c2ecf20Sopenharmony_ci	toggle_done(0, NULL, NULL);
3768c2ecf20Sopenharmony_ci	printinfo("main thread: toggling done\n");
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	for (i = 0; i < nthreads; i++) {
3798c2ecf20Sopenharmony_ci		ret = pthread_join(worker[i].thread, NULL);
3808c2ecf20Sopenharmony_ci		if (ret)
3818c2ecf20Sopenharmony_ci			err(EXIT_FAILURE, "pthread_join");
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* cleanup & report results */
3858c2ecf20Sopenharmony_ci	pthread_cond_destroy(&thread_parent);
3868c2ecf20Sopenharmony_ci	pthread_cond_destroy(&thread_worker);
3878c2ecf20Sopenharmony_ci	pthread_mutex_destroy(&thread_lock);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	for (i = 0; i < nthreads; i++) {
3908c2ecf20Sopenharmony_ci		unsigned long t[EPOLL_NR_OPS];
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci		for (j = 0; j < EPOLL_NR_OPS; j++) {
3938c2ecf20Sopenharmony_ci			t[j] = worker[i].ops[j];
3948c2ecf20Sopenharmony_ci			update_stats(&all_stats[j], t[j]);
3958c2ecf20Sopenharmony_ci		}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci		if (nfds == 1)
3988c2ecf20Sopenharmony_ci			printf("[thread %2d] fdmap: %p [ add: %04ld; mod: %04ld; del: %04lds ops ]\n",
3998c2ecf20Sopenharmony_ci			       worker[i].tid, &worker[i].fdmap[0],
4008c2ecf20Sopenharmony_ci			       t[OP_EPOLL_ADD], t[OP_EPOLL_MOD], t[OP_EPOLL_DEL]);
4018c2ecf20Sopenharmony_ci		else
4028c2ecf20Sopenharmony_ci			printf("[thread %2d] fdmap: %p ... %p [ add: %04ld ops; mod: %04ld ops; del: %04ld ops ]\n",
4038c2ecf20Sopenharmony_ci			       worker[i].tid, &worker[i].fdmap[0],
4048c2ecf20Sopenharmony_ci			       &worker[i].fdmap[nfds-1],
4058c2ecf20Sopenharmony_ci			       t[OP_EPOLL_ADD], t[OP_EPOLL_MOD], t[OP_EPOLL_DEL]);
4068c2ecf20Sopenharmony_ci	}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	print_summary();
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	close(epollfd);
4118c2ecf20Sopenharmony_ci	return ret;
4128c2ecf20Sopenharmony_cierrmem:
4138c2ecf20Sopenharmony_ci	err(EXIT_FAILURE, "calloc");
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci#endif // HAVE_EVENTFD_SUPPORT
416