1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
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 -- thus mimicking a regular futex_wait.
12 /* For the CLR_() macros */
17 #include "../util/stat.h"
18 #include <subcmd/parse-options.h>
19 #include <linux/compiler.h>
20 #include <linux/kernel.h>
21 #include <linux/time64.h>
23 #include <perf/cpumap.h>
32 static u_int32_t futex1 = 0, futex2 = 0;
34 static pthread_t *worker;
35 static bool done = false;
36 static pthread_mutex_t thread_lock;
37 static pthread_cond_t thread_parent, thread_worker;
38 static struct stats requeuetime_stats, requeued_stats;
39 static unsigned int threads_starting;
40 static int futex_flag = 0;
42 static struct bench_futex_parameters params = {
44 * How many tasks to requeue at a time.
45 * Default to 1 in order to make the kernel work more.
50 static const struct option options[] = {
51 OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"),
52 OPT_UINTEGER('q', "nrequeue", ¶ms.nrequeue, "Specify amount of threads to requeue at once"),
53 OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"),
54 OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"),
55 OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"),
56 OPT_BOOLEAN( 'B', "broadcast", ¶ms.broadcast, "Requeue all threads at once"),
60 static const char * const bench_futex_requeue_usage[] = {
61 "perf bench futex requeue <options>",
65 static void print_summary(void)
67 double requeuetime_avg = avg_stats(&requeuetime_stats);
68 double requeuetime_stddev = stddev_stats(&requeuetime_stats);
69 unsigned int requeued_avg = avg_stats(&requeued_stats);
71 printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
74 requeuetime_avg / USEC_PER_MSEC,
75 rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
78 static void *workerfn(void *arg __maybe_unused)
80 pthread_mutex_lock(&thread_lock);
82 if (!threads_starting)
83 pthread_cond_signal(&thread_parent);
84 pthread_cond_wait(&thread_worker, &thread_lock);
85 pthread_mutex_unlock(&thread_lock);
87 futex_wait(&futex1, 0, NULL, futex_flag);
91 static void block_threads(pthread_t *w,
92 pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
97 threads_starting = params.nthreads;
99 /* create and block all threads */
100 for (i = 0; i < params.nthreads; i++) {
102 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
104 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
105 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
107 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
108 err(EXIT_FAILURE, "pthread_create");
112 static void toggle_done(int sig __maybe_unused,
113 siginfo_t *info __maybe_unused,
114 void *uc __maybe_unused)
119 int bench_futex_requeue(int argc, const char **argv)
123 struct sigaction act;
124 pthread_attr_t thread_attr;
125 struct perf_cpu_map *cpu;
127 argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
131 cpu = perf_cpu_map__new(NULL);
133 err(EXIT_FAILURE, "cpu_map__new");
135 memset(&act, 0, sizeof(act));
136 sigfillset(&act.sa_mask);
137 act.sa_sigaction = toggle_done;
138 sigaction(SIGINT, &act, NULL);
140 if (params.mlockall) {
141 if (mlockall(MCL_CURRENT | MCL_FUTURE))
142 err(EXIT_FAILURE, "mlockall");
145 if (!params.nthreads)
146 params.nthreads = cpu->nr;
148 worker = calloc(params.nthreads, sizeof(*worker));
150 err(EXIT_FAILURE, "calloc");
153 futex_flag = FUTEX_PRIVATE_FLAG;
155 if (params.nrequeue > params.nthreads)
156 params.nrequeue = params.nthreads;
158 if (params.broadcast)
159 params.nrequeue = params.nthreads;
161 printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
162 "%d at a time.\n\n", getpid(), params.nthreads,
163 params.fshared ? "shared":"private", &futex1, &futex2, params.nrequeue);
165 init_stats(&requeued_stats);
166 init_stats(&requeuetime_stats);
167 pthread_attr_init(&thread_attr);
168 pthread_mutex_init(&thread_lock, NULL);
169 pthread_cond_init(&thread_parent, NULL);
170 pthread_cond_init(&thread_worker, NULL);
172 for (j = 0; j < bench_repeat && !done; j++) {
173 unsigned int nrequeued = 0;
174 struct timeval start, end, runtime;
176 /* create, launch & block all threads */
177 block_threads(worker, thread_attr, cpu);
179 /* make sure all threads are already blocked */
180 pthread_mutex_lock(&thread_lock);
181 while (threads_starting)
182 pthread_cond_wait(&thread_parent, &thread_lock);
183 pthread_cond_broadcast(&thread_worker);
184 pthread_mutex_unlock(&thread_lock);
188 /* Ok, all threads are patiently blocked, start requeueing */
189 gettimeofday(&start, NULL);
190 while (nrequeued < params.nthreads) {
192 * Do not wakeup any tasks blocked on futex1, allowing
193 * us to really measure futex_wait functionality.
195 nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0,
200 gettimeofday(&end, NULL);
201 timersub(&end, &start, &runtime);
203 update_stats(&requeued_stats, nrequeued);
204 update_stats(&requeuetime_stats, runtime.tv_usec);
206 if (!params.silent) {
207 printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
208 j + 1, nrequeued, params.nthreads,
209 runtime.tv_usec / (double)USEC_PER_MSEC);
212 /* everybody should be blocked on futex2, wake'em up */
213 nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
214 if (params.nthreads != nrequeued)
215 warnx("couldn't wakeup all tasks (%d/%d)",
216 nrequeued, params.nthreads);
218 for (i = 0; i < params.nthreads; i++) {
219 ret = pthread_join(worker[i], NULL);
221 err(EXIT_FAILURE, "pthread_join");
225 /* cleanup & report results */
226 pthread_cond_destroy(&thread_parent);
227 pthread_cond_destroy(&thread_worker);
228 pthread_mutex_destroy(&thread_lock);
229 pthread_attr_destroy(&thread_attr);
236 usage_with_options(bench_futex_requeue_usage, options);