xref: /OK3568_Linux_fs/kernel/tools/perf/bench/futex-wake.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2013  Davidlohr Bueso <davidlohr@hp.com>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * futex-wake: Block a bunch of threads on a futex and wake'em up, N at a time.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This program is particularly useful to measure the latency of nthread wakeups
8*4882a593Smuzhiyun  * in non-error situations:  all waiters are queued and all wake calls wakeup
9*4882a593Smuzhiyun  * one or more tasks, and thus the waitqueue is never empty.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun /* For the CLR_() macros */
13*4882a593Smuzhiyun #include <string.h>
14*4882a593Smuzhiyun #include <pthread.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <signal.h>
17*4882a593Smuzhiyun #include "../util/stat.h"
18*4882a593Smuzhiyun #include <subcmd/parse-options.h>
19*4882a593Smuzhiyun #include <linux/compiler.h>
20*4882a593Smuzhiyun #include <linux/kernel.h>
21*4882a593Smuzhiyun #include <linux/time64.h>
22*4882a593Smuzhiyun #include <errno.h>
23*4882a593Smuzhiyun #include <internal/cpumap.h>
24*4882a593Smuzhiyun #include <perf/cpumap.h>
25*4882a593Smuzhiyun #include "bench.h"
26*4882a593Smuzhiyun #include "futex.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #include <err.h>
29*4882a593Smuzhiyun #include <stdlib.h>
30*4882a593Smuzhiyun #include <sys/time.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /* all threads will block on the same futex */
33*4882a593Smuzhiyun static u_int32_t futex1 = 0;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * How many wakeups to do at a time.
37*4882a593Smuzhiyun  * Default to 1 in order to make the kernel work more.
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun static unsigned int nwakes = 1;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun pthread_t *worker;
42*4882a593Smuzhiyun static bool done = false, silent = false, fshared = false;
43*4882a593Smuzhiyun static pthread_mutex_t thread_lock;
44*4882a593Smuzhiyun static pthread_cond_t thread_parent, thread_worker;
45*4882a593Smuzhiyun static struct stats waketime_stats, wakeup_stats;
46*4882a593Smuzhiyun static unsigned int threads_starting, nthreads = 0;
47*4882a593Smuzhiyun static int futex_flag = 0;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun static const struct option options[] = {
50*4882a593Smuzhiyun 	OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
51*4882a593Smuzhiyun 	OPT_UINTEGER('w', "nwakes",  &nwakes,   "Specify amount of threads to wake at once"),
52*4882a593Smuzhiyun 	OPT_BOOLEAN( 's', "silent",  &silent,   "Silent mode: do not display data/details"),
53*4882a593Smuzhiyun 	OPT_BOOLEAN( 'S', "shared",  &fshared,  "Use shared futexes instead of private ones"),
54*4882a593Smuzhiyun 	OPT_END()
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun static const char * const bench_futex_wake_usage[] = {
58*4882a593Smuzhiyun 	"perf bench futex wake <options>",
59*4882a593Smuzhiyun 	NULL
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun 
workerfn(void * arg __maybe_unused)62*4882a593Smuzhiyun static void *workerfn(void *arg __maybe_unused)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	pthread_mutex_lock(&thread_lock);
65*4882a593Smuzhiyun 	threads_starting--;
66*4882a593Smuzhiyun 	if (!threads_starting)
67*4882a593Smuzhiyun 		pthread_cond_signal(&thread_parent);
68*4882a593Smuzhiyun 	pthread_cond_wait(&thread_worker, &thread_lock);
69*4882a593Smuzhiyun 	pthread_mutex_unlock(&thread_lock);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	while (1) {
72*4882a593Smuzhiyun 		if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
73*4882a593Smuzhiyun 			break;
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	pthread_exit(NULL);
77*4882a593Smuzhiyun 	return NULL;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
print_summary(void)80*4882a593Smuzhiyun static void print_summary(void)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	double waketime_avg = avg_stats(&waketime_stats);
83*4882a593Smuzhiyun 	double waketime_stddev = stddev_stats(&waketime_stats);
84*4882a593Smuzhiyun 	unsigned int wakeup_avg = avg_stats(&wakeup_stats);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
87*4882a593Smuzhiyun 	       wakeup_avg,
88*4882a593Smuzhiyun 	       nthreads,
89*4882a593Smuzhiyun 	       waketime_avg / USEC_PER_MSEC,
90*4882a593Smuzhiyun 	       rel_stddev_stats(waketime_stddev, waketime_avg));
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
block_threads(pthread_t * w,pthread_attr_t thread_attr,struct perf_cpu_map * cpu)93*4882a593Smuzhiyun static void block_threads(pthread_t *w,
94*4882a593Smuzhiyun 			  pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	cpu_set_t cpuset;
97*4882a593Smuzhiyun 	unsigned int i;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	threads_starting = nthreads;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	/* create and block all threads */
102*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
103*4882a593Smuzhiyun 		CPU_ZERO(&cpuset);
104*4882a593Smuzhiyun 		CPU_SET(cpu->map[i % cpu->nr], &cpuset);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 		if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
107*4882a593Smuzhiyun 			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 		if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
110*4882a593Smuzhiyun 			err(EXIT_FAILURE, "pthread_create");
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
toggle_done(int sig __maybe_unused,siginfo_t * info __maybe_unused,void * uc __maybe_unused)114*4882a593Smuzhiyun static void toggle_done(int sig __maybe_unused,
115*4882a593Smuzhiyun 			siginfo_t *info __maybe_unused,
116*4882a593Smuzhiyun 			void *uc __maybe_unused)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	done = true;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
bench_futex_wake(int argc,const char ** argv)121*4882a593Smuzhiyun int bench_futex_wake(int argc, const char **argv)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	int ret = 0;
124*4882a593Smuzhiyun 	unsigned int i, j;
125*4882a593Smuzhiyun 	struct sigaction act;
126*4882a593Smuzhiyun 	pthread_attr_t thread_attr;
127*4882a593Smuzhiyun 	struct perf_cpu_map *cpu;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
130*4882a593Smuzhiyun 	if (argc) {
131*4882a593Smuzhiyun 		usage_with_options(bench_futex_wake_usage, options);
132*4882a593Smuzhiyun 		exit(EXIT_FAILURE);
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	cpu = perf_cpu_map__new(NULL);
136*4882a593Smuzhiyun 	if (!cpu)
137*4882a593Smuzhiyun 		err(EXIT_FAILURE, "calloc");
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	memset(&act, 0, sizeof(act));
140*4882a593Smuzhiyun 	sigfillset(&act.sa_mask);
141*4882a593Smuzhiyun 	act.sa_sigaction = toggle_done;
142*4882a593Smuzhiyun 	sigaction(SIGINT, &act, NULL);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	if (!nthreads)
145*4882a593Smuzhiyun 		nthreads = cpu->nr;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	worker = calloc(nthreads, sizeof(*worker));
148*4882a593Smuzhiyun 	if (!worker)
149*4882a593Smuzhiyun 		err(EXIT_FAILURE, "calloc");
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	if (!fshared)
152*4882a593Smuzhiyun 		futex_flag = FUTEX_PRIVATE_FLAG;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
155*4882a593Smuzhiyun 	       "waking up %d at a time.\n\n",
156*4882a593Smuzhiyun 	       getpid(), nthreads, fshared ? "shared":"private",  &futex1, nwakes);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	init_stats(&wakeup_stats);
159*4882a593Smuzhiyun 	init_stats(&waketime_stats);
160*4882a593Smuzhiyun 	pthread_attr_init(&thread_attr);
161*4882a593Smuzhiyun 	pthread_mutex_init(&thread_lock, NULL);
162*4882a593Smuzhiyun 	pthread_cond_init(&thread_parent, NULL);
163*4882a593Smuzhiyun 	pthread_cond_init(&thread_worker, NULL);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	for (j = 0; j < bench_repeat && !done; j++) {
166*4882a593Smuzhiyun 		unsigned int nwoken = 0;
167*4882a593Smuzhiyun 		struct timeval start, end, runtime;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 		/* create, launch & block all threads */
170*4882a593Smuzhiyun 		block_threads(worker, thread_attr, cpu);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 		/* make sure all threads are already blocked */
173*4882a593Smuzhiyun 		pthread_mutex_lock(&thread_lock);
174*4882a593Smuzhiyun 		while (threads_starting)
175*4882a593Smuzhiyun 			pthread_cond_wait(&thread_parent, &thread_lock);
176*4882a593Smuzhiyun 		pthread_cond_broadcast(&thread_worker);
177*4882a593Smuzhiyun 		pthread_mutex_unlock(&thread_lock);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 		usleep(100000);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 		/* Ok, all threads are patiently blocked, start waking folks up */
182*4882a593Smuzhiyun 		gettimeofday(&start, NULL);
183*4882a593Smuzhiyun 		while (nwoken != nthreads)
184*4882a593Smuzhiyun 			nwoken += futex_wake(&futex1, nwakes, futex_flag);
185*4882a593Smuzhiyun 		gettimeofday(&end, NULL);
186*4882a593Smuzhiyun 		timersub(&end, &start, &runtime);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 		update_stats(&wakeup_stats, nwoken);
189*4882a593Smuzhiyun 		update_stats(&waketime_stats, runtime.tv_usec);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 		if (!silent) {
192*4882a593Smuzhiyun 			printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
193*4882a593Smuzhiyun 			       j + 1, nwoken, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
194*4882a593Smuzhiyun 		}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 		for (i = 0; i < nthreads; i++) {
197*4882a593Smuzhiyun 			ret = pthread_join(worker[i], NULL);
198*4882a593Smuzhiyun 			if (ret)
199*4882a593Smuzhiyun 				err(EXIT_FAILURE, "pthread_join");
200*4882a593Smuzhiyun 		}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	}
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	/* cleanup & report results */
205*4882a593Smuzhiyun 	pthread_cond_destroy(&thread_parent);
206*4882a593Smuzhiyun 	pthread_cond_destroy(&thread_worker);
207*4882a593Smuzhiyun 	pthread_mutex_destroy(&thread_lock);
208*4882a593Smuzhiyun 	pthread_attr_destroy(&thread_attr);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	print_summary();
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	free(worker);
213*4882a593Smuzhiyun 	perf_cpu_map__put(cpu);
214*4882a593Smuzhiyun 	return ret;
215*4882a593Smuzhiyun }
216