2 * Copyright (C) 2015 Davidlohr Bueso.
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.
11 #include "../util/util.h"
12 #include "../util/stat.h"
13 #include <subcmd/parse-options.h>
14 #include "../util/header.h"
26 struct timeval runtime
;
29 static unsigned int nwakes
= 1;
31 /* all threads will block on the same futex -- hash bucket chaos ;) */
32 static u_int32_t futex
= 0;
34 static pthread_t
*blocked_worker
;
35 static bool done
= false, silent
= false, fshared
= false;
36 static unsigned int nblocked_threads
= 0, nwaking_threads
= 0;
37 static pthread_mutex_t thread_lock
;
38 static pthread_cond_t thread_parent
, thread_worker
;
39 static struct stats waketime_stats
, wakeup_stats
;
40 static unsigned int ncpus
, threads_starting
;
41 static int futex_flag
= 0;
43 static const struct option options
[] = {
44 OPT_UINTEGER('t', "threads", &nblocked_threads
, "Specify amount of threads"),
45 OPT_UINTEGER('w', "nwakers", &nwaking_threads
, "Specify amount of waking threads"),
46 OPT_BOOLEAN( 's', "silent", &silent
, "Silent mode: do not display data/details"),
47 OPT_BOOLEAN( 'S', "shared", &fshared
, "Use shared futexes instead of private ones"),
51 static const char * const bench_futex_wake_parallel_usage
[] = {
52 "perf bench futex wake-parallel <options>",
56 static void *waking_workerfn(void *arg
)
58 struct thread_data
*waker
= (struct thread_data
*) arg
;
59 struct timeval start
, end
;
61 gettimeofday(&start
, NULL
);
63 waker
->nwoken
= futex_wake(&futex
, nwakes
, futex_flag
);
64 if (waker
->nwoken
!= nwakes
)
65 warnx("couldn't wakeup all tasks (%d/%d)",
66 waker
->nwoken
, nwakes
);
68 gettimeofday(&end
, NULL
);
69 timersub(&end
, &start
, &waker
->runtime
);
75 static void wakeup_threads(struct thread_data
*td
, pthread_attr_t thread_attr
)
79 pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_JOINABLE
);
81 /* create and block all threads */
82 for (i
= 0; i
< nwaking_threads
; i
++) {
84 * Thread creation order will impact per-thread latency
85 * as it will affect the order to acquire the hb spinlock.
86 * For now let the scheduler decide.
88 if (pthread_create(&td
[i
].worker
, &thread_attr
,
89 waking_workerfn
, (void *)&td
[i
]))
90 err(EXIT_FAILURE
, "pthread_create");
93 for (i
= 0; i
< nwaking_threads
; i
++)
94 if (pthread_join(td
[i
].worker
, NULL
))
95 err(EXIT_FAILURE
, "pthread_join");
98 static void *blocked_workerfn(void *arg __maybe_unused
)
100 pthread_mutex_lock(&thread_lock
);
102 if (!threads_starting
)
103 pthread_cond_signal(&thread_parent
);
104 pthread_cond_wait(&thread_worker
, &thread_lock
);
105 pthread_mutex_unlock(&thread_lock
);
107 while (1) { /* handle spurious wakeups */
108 if (futex_wait(&futex
, 0, NULL
, futex_flag
) != EINTR
)
116 static void block_threads(pthread_t
*w
, pthread_attr_t thread_attr
)
121 threads_starting
= nblocked_threads
;
123 /* create and block all threads */
124 for (i
= 0; i
< nblocked_threads
; i
++) {
126 CPU_SET(i
% ncpus
, &cpu
);
128 if (pthread_attr_setaffinity_np(&thread_attr
, sizeof(cpu_set_t
), &cpu
))
129 err(EXIT_FAILURE
, "pthread_attr_setaffinity_np");
131 if (pthread_create(&w
[i
], &thread_attr
, blocked_workerfn
, NULL
))
132 err(EXIT_FAILURE
, "pthread_create");
136 static void print_run(struct thread_data
*waking_worker
, unsigned int run_num
)
138 unsigned int i
, wakeup_avg
;
139 double waketime_avg
, waketime_stddev
;
140 struct stats __waketime_stats
, __wakeup_stats
;
142 init_stats(&__wakeup_stats
);
143 init_stats(&__waketime_stats
);
145 for (i
= 0; i
< nwaking_threads
; i
++) {
146 update_stats(&__waketime_stats
, waking_worker
[i
].runtime
.tv_usec
);
147 update_stats(&__wakeup_stats
, waking_worker
[i
].nwoken
);
150 waketime_avg
= avg_stats(&__waketime_stats
);
151 waketime_stddev
= stddev_stats(&__waketime_stats
);
152 wakeup_avg
= avg_stats(&__wakeup_stats
);
154 printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
155 "in %.4f ms (+-%.2f%%)\n", run_num
+ 1, wakeup_avg
,
156 nblocked_threads
, waketime_avg
/1e3
,
157 rel_stddev_stats(waketime_stddev
, waketime_avg
));
160 static void print_summary(void)
162 unsigned int wakeup_avg
;
163 double waketime_avg
, waketime_stddev
;
165 waketime_avg
= avg_stats(&waketime_stats
);
166 waketime_stddev
= stddev_stats(&waketime_stats
);
167 wakeup_avg
= avg_stats(&wakeup_stats
);
169 printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
173 rel_stddev_stats(waketime_stddev
, waketime_avg
));
177 static void do_run_stats(struct thread_data
*waking_worker
)
181 for (i
= 0; i
< nwaking_threads
; i
++) {
182 update_stats(&waketime_stats
, waking_worker
[i
].runtime
.tv_usec
);
183 update_stats(&wakeup_stats
, waking_worker
[i
].nwoken
);
188 static void toggle_done(int sig __maybe_unused
,
189 siginfo_t
*info __maybe_unused
,
190 void *uc __maybe_unused
)
195 int bench_futex_wake_parallel(int argc
, const char **argv
,
196 const char *prefix __maybe_unused
)
200 struct sigaction act
;
201 pthread_attr_t thread_attr
;
202 struct thread_data
*waking_worker
;
204 argc
= parse_options(argc
, argv
, options
,
205 bench_futex_wake_parallel_usage
, 0);
207 usage_with_options(bench_futex_wake_parallel_usage
, options
);
211 sigfillset(&act
.sa_mask
);
212 act
.sa_sigaction
= toggle_done
;
213 sigaction(SIGINT
, &act
, NULL
);
215 ncpus
= sysconf(_SC_NPROCESSORS_ONLN
);
216 if (!nblocked_threads
)
217 nblocked_threads
= ncpus
;
219 /* some sanity checks */
220 if (nwaking_threads
> nblocked_threads
|| !nwaking_threads
)
221 nwaking_threads
= nblocked_threads
;
223 if (nblocked_threads
% nwaking_threads
)
224 errx(EXIT_FAILURE
, "Must be perfectly divisible");
226 * Each thread will wakeup nwakes tasks in
227 * a single futex_wait call.
229 nwakes
= nblocked_threads
/nwaking_threads
;
231 blocked_worker
= calloc(nblocked_threads
, sizeof(*blocked_worker
));
233 err(EXIT_FAILURE
, "calloc");
236 futex_flag
= FUTEX_PRIVATE_FLAG
;
238 printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
239 "futex %p), %d threads waking up %d at a time.\n\n",
240 getpid(), nblocked_threads
, fshared
? "shared":"private",
241 &futex
, nwaking_threads
, nwakes
);
243 init_stats(&wakeup_stats
);
244 init_stats(&waketime_stats
);
246 pthread_attr_init(&thread_attr
);
247 pthread_mutex_init(&thread_lock
, NULL
);
248 pthread_cond_init(&thread_parent
, NULL
);
249 pthread_cond_init(&thread_worker
, NULL
);
251 for (j
= 0; j
< bench_repeat
&& !done
; j
++) {
252 waking_worker
= calloc(nwaking_threads
, sizeof(*waking_worker
));
254 err(EXIT_FAILURE
, "calloc");
256 /* create, launch & block all threads */
257 block_threads(blocked_worker
, thread_attr
);
259 /* make sure all threads are already blocked */
260 pthread_mutex_lock(&thread_lock
);
261 while (threads_starting
)
262 pthread_cond_wait(&thread_parent
, &thread_lock
);
263 pthread_cond_broadcast(&thread_worker
);
264 pthread_mutex_unlock(&thread_lock
);
268 /* Ok, all threads are patiently blocked, start waking folks up */
269 wakeup_threads(waking_worker
, thread_attr
);
271 for (i
= 0; i
< nblocked_threads
; i
++) {
272 ret
= pthread_join(blocked_worker
[i
], NULL
);
274 err(EXIT_FAILURE
, "pthread_join");
277 do_run_stats(waking_worker
);
279 print_run(waking_worker
, j
);
284 /* cleanup & report results */
285 pthread_cond_destroy(&thread_parent
);
286 pthread_cond_destroy(&thread_worker
);
287 pthread_mutex_destroy(&thread_lock
);
288 pthread_attr_destroy(&thread_attr
);
292 free(blocked_worker
);