Merge tag 'probes-fixes-v6.16-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / tools / perf / bench / futex-requeue.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
0fb298cf
DB
2/*
3 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
4 *
5 * futex-requeue: Block a bunch of threads on futex1 and requeue them
6 * on futex2, N at a time.
7 *
8 * This program is particularly useful to measure the latency of nthread
46f81532
DB
9 * requeues without waking up any tasks (in the non-pi case) -- thus
10 * mimicking a regular futex_wait.
0fb298cf
DB
11 */
12
8a158589 13/* For the CLR_() macros */
a0f213e1 14#include <string.h>
8a158589
ACM
15#include <pthread.h>
16
9c304f6c 17#include <signal.h>
a64d3af5 18#include "../util/mutex.h"
0fb298cf 19#include "../util/stat.h"
4b6ab94e 20#include <subcmd/parse-options.h>
86695f59 21#include <linux/compiler.h>
9c304f6c 22#include <linux/kernel.h>
565e6911 23#include <linux/time64.h>
9c304f6c 24#include <errno.h>
9c3516d1 25#include <perf/cpumap.h>
0fb298cf
DB
26#include "bench.h"
27#include "futex.h"
28
29#include <err.h>
30#include <stdlib.h>
31#include <sys/time.h>
9f9a3ffe 32#include <sys/mman.h>
0fb298cf
DB
33
34static u_int32_t futex1 = 0, futex2 = 0;
35
0fb298cf 36static pthread_t *worker;
09590463 37static bool done = false;
a64d3af5
IR
38static struct mutex thread_lock;
39static struct cond thread_parent, thread_worker;
0fb298cf 40static struct stats requeuetime_stats, requeued_stats;
09590463 41static unsigned int threads_starting;
86c87e13 42static int futex_flag = 0;
0fb298cf 43
09590463 44static struct bench_futex_parameters params = {
60035a39 45 .nbuckets = -1,
09590463
DB
46 /*
47 * How many tasks to requeue at a time.
48 * Default to 1 in order to make the kernel work more.
49 */
50 .nrequeue = 1,
51};
52
0fb298cf 53static const struct option options[] = {
60035a39
SAS
54 OPT_INTEGER( 'b', "buckets", &params.nbuckets, "Specify amount of hash buckets"),
55 OPT_BOOLEAN( 'I', "immutable", &params.buckets_immutable, "Make the hash buckets immutable"),
09590463
DB
56 OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
57 OPT_UINTEGER('q', "nrequeue", &params.nrequeue, "Specify amount of threads to requeue at once"),
58 OPT_BOOLEAN( 's', "silent", &params.silent, "Silent mode: do not display data/details"),
59 OPT_BOOLEAN( 'S', "shared", &params.fshared, "Use shared futexes instead of private ones"),
9f9a3ffe 60 OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
d262e6a9 61 OPT_BOOLEAN( 'B', "broadcast", &params.broadcast, "Requeue all threads at once"),
46f81532
DB
62 OPT_BOOLEAN( 'p', "pi", &params.pi, "Use PI-aware variants of FUTEX_CMP_REQUEUE"),
63
0fb298cf
DB
64 OPT_END()
65};
66
67static const char * const bench_futex_requeue_usage[] = {
68 "perf bench futex requeue <options>",
69 NULL
70};
71
72static void print_summary(void)
73{
74 double requeuetime_avg = avg_stats(&requeuetime_stats);
75 double requeuetime_stddev = stddev_stats(&requeuetime_stats);
76 unsigned int requeued_avg = avg_stats(&requeued_stats);
77
78 printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
79 requeued_avg,
09590463 80 params.nthreads,
565e6911 81 requeuetime_avg / USEC_PER_MSEC,
0fb298cf 82 rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
60035a39 83 futex_print_nbuckets(&params);
0fb298cf
DB
84}
85
86static void *workerfn(void *arg __maybe_unused)
87{
6f9661b2
DB
88 int ret;
89
a64d3af5 90 mutex_lock(&thread_lock);
0fb298cf
DB
91 threads_starting--;
92 if (!threads_starting)
a64d3af5
IR
93 cond_signal(&thread_parent);
94 cond_wait(&thread_worker, &thread_lock);
95 mutex_unlock(&thread_lock);
0fb298cf 96
6f9661b2 97 while (1) {
46f81532
DB
98 if (!params.pi) {
99 ret = futex_wait(&futex1, 0, NULL, futex_flag);
100 if (!ret)
101 break;
102
103 if (ret && errno != EAGAIN) {
104 if (!params.silent)
105 warnx("futex_wait");
106 break;
107 }
108 } else {
109 ret = futex_wait_requeue_pi(&futex1, 0, &futex2,
110 NULL, futex_flag);
111 if (!ret) {
112 /* got the lock at futex2 */
113 futex_unlock_pi(&futex2, futex_flag);
114 break;
115 }
116
117 if (ret && errno != EAGAIN) {
118 if (!params.silent)
119 warnx("futex_wait_requeue_pi");
120 break;
121 }
6f9661b2
DB
122 }
123 }
124
0fb298cf
DB
125 return NULL;
126}
127
8351498d 128static void block_threads(pthread_t *w, struct perf_cpu_map *cpu)
0fb298cf 129{
c9c2a427 130 cpu_set_t *cpuset;
0fb298cf 131 unsigned int i;
18337358 132 int nrcpus = cpu__max_cpu().cpu;
c9c2a427 133 size_t size;
0fb298cf 134
09590463 135 threads_starting = params.nthreads;
0fb298cf 136
c9c2a427
AR
137 cpuset = CPU_ALLOC(nrcpus);
138 BUG_ON(!cpuset);
139 size = CPU_ALLOC_SIZE(nrcpus);
140
0fb298cf 141 /* create and block all threads */
09590463 142 for (i = 0; i < params.nthreads; i++) {
8351498d
IR
143 pthread_attr_t thread_attr;
144
145 pthread_attr_init(&thread_attr);
c9c2a427
AR
146 CPU_ZERO_S(size, cpuset);
147 CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
0fb298cf 148
c9c2a427
AR
149 if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
150 CPU_FREE(cpuset);
0fb298cf 151 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
c9c2a427 152 }
0fb298cf 153
c9c2a427
AR
154 if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) {
155 CPU_FREE(cpuset);
0fb298cf 156 err(EXIT_FAILURE, "pthread_create");
c9c2a427 157 }
8351498d 158 pthread_attr_destroy(&thread_attr);
0fb298cf 159 }
c9c2a427 160 CPU_FREE(cpuset);
0fb298cf
DB
161}
162
163static void toggle_done(int sig __maybe_unused,
164 siginfo_t *info __maybe_unused,
165 void *uc __maybe_unused)
166{
167 done = true;
168}
169
b0ad8ea6 170int bench_futex_requeue(int argc, const char **argv)
0fb298cf
DB
171{
172 int ret = 0;
173 unsigned int i, j;
174 struct sigaction act;
f854839b 175 struct perf_cpu_map *cpu;
0fb298cf
DB
176
177 argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
178 if (argc)
179 goto err;
180
effe957c 181 cpu = perf_cpu_map__new_online_cpus();
3b2323c2
DB
182 if (!cpu)
183 err(EXIT_FAILURE, "cpu_map__new");
0fb298cf 184
7b919a53 185 memset(&act, 0, sizeof(act));
0fb298cf
DB
186 sigfillset(&act.sa_mask);
187 act.sa_sigaction = toggle_done;
188 sigaction(SIGINT, &act, NULL);
189
9f9a3ffe
DB
190 if (params.mlockall) {
191 if (mlockall(MCL_CURRENT | MCL_FUTURE))
192 err(EXIT_FAILURE, "mlockall");
193 }
194
09590463 195 if (!params.nthreads)
44028699 196 params.nthreads = perf_cpu_map__nr(cpu);
0fb298cf 197
09590463 198 worker = calloc(params.nthreads, sizeof(*worker));
0fb298cf
DB
199 if (!worker)
200 err(EXIT_FAILURE, "calloc");
201
09590463 202 if (!params.fshared)
86c87e13
DB
203 futex_flag = FUTEX_PRIVATE_FLAG;
204
09590463
DB
205 if (params.nrequeue > params.nthreads)
206 params.nrequeue = params.nthreads;
052b0f6e 207
d262e6a9
DB
208 if (params.broadcast)
209 params.nrequeue = params.nthreads;
210
60035a39
SAS
211 futex_set_nbuckets_param(&params);
212
46f81532 213 printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %s%p), "
09590463 214 "%d at a time.\n\n", getpid(), params.nthreads,
46f81532
DB
215 params.fshared ? "shared":"private", &futex1,
216 params.pi ? "PI ": "", &futex2, params.nrequeue);
0fb298cf
DB
217
218 init_stats(&requeued_stats);
219 init_stats(&requeuetime_stats);
a64d3af5
IR
220 mutex_init(&thread_lock);
221 cond_init(&thread_parent);
222 cond_init(&thread_worker);
0fb298cf 223
d9de84af 224 for (j = 0; j < bench_repeat && !done; j++) {
46f81532 225 unsigned int nrequeued = 0, wakeups = 0;
0fb298cf
DB
226 struct timeval start, end, runtime;
227
228 /* create, launch & block all threads */
8351498d 229 block_threads(worker, cpu);
0fb298cf
DB
230
231 /* make sure all threads are already blocked */
a64d3af5 232 mutex_lock(&thread_lock);
0fb298cf 233 while (threads_starting)
a64d3af5
IR
234 cond_wait(&thread_parent, &thread_lock);
235 cond_broadcast(&thread_worker);
236 mutex_unlock(&thread_lock);
0fb298cf
DB
237
238 usleep(100000);
239
240 /* Ok, all threads are patiently blocked, start requeueing */
241 gettimeofday(&start, NULL);
09590463 242 while (nrequeued < params.nthreads) {
46f81532
DB
243 int r;
244
0fb298cf 245 /*
46f81532
DB
246 * For the regular non-pi case, do not wakeup any tasks
247 * blocked on futex1, allowing us to really measure
248 * futex_wait functionality. For the PI case the first
249 * waiter is always awoken.
0fb298cf 250 */
46f81532
DB
251 if (!params.pi) {
252 r = futex_cmp_requeue(&futex1, 0, &futex2, 0,
253 params.nrequeue,
254 futex_flag);
255 } else {
256 r = futex_cmp_requeue_pi(&futex1, 0, &futex2,
257 params.nrequeue,
258 futex_flag);
259 wakeups++; /* assume no error */
260 }
261
262 if (r < 0)
263 err(EXIT_FAILURE, "couldn't requeue from %p to %p",
264 &futex1, &futex2);
265
266 nrequeued += r;
86c87e13 267 }
052b0f6e 268
0fb298cf
DB
269 gettimeofday(&end, NULL);
270 timersub(&end, &start, &runtime);
271
272 update_stats(&requeued_stats, nrequeued);
273 update_stats(&requeuetime_stats, runtime.tv_usec);
274
09590463 275 if (!params.silent) {
46f81532
DB
276 if (!params.pi)
277 printf("[Run %d]: Requeued %d of %d threads in "
278 "%.4f ms\n", j + 1, nrequeued,
279 params.nthreads,
280 runtime.tv_usec / (double)USEC_PER_MSEC);
281 else {
282 nrequeued -= wakeups;
283 printf("[Run %d]: Awoke and Requeued (%d+%d) of "
284 "%d threads in %.4f ms\n",
285 j + 1, wakeups, nrequeued,
286 params.nthreads,
287 runtime.tv_usec / (double)USEC_PER_MSEC);
288 }
289
0fb298cf
DB
290 }
291
46f81532
DB
292 if (!params.pi) {
293 /* everybody should be blocked on futex2, wake'em up */
294 nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
295 if (params.nthreads != nrequeued)
296 warnx("couldn't wakeup all tasks (%d/%d)",
297 nrequeued, params.nthreads);
298 }
0fb298cf 299
09590463 300 for (i = 0; i < params.nthreads; i++) {
0fb298cf
DB
301 ret = pthread_join(worker[i], NULL);
302 if (ret)
303 err(EXIT_FAILURE, "pthread_join");
304 }
0fb298cf
DB
305 }
306
307 /* cleanup & report results */
a64d3af5
IR
308 cond_destroy(&thread_parent);
309 cond_destroy(&thread_worker);
310 mutex_destroy(&thread_lock);
0fb298cf
DB
311
312 print_summary();
313
314 free(worker);
88e48238 315 perf_cpu_map__put(cpu);
0fb298cf
DB
316 return ret;
317err:
318 usage_with_options(bench_futex_requeue_usage, options);
319 exit(EXIT_FAILURE);
320}