2 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
4 * futex-wake: Block a bunch of threads on a futex and wake'em up, N at a time.
6 * This program is particularly useful to measure the latency of nthread wakeups
7 * in non-error situations: all waiters are queued and all wake calls wakeup
8 * one or more tasks, and thus the waitqueue is never empty.
11 /* For the CLR_() macros */
15 #include "../util/stat.h"
16 #include <subcmd/parse-options.h>
17 #include <linux/compiler.h>
18 #include <linux/kernel.h>
27 /* all threads will block on the same futex */
28 static u_int32_t futex1
= 0;
31 * How many wakeups to do at a time.
32 * Default to 1 in order to make the kernel work more.
34 static unsigned int nwakes
= 1;
37 static bool done
= false, silent
= false, fshared
= false;
38 static pthread_mutex_t thread_lock
;
39 static pthread_cond_t thread_parent
, thread_worker
;
40 static struct stats waketime_stats
, wakeup_stats
;
41 static unsigned int ncpus
, threads_starting
, nthreads
= 0;
42 static int futex_flag
= 0;
44 static const struct option options
[] = {
45 OPT_UINTEGER('t', "threads", &nthreads
, "Specify amount of threads"),
46 OPT_UINTEGER('w', "nwakes", &nwakes
, "Specify amount of threads to wake at once"),
47 OPT_BOOLEAN( 's', "silent", &silent
, "Silent mode: do not display data/details"),
48 OPT_BOOLEAN( 'S', "shared", &fshared
, "Use shared futexes instead of private ones"),
52 static const char * const bench_futex_wake_usage
[] = {
53 "perf bench futex wake <options>",
57 static void *workerfn(void *arg __maybe_unused
)
59 pthread_mutex_lock(&thread_lock
);
61 if (!threads_starting
)
62 pthread_cond_signal(&thread_parent
);
63 pthread_cond_wait(&thread_worker
, &thread_lock
);
64 pthread_mutex_unlock(&thread_lock
);
67 if (futex_wait(&futex1
, 0, NULL
, futex_flag
) != EINTR
)
75 static void print_summary(void)
77 double waketime_avg
= avg_stats(&waketime_stats
);
78 double waketime_stddev
= stddev_stats(&waketime_stats
);
79 unsigned int wakeup_avg
= avg_stats(&wakeup_stats
);
81 printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
85 rel_stddev_stats(waketime_stddev
, waketime_avg
));
88 static void block_threads(pthread_t
*w
,
89 pthread_attr_t thread_attr
)
94 threads_starting
= nthreads
;
96 /* create and block all threads */
97 for (i
= 0; i
< nthreads
; i
++) {
99 CPU_SET(i
% ncpus
, &cpu
);
101 if (pthread_attr_setaffinity_np(&thread_attr
, sizeof(cpu_set_t
), &cpu
))
102 err(EXIT_FAILURE
, "pthread_attr_setaffinity_np");
104 if (pthread_create(&w
[i
], &thread_attr
, workerfn
, NULL
))
105 err(EXIT_FAILURE
, "pthread_create");
109 static void toggle_done(int sig __maybe_unused
,
110 siginfo_t
*info __maybe_unused
,
111 void *uc __maybe_unused
)
116 int bench_futex_wake(int argc
, const char **argv
,
117 const char *prefix __maybe_unused
)
121 struct sigaction act
;
122 pthread_attr_t thread_attr
;
124 argc
= parse_options(argc
, argv
, options
, bench_futex_wake_usage
, 0);
126 usage_with_options(bench_futex_wake_usage
, options
);
130 ncpus
= sysconf(_SC_NPROCESSORS_ONLN
);
132 sigfillset(&act
.sa_mask
);
133 act
.sa_sigaction
= toggle_done
;
134 sigaction(SIGINT
, &act
, NULL
);
139 worker
= calloc(nthreads
, sizeof(*worker
));
141 err(EXIT_FAILURE
, "calloc");
144 futex_flag
= FUTEX_PRIVATE_FLAG
;
146 printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
147 "waking up %d at a time.\n\n",
148 getpid(), nthreads
, fshared
? "shared":"private", &futex1
, nwakes
);
150 init_stats(&wakeup_stats
);
151 init_stats(&waketime_stats
);
152 pthread_attr_init(&thread_attr
);
153 pthread_mutex_init(&thread_lock
, NULL
);
154 pthread_cond_init(&thread_parent
, NULL
);
155 pthread_cond_init(&thread_worker
, NULL
);
157 for (j
= 0; j
< bench_repeat
&& !done
; j
++) {
158 unsigned int nwoken
= 0;
159 struct timeval start
, end
, runtime
;
161 /* create, launch & block all threads */
162 block_threads(worker
, thread_attr
);
164 /* make sure all threads are already blocked */
165 pthread_mutex_lock(&thread_lock
);
166 while (threads_starting
)
167 pthread_cond_wait(&thread_parent
, &thread_lock
);
168 pthread_cond_broadcast(&thread_worker
);
169 pthread_mutex_unlock(&thread_lock
);
173 /* Ok, all threads are patiently blocked, start waking folks up */
174 gettimeofday(&start
, NULL
);
175 while (nwoken
!= nthreads
)
176 nwoken
+= futex_wake(&futex1
, nwakes
, futex_flag
);
177 gettimeofday(&end
, NULL
);
178 timersub(&end
, &start
, &runtime
);
180 update_stats(&wakeup_stats
, nwoken
);
181 update_stats(&waketime_stats
, runtime
.tv_usec
);
184 printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
185 j
+ 1, nwoken
, nthreads
, runtime
.tv_usec
/1e3
);
188 for (i
= 0; i
< nthreads
; i
++) {
189 ret
= pthread_join(worker
[i
], NULL
);
191 err(EXIT_FAILURE
, "pthread_join");
196 /* cleanup & report results */
197 pthread_cond_destroy(&thread_parent
);
198 pthread_cond_destroy(&thread_worker
);
199 pthread_mutex_destroy(&thread_lock
);
200 pthread_attr_destroy(&thread_attr
);