xref: /OK3568_Linux_fs/kernel/tools/perf/bench/futex-hash.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-hash: Stress the hell out of the Linux kernel futex uaddr hashing.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This program is particularly useful for measuring the kernel's futex hash
8*4882a593Smuzhiyun  * table/function implementation. In order for it to make sense, use with as
9*4882a593Smuzhiyun  * many threads and futexes as possible.
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 <errno.h>
17*4882a593Smuzhiyun #include <signal.h>
18*4882a593Smuzhiyun #include <stdlib.h>
19*4882a593Smuzhiyun #include <linux/compiler.h>
20*4882a593Smuzhiyun #include <linux/kernel.h>
21*4882a593Smuzhiyun #include <linux/zalloc.h>
22*4882a593Smuzhiyun #include <sys/time.h>
23*4882a593Smuzhiyun #include <internal/cpumap.h>
24*4882a593Smuzhiyun #include <perf/cpumap.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include "../util/stat.h"
27*4882a593Smuzhiyun #include <subcmd/parse-options.h>
28*4882a593Smuzhiyun #include "bench.h"
29*4882a593Smuzhiyun #include "futex.h"
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <err.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static unsigned int nthreads = 0;
34*4882a593Smuzhiyun static unsigned int nsecs    = 10;
35*4882a593Smuzhiyun /* amount of futexes per thread */
36*4882a593Smuzhiyun static unsigned int nfutexes = 1024;
37*4882a593Smuzhiyun static bool fshared = false, done = false, silent = false;
38*4882a593Smuzhiyun static int futex_flag = 0;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun struct timeval bench__start, bench__end, bench__runtime;
41*4882a593Smuzhiyun static pthread_mutex_t thread_lock;
42*4882a593Smuzhiyun static unsigned int threads_starting;
43*4882a593Smuzhiyun static struct stats throughput_stats;
44*4882a593Smuzhiyun static pthread_cond_t thread_parent, thread_worker;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun struct worker {
47*4882a593Smuzhiyun 	int tid;
48*4882a593Smuzhiyun 	u_int32_t *futex;
49*4882a593Smuzhiyun 	pthread_t thread;
50*4882a593Smuzhiyun 	unsigned long ops;
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static const struct option options[] = {
54*4882a593Smuzhiyun 	OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
55*4882a593Smuzhiyun 	OPT_UINTEGER('r', "runtime", &nsecs,    "Specify runtime (in seconds)"),
56*4882a593Smuzhiyun 	OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"),
57*4882a593Smuzhiyun 	OPT_BOOLEAN( 's', "silent",  &silent,   "Silent mode: do not display data/details"),
58*4882a593Smuzhiyun 	OPT_BOOLEAN( 'S', "shared",  &fshared,  "Use shared futexes instead of private ones"),
59*4882a593Smuzhiyun 	OPT_END()
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun static const char * const bench_futex_hash_usage[] = {
63*4882a593Smuzhiyun 	"perf bench futex hash <options>",
64*4882a593Smuzhiyun 	NULL
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun 
workerfn(void * arg)67*4882a593Smuzhiyun static void *workerfn(void *arg)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	int ret;
70*4882a593Smuzhiyun 	struct worker *w = (struct worker *) arg;
71*4882a593Smuzhiyun 	unsigned int i;
72*4882a593Smuzhiyun 	unsigned long ops = w->ops; /* avoid cacheline bouncing */
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	pthread_mutex_lock(&thread_lock);
75*4882a593Smuzhiyun 	threads_starting--;
76*4882a593Smuzhiyun 	if (!threads_starting)
77*4882a593Smuzhiyun 		pthread_cond_signal(&thread_parent);
78*4882a593Smuzhiyun 	pthread_cond_wait(&thread_worker, &thread_lock);
79*4882a593Smuzhiyun 	pthread_mutex_unlock(&thread_lock);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	do {
82*4882a593Smuzhiyun 		for (i = 0; i < nfutexes; i++, ops++) {
83*4882a593Smuzhiyun 			/*
84*4882a593Smuzhiyun 			 * We want the futex calls to fail in order to stress
85*4882a593Smuzhiyun 			 * the hashing of uaddr and not measure other steps,
86*4882a593Smuzhiyun 			 * such as internal waitqueue handling, thus enlarging
87*4882a593Smuzhiyun 			 * the critical region protected by hb->lock.
88*4882a593Smuzhiyun 			 */
89*4882a593Smuzhiyun 			ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
90*4882a593Smuzhiyun 			if (!silent &&
91*4882a593Smuzhiyun 			    (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
92*4882a593Smuzhiyun 				warn("Non-expected futex return call");
93*4882a593Smuzhiyun 		}
94*4882a593Smuzhiyun 	}  while (!done);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	w->ops = ops;
97*4882a593Smuzhiyun 	return NULL;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
toggle_done(int sig __maybe_unused,siginfo_t * info __maybe_unused,void * uc __maybe_unused)100*4882a593Smuzhiyun static void toggle_done(int sig __maybe_unused,
101*4882a593Smuzhiyun 			siginfo_t *info __maybe_unused,
102*4882a593Smuzhiyun 			void *uc __maybe_unused)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	/* inform all threads that we're done for the day */
105*4882a593Smuzhiyun 	done = true;
106*4882a593Smuzhiyun 	gettimeofday(&bench__end, NULL);
107*4882a593Smuzhiyun 	timersub(&bench__end, &bench__start, &bench__runtime);
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun 
print_summary(void)110*4882a593Smuzhiyun static void print_summary(void)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	unsigned long avg = avg_stats(&throughput_stats);
113*4882a593Smuzhiyun 	double stddev = stddev_stats(&throughput_stats);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
116*4882a593Smuzhiyun 	       !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
117*4882a593Smuzhiyun 	       (int)bench__runtime.tv_sec);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
bench_futex_hash(int argc,const char ** argv)120*4882a593Smuzhiyun int bench_futex_hash(int argc, const char **argv)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	int ret = 0;
123*4882a593Smuzhiyun 	cpu_set_t cpuset;
124*4882a593Smuzhiyun 	struct sigaction act;
125*4882a593Smuzhiyun 	unsigned int i;
126*4882a593Smuzhiyun 	pthread_attr_t thread_attr;
127*4882a593Smuzhiyun 	struct worker *worker = NULL;
128*4882a593Smuzhiyun 	struct perf_cpu_map *cpu;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
131*4882a593Smuzhiyun 	if (argc) {
132*4882a593Smuzhiyun 		usage_with_options(bench_futex_hash_usage, options);
133*4882a593Smuzhiyun 		exit(EXIT_FAILURE);
134*4882a593Smuzhiyun 	}
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	cpu = perf_cpu_map__new(NULL);
137*4882a593Smuzhiyun 	if (!cpu)
138*4882a593Smuzhiyun 		goto errmem;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	memset(&act, 0, sizeof(act));
141*4882a593Smuzhiyun 	sigfillset(&act.sa_mask);
142*4882a593Smuzhiyun 	act.sa_sigaction = toggle_done;
143*4882a593Smuzhiyun 	sigaction(SIGINT, &act, NULL);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	if (!nthreads) /* default to the number of CPUs */
146*4882a593Smuzhiyun 		nthreads = cpu->nr;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	worker = calloc(nthreads, sizeof(*worker));
149*4882a593Smuzhiyun 	if (!worker)
150*4882a593Smuzhiyun 		goto errmem;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (!fshared)
153*4882a593Smuzhiyun 		futex_flag = FUTEX_PRIVATE_FLAG;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
156*4882a593Smuzhiyun 	       getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	init_stats(&throughput_stats);
159*4882a593Smuzhiyun 	pthread_mutex_init(&thread_lock, NULL);
160*4882a593Smuzhiyun 	pthread_cond_init(&thread_parent, NULL);
161*4882a593Smuzhiyun 	pthread_cond_init(&thread_worker, NULL);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	threads_starting = nthreads;
164*4882a593Smuzhiyun 	pthread_attr_init(&thread_attr);
165*4882a593Smuzhiyun 	gettimeofday(&bench__start, NULL);
166*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
167*4882a593Smuzhiyun 		worker[i].tid = i;
168*4882a593Smuzhiyun 		worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
169*4882a593Smuzhiyun 		if (!worker[i].futex)
170*4882a593Smuzhiyun 			goto errmem;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 		CPU_ZERO(&cpuset);
173*4882a593Smuzhiyun 		CPU_SET(cpu->map[i % cpu->nr], &cpuset);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 		ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset);
176*4882a593Smuzhiyun 		if (ret)
177*4882a593Smuzhiyun 			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 		ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
180*4882a593Smuzhiyun 				     (void *)(struct worker *) &worker[i]);
181*4882a593Smuzhiyun 		if (ret)
182*4882a593Smuzhiyun 			err(EXIT_FAILURE, "pthread_create");
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 	pthread_attr_destroy(&thread_attr);
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	pthread_mutex_lock(&thread_lock);
188*4882a593Smuzhiyun 	while (threads_starting)
189*4882a593Smuzhiyun 		pthread_cond_wait(&thread_parent, &thread_lock);
190*4882a593Smuzhiyun 	pthread_cond_broadcast(&thread_worker);
191*4882a593Smuzhiyun 	pthread_mutex_unlock(&thread_lock);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	sleep(nsecs);
194*4882a593Smuzhiyun 	toggle_done(0, NULL, NULL);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
197*4882a593Smuzhiyun 		ret = pthread_join(worker[i].thread, NULL);
198*4882a593Smuzhiyun 		if (ret)
199*4882a593Smuzhiyun 			err(EXIT_FAILURE, "pthread_join");
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/* cleanup & report results */
203*4882a593Smuzhiyun 	pthread_cond_destroy(&thread_parent);
204*4882a593Smuzhiyun 	pthread_cond_destroy(&thread_worker);
205*4882a593Smuzhiyun 	pthread_mutex_destroy(&thread_lock);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
208*4882a593Smuzhiyun 		unsigned long t = bench__runtime.tv_sec > 0 ?
209*4882a593Smuzhiyun 			worker[i].ops / bench__runtime.tv_sec : 0;
210*4882a593Smuzhiyun 		update_stats(&throughput_stats, t);
211*4882a593Smuzhiyun 		if (!silent) {
212*4882a593Smuzhiyun 			if (nfutexes == 1)
213*4882a593Smuzhiyun 				printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
214*4882a593Smuzhiyun 				       worker[i].tid, &worker[i].futex[0], t);
215*4882a593Smuzhiyun 			else
216*4882a593Smuzhiyun 				printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
217*4882a593Smuzhiyun 				       worker[i].tid, &worker[i].futex[0],
218*4882a593Smuzhiyun 				       &worker[i].futex[nfutexes-1], t);
219*4882a593Smuzhiyun 		}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 		zfree(&worker[i].futex);
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	print_summary();
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	free(worker);
227*4882a593Smuzhiyun 	free(cpu);
228*4882a593Smuzhiyun 	return ret;
229*4882a593Smuzhiyun errmem:
230*4882a593Smuzhiyun 	err(EXIT_FAILURE, "calloc");
231*4882a593Smuzhiyun }
232