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.
10 /* For the CLR_() macros */
14 #include "../util/stat.h"
15 #include <subcmd/parse-options.h>
16 #include <linux/compiler.h>
17 #include <linux/kernel.h>
29 struct timeval runtime
;
32 static unsigned int nwakes
= 1;
34 /* all threads will block on the same futex -- hash bucket chaos ;) */
35 static u_int32_t futex
= 0;
37 static pthread_t
*blocked_worker
;
38 static bool done
= false, silent
= false, fshared
= false;
39 static unsigned int nblocked_threads
= 0, nwaking_threads
= 0;
40 static pthread_mutex_t thread_lock
;
41 static pthread_cond_t thread_parent
, thread_worker
;
42 static struct stats waketime_stats
, wakeup_stats
;
43 static unsigned int ncpus
, threads_starting
;
44 static int futex_flag
= 0;
46 static const struct option options
[] = {
47 OPT_UINTEGER('t', "threads", &nblocked_threads
, "Specify amount of threads"),
48 OPT_UINTEGER('w', "nwakers", &nwaking_threads
, "Specify amount of waking threads"),
49 OPT_BOOLEAN( 's', "silent", &silent
, "Silent mode: do not display data/details"),
50 OPT_BOOLEAN( 'S', "shared", &fshared
, "Use shared futexes instead of private ones"),
54 static const char * const bench_futex_wake_parallel_usage
[] = {
55 "perf bench futex wake-parallel <options>",
59 static void *waking_workerfn(void *arg
)
61 struct thread_data
*waker
= (struct thread_data
*) arg
;
62 struct timeval start
, end
;
64 gettimeofday(&start
, NULL
);
66 waker
->nwoken
= futex_wake(&futex
, nwakes
, futex_flag
);
67 if (waker
->nwoken
!= nwakes
)
68 warnx("couldn't wakeup all tasks (%d/%d)",
69 waker
->nwoken
, nwakes
);
71 gettimeofday(&end
, NULL
);
72 timersub(&end
, &start
, &waker
->runtime
);
78 static void wakeup_threads(struct thread_data
*td
, pthread_attr_t thread_attr
)
82 pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_JOINABLE
);
84 /* create and block all threads */
85 for (i
= 0; i
< nwaking_threads
; i
++) {
87 * Thread creation order will impact per-thread latency
88 * as it will affect the order to acquire the hb spinlock.
89 * For now let the scheduler decide.
91 if (pthread_create(&td
[i
].worker
, &thread_attr
,
92 waking_workerfn
, (void *)&td
[i
]))
93 err(EXIT_FAILURE
, "pthread_create");
96 for (i
= 0; i
< nwaking_threads
; i
++)
97 if (pthread_join(td
[i
].worker
, NULL
))
98 err(EXIT_FAILURE
, "pthread_join");
101 static void *blocked_workerfn(void *arg __maybe_unused
)
103 pthread_mutex_lock(&thread_lock
);
105 if (!threads_starting
)
106 pthread_cond_signal(&thread_parent
);
107 pthread_cond_wait(&thread_worker
, &thread_lock
);
108 pthread_mutex_unlock(&thread_lock
);
110 while (1) { /* handle spurious wakeups */
111 if (futex_wait(&futex
, 0, NULL
, futex_flag
) != EINTR
)
119 static void block_threads(pthread_t
*w
, pthread_attr_t thread_attr
)
124 threads_starting
= nblocked_threads
;
126 /* create and block all threads */
127 for (i
= 0; i
< nblocked_threads
; i
++) {
129 CPU_SET(i
% ncpus
, &cpu
);
131 if (pthread_attr_setaffinity_np(&thread_attr
, sizeof(cpu_set_t
), &cpu
))
132 err(EXIT_FAILURE
, "pthread_attr_setaffinity_np");
134 if (pthread_create(&w
[i
], &thread_attr
, blocked_workerfn
, NULL
))
135 err(EXIT_FAILURE
, "pthread_create");
139 static void print_run(struct thread_data
*waking_worker
, unsigned int run_num
)
141 unsigned int i
, wakeup_avg
;
142 double waketime_avg
, waketime_stddev
;
143 struct stats __waketime_stats
, __wakeup_stats
;
145 init_stats(&__wakeup_stats
);
146 init_stats(&__waketime_stats
);
148 for (i
= 0; i
< nwaking_threads
; i
++) {
149 update_stats(&__waketime_stats
, waking_worker
[i
].runtime
.tv_usec
);
150 update_stats(&__wakeup_stats
, waking_worker
[i
].nwoken
);
153 waketime_avg
= avg_stats(&__waketime_stats
);
154 waketime_stddev
= stddev_stats(&__waketime_stats
);
155 wakeup_avg
= avg_stats(&__wakeup_stats
);
157 printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
158 "in %.4f ms (+-%.2f%%)\n", run_num
+ 1, wakeup_avg
,
159 nblocked_threads
, waketime_avg
/1e3
,
160 rel_stddev_stats(waketime_stddev
, waketime_avg
));
163 static void print_summary(void)
165 unsigned int wakeup_avg
;
166 double waketime_avg
, waketime_stddev
;
168 waketime_avg
= avg_stats(&waketime_stats
);
169 waketime_stddev
= stddev_stats(&waketime_stats
);
170 wakeup_avg
= avg_stats(&wakeup_stats
);
172 printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
176 rel_stddev_stats(waketime_stddev
, waketime_avg
));
180 static void do_run_stats(struct thread_data
*waking_worker
)
184 for (i
= 0; i
< nwaking_threads
; i
++) {
185 update_stats(&waketime_stats
, waking_worker
[i
].runtime
.tv_usec
);
186 update_stats(&wakeup_stats
, waking_worker
[i
].nwoken
);
191 static void toggle_done(int sig __maybe_unused
,
192 siginfo_t
*info __maybe_unused
,
193 void *uc __maybe_unused
)
198 int bench_futex_wake_parallel(int argc
, const char **argv
,
199 const char *prefix __maybe_unused
)
203 struct sigaction act
;
204 pthread_attr_t thread_attr
;
205 struct thread_data
*waking_worker
;
207 argc
= parse_options(argc
, argv
, options
,
208 bench_futex_wake_parallel_usage
, 0);
210 usage_with_options(bench_futex_wake_parallel_usage
, options
);
214 sigfillset(&act
.sa_mask
);
215 act
.sa_sigaction
= toggle_done
;
216 sigaction(SIGINT
, &act
, NULL
);
218 ncpus
= sysconf(_SC_NPROCESSORS_ONLN
);
219 if (!nblocked_threads
)
220 nblocked_threads
= ncpus
;
222 /* some sanity checks */
223 if (nwaking_threads
> nblocked_threads
|| !nwaking_threads
)
224 nwaking_threads
= nblocked_threads
;
226 if (nblocked_threads
% nwaking_threads
)
227 errx(EXIT_FAILURE
, "Must be perfectly divisible");
229 * Each thread will wakeup nwakes tasks in
230 * a single futex_wait call.
232 nwakes
= nblocked_threads
/nwaking_threads
;
234 blocked_worker
= calloc(nblocked_threads
, sizeof(*blocked_worker
));
236 err(EXIT_FAILURE
, "calloc");
239 futex_flag
= FUTEX_PRIVATE_FLAG
;
241 printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
242 "futex %p), %d threads waking up %d at a time.\n\n",
243 getpid(), nblocked_threads
, fshared
? "shared":"private",
244 &futex
, nwaking_threads
, nwakes
);
246 init_stats(&wakeup_stats
);
247 init_stats(&waketime_stats
);
249 pthread_attr_init(&thread_attr
);
250 pthread_mutex_init(&thread_lock
, NULL
);
251 pthread_cond_init(&thread_parent
, NULL
);
252 pthread_cond_init(&thread_worker
, NULL
);
254 for (j
= 0; j
< bench_repeat
&& !done
; j
++) {
255 waking_worker
= calloc(nwaking_threads
, sizeof(*waking_worker
));
257 err(EXIT_FAILURE
, "calloc");
259 /* create, launch & block all threads */
260 block_threads(blocked_worker
, thread_attr
);
262 /* make sure all threads are already blocked */
263 pthread_mutex_lock(&thread_lock
);
264 while (threads_starting
)
265 pthread_cond_wait(&thread_parent
, &thread_lock
);
266 pthread_cond_broadcast(&thread_worker
);
267 pthread_mutex_unlock(&thread_lock
);
271 /* Ok, all threads are patiently blocked, start waking folks up */
272 wakeup_threads(waking_worker
, thread_attr
);
274 for (i
= 0; i
< nblocked_threads
; i
++) {
275 ret
= pthread_join(blocked_worker
[i
], NULL
);
277 err(EXIT_FAILURE
, "pthread_join");
280 do_run_stats(waking_worker
);
282 print_run(waking_worker
, j
);
287 /* cleanup & report results */
288 pthread_cond_destroy(&thread_parent
);
289 pthread_cond_destroy(&thread_worker
);
290 pthread_mutex_destroy(&thread_lock
);
291 pthread_attr_destroy(&thread_attr
);
295 free(blocked_worker
);