Commit | Line | Data |
---|---|---|
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 | |
34 | static u_int32_t futex1 = 0, futex2 = 0; | |
35 | ||
0fb298cf | 36 | static pthread_t *worker; |
09590463 | 37 | static bool done = false; |
a64d3af5 IR |
38 | static struct mutex thread_lock; |
39 | static struct cond thread_parent, thread_worker; | |
0fb298cf | 40 | static struct stats requeuetime_stats, requeued_stats; |
09590463 | 41 | static unsigned int threads_starting; |
86c87e13 | 42 | static int futex_flag = 0; |
0fb298cf | 43 | |
09590463 DB |
44 | static 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 | ||
0fb298cf | 52 | static const struct option options[] = { |
09590463 DB |
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"), | |
9f9a3ffe | 57 | OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"), |
d262e6a9 | 58 | OPT_BOOLEAN( 'B', "broadcast", ¶ms.broadcast, "Requeue all threads at once"), |
46f81532 DB |
59 | OPT_BOOLEAN( 'p', "pi", ¶ms.pi, "Use PI-aware variants of FUTEX_CMP_REQUEUE"), |
60 | ||
0fb298cf DB |
61 | OPT_END() |
62 | }; | |
63 | ||
64 | static const char * const bench_futex_requeue_usage[] = { | |
65 | "perf bench futex requeue <options>", | |
66 | NULL | |
67 | }; | |
68 | ||
69 | static 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, | |
09590463 | 77 | params.nthreads, |
565e6911 | 78 | requeuetime_avg / USEC_PER_MSEC, |
0fb298cf DB |
79 | rel_stddev_stats(requeuetime_stddev, requeuetime_avg)); |
80 | } | |
81 | ||
82 | static void *workerfn(void *arg __maybe_unused) | |
83 | { | |
6f9661b2 DB |
84 | int ret; |
85 | ||
a64d3af5 | 86 | mutex_lock(&thread_lock); |
0fb298cf DB |
87 | threads_starting--; |
88 | if (!threads_starting) | |
a64d3af5 IR |
89 | cond_signal(&thread_parent); |
90 | cond_wait(&thread_worker, &thread_lock); | |
91 | mutex_unlock(&thread_lock); | |
0fb298cf | 92 | |
6f9661b2 | 93 | while (1) { |
46f81532 DB |
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 | } | |
6f9661b2 DB |
118 | } |
119 | } | |
120 | ||
0fb298cf DB |
121 | return NULL; |
122 | } | |
123 | ||
124 | static void block_threads(pthread_t *w, | |
f854839b | 125 | pthread_attr_t thread_attr, struct perf_cpu_map *cpu) |
0fb298cf | 126 | { |
c9c2a427 | 127 | cpu_set_t *cpuset; |
0fb298cf | 128 | unsigned int i; |
c9c2a427 AR |
129 | int nrcpus = perf_cpu_map__nr(cpu); |
130 | size_t size; | |
0fb298cf | 131 | |
09590463 | 132 | threads_starting = params.nthreads; |
0fb298cf | 133 | |
c9c2a427 AR |
134 | cpuset = CPU_ALLOC(nrcpus); |
135 | BUG_ON(!cpuset); | |
136 | size = CPU_ALLOC_SIZE(nrcpus); | |
137 | ||
0fb298cf | 138 | /* create and block all threads */ |
09590463 | 139 | for (i = 0; i < params.nthreads; i++) { |
c9c2a427 AR |
140 | CPU_ZERO_S(size, cpuset); |
141 | CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset); | |
0fb298cf | 142 | |
c9c2a427 AR |
143 | if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) { |
144 | CPU_FREE(cpuset); | |
0fb298cf | 145 | err(EXIT_FAILURE, "pthread_attr_setaffinity_np"); |
c9c2a427 | 146 | } |
0fb298cf | 147 | |
c9c2a427 AR |
148 | if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) { |
149 | CPU_FREE(cpuset); | |
0fb298cf | 150 | err(EXIT_FAILURE, "pthread_create"); |
c9c2a427 | 151 | } |
0fb298cf | 152 | } |
c9c2a427 | 153 | CPU_FREE(cpuset); |
0fb298cf DB |
154 | } |
155 | ||
156 | static void toggle_done(int sig __maybe_unused, | |
157 | siginfo_t *info __maybe_unused, | |
158 | void *uc __maybe_unused) | |
159 | { | |
160 | done = true; | |
161 | } | |
162 | ||
b0ad8ea6 | 163 | int bench_futex_requeue(int argc, const char **argv) |
0fb298cf DB |
164 | { |
165 | int ret = 0; | |
166 | unsigned int i, j; | |
167 | struct sigaction act; | |
168 | pthread_attr_t thread_attr; | |
f854839b | 169 | struct perf_cpu_map *cpu; |
0fb298cf DB |
170 | |
171 | argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0); | |
172 | if (argc) | |
173 | goto err; | |
174 | ||
9c3516d1 | 175 | cpu = perf_cpu_map__new(NULL); |
3b2323c2 DB |
176 | if (!cpu) |
177 | err(EXIT_FAILURE, "cpu_map__new"); | |
0fb298cf | 178 | |
7b919a53 | 179 | memset(&act, 0, sizeof(act)); |
0fb298cf DB |
180 | sigfillset(&act.sa_mask); |
181 | act.sa_sigaction = toggle_done; | |
182 | sigaction(SIGINT, &act, NULL); | |
183 | ||
9f9a3ffe DB |
184 | if (params.mlockall) { |
185 | if (mlockall(MCL_CURRENT | MCL_FUTURE)) | |
186 | err(EXIT_FAILURE, "mlockall"); | |
187 | } | |
188 | ||
09590463 | 189 | if (!params.nthreads) |
44028699 | 190 | params.nthreads = perf_cpu_map__nr(cpu); |
0fb298cf | 191 | |
09590463 | 192 | worker = calloc(params.nthreads, sizeof(*worker)); |
0fb298cf DB |
193 | if (!worker) |
194 | err(EXIT_FAILURE, "calloc"); | |
195 | ||
09590463 | 196 | if (!params.fshared) |
86c87e13 DB |
197 | futex_flag = FUTEX_PRIVATE_FLAG; |
198 | ||
09590463 DB |
199 | if (params.nrequeue > params.nthreads) |
200 | params.nrequeue = params.nthreads; | |
052b0f6e | 201 | |
d262e6a9 DB |
202 | if (params.broadcast) |
203 | params.nrequeue = params.nthreads; | |
204 | ||
46f81532 | 205 | printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %s%p), " |
09590463 | 206 | "%d at a time.\n\n", getpid(), params.nthreads, |
46f81532 DB |
207 | params.fshared ? "shared":"private", &futex1, |
208 | params.pi ? "PI ": "", &futex2, params.nrequeue); | |
0fb298cf DB |
209 | |
210 | init_stats(&requeued_stats); | |
211 | init_stats(&requeuetime_stats); | |
212 | pthread_attr_init(&thread_attr); | |
a64d3af5 IR |
213 | mutex_init(&thread_lock); |
214 | cond_init(&thread_parent); | |
215 | cond_init(&thread_worker); | |
0fb298cf | 216 | |
d9de84af | 217 | for (j = 0; j < bench_repeat && !done; j++) { |
46f81532 | 218 | unsigned int nrequeued = 0, wakeups = 0; |
0fb298cf DB |
219 | struct timeval start, end, runtime; |
220 | ||
221 | /* create, launch & block all threads */ | |
3b2323c2 | 222 | block_threads(worker, thread_attr, cpu); |
0fb298cf DB |
223 | |
224 | /* make sure all threads are already blocked */ | |
a64d3af5 | 225 | mutex_lock(&thread_lock); |
0fb298cf | 226 | while (threads_starting) |
a64d3af5 IR |
227 | cond_wait(&thread_parent, &thread_lock); |
228 | cond_broadcast(&thread_worker); | |
229 | mutex_unlock(&thread_lock); | |
0fb298cf DB |
230 | |
231 | usleep(100000); | |
232 | ||
233 | /* Ok, all threads are patiently blocked, start requeueing */ | |
234 | gettimeofday(&start, NULL); | |
09590463 | 235 | while (nrequeued < params.nthreads) { |
46f81532 DB |
236 | int r; |
237 | ||
0fb298cf | 238 | /* |
46f81532 DB |
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. | |
0fb298cf | 243 | */ |
46f81532 DB |
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; | |
86c87e13 | 260 | } |
052b0f6e | 261 | |
0fb298cf DB |
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 | ||
09590463 | 268 | if (!params.silent) { |
46f81532 DB |
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 | ||
0fb298cf DB |
283 | } |
284 | ||
46f81532 DB |
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 | } | |
0fb298cf | 292 | |
09590463 | 293 | for (i = 0; i < params.nthreads; i++) { |
0fb298cf DB |
294 | ret = pthread_join(worker[i], NULL); |
295 | if (ret) | |
296 | err(EXIT_FAILURE, "pthread_join"); | |
297 | } | |
0fb298cf DB |
298 | } |
299 | ||
300 | /* cleanup & report results */ | |
a64d3af5 IR |
301 | cond_destroy(&thread_parent); |
302 | cond_destroy(&thread_worker); | |
303 | mutex_destroy(&thread_lock); | |
0fb298cf DB |
304 | pthread_attr_destroy(&thread_attr); |
305 | ||
306 | print_summary(); | |
307 | ||
308 | free(worker); | |
88e48238 | 309 | perf_cpu_map__put(cpu); |
0fb298cf DB |
310 | return ret; |
311 | err: | |
312 | usage_with_options(bench_futex_requeue_usage, options); | |
313 | exit(EXIT_FAILURE); | |
314 | } |