xref: /openbmc/linux/tools/perf/bench/futex-hash.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a0439711SDavidlohr Bueso /*
3a0439711SDavidlohr Bueso  * Copyright (C) 2013  Davidlohr Bueso <davidlohr@hp.com>
4a0439711SDavidlohr Bueso  *
5a0439711SDavidlohr Bueso  * futex-hash: Stress the hell out of the Linux kernel futex uaddr hashing.
6a0439711SDavidlohr Bueso  *
7a0439711SDavidlohr Bueso  * This program is particularly useful for measuring the kernel's futex hash
8a0439711SDavidlohr Bueso  * table/function implementation. In order for it to make sense, use with as
9a0439711SDavidlohr Bueso  * many threads and futexes as possible.
10a0439711SDavidlohr Bueso  */
11a0439711SDavidlohr Bueso 
128a158589SArnaldo Carvalho de Melo /* For the CLR_() macros */
13a0f213e1SArnaldo Carvalho de Melo #include <string.h>
148a158589SArnaldo Carvalho de Melo #include <pthread.h>
158a158589SArnaldo Carvalho de Melo 
169c304f6cSArnaldo Carvalho de Melo #include <errno.h>
179c304f6cSArnaldo Carvalho de Melo #include <signal.h>
189c304f6cSArnaldo Carvalho de Melo #include <stdlib.h>
1986695f59SArnaldo Carvalho de Melo #include <linux/compiler.h>
209c304f6cSArnaldo Carvalho de Melo #include <linux/kernel.h>
21d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
229c304f6cSArnaldo Carvalho de Melo #include <sys/time.h>
239f9a3ffeSDavidlohr Bueso #include <sys/mman.h>
249c3516d1SJiri Olsa #include <perf/cpumap.h>
259c304f6cSArnaldo Carvalho de Melo 
26*a64d3af5SIan Rogers #include "../util/mutex.h"
27a0439711SDavidlohr Bueso #include "../util/stat.h"
284b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
29a0439711SDavidlohr Bueso #include "bench.h"
30a0439711SDavidlohr Bueso #include "futex.h"
31a0439711SDavidlohr Bueso 
32a0439711SDavidlohr Bueso #include <err.h>
33a0439711SDavidlohr Bueso 
3409590463SDavidlohr Bueso static bool done = false;
3586c87e13SDavidlohr Bueso static int futex_flag = 0;
36a0439711SDavidlohr Bueso 
37e4d9b04bSArnaldo Carvalho de Melo struct timeval bench__start, bench__end, bench__runtime;
38*a64d3af5SIan Rogers static struct mutex thread_lock;
39a0439711SDavidlohr Bueso static unsigned int threads_starting;
40a0439711SDavidlohr Bueso static struct stats throughput_stats;
41*a64d3af5SIan Rogers static struct cond thread_parent, thread_worker;
42a0439711SDavidlohr Bueso 
43a0439711SDavidlohr Bueso struct worker {
44a0439711SDavidlohr Bueso 	int tid;
45a0439711SDavidlohr Bueso 	u_int32_t *futex;
46a0439711SDavidlohr Bueso 	pthread_t thread;
47a0439711SDavidlohr Bueso 	unsigned long ops;
48e2e1680fSDavidlohr Bueso };
49a0439711SDavidlohr Bueso 
5009590463SDavidlohr Bueso static struct bench_futex_parameters params = {
5109590463SDavidlohr Bueso 	.nfutexes = 1024,
5209590463SDavidlohr Bueso 	.runtime  = 10,
5309590463SDavidlohr Bueso };
5409590463SDavidlohr Bueso 
55a0439711SDavidlohr Bueso static const struct option options[] = {
5609590463SDavidlohr Bueso 	OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
5709590463SDavidlohr Bueso 	OPT_UINTEGER('r', "runtime", &params.runtime, "Specify runtime (in seconds)"),
5809590463SDavidlohr Bueso 	OPT_UINTEGER('f', "futexes", &params.nfutexes, "Specify amount of futexes per threads"),
5909590463SDavidlohr Bueso 	OPT_BOOLEAN( 's', "silent",  &params.silent, "Silent mode: do not display data/details"),
6009590463SDavidlohr Bueso 	OPT_BOOLEAN( 'S', "shared",  &params.fshared, "Use shared futexes instead of private ones"),
619f9a3ffeSDavidlohr Bueso 	OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
62a0439711SDavidlohr Bueso 	OPT_END()
63a0439711SDavidlohr Bueso };
64a0439711SDavidlohr Bueso 
65a0439711SDavidlohr Bueso static const char * const bench_futex_hash_usage[] = {
66a0439711SDavidlohr Bueso 	"perf bench futex hash <options>",
67a0439711SDavidlohr Bueso 	NULL
68a0439711SDavidlohr Bueso };
69a0439711SDavidlohr Bueso 
workerfn(void * arg)70a0439711SDavidlohr Bueso static void *workerfn(void *arg)
71a0439711SDavidlohr Bueso {
72a0439711SDavidlohr Bueso 	int ret;
73a0439711SDavidlohr Bueso 	struct worker *w = (struct worker *) arg;
74e2e1680fSDavidlohr Bueso 	unsigned int i;
75e2e1680fSDavidlohr Bueso 	unsigned long ops = w->ops; /* avoid cacheline bouncing */
76a0439711SDavidlohr Bueso 
77*a64d3af5SIan Rogers 	mutex_lock(&thread_lock);
78a0439711SDavidlohr Bueso 	threads_starting--;
79a0439711SDavidlohr Bueso 	if (!threads_starting)
80*a64d3af5SIan Rogers 		cond_signal(&thread_parent);
81*a64d3af5SIan Rogers 	cond_wait(&thread_worker, &thread_lock);
82*a64d3af5SIan Rogers 	mutex_unlock(&thread_lock);
83a0439711SDavidlohr Bueso 
84a0439711SDavidlohr Bueso 	do {
8509590463SDavidlohr Bueso 		for (i = 0; i < params.nfutexes; i++, ops++) {
86a0439711SDavidlohr Bueso 			/*
87a0439711SDavidlohr Bueso 			 * We want the futex calls to fail in order to stress
88a0439711SDavidlohr Bueso 			 * the hashing of uaddr and not measure other steps,
89a0439711SDavidlohr Bueso 			 * such as internal waitqueue handling, thus enlarging
90a0439711SDavidlohr Bueso 			 * the critical region protected by hb->lock.
91a0439711SDavidlohr Bueso 			 */
9286c87e13SDavidlohr Bueso 			ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
9309590463SDavidlohr Bueso 			if (!params.silent &&
94a0439711SDavidlohr Bueso 			    (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
95a0439711SDavidlohr Bueso 				warn("Non-expected futex return call");
96a0439711SDavidlohr Bueso 		}
97a0439711SDavidlohr Bueso 	}  while (!done);
98a0439711SDavidlohr Bueso 
99e2e1680fSDavidlohr Bueso 	w->ops = ops;
100a0439711SDavidlohr Bueso 	return NULL;
101a0439711SDavidlohr Bueso }
102a0439711SDavidlohr Bueso 
toggle_done(int sig __maybe_unused,siginfo_t * info __maybe_unused,void * uc __maybe_unused)103a0439711SDavidlohr Bueso static void toggle_done(int sig __maybe_unused,
104a0439711SDavidlohr Bueso 			siginfo_t *info __maybe_unused,
105a0439711SDavidlohr Bueso 			void *uc __maybe_unused)
106a0439711SDavidlohr Bueso {
107a0439711SDavidlohr Bueso 	/* inform all threads that we're done for the day */
108a0439711SDavidlohr Bueso 	done = true;
109e4d9b04bSArnaldo Carvalho de Melo 	gettimeofday(&bench__end, NULL);
110e4d9b04bSArnaldo Carvalho de Melo 	timersub(&bench__end, &bench__start, &bench__runtime);
111a0439711SDavidlohr Bueso }
112a0439711SDavidlohr Bueso 
print_summary(void)113a0439711SDavidlohr Bueso static void print_summary(void)
114a0439711SDavidlohr Bueso {
115a0439711SDavidlohr Bueso 	unsigned long avg = avg_stats(&throughput_stats);
116a0439711SDavidlohr Bueso 	double stddev = stddev_stats(&throughput_stats);
117a0439711SDavidlohr Bueso 
118a0439711SDavidlohr Bueso 	printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
11909590463SDavidlohr Bueso 	       !params.silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
120e4d9b04bSArnaldo Carvalho de Melo 	       (int)bench__runtime.tv_sec);
121a0439711SDavidlohr Bueso }
122a0439711SDavidlohr Bueso 
bench_futex_hash(int argc,const char ** argv)123b0ad8ea6SArnaldo Carvalho de Melo int bench_futex_hash(int argc, const char **argv)
124a0439711SDavidlohr Bueso {
125a0439711SDavidlohr Bueso 	int ret = 0;
126c9c2a427SAthira Rajeev 	cpu_set_t *cpuset;
127a0439711SDavidlohr Bueso 	struct sigaction act;
1283b2323c2SDavidlohr Bueso 	unsigned int i;
129a0439711SDavidlohr Bueso 	pthread_attr_t thread_attr;
130a0439711SDavidlohr Bueso 	struct worker *worker = NULL;
131f854839bSJiri Olsa 	struct perf_cpu_map *cpu;
132c9c2a427SAthira Rajeev 	int nrcpus;
133c9c2a427SAthira Rajeev 	size_t size;
134a0439711SDavidlohr Bueso 
135a0439711SDavidlohr Bueso 	argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
136a0439711SDavidlohr Bueso 	if (argc) {
137a0439711SDavidlohr Bueso 		usage_with_options(bench_futex_hash_usage, options);
138a0439711SDavidlohr Bueso 		exit(EXIT_FAILURE);
139a0439711SDavidlohr Bueso 	}
140a0439711SDavidlohr Bueso 
1419c3516d1SJiri Olsa 	cpu = perf_cpu_map__new(NULL);
1423b2323c2SDavidlohr Bueso 	if (!cpu)
1433b2323c2SDavidlohr Bueso 		goto errmem;
144a0439711SDavidlohr Bueso 
1457b919a53STommi Rantala 	memset(&act, 0, sizeof(act));
146a0439711SDavidlohr Bueso 	sigfillset(&act.sa_mask);
147a0439711SDavidlohr Bueso 	act.sa_sigaction = toggle_done;
148a0439711SDavidlohr Bueso 	sigaction(SIGINT, &act, NULL);
149a0439711SDavidlohr Bueso 
1509f9a3ffeSDavidlohr Bueso 	if (params.mlockall) {
1519f9a3ffeSDavidlohr Bueso 		if (mlockall(MCL_CURRENT | MCL_FUTURE))
1529f9a3ffeSDavidlohr Bueso 			err(EXIT_FAILURE, "mlockall");
1539f9a3ffeSDavidlohr Bueso 	}
1549f9a3ffeSDavidlohr Bueso 
15509590463SDavidlohr Bueso 	if (!params.nthreads) /* default to the number of CPUs */
15644028699SIan Rogers 		params.nthreads = perf_cpu_map__nr(cpu);
157a0439711SDavidlohr Bueso 
15809590463SDavidlohr Bueso 	worker = calloc(params.nthreads, sizeof(*worker));
159a0439711SDavidlohr Bueso 	if (!worker)
160a0439711SDavidlohr Bueso 		goto errmem;
161a0439711SDavidlohr Bueso 
16209590463SDavidlohr Bueso 	if (!params.fshared)
16386c87e13SDavidlohr Bueso 		futex_flag = FUTEX_PRIVATE_FLAG;
16486c87e13SDavidlohr Bueso 
165a0439711SDavidlohr Bueso 	printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
16609590463SDavidlohr Bueso 	       getpid(), params.nthreads, params.nfutexes, params.fshared ? "shared":"private", params.runtime);
167a0439711SDavidlohr Bueso 
168a0439711SDavidlohr Bueso 	init_stats(&throughput_stats);
169*a64d3af5SIan Rogers 	mutex_init(&thread_lock);
170*a64d3af5SIan Rogers 	cond_init(&thread_parent);
171*a64d3af5SIan Rogers 	cond_init(&thread_worker);
172a0439711SDavidlohr Bueso 
17309590463SDavidlohr Bueso 	threads_starting = params.nthreads;
174a0439711SDavidlohr Bueso 	pthread_attr_init(&thread_attr);
175e4d9b04bSArnaldo Carvalho de Melo 	gettimeofday(&bench__start, NULL);
176c9c2a427SAthira Rajeev 
177c9c2a427SAthira Rajeev 	nrcpus = perf_cpu_map__nr(cpu);
178c9c2a427SAthira Rajeev 	cpuset = CPU_ALLOC(nrcpus);
179c9c2a427SAthira Rajeev 	BUG_ON(!cpuset);
180c9c2a427SAthira Rajeev 	size = CPU_ALLOC_SIZE(nrcpus);
181c9c2a427SAthira Rajeev 
18209590463SDavidlohr Bueso 	for (i = 0; i < params.nthreads; i++) {
183a0439711SDavidlohr Bueso 		worker[i].tid = i;
18409590463SDavidlohr Bueso 		worker[i].futex = calloc(params.nfutexes, sizeof(*worker[i].futex));
185a0439711SDavidlohr Bueso 		if (!worker[i].futex)
186a0439711SDavidlohr Bueso 			goto errmem;
187a0439711SDavidlohr Bueso 
188c9c2a427SAthira Rajeev 		CPU_ZERO_S(size, cpuset);
189a0439711SDavidlohr Bueso 
190c9c2a427SAthira Rajeev 		CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
191c9c2a427SAthira Rajeev 		ret = pthread_attr_setaffinity_np(&thread_attr, size, cpuset);
192c9c2a427SAthira Rajeev 		if (ret) {
193c9c2a427SAthira Rajeev 			CPU_FREE(cpuset);
194a0439711SDavidlohr Bueso 			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
195c9c2a427SAthira Rajeev 		}
196a0439711SDavidlohr Bueso 		ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
197a0439711SDavidlohr Bueso 				     (void *)(struct worker *) &worker[i]);
198c9c2a427SAthira Rajeev 		if (ret) {
199c9c2a427SAthira Rajeev 			CPU_FREE(cpuset);
200a0439711SDavidlohr Bueso 			err(EXIT_FAILURE, "pthread_create");
201c9c2a427SAthira Rajeev 		}
202a0439711SDavidlohr Bueso 
203a0439711SDavidlohr Bueso 	}
204c9c2a427SAthira Rajeev 	CPU_FREE(cpuset);
205a0439711SDavidlohr Bueso 	pthread_attr_destroy(&thread_attr);
206a0439711SDavidlohr Bueso 
207*a64d3af5SIan Rogers 	mutex_lock(&thread_lock);
208a0439711SDavidlohr Bueso 	while (threads_starting)
209*a64d3af5SIan Rogers 		cond_wait(&thread_parent, &thread_lock);
210*a64d3af5SIan Rogers 	cond_broadcast(&thread_worker);
211*a64d3af5SIan Rogers 	mutex_unlock(&thread_lock);
212a0439711SDavidlohr Bueso 
21309590463SDavidlohr Bueso 	sleep(params.runtime);
214a0439711SDavidlohr Bueso 	toggle_done(0, NULL, NULL);
215a0439711SDavidlohr Bueso 
21609590463SDavidlohr Bueso 	for (i = 0; i < params.nthreads; i++) {
217a0439711SDavidlohr Bueso 		ret = pthread_join(worker[i].thread, NULL);
218a0439711SDavidlohr Bueso 		if (ret)
219a0439711SDavidlohr Bueso 			err(EXIT_FAILURE, "pthread_join");
220a0439711SDavidlohr Bueso 	}
221a0439711SDavidlohr Bueso 
222a0439711SDavidlohr Bueso 	/* cleanup & report results */
223*a64d3af5SIan Rogers 	cond_destroy(&thread_parent);
224*a64d3af5SIan Rogers 	cond_destroy(&thread_worker);
225*a64d3af5SIan Rogers 	mutex_destroy(&thread_lock);
226a0439711SDavidlohr Bueso 
22709590463SDavidlohr Bueso 	for (i = 0; i < params.nthreads; i++) {
22841e7c32bSTommi Rantala 		unsigned long t = bench__runtime.tv_sec > 0 ?
22941e7c32bSTommi Rantala 			worker[i].ops / bench__runtime.tv_sec : 0;
230a0439711SDavidlohr Bueso 		update_stats(&throughput_stats, t);
23109590463SDavidlohr Bueso 		if (!params.silent) {
23209590463SDavidlohr Bueso 			if (params.nfutexes == 1)
233a0439711SDavidlohr Bueso 				printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
234a0439711SDavidlohr Bueso 				       worker[i].tid, &worker[i].futex[0], t);
235a0439711SDavidlohr Bueso 			else
236a0439711SDavidlohr Bueso 				printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
237a0439711SDavidlohr Bueso 				       worker[i].tid, &worker[i].futex[0],
23809590463SDavidlohr Bueso 				       &worker[i].futex[params.nfutexes-1], t);
239a0439711SDavidlohr Bueso 		}
240a0439711SDavidlohr Bueso 
241d8f9da24SArnaldo Carvalho de Melo 		zfree(&worker[i].futex);
242a0439711SDavidlohr Bueso 	}
243a0439711SDavidlohr Bueso 
244a0439711SDavidlohr Bueso 	print_summary();
245a0439711SDavidlohr Bueso 
246a0439711SDavidlohr Bueso 	free(worker);
2473b2323c2SDavidlohr Bueso 	free(cpu);
248a0439711SDavidlohr Bueso 	return ret;
249a0439711SDavidlohr Bueso errmem:
250a0439711SDavidlohr Bueso 	err(EXIT_FAILURE, "calloc");
251a0439711SDavidlohr Bueso }
252