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