Merge 'v4.12-rc1' into MTD
[linux-2.6-block.git] / tools / perf / bench / futex-wake-parallel.c
CommitLineData
d65817b4
DB
1/*
2 * Copyright (C) 2015 Davidlohr Bueso.
3 *
4 * Block a bunch of threads and let parallel waker threads wakeup an
5 * equal amount of them. The program output reflects the avg latency
6 * for each individual thread to service its share of work. Ultimately
7 * it can be used to measure futex_wake() changes.
8 */
9
8a158589 10/* For the CLR_() macros */
a0f213e1 11#include <string.h>
8a158589
ACM
12#include <pthread.h>
13
9c304f6c 14#include <signal.h>
d65817b4 15#include "../util/stat.h"
4b6ab94e 16#include <subcmd/parse-options.h>
86695f59 17#include <linux/compiler.h>
9c304f6c 18#include <linux/kernel.h>
565e6911 19#include <linux/time64.h>
9c304f6c 20#include <errno.h>
d65817b4
DB
21#include "bench.h"
22#include "futex.h"
23
24#include <err.h>
25#include <stdlib.h>
26#include <sys/time.h>
d65817b4
DB
27
28struct thread_data {
29 pthread_t worker;
30 unsigned int nwoken;
31 struct timeval runtime;
32};
33
34static unsigned int nwakes = 1;
35
36/* all threads will block on the same futex -- hash bucket chaos ;) */
37static u_int32_t futex = 0;
38
39static pthread_t *blocked_worker;
40static bool done = false, silent = false, fshared = false;
41static unsigned int nblocked_threads = 0, nwaking_threads = 0;
42static pthread_mutex_t thread_lock;
43static pthread_cond_t thread_parent, thread_worker;
44static struct stats waketime_stats, wakeup_stats;
45static unsigned int ncpus, threads_starting;
46static int futex_flag = 0;
47
48static const struct option options[] = {
49 OPT_UINTEGER('t', "threads", &nblocked_threads, "Specify amount of threads"),
50 OPT_UINTEGER('w', "nwakers", &nwaking_threads, "Specify amount of waking threads"),
51 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
52 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
53 OPT_END()
54};
55
56static const char * const bench_futex_wake_parallel_usage[] = {
57 "perf bench futex wake-parallel <options>",
58 NULL
59};
60
61static void *waking_workerfn(void *arg)
62{
63 struct thread_data *waker = (struct thread_data *) arg;
64 struct timeval start, end;
65
66 gettimeofday(&start, NULL);
67
68 waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
69 if (waker->nwoken != nwakes)
70 warnx("couldn't wakeup all tasks (%d/%d)",
71 waker->nwoken, nwakes);
72
73 gettimeofday(&end, NULL);
74 timersub(&end, &start, &waker->runtime);
75
76 pthread_exit(NULL);
77 return NULL;
78}
79
80static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
81{
82 unsigned int i;
83
84 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
85
86 /* create and block all threads */
87 for (i = 0; i < nwaking_threads; i++) {
88 /*
89 * Thread creation order will impact per-thread latency
90 * as it will affect the order to acquire the hb spinlock.
91 * For now let the scheduler decide.
92 */
93 if (pthread_create(&td[i].worker, &thread_attr,
94 waking_workerfn, (void *)&td[i]))
95 err(EXIT_FAILURE, "pthread_create");
96 }
97
98 for (i = 0; i < nwaking_threads; i++)
99 if (pthread_join(td[i].worker, NULL))
100 err(EXIT_FAILURE, "pthread_join");
101}
102
103static void *blocked_workerfn(void *arg __maybe_unused)
104{
105 pthread_mutex_lock(&thread_lock);
106 threads_starting--;
107 if (!threads_starting)
108 pthread_cond_signal(&thread_parent);
109 pthread_cond_wait(&thread_worker, &thread_lock);
110 pthread_mutex_unlock(&thread_lock);
111
112 while (1) { /* handle spurious wakeups */
113 if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
114 break;
115 }
116
117 pthread_exit(NULL);
118 return NULL;
119}
120
121static void block_threads(pthread_t *w, pthread_attr_t thread_attr)
122{
123 cpu_set_t cpu;
124 unsigned int i;
125
126 threads_starting = nblocked_threads;
127
128 /* create and block all threads */
129 for (i = 0; i < nblocked_threads; i++) {
130 CPU_ZERO(&cpu);
131 CPU_SET(i % ncpus, &cpu);
132
133 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
134 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
135
136 if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL))
137 err(EXIT_FAILURE, "pthread_create");
138 }
139}
140
141static void print_run(struct thread_data *waking_worker, unsigned int run_num)
142{
143 unsigned int i, wakeup_avg;
144 double waketime_avg, waketime_stddev;
145 struct stats __waketime_stats, __wakeup_stats;
146
147 init_stats(&__wakeup_stats);
148 init_stats(&__waketime_stats);
149
150 for (i = 0; i < nwaking_threads; i++) {
151 update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
152 update_stats(&__wakeup_stats, waking_worker[i].nwoken);
153 }
154
155 waketime_avg = avg_stats(&__waketime_stats);
156 waketime_stddev = stddev_stats(&__waketime_stats);
157 wakeup_avg = avg_stats(&__wakeup_stats);
158
159 printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
160 "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
565e6911 161 nblocked_threads, waketime_avg / USEC_PER_MSEC,
d65817b4
DB
162 rel_stddev_stats(waketime_stddev, waketime_avg));
163}
164
165static void print_summary(void)
166{
167 unsigned int wakeup_avg;
168 double waketime_avg, waketime_stddev;
169
170 waketime_avg = avg_stats(&waketime_stats);
171 waketime_stddev = stddev_stats(&waketime_stats);
172 wakeup_avg = avg_stats(&wakeup_stats);
173
174 printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
175 wakeup_avg,
176 nblocked_threads,
565e6911 177 waketime_avg / USEC_PER_MSEC,
d65817b4
DB
178 rel_stddev_stats(waketime_stddev, waketime_avg));
179}
180
181
182static void do_run_stats(struct thread_data *waking_worker)
183{
184 unsigned int i;
185
186 for (i = 0; i < nwaking_threads; i++) {
187 update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
188 update_stats(&wakeup_stats, waking_worker[i].nwoken);
189 }
190
191}
192
193static void toggle_done(int sig __maybe_unused,
194 siginfo_t *info __maybe_unused,
195 void *uc __maybe_unused)
196{
197 done = true;
198}
199
b0ad8ea6 200int bench_futex_wake_parallel(int argc, const char **argv)
d65817b4
DB
201{
202 int ret = 0;
203 unsigned int i, j;
204 struct sigaction act;
205 pthread_attr_t thread_attr;
206 struct thread_data *waking_worker;
207
208 argc = parse_options(argc, argv, options,
209 bench_futex_wake_parallel_usage, 0);
210 if (argc) {
211 usage_with_options(bench_futex_wake_parallel_usage, options);
212 exit(EXIT_FAILURE);
213 }
214
215 sigfillset(&act.sa_mask);
216 act.sa_sigaction = toggle_done;
217 sigaction(SIGINT, &act, NULL);
218
219 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
220 if (!nblocked_threads)
221 nblocked_threads = ncpus;
222
223 /* some sanity checks */
224 if (nwaking_threads > nblocked_threads || !nwaking_threads)
225 nwaking_threads = nblocked_threads;
226
227 if (nblocked_threads % nwaking_threads)
228 errx(EXIT_FAILURE, "Must be perfectly divisible");
229 /*
230 * Each thread will wakeup nwakes tasks in
231 * a single futex_wait call.
232 */
233 nwakes = nblocked_threads/nwaking_threads;
234
235 blocked_worker = calloc(nblocked_threads, sizeof(*blocked_worker));
236 if (!blocked_worker)
237 err(EXIT_FAILURE, "calloc");
238
239 if (!fshared)
240 futex_flag = FUTEX_PRIVATE_FLAG;
241
242 printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
243 "futex %p), %d threads waking up %d at a time.\n\n",
244 getpid(), nblocked_threads, fshared ? "shared":"private",
245 &futex, nwaking_threads, nwakes);
246
247 init_stats(&wakeup_stats);
248 init_stats(&waketime_stats);
249
250 pthread_attr_init(&thread_attr);
251 pthread_mutex_init(&thread_lock, NULL);
252 pthread_cond_init(&thread_parent, NULL);
253 pthread_cond_init(&thread_worker, NULL);
254
255 for (j = 0; j < bench_repeat && !done; j++) {
256 waking_worker = calloc(nwaking_threads, sizeof(*waking_worker));
257 if (!waking_worker)
258 err(EXIT_FAILURE, "calloc");
259
260 /* create, launch & block all threads */
261 block_threads(blocked_worker, thread_attr);
262
263 /* make sure all threads are already blocked */
264 pthread_mutex_lock(&thread_lock);
265 while (threads_starting)
266 pthread_cond_wait(&thread_parent, &thread_lock);
267 pthread_cond_broadcast(&thread_worker);
268 pthread_mutex_unlock(&thread_lock);
269
270 usleep(100000);
271
272 /* Ok, all threads are patiently blocked, start waking folks up */
273 wakeup_threads(waking_worker, thread_attr);
274
275 for (i = 0; i < nblocked_threads; i++) {
276 ret = pthread_join(blocked_worker[i], NULL);
277 if (ret)
278 err(EXIT_FAILURE, "pthread_join");
279 }
280
281 do_run_stats(waking_worker);
282 if (!silent)
283 print_run(waking_worker, j);
284
285 free(waking_worker);
286 }
287
288 /* cleanup & report results */
289 pthread_cond_destroy(&thread_parent);
290 pthread_cond_destroy(&thread_worker);
291 pthread_mutex_destroy(&thread_lock);
292 pthread_attr_destroy(&thread_attr);
293
294 print_summary();
295
296 free(blocked_worker);
297 return ret;
298}