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