1 // SPDX-License-Identifier: GPL-2.0
5 * futex-requeue: Block a bunch of threads on futex1 and requeue them
6 * on futex2, N at a time.
8 * This program is particularly useful to measure the latency of nthread
9 * requeues without waking up any tasks (in the non-pi case) -- thus
10 * mimicking a regular futex_wait.
13 /* For the CLR_() macros */
18 #include "../util/stat.h"
19 #include <subcmd/parse-options.h>
20 #include <linux/compiler.h>
21 #include <linux/kernel.h>
22 #include <linux/time64.h>
24 #include <perf/cpumap.h>
33 static u_int32_t futex1 = 0, futex2 = 0;
35 static pthread_t *worker;
36 static bool done = false;
37 static pthread_mutex_t thread_lock;
38 static pthread_cond_t thread_parent, thread_worker;
39 static struct stats requeuetime_stats, requeued_stats;
40 static unsigned int threads_starting;
41 static int futex_flag = 0;
43 static struct bench_futex_parameters params = {
45 * How many tasks to requeue at a time.
46 * Default to 1 in order to make the kernel work more.
51 static const struct option options[] = {
52 OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"),
53 OPT_UINTEGER('q', "nrequeue", ¶ms.nrequeue, "Specify amount of threads to requeue at once"),
54 OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"),
55 OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"),
56 OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"),
57 OPT_BOOLEAN( 'B', "broadcast", ¶ms.broadcast, "Requeue all threads at once"),
58 OPT_BOOLEAN( 'p', "pi", ¶ms.pi, "Use PI-aware variants of FUTEX_CMP_REQUEUE"),
63 static const char * const bench_futex_requeue_usage[] = {
64 "perf bench futex requeue <options>",
68 static void print_summary(void)
70 double requeuetime_avg = avg_stats(&requeuetime_stats);
71 double requeuetime_stddev = stddev_stats(&requeuetime_stats);
72 unsigned int requeued_avg = avg_stats(&requeued_stats);
74 printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
77 requeuetime_avg / USEC_PER_MSEC,
78 rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
81 static void *workerfn(void *arg __maybe_unused)
85 pthread_mutex_lock(&thread_lock);
87 if (!threads_starting)
88 pthread_cond_signal(&thread_parent);
89 pthread_cond_wait(&thread_worker, &thread_lock);
90 pthread_mutex_unlock(&thread_lock);
94 ret = futex_wait(&futex1, 0, NULL, futex_flag);
98 if (ret && errno != EAGAIN) {
104 ret = futex_wait_requeue_pi(&futex1, 0, &futex2,
107 /* got the lock at futex2 */
108 futex_unlock_pi(&futex2, futex_flag);
112 if (ret && errno != EAGAIN) {
114 warnx("futex_wait_requeue_pi");
123 static void block_threads(pthread_t *w,
124 pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
129 threads_starting = params.nthreads;
131 /* create and block all threads */
132 for (i = 0; i < params.nthreads; i++) {
134 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
136 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
137 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
139 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
140 err(EXIT_FAILURE, "pthread_create");
144 static void toggle_done(int sig __maybe_unused,
145 siginfo_t *info __maybe_unused,
146 void *uc __maybe_unused)
151 int bench_futex_requeue(int argc, const char **argv)
155 struct sigaction act;
156 pthread_attr_t thread_attr;
157 struct perf_cpu_map *cpu;
159 argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
163 cpu = perf_cpu_map__new(NULL);
165 err(EXIT_FAILURE, "cpu_map__new");
167 memset(&act, 0, sizeof(act));
168 sigfillset(&act.sa_mask);
169 act.sa_sigaction = toggle_done;
170 sigaction(SIGINT, &act, NULL);
172 if (params.mlockall) {
173 if (mlockall(MCL_CURRENT | MCL_FUTURE))
174 err(EXIT_FAILURE, "mlockall");
177 if (!params.nthreads)
178 params.nthreads = cpu->nr;
180 worker = calloc(params.nthreads, sizeof(*worker));
182 err(EXIT_FAILURE, "calloc");
185 futex_flag = FUTEX_PRIVATE_FLAG;
187 if (params.nrequeue > params.nthreads)
188 params.nrequeue = params.nthreads;
190 if (params.broadcast)
191 params.nrequeue = params.nthreads;
193 printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %s%p), "
194 "%d at a time.\n\n", getpid(), params.nthreads,
195 params.fshared ? "shared":"private", &futex1,
196 params.pi ? "PI ": "", &futex2, params.nrequeue);
198 init_stats(&requeued_stats);
199 init_stats(&requeuetime_stats);
200 pthread_attr_init(&thread_attr);
201 pthread_mutex_init(&thread_lock, NULL);
202 pthread_cond_init(&thread_parent, NULL);
203 pthread_cond_init(&thread_worker, NULL);
205 for (j = 0; j < bench_repeat && !done; j++) {
206 unsigned int nrequeued = 0, wakeups = 0;
207 struct timeval start, end, runtime;
209 /* create, launch & block all threads */
210 block_threads(worker, thread_attr, cpu);
212 /* make sure all threads are already blocked */
213 pthread_mutex_lock(&thread_lock);
214 while (threads_starting)
215 pthread_cond_wait(&thread_parent, &thread_lock);
216 pthread_cond_broadcast(&thread_worker);
217 pthread_mutex_unlock(&thread_lock);
221 /* Ok, all threads are patiently blocked, start requeueing */
222 gettimeofday(&start, NULL);
223 while (nrequeued < params.nthreads) {
227 * For the regular non-pi case, do not wakeup any tasks
228 * blocked on futex1, allowing us to really measure
229 * futex_wait functionality. For the PI case the first
230 * waiter is always awoken.
233 r = futex_cmp_requeue(&futex1, 0, &futex2, 0,
237 r = futex_cmp_requeue_pi(&futex1, 0, &futex2,
240 wakeups++; /* assume no error */
244 err(EXIT_FAILURE, "couldn't requeue from %p to %p",
250 gettimeofday(&end, NULL);
251 timersub(&end, &start, &runtime);
253 update_stats(&requeued_stats, nrequeued);
254 update_stats(&requeuetime_stats, runtime.tv_usec);
256 if (!params.silent) {
258 printf("[Run %d]: Requeued %d of %d threads in "
259 "%.4f ms\n", j + 1, nrequeued,
261 runtime.tv_usec / (double)USEC_PER_MSEC);
263 nrequeued -= wakeups;
264 printf("[Run %d]: Awoke and Requeued (%d+%d) of "
265 "%d threads in %.4f ms\n",
266 j + 1, wakeups, nrequeued,
268 runtime.tv_usec / (double)USEC_PER_MSEC);
274 /* everybody should be blocked on futex2, wake'em up */
275 nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
276 if (params.nthreads != nrequeued)
277 warnx("couldn't wakeup all tasks (%d/%d)",
278 nrequeued, params.nthreads);
281 for (i = 0; i < params.nthreads; i++) {
282 ret = pthread_join(worker[i], NULL);
284 err(EXIT_FAILURE, "pthread_join");
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);
297 perf_cpu_map__put(cpu);
300 usage_with_options(bench_futex_requeue_usage, options);