xref: /openbmc/linux/tools/perf/bench/futex-wake.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
227db7830SDavidlohr Bueso /*
327db7830SDavidlohr Bueso  * Copyright (C) 2013  Davidlohr Bueso <davidlohr@hp.com>
427db7830SDavidlohr Bueso  *
527db7830SDavidlohr Bueso  * futex-wake: Block a bunch of threads on a futex and wake'em up, N at a time.
627db7830SDavidlohr Bueso  *
727db7830SDavidlohr Bueso  * This program is particularly useful to measure the latency of nthread wakeups
827db7830SDavidlohr Bueso  * in non-error situations:  all waiters are queued and all wake calls wakeup
927db7830SDavidlohr Bueso  * one or more tasks, and thus the waitqueue is never empty.
1027db7830SDavidlohr Bueso  */
1127db7830SDavidlohr 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 <signal.h>
17a64d3af5SIan Rogers #include "../util/mutex.h"
1827db7830SDavidlohr Bueso #include "../util/stat.h"
194b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
2086695f59SArnaldo Carvalho de Melo #include <linux/compiler.h>
219c304f6cSArnaldo Carvalho de Melo #include <linux/kernel.h>
22565e6911SArnaldo Carvalho de Melo #include <linux/time64.h>
239c304f6cSArnaldo Carvalho de Melo #include <errno.h>
249c3516d1SJiri Olsa #include <perf/cpumap.h>
2527db7830SDavidlohr Bueso #include "bench.h"
2627db7830SDavidlohr Bueso #include "futex.h"
2727db7830SDavidlohr Bueso 
2827db7830SDavidlohr Bueso #include <err.h>
2927db7830SDavidlohr Bueso #include <stdlib.h>
3027db7830SDavidlohr Bueso #include <sys/time.h>
319f9a3ffeSDavidlohr Bueso #include <sys/mman.h>
3227db7830SDavidlohr Bueso 
3327db7830SDavidlohr Bueso /* all threads will block on the same futex */
3427db7830SDavidlohr Bueso static u_int32_t futex1 = 0;
3527db7830SDavidlohr Bueso 
3609590463SDavidlohr Bueso static pthread_t *worker;
3709590463SDavidlohr Bueso static bool done = false;
38a64d3af5SIan Rogers static struct mutex thread_lock;
39a64d3af5SIan Rogers static struct cond thread_parent, thread_worker;
4009590463SDavidlohr Bueso static struct stats waketime_stats, wakeup_stats;
4109590463SDavidlohr Bueso static unsigned int threads_starting;
4209590463SDavidlohr Bueso static int futex_flag = 0;
4309590463SDavidlohr Bueso 
4409590463SDavidlohr Bueso static struct bench_futex_parameters params = {
4527db7830SDavidlohr Bueso 	/*
4627db7830SDavidlohr Bueso 	 * How many wakeups to do at a time.
4727db7830SDavidlohr Bueso 	 * Default to 1 in order to make the kernel work more.
4827db7830SDavidlohr Bueso 	 */
4909590463SDavidlohr Bueso 	.nwakes  = 1,
5009590463SDavidlohr Bueso };
5127db7830SDavidlohr Bueso 
5227db7830SDavidlohr Bueso static const struct option options[] = {
5309590463SDavidlohr Bueso 	OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
5409590463SDavidlohr Bueso 	OPT_UINTEGER('w', "nwakes",  &params.nwakes, "Specify amount of threads to wake at once"),
5509590463SDavidlohr Bueso 	OPT_BOOLEAN( 's', "silent",  &params.silent, "Silent mode: do not display data/details"),
5609590463SDavidlohr Bueso 	OPT_BOOLEAN( 'S', "shared",  &params.fshared, "Use shared futexes instead of private ones"),
579f9a3ffeSDavidlohr Bueso 	OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
589f9a3ffeSDavidlohr Bueso 
5927db7830SDavidlohr Bueso 	OPT_END()
6027db7830SDavidlohr Bueso };
6127db7830SDavidlohr Bueso 
6227db7830SDavidlohr Bueso static const char * const bench_futex_wake_usage[] = {
6327db7830SDavidlohr Bueso 	"perf bench futex wake <options>",
6427db7830SDavidlohr Bueso 	NULL
6527db7830SDavidlohr Bueso };
6627db7830SDavidlohr Bueso 
workerfn(void * arg __maybe_unused)6727db7830SDavidlohr Bueso static void *workerfn(void *arg __maybe_unused)
6827db7830SDavidlohr Bueso {
69a64d3af5SIan Rogers 	mutex_lock(&thread_lock);
7027db7830SDavidlohr Bueso 	threads_starting--;
7127db7830SDavidlohr Bueso 	if (!threads_starting)
72a64d3af5SIan Rogers 		cond_signal(&thread_parent);
73a64d3af5SIan Rogers 	cond_wait(&thread_worker, &thread_lock);
74a64d3af5SIan Rogers 	mutex_unlock(&thread_lock);
7527db7830SDavidlohr Bueso 
76598adc5cSDavidlohr Bueso 	while (1) {
77598adc5cSDavidlohr Bueso 		if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
78598adc5cSDavidlohr Bueso 			break;
79598adc5cSDavidlohr Bueso 	}
80598adc5cSDavidlohr Bueso 
81598adc5cSDavidlohr Bueso 	pthread_exit(NULL);
8227db7830SDavidlohr Bueso 	return NULL;
8327db7830SDavidlohr Bueso }
8427db7830SDavidlohr Bueso 
print_summary(void)8527db7830SDavidlohr Bueso static void print_summary(void)
8627db7830SDavidlohr Bueso {
8727db7830SDavidlohr Bueso 	double waketime_avg = avg_stats(&waketime_stats);
8827db7830SDavidlohr Bueso 	double waketime_stddev = stddev_stats(&waketime_stats);
8927db7830SDavidlohr Bueso 	unsigned int wakeup_avg = avg_stats(&wakeup_stats);
9027db7830SDavidlohr Bueso 
9127db7830SDavidlohr Bueso 	printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
9227db7830SDavidlohr Bueso 	       wakeup_avg,
9309590463SDavidlohr Bueso 	       params.nthreads,
94565e6911SArnaldo Carvalho de Melo 	       waketime_avg / USEC_PER_MSEC,
9527db7830SDavidlohr Bueso 	       rel_stddev_stats(waketime_stddev, waketime_avg));
9627db7830SDavidlohr Bueso }
9727db7830SDavidlohr Bueso 
block_threads(pthread_t * w,struct perf_cpu_map * cpu)98*8351498dSIan Rogers static void block_threads(pthread_t *w, struct perf_cpu_map *cpu)
9927db7830SDavidlohr Bueso {
100c9c2a427SAthira Rajeev 	cpu_set_t *cpuset;
10127db7830SDavidlohr Bueso 	unsigned int i;
102c9c2a427SAthira Rajeev 	size_t size;
103c9c2a427SAthira Rajeev 	int nrcpus = perf_cpu_map__nr(cpu);
10409590463SDavidlohr Bueso 	threads_starting = params.nthreads;
10527db7830SDavidlohr Bueso 
106c9c2a427SAthira Rajeev 	cpuset = CPU_ALLOC(nrcpus);
107c9c2a427SAthira Rajeev 	BUG_ON(!cpuset);
108c9c2a427SAthira Rajeev 	size = CPU_ALLOC_SIZE(nrcpus);
109c9c2a427SAthira Rajeev 
11027db7830SDavidlohr Bueso 	/* create and block all threads */
11109590463SDavidlohr Bueso 	for (i = 0; i < params.nthreads; i++) {
112*8351498dSIan Rogers 		pthread_attr_t thread_attr;
113*8351498dSIan Rogers 
114*8351498dSIan Rogers 		pthread_attr_init(&thread_attr);
115c9c2a427SAthira Rajeev 		CPU_ZERO_S(size, cpuset);
116c9c2a427SAthira Rajeev 		CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
11727db7830SDavidlohr Bueso 
118c9c2a427SAthira Rajeev 		if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
119c9c2a427SAthira Rajeev 			CPU_FREE(cpuset);
12027db7830SDavidlohr Bueso 			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
121c9c2a427SAthira Rajeev 		}
12227db7830SDavidlohr Bueso 
123c9c2a427SAthira Rajeev 		if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) {
124c9c2a427SAthira Rajeev 			CPU_FREE(cpuset);
12527db7830SDavidlohr Bueso 			err(EXIT_FAILURE, "pthread_create");
12627db7830SDavidlohr Bueso 		}
127*8351498dSIan Rogers 		pthread_attr_destroy(&thread_attr);
12827db7830SDavidlohr Bueso 	}
129c9c2a427SAthira Rajeev 	CPU_FREE(cpuset);
130c9c2a427SAthira Rajeev }
13127db7830SDavidlohr Bueso 
toggle_done(int sig __maybe_unused,siginfo_t * info __maybe_unused,void * uc __maybe_unused)13227db7830SDavidlohr Bueso static void toggle_done(int sig __maybe_unused,
13327db7830SDavidlohr Bueso 			siginfo_t *info __maybe_unused,
13427db7830SDavidlohr Bueso 			void *uc __maybe_unused)
13527db7830SDavidlohr Bueso {
13627db7830SDavidlohr Bueso 	done = true;
13727db7830SDavidlohr Bueso }
13827db7830SDavidlohr Bueso 
bench_futex_wake(int argc,const char ** argv)139b0ad8ea6SArnaldo Carvalho de Melo int bench_futex_wake(int argc, const char **argv)
14027db7830SDavidlohr Bueso {
14127db7830SDavidlohr Bueso 	int ret = 0;
14227db7830SDavidlohr Bueso 	unsigned int i, j;
14327db7830SDavidlohr Bueso 	struct sigaction act;
144f854839bSJiri Olsa 	struct perf_cpu_map *cpu;
14527db7830SDavidlohr Bueso 
14627db7830SDavidlohr Bueso 	argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
14727db7830SDavidlohr Bueso 	if (argc) {
14827db7830SDavidlohr Bueso 		usage_with_options(bench_futex_wake_usage, options);
14927db7830SDavidlohr Bueso 		exit(EXIT_FAILURE);
15027db7830SDavidlohr Bueso 	}
15127db7830SDavidlohr Bueso 
1529c3516d1SJiri Olsa 	cpu = perf_cpu_map__new(NULL);
1533b2323c2SDavidlohr Bueso 	if (!cpu)
1543b2323c2SDavidlohr Bueso 		err(EXIT_FAILURE, "calloc");
15527db7830SDavidlohr Bueso 
1567b919a53STommi Rantala 	memset(&act, 0, sizeof(act));
15727db7830SDavidlohr Bueso 	sigfillset(&act.sa_mask);
15827db7830SDavidlohr Bueso 	act.sa_sigaction = toggle_done;
15927db7830SDavidlohr Bueso 	sigaction(SIGINT, &act, NULL);
16027db7830SDavidlohr Bueso 
1619f9a3ffeSDavidlohr Bueso 	if (params.mlockall) {
1629f9a3ffeSDavidlohr Bueso 		if (mlockall(MCL_CURRENT | MCL_FUTURE))
1639f9a3ffeSDavidlohr Bueso 			err(EXIT_FAILURE, "mlockall");
1649f9a3ffeSDavidlohr Bueso 	}
1659f9a3ffeSDavidlohr Bueso 
16609590463SDavidlohr Bueso 	if (!params.nthreads)
16744028699SIan Rogers 		params.nthreads = perf_cpu_map__nr(cpu);
16827db7830SDavidlohr Bueso 
16909590463SDavidlohr Bueso 	worker = calloc(params.nthreads, sizeof(*worker));
17027db7830SDavidlohr Bueso 	if (!worker)
17127db7830SDavidlohr Bueso 		err(EXIT_FAILURE, "calloc");
17227db7830SDavidlohr Bueso 
17309590463SDavidlohr Bueso 	if (!params.fshared)
17486c87e13SDavidlohr Bueso 		futex_flag = FUTEX_PRIVATE_FLAG;
17586c87e13SDavidlohr Bueso 
17686c87e13SDavidlohr Bueso 	printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
17727db7830SDavidlohr Bueso 	       "waking up %d at a time.\n\n",
17809590463SDavidlohr Bueso 	       getpid(), params.nthreads, params.fshared ? "shared":"private",
17909590463SDavidlohr Bueso 	       &futex1, params.nwakes);
18027db7830SDavidlohr Bueso 
18127db7830SDavidlohr Bueso 	init_stats(&wakeup_stats);
18227db7830SDavidlohr Bueso 	init_stats(&waketime_stats);
183a64d3af5SIan Rogers 	mutex_init(&thread_lock);
184a64d3af5SIan Rogers 	cond_init(&thread_parent);
185a64d3af5SIan Rogers 	cond_init(&thread_worker);
18627db7830SDavidlohr Bueso 
187d9de84afSDavidlohr Bueso 	for (j = 0; j < bench_repeat && !done; j++) {
18827db7830SDavidlohr Bueso 		unsigned int nwoken = 0;
18927db7830SDavidlohr Bueso 		struct timeval start, end, runtime;
19027db7830SDavidlohr Bueso 
19127db7830SDavidlohr Bueso 		/* create, launch & block all threads */
192*8351498dSIan Rogers 		block_threads(worker, cpu);
19327db7830SDavidlohr Bueso 
19427db7830SDavidlohr Bueso 		/* make sure all threads are already blocked */
195a64d3af5SIan Rogers 		mutex_lock(&thread_lock);
19627db7830SDavidlohr Bueso 		while (threads_starting)
197a64d3af5SIan Rogers 			cond_wait(&thread_parent, &thread_lock);
198a64d3af5SIan Rogers 		cond_broadcast(&thread_worker);
199a64d3af5SIan Rogers 		mutex_unlock(&thread_lock);
20027db7830SDavidlohr Bueso 
20127db7830SDavidlohr Bueso 		usleep(100000);
20227db7830SDavidlohr Bueso 
20327db7830SDavidlohr Bueso 		/* Ok, all threads are patiently blocked, start waking folks up */
20427db7830SDavidlohr Bueso 		gettimeofday(&start, NULL);
20509590463SDavidlohr Bueso 		while (nwoken != params.nthreads)
20609590463SDavidlohr Bueso 			nwoken += futex_wake(&futex1,
20709590463SDavidlohr Bueso 					     params.nwakes, futex_flag);
20827db7830SDavidlohr Bueso 		gettimeofday(&end, NULL);
20927db7830SDavidlohr Bueso 		timersub(&end, &start, &runtime);
21027db7830SDavidlohr Bueso 
21127db7830SDavidlohr Bueso 		update_stats(&wakeup_stats, nwoken);
21227db7830SDavidlohr Bueso 		update_stats(&waketime_stats, runtime.tv_usec);
21327db7830SDavidlohr Bueso 
21409590463SDavidlohr Bueso 		if (!params.silent) {
21527db7830SDavidlohr Bueso 			printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
21609590463SDavidlohr Bueso 			       j + 1, nwoken, params.nthreads,
21709590463SDavidlohr Bueso 			       runtime.tv_usec / (double)USEC_PER_MSEC);
21827db7830SDavidlohr Bueso 		}
21927db7830SDavidlohr Bueso 
22009590463SDavidlohr Bueso 		for (i = 0; i < params.nthreads; i++) {
22127db7830SDavidlohr Bueso 			ret = pthread_join(worker[i], NULL);
22227db7830SDavidlohr Bueso 			if (ret)
22327db7830SDavidlohr Bueso 				err(EXIT_FAILURE, "pthread_join");
22427db7830SDavidlohr Bueso 		}
22527db7830SDavidlohr Bueso 
22627db7830SDavidlohr Bueso 	}
22727db7830SDavidlohr Bueso 
22827db7830SDavidlohr Bueso 	/* cleanup & report results */
229a64d3af5SIan Rogers 	cond_destroy(&thread_parent);
230a64d3af5SIan Rogers 	cond_destroy(&thread_worker);
231a64d3af5SIan Rogers 	mutex_destroy(&thread_lock);
23227db7830SDavidlohr Bueso 
23327db7830SDavidlohr Bueso 	print_summary();
23427db7830SDavidlohr Bueso 
23527db7830SDavidlohr Bueso 	free(worker);
23688e48238SSohaib Mohamed 	perf_cpu_map__put(cpu);
23727db7830SDavidlohr Bueso 	return ret;
23827db7830SDavidlohr Bueso }
239