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)
82 pthread_mutex_lock(&thread_lock);
84 if (!threads_starting)
85 pthread_cond_signal(&thread_parent);
86 pthread_cond_wait(&thread_worker, &thread_lock);
87 pthread_mutex_unlock(&thread_lock);
90 ret = futex_wait(&futex1, 0, NULL, futex_flag);
94 if (ret && errno != EAGAIN) {
104 static void block_threads(pthread_t *w,
105 pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
110 threads_starting = params.nthreads;
112 /* create and block all threads */
113 for (i = 0; i < params.nthreads; i++) {
115 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
117 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
118 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
120 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
121 err(EXIT_FAILURE, "pthread_create");
125 static void toggle_done(int sig __maybe_unused,
126 siginfo_t *info __maybe_unused,
127 void *uc __maybe_unused)
132 int bench_futex_requeue(int argc, const char **argv)
136 struct sigaction act;
137 pthread_attr_t thread_attr;
138 struct perf_cpu_map *cpu;
140 argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
144 cpu = perf_cpu_map__new(NULL);
146 err(EXIT_FAILURE, "cpu_map__new");
148 memset(&act, 0, sizeof(act));
149 sigfillset(&act.sa_mask);
150 act.sa_sigaction = toggle_done;
151 sigaction(SIGINT, &act, NULL);
153 if (params.mlockall) {
154 if (mlockall(MCL_CURRENT | MCL_FUTURE))
155 err(EXIT_FAILURE, "mlockall");
158 if (!params.nthreads)
159 params.nthreads = cpu->nr;
161 worker = calloc(params.nthreads, sizeof(*worker));
163 err(EXIT_FAILURE, "calloc");
166 futex_flag = FUTEX_PRIVATE_FLAG;
168 if (params.nrequeue > params.nthreads)
169 params.nrequeue = params.nthreads;
171 if (params.broadcast)
172 params.nrequeue = params.nthreads;
174 printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
175 "%d at a time.\n\n", getpid(), params.nthreads,
176 params.fshared ? "shared":"private", &futex1, &futex2, params.nrequeue);
178 init_stats(&requeued_stats);
179 init_stats(&requeuetime_stats);
180 pthread_attr_init(&thread_attr);
181 pthread_mutex_init(&thread_lock, NULL);
182 pthread_cond_init(&thread_parent, NULL);
183 pthread_cond_init(&thread_worker, NULL);
185 for (j = 0; j < bench_repeat && !done; j++) {
186 unsigned int nrequeued = 0;
187 struct timeval start, end, runtime;
189 /* create, launch & block all threads */
190 block_threads(worker, thread_attr, cpu);
192 /* make sure all threads are already blocked */
193 pthread_mutex_lock(&thread_lock);
194 while (threads_starting)
195 pthread_cond_wait(&thread_parent, &thread_lock);
196 pthread_cond_broadcast(&thread_worker);
197 pthread_mutex_unlock(&thread_lock);
201 /* Ok, all threads are patiently blocked, start requeueing */
202 gettimeofday(&start, NULL);
203 while (nrequeued < params.nthreads) {
205 * Do not wakeup any tasks blocked on futex1, allowing
206 * us to really measure futex_wait functionality.
208 nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0,
213 gettimeofday(&end, NULL);
214 timersub(&end, &start, &runtime);
216 update_stats(&requeued_stats, nrequeued);
217 update_stats(&requeuetime_stats, runtime.tv_usec);
219 if (!params.silent) {
220 printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
221 j + 1, nrequeued, params.nthreads,
222 runtime.tv_usec / (double)USEC_PER_MSEC);
225 /* everybody should be blocked on futex2, wake'em up */
226 nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
227 if (params.nthreads != nrequeued)
228 warnx("couldn't wakeup all tasks (%d/%d)",
229 nrequeued, params.nthreads);
231 for (i = 0; i < params.nthreads; i++) {
232 ret = pthread_join(worker[i], NULL);
234 err(EXIT_FAILURE, "pthread_join");
238 /* cleanup & report results */
239 pthread_cond_destroy(&thread_parent);
240 pthread_cond_destroy(&thread_worker);
241 pthread_mutex_destroy(&thread_lock);
242 pthread_attr_destroy(&thread_attr);
249 usage_with_options(bench_futex_requeue_usage, options);