blob: aca47ce8b1e7ebdaf1008c8e0d015caa9f1030d0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -08002/*
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
Davidlohr Bueso46f815322021-08-08 21:33:01 -07009 * requeues without waking up any tasks (in the non-pi case) -- thus
10 * mimicking a regular futex_wait.
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080011 */
12
Arnaldo Carvalho de Melo8a158582016-07-06 12:14:56 -030013/* For the CLR_() macros */
Arnaldo Carvalho de Meloa0f213e142017-03-02 15:44:19 -030014#include <string.h>
Arnaldo Carvalho de Melo8a158582016-07-06 12:14:56 -030015#include <pthread.h>
16
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030017#include <signal.h>
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080018#include "../util/stat.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060019#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo86695f52016-07-11 10:05:52 -030020#include <linux/compiler.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030021#include <linux/kernel.h>
Arnaldo Carvalho de Melo565e6912016-08-08 15:35:21 -030022#include <linux/time64.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030023#include <errno.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +020024#include <perf/cpumap.h>
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080025#include "bench.h"
26#include "futex.h"
27
28#include <err.h>
29#include <stdlib.h>
30#include <sys/time.h>
Davidlohr Bueso9f9a3ff2021-08-08 21:32:58 -070031#include <sys/mman.h>
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080032
33static u_int32_t futex1 = 0, futex2 = 0;
34
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080035static pthread_t *worker;
Davidlohr Bueso09590462021-08-08 21:32:55 -070036static bool done = false;
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080037static pthread_mutex_t thread_lock;
38static pthread_cond_t thread_parent, thread_worker;
39static struct stats requeuetime_stats, requeued_stats;
Davidlohr Bueso09590462021-08-08 21:32:55 -070040static unsigned int threads_starting;
Davidlohr Bueso86c87e12014-09-29 09:41:07 -070041static int futex_flag = 0;
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080042
Davidlohr Bueso09590462021-08-08 21:32:55 -070043static struct bench_futex_parameters params = {
44 /*
45 * How many tasks to requeue at a time.
46 * Default to 1 in order to make the kernel work more.
47 */
48 .nrequeue = 1,
49};
50
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080051static const struct option options[] = {
Davidlohr Bueso09590462021-08-08 21:32:55 -070052 OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
53 OPT_UINTEGER('q', "nrequeue", &params.nrequeue, "Specify amount of threads to requeue at once"),
54 OPT_BOOLEAN( 's', "silent", &params.silent, "Silent mode: do not display data/details"),
55 OPT_BOOLEAN( 'S', "shared", &params.fshared, "Use shared futexes instead of private ones"),
Davidlohr Bueso9f9a3ff2021-08-08 21:32:58 -070056 OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
Davidlohr Buesod262e6a2021-08-08 21:32:59 -070057 OPT_BOOLEAN( 'B', "broadcast", &params.broadcast, "Requeue all threads at once"),
Davidlohr Bueso46f815322021-08-08 21:33:01 -070058 OPT_BOOLEAN( 'p', "pi", &params.pi, "Use PI-aware variants of FUTEX_CMP_REQUEUE"),
59
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080060 OPT_END()
61};
62
63static const char * const bench_futex_requeue_usage[] = {
64 "perf bench futex requeue <options>",
65 NULL
66};
67
68static void print_summary(void)
69{
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);
73
74 printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
75 requeued_avg,
Davidlohr Bueso09590462021-08-08 21:32:55 -070076 params.nthreads,
Arnaldo Carvalho de Melo565e6912016-08-08 15:35:21 -030077 requeuetime_avg / USEC_PER_MSEC,
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080078 rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
79}
80
81static void *workerfn(void *arg __maybe_unused)
82{
Davidlohr Bueso6f9661b2021-08-08 21:33:00 -070083 int ret;
84
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -080085 pthread_mutex_lock(&thread_lock);
86 threads_starting--;
87 if (!threads_starting)
88 pthread_cond_signal(&thread_parent);
89 pthread_cond_wait(&thread_worker, &thread_lock);
90 pthread_mutex_unlock(&thread_lock);
91
Davidlohr Bueso6f9661b2021-08-08 21:33:00 -070092 while (1) {
Davidlohr Bueso46f815322021-08-08 21:33:01 -070093 if (!params.pi) {
94 ret = futex_wait(&futex1, 0, NULL, futex_flag);
95 if (!ret)
96 break;
Davidlohr Bueso6f9661b2021-08-08 21:33:00 -070097
Davidlohr Bueso46f815322021-08-08 21:33:01 -070098 if (ret && errno != EAGAIN) {
99 if (!params.silent)
100 warnx("futex_wait");
101 break;
102 }
103 } else {
104 ret = futex_wait_requeue_pi(&futex1, 0, &futex2,
105 NULL, futex_flag);
106 if (!ret) {
107 /* got the lock at futex2 */
108 futex_unlock_pi(&futex2, futex_flag);
109 break;
110 }
111
112 if (ret && errno != EAGAIN) {
113 if (!params.silent)
114 warnx("futex_wait_requeue_pi");
115 break;
116 }
Davidlohr Bueso6f9661b2021-08-08 21:33:00 -0700117 }
118 }
119
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800120 return NULL;
121}
122
123static void block_threads(pthread_t *w,
Jiri Olsaf8548392019-07-21 13:23:49 +0200124 pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800125{
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800126 cpu_set_t cpuset;
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800127 unsigned int i;
128
Davidlohr Bueso09590462021-08-08 21:32:55 -0700129 threads_starting = params.nthreads;
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800130
131 /* create and block all threads */
Davidlohr Bueso09590462021-08-08 21:32:55 -0700132 for (i = 0; i < params.nthreads; i++) {
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800133 CPU_ZERO(&cpuset);
Ian Rogers6d188042022-01-04 22:13:51 -0800134 CPU_SET(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, &cpuset);
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800135
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800136 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800137 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
138
139 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
140 err(EXIT_FAILURE, "pthread_create");
141 }
142}
143
144static void toggle_done(int sig __maybe_unused,
145 siginfo_t *info __maybe_unused,
146 void *uc __maybe_unused)
147{
148 done = true;
149}
150
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300151int bench_futex_requeue(int argc, const char **argv)
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800152{
153 int ret = 0;
154 unsigned int i, j;
155 struct sigaction act;
156 pthread_attr_t thread_attr;
Jiri Olsaf8548392019-07-21 13:23:49 +0200157 struct perf_cpu_map *cpu;
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800158
159 argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
160 if (argc)
161 goto err;
162
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200163 cpu = perf_cpu_map__new(NULL);
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800164 if (!cpu)
165 err(EXIT_FAILURE, "cpu_map__new");
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800166
Tommi Rantala7b919a52020-03-05 10:37:14 +0200167 memset(&act, 0, sizeof(act));
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800168 sigfillset(&act.sa_mask);
169 act.sa_sigaction = toggle_done;
170 sigaction(SIGINT, &act, NULL);
171
Davidlohr Bueso9f9a3ff2021-08-08 21:32:58 -0700172 if (params.mlockall) {
173 if (mlockall(MCL_CURRENT | MCL_FUTURE))
174 err(EXIT_FAILURE, "mlockall");
175 }
176
Davidlohr Bueso09590462021-08-08 21:32:55 -0700177 if (!params.nthreads)
178 params.nthreads = cpu->nr;
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800179
Davidlohr Bueso09590462021-08-08 21:32:55 -0700180 worker = calloc(params.nthreads, sizeof(*worker));
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800181 if (!worker)
182 err(EXIT_FAILURE, "calloc");
183
Davidlohr Bueso09590462021-08-08 21:32:55 -0700184 if (!params.fshared)
Davidlohr Bueso86c87e12014-09-29 09:41:07 -0700185 futex_flag = FUTEX_PRIVATE_FLAG;
186
Davidlohr Bueso09590462021-08-08 21:32:55 -0700187 if (params.nrequeue > params.nthreads)
188 params.nrequeue = params.nthreads;
Davidlohr Bueso052b0f62015-04-24 10:00:48 -0700189
Davidlohr Buesod262e6a2021-08-08 21:32:59 -0700190 if (params.broadcast)
191 params.nrequeue = params.nthreads;
192
Davidlohr Bueso46f815322021-08-08 21:33:01 -0700193 printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %s%p), "
Davidlohr Bueso09590462021-08-08 21:32:55 -0700194 "%d at a time.\n\n", getpid(), params.nthreads,
Davidlohr Bueso46f815322021-08-08 21:33:01 -0700195 params.fshared ? "shared":"private", &futex1,
196 params.pi ? "PI ": "", &futex2, params.nrequeue);
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800197
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);
204
Davidlohr Buesod9de84a2014-06-16 11:14:23 -0700205 for (j = 0; j < bench_repeat && !done; j++) {
Davidlohr Bueso46f815322021-08-08 21:33:01 -0700206 unsigned int nrequeued = 0, wakeups = 0;
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800207 struct timeval start, end, runtime;
208
209 /* create, launch & block all threads */
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800210 block_threads(worker, thread_attr, cpu);
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800211
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);
218
219 usleep(100000);
220
221 /* Ok, all threads are patiently blocked, start requeueing */
222 gettimeofday(&start, NULL);
Davidlohr Bueso09590462021-08-08 21:32:55 -0700223 while (nrequeued < params.nthreads) {
Davidlohr Bueso46f815322021-08-08 21:33:01 -0700224 int r;
225
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800226 /*
Davidlohr Bueso46f815322021-08-08 21:33:01 -0700227 * 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.
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800231 */
Davidlohr Bueso46f815322021-08-08 21:33:01 -0700232 if (!params.pi) {
233 r = futex_cmp_requeue(&futex1, 0, &futex2, 0,
234 params.nrequeue,
235 futex_flag);
236 } else {
237 r = futex_cmp_requeue_pi(&futex1, 0, &futex2,
238 params.nrequeue,
239 futex_flag);
240 wakeups++; /* assume no error */
241 }
242
243 if (r < 0)
244 err(EXIT_FAILURE, "couldn't requeue from %p to %p",
245 &futex1, &futex2);
246
247 nrequeued += r;
Davidlohr Bueso86c87e12014-09-29 09:41:07 -0700248 }
Davidlohr Bueso052b0f62015-04-24 10:00:48 -0700249
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800250 gettimeofday(&end, NULL);
251 timersub(&end, &start, &runtime);
252
253 update_stats(&requeued_stats, nrequeued);
254 update_stats(&requeuetime_stats, runtime.tv_usec);
255
Davidlohr Bueso09590462021-08-08 21:32:55 -0700256 if (!params.silent) {
Davidlohr Bueso46f815322021-08-08 21:33:01 -0700257 if (!params.pi)
258 printf("[Run %d]: Requeued %d of %d threads in "
259 "%.4f ms\n", j + 1, nrequeued,
260 params.nthreads,
261 runtime.tv_usec / (double)USEC_PER_MSEC);
262 else {
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,
267 params.nthreads,
268 runtime.tv_usec / (double)USEC_PER_MSEC);
269 }
270
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800271 }
272
Davidlohr Bueso46f815322021-08-08 21:33:01 -0700273 if (!params.pi) {
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);
279 }
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800280
Davidlohr Bueso09590462021-08-08 21:32:55 -0700281 for (i = 0; i < params.nthreads; i++) {
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800282 ret = pthread_join(worker[i], NULL);
283 if (ret)
284 err(EXIT_FAILURE, "pthread_join");
285 }
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800286 }
287
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);
293
294 print_summary();
295
296 free(worker);
Sohaib Mohamed88e48232021-11-12 22:11:33 +0200297 perf_cpu_map__put(cpu);
Davidlohr Bueso0fb298c2013-12-14 20:31:57 -0800298 return ret;
299err:
300 usage_with_options(bench_futex_requeue_usage, options);
301 exit(EXIT_FAILURE);
302}