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
workerfn(void * arg)63 static void *workerfn(void *arg)
64 {
65 int ret;
66 unsigned int i;
67 struct worker *w = (struct worker *) arg;
68
69 pthread_mutex_lock(&thread_lock);
70 threads_starting--;
71 if (!threads_starting)
72 pthread_cond_signal(&thread_parent);
73 pthread_cond_wait(&thread_worker, &thread_lock);
74 pthread_mutex_unlock(&thread_lock);
75
76 do {
77 for (i = 0; i < nfutexes; i++, w->ops++) {
78 /*
79 * We want the futex calls to fail in order to stress
80 * the hashing of uaddr and not measure other steps,
81 * such as internal waitqueue handling, thus enlarging
82 * the critical region protected by hb->lock.
83 */
84 ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
85 if (!silent &&
86 (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
87 warn("Non-expected futex return call");
88 }
89 } while (!done);
90
91 return NULL;
92 }
93
toggle_done(int sig __maybe_unused,siginfo_t * info __maybe_unused,void * uc __maybe_unused)94 static void toggle_done(int sig __maybe_unused,
95 siginfo_t *info __maybe_unused,
96 void *uc __maybe_unused)
97 {
98 /* inform all threads that we're done for the day */
99 done = true;
100 gettimeofday(&end, NULL);
101 timersub(&end, &start, &runtime);
102 }
103
print_summary(void)104 static void print_summary(void)
105 {
106 unsigned long avg = avg_stats(&throughput_stats);
107 double stddev = stddev_stats(&throughput_stats);
108
109 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
110 !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
111 (int) runtime.tv_sec);
112 }
113
bench_futex_hash(int argc,const char ** argv,const char * prefix __maybe_unused)114 int bench_futex_hash(int argc, const char **argv,
115 const char *prefix __maybe_unused)
116 {
117 int ret = 0;
118 cpu_set_t cpu;
119 struct sigaction act;
120 unsigned int i, ncpus;
121 pthread_attr_t thread_attr;
122 struct worker *worker = NULL;
123
124 argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
125 if (argc) {
126 usage_with_options(bench_futex_hash_usage, options);
127 exit(EXIT_FAILURE);
128 }
129
130 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
131
132 sigfillset(&act.sa_mask);
133 act.sa_sigaction = toggle_done;
134 sigaction(SIGINT, &act, NULL);
135
136 if (!nthreads) /* default to the number of CPUs */
137 nthreads = ncpus;
138
139 worker = calloc(nthreads, sizeof(*worker));
140 if (!worker)
141 goto errmem;
142
143 if (!fshared)
144 futex_flag = FUTEX_PRIVATE_FLAG;
145
146 printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
147 getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
148
149 init_stats(&throughput_stats);
150 pthread_mutex_init(&thread_lock, NULL);
151 pthread_cond_init(&thread_parent, NULL);
152 pthread_cond_init(&thread_worker, NULL);
153
154 threads_starting = nthreads;
155 pthread_attr_init(&thread_attr);
156 gettimeofday(&start, NULL);
157 for (i = 0; i < nthreads; i++) {
158 worker[i].tid = i;
159 worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
160 if (!worker[i].futex)
161 goto errmem;
162
163 CPU_ZERO(&cpu);
164 CPU_SET(i % ncpus, &cpu);
165
166 ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu);
167 if (ret)
168 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
169
170 ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
171 (void *)(struct worker *) &worker[i]);
172 if (ret)
173 err(EXIT_FAILURE, "pthread_create");
174
175 }
176 pthread_attr_destroy(&thread_attr);
177
178 pthread_mutex_lock(&thread_lock);
179 while (threads_starting)
180 pthread_cond_wait(&thread_parent, &thread_lock);
181 pthread_cond_broadcast(&thread_worker);
182 pthread_mutex_unlock(&thread_lock);
183
184 sleep(nsecs);
185 toggle_done(0, NULL, NULL);
186
187 for (i = 0; i < nthreads; i++) {
188 ret = pthread_join(worker[i].thread, NULL);
189 if (ret)
190 err(EXIT_FAILURE, "pthread_join");
191 }
192
193 /* cleanup & report results */
194 pthread_cond_destroy(&thread_parent);
195 pthread_cond_destroy(&thread_worker);
196 pthread_mutex_destroy(&thread_lock);
197
198 for (i = 0; i < nthreads; i++) {
199 unsigned long t = worker[i].ops/runtime.tv_sec;
200 update_stats(&throughput_stats, t);
201 if (!silent) {
202 if (nfutexes == 1)
203 printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
204 worker[i].tid, &worker[i].futex[0], t);
205 else
206 printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
207 worker[i].tid, &worker[i].futex[0],
208 &worker[i].futex[nfutexes-1], t);
209 }
210
211 free(worker[i].futex);
212 }
213
214 print_summary();
215
216 free(worker);
217 return ret;
218 errmem:
219 err(EXIT_FAILURE, "calloc");
220 }
221