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