xref: /OK3568_Linux_fs/kernel/tools/perf/bench/futex-requeue.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-requeue: Block a bunch of threads on futex1 and requeue them
6*4882a593Smuzhiyun  *                on futex2, N at a time.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * This program is particularly useful to measure the latency of nthread
9*4882a593Smuzhiyun  * requeues without waking up any tasks -- thus mimicking a regular futex_wait.
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 static u_int32_t futex1 = 0, futex2 = 0;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun  * How many tasks to requeue at a time.
36*4882a593Smuzhiyun  * Default to 1 in order to make the kernel work more.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun static unsigned int nrequeue = 1;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static pthread_t *worker;
41*4882a593Smuzhiyun static bool done = false, silent = false, fshared = false;
42*4882a593Smuzhiyun static pthread_mutex_t thread_lock;
43*4882a593Smuzhiyun static pthread_cond_t thread_parent, thread_worker;
44*4882a593Smuzhiyun static struct stats requeuetime_stats, requeued_stats;
45*4882a593Smuzhiyun static unsigned int threads_starting, nthreads = 0;
46*4882a593Smuzhiyun static int futex_flag = 0;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun static const struct option options[] = {
49*4882a593Smuzhiyun 	OPT_UINTEGER('t', "threads",  &nthreads, "Specify amount of threads"),
50*4882a593Smuzhiyun 	OPT_UINTEGER('q', "nrequeue", &nrequeue, "Specify amount of threads to requeue at once"),
51*4882a593Smuzhiyun 	OPT_BOOLEAN( 's', "silent",   &silent,   "Silent mode: do not display data/details"),
52*4882a593Smuzhiyun 	OPT_BOOLEAN( 'S', "shared",   &fshared,  "Use shared futexes instead of private ones"),
53*4882a593Smuzhiyun 	OPT_END()
54*4882a593Smuzhiyun };
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun static const char * const bench_futex_requeue_usage[] = {
57*4882a593Smuzhiyun 	"perf bench futex requeue <options>",
58*4882a593Smuzhiyun 	NULL
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun 
print_summary(void)61*4882a593Smuzhiyun static void print_summary(void)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	double requeuetime_avg = avg_stats(&requeuetime_stats);
64*4882a593Smuzhiyun 	double requeuetime_stddev = stddev_stats(&requeuetime_stats);
65*4882a593Smuzhiyun 	unsigned int requeued_avg = avg_stats(&requeued_stats);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
68*4882a593Smuzhiyun 	       requeued_avg,
69*4882a593Smuzhiyun 	       nthreads,
70*4882a593Smuzhiyun 	       requeuetime_avg / USEC_PER_MSEC,
71*4882a593Smuzhiyun 	       rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
workerfn(void * arg __maybe_unused)74*4882a593Smuzhiyun static void *workerfn(void *arg __maybe_unused)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	pthread_mutex_lock(&thread_lock);
77*4882a593Smuzhiyun 	threads_starting--;
78*4882a593Smuzhiyun 	if (!threads_starting)
79*4882a593Smuzhiyun 		pthread_cond_signal(&thread_parent);
80*4882a593Smuzhiyun 	pthread_cond_wait(&thread_worker, &thread_lock);
81*4882a593Smuzhiyun 	pthread_mutex_unlock(&thread_lock);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	futex_wait(&futex1, 0, NULL, futex_flag);
84*4882a593Smuzhiyun 	return NULL;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
block_threads(pthread_t * w,pthread_attr_t thread_attr,struct perf_cpu_map * cpu)87*4882a593Smuzhiyun static void block_threads(pthread_t *w,
88*4882a593Smuzhiyun 			  pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	cpu_set_t cpuset;
91*4882a593Smuzhiyun 	unsigned int i;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	threads_starting = nthreads;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	/* create and block all threads */
96*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
97*4882a593Smuzhiyun 		CPU_ZERO(&cpuset);
98*4882a593Smuzhiyun 		CPU_SET(cpu->map[i % cpu->nr], &cpuset);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 		if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
101*4882a593Smuzhiyun 			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 		if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
104*4882a593Smuzhiyun 			err(EXIT_FAILURE, "pthread_create");
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
toggle_done(int sig __maybe_unused,siginfo_t * info __maybe_unused,void * uc __maybe_unused)108*4882a593Smuzhiyun static void toggle_done(int sig __maybe_unused,
109*4882a593Smuzhiyun 			siginfo_t *info __maybe_unused,
110*4882a593Smuzhiyun 			void *uc __maybe_unused)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	done = true;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
bench_futex_requeue(int argc,const char ** argv)115*4882a593Smuzhiyun int bench_futex_requeue(int argc, const char **argv)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	int ret = 0;
118*4882a593Smuzhiyun 	unsigned int i, j;
119*4882a593Smuzhiyun 	struct sigaction act;
120*4882a593Smuzhiyun 	pthread_attr_t thread_attr;
121*4882a593Smuzhiyun 	struct perf_cpu_map *cpu;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
124*4882a593Smuzhiyun 	if (argc)
125*4882a593Smuzhiyun 		goto err;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	cpu = perf_cpu_map__new(NULL);
128*4882a593Smuzhiyun 	if (!cpu)
129*4882a593Smuzhiyun 		err(EXIT_FAILURE, "cpu_map__new");
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	memset(&act, 0, sizeof(act));
132*4882a593Smuzhiyun 	sigfillset(&act.sa_mask);
133*4882a593Smuzhiyun 	act.sa_sigaction = toggle_done;
134*4882a593Smuzhiyun 	sigaction(SIGINT, &act, NULL);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	if (!nthreads)
137*4882a593Smuzhiyun 		nthreads = cpu->nr;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	worker = calloc(nthreads, sizeof(*worker));
140*4882a593Smuzhiyun 	if (!worker)
141*4882a593Smuzhiyun 		err(EXIT_FAILURE, "calloc");
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	if (!fshared)
144*4882a593Smuzhiyun 		futex_flag = FUTEX_PRIVATE_FLAG;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	if (nrequeue > nthreads)
147*4882a593Smuzhiyun 		nrequeue = nthreads;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
150*4882a593Smuzhiyun 	       "%d at a time.\n\n",  getpid(), nthreads,
151*4882a593Smuzhiyun 	       fshared ? "shared":"private", &futex1, &futex2, nrequeue);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	init_stats(&requeued_stats);
154*4882a593Smuzhiyun 	init_stats(&requeuetime_stats);
155*4882a593Smuzhiyun 	pthread_attr_init(&thread_attr);
156*4882a593Smuzhiyun 	pthread_mutex_init(&thread_lock, NULL);
157*4882a593Smuzhiyun 	pthread_cond_init(&thread_parent, NULL);
158*4882a593Smuzhiyun 	pthread_cond_init(&thread_worker, NULL);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	for (j = 0; j < bench_repeat && !done; j++) {
161*4882a593Smuzhiyun 		unsigned int nrequeued = 0;
162*4882a593Smuzhiyun 		struct timeval start, end, runtime;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 		/* create, launch & block all threads */
165*4882a593Smuzhiyun 		block_threads(worker, thread_attr, cpu);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 		/* make sure all threads are already blocked */
168*4882a593Smuzhiyun 		pthread_mutex_lock(&thread_lock);
169*4882a593Smuzhiyun 		while (threads_starting)
170*4882a593Smuzhiyun 			pthread_cond_wait(&thread_parent, &thread_lock);
171*4882a593Smuzhiyun 		pthread_cond_broadcast(&thread_worker);
172*4882a593Smuzhiyun 		pthread_mutex_unlock(&thread_lock);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 		usleep(100000);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 		/* Ok, all threads are patiently blocked, start requeueing */
177*4882a593Smuzhiyun 		gettimeofday(&start, NULL);
178*4882a593Smuzhiyun 		while (nrequeued < nthreads) {
179*4882a593Smuzhiyun 			/*
180*4882a593Smuzhiyun 			 * Do not wakeup any tasks blocked on futex1, allowing
181*4882a593Smuzhiyun 			 * us to really measure futex_wait functionality.
182*4882a593Smuzhiyun 			 */
183*4882a593Smuzhiyun 			nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0,
184*4882a593Smuzhiyun 						       nrequeue, futex_flag);
185*4882a593Smuzhiyun 		}
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 		gettimeofday(&end, NULL);
188*4882a593Smuzhiyun 		timersub(&end, &start, &runtime);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 		update_stats(&requeued_stats, nrequeued);
191*4882a593Smuzhiyun 		update_stats(&requeuetime_stats, runtime.tv_usec);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 		if (!silent) {
194*4882a593Smuzhiyun 			printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
195*4882a593Smuzhiyun 			       j + 1, nrequeued, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
196*4882a593Smuzhiyun 		}
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 		/* everybody should be blocked on futex2, wake'em up */
199*4882a593Smuzhiyun 		nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
200*4882a593Smuzhiyun 		if (nthreads != nrequeued)
201*4882a593Smuzhiyun 			warnx("couldn't wakeup all tasks (%d/%d)", nrequeued, nthreads);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 		for (i = 0; i < nthreads; i++) {
204*4882a593Smuzhiyun 			ret = pthread_join(worker[i], NULL);
205*4882a593Smuzhiyun 			if (ret)
206*4882a593Smuzhiyun 				err(EXIT_FAILURE, "pthread_join");
207*4882a593Smuzhiyun 		}
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* cleanup & report results */
211*4882a593Smuzhiyun 	pthread_cond_destroy(&thread_parent);
212*4882a593Smuzhiyun 	pthread_cond_destroy(&thread_worker);
213*4882a593Smuzhiyun 	pthread_mutex_destroy(&thread_lock);
214*4882a593Smuzhiyun 	pthread_attr_destroy(&thread_attr);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	print_summary();
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	free(worker);
219*4882a593Smuzhiyun 	perf_cpu_map__put(cpu);
220*4882a593Smuzhiyun 	return ret;
221*4882a593Smuzhiyun err:
222*4882a593Smuzhiyun 	usage_with_options(bench_futex_requeue_usage, options);
223*4882a593Smuzhiyun 	exit(EXIT_FAILURE);
224*4882a593Smuzhiyun }
225