Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 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 |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 9 | * requeues without waking up any tasks (in the non-pi case) -- thus |
| 10 | * mimicking a regular futex_wait. |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Arnaldo Carvalho de Melo | 8a15858 | 2016-07-06 12:14:56 -0300 | [diff] [blame] | 13 | /* For the CLR_() macros */ |
Arnaldo Carvalho de Melo | a0f213e14 | 2017-03-02 15:44:19 -0300 | [diff] [blame] | 14 | #include <string.h> |
Arnaldo Carvalho de Melo | 8a15858 | 2016-07-06 12:14:56 -0300 | [diff] [blame] | 15 | #include <pthread.h> |
| 16 | |
Arnaldo Carvalho de Melo | 9c304f6 | 2016-07-07 11:01:46 -0300 | [diff] [blame] | 17 | #include <signal.h> |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 18 | #include "../util/stat.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 19 | #include <subcmd/parse-options.h> |
Arnaldo Carvalho de Melo | 86695f5 | 2016-07-11 10:05:52 -0300 | [diff] [blame] | 20 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | 9c304f6 | 2016-07-07 11:01:46 -0300 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 565e691 | 2016-08-08 15:35:21 -0300 | [diff] [blame] | 22 | #include <linux/time64.h> |
Arnaldo Carvalho de Melo | 9c304f6 | 2016-07-07 11:01:46 -0300 | [diff] [blame] | 23 | #include <errno.h> |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 24 | #include <perf/cpumap.h> |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 25 | #include "bench.h" |
| 26 | #include "futex.h" |
| 27 | |
| 28 | #include <err.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <sys/time.h> |
Davidlohr Bueso | 9f9a3ff | 2021-08-08 21:32:58 -0700 | [diff] [blame] | 31 | #include <sys/mman.h> |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 32 | |
| 33 | static u_int32_t futex1 = 0, futex2 = 0; |
| 34 | |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 35 | static pthread_t *worker; |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 36 | static bool done = false; |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 37 | static pthread_mutex_t thread_lock; |
| 38 | static pthread_cond_t thread_parent, thread_worker; |
| 39 | static struct stats requeuetime_stats, requeued_stats; |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 40 | static unsigned int threads_starting; |
Davidlohr Bueso | 86c87e1 | 2014-09-29 09:41:07 -0700 | [diff] [blame] | 41 | static int futex_flag = 0; |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 42 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 43 | static 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 Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 51 | static const struct option options[] = { |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 52 | OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"), |
| 53 | OPT_UINTEGER('q', "nrequeue", ¶ms.nrequeue, "Specify amount of threads to requeue at once"), |
| 54 | OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"), |
| 55 | OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"), |
Davidlohr Bueso | 9f9a3ff | 2021-08-08 21:32:58 -0700 | [diff] [blame] | 56 | OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"), |
Davidlohr Bueso | d262e6a | 2021-08-08 21:32:59 -0700 | [diff] [blame] | 57 | OPT_BOOLEAN( 'B', "broadcast", ¶ms.broadcast, "Requeue all threads at once"), |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 58 | OPT_BOOLEAN( 'p', "pi", ¶ms.pi, "Use PI-aware variants of FUTEX_CMP_REQUEUE"), |
| 59 | |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 60 | OPT_END() |
| 61 | }; |
| 62 | |
| 63 | static const char * const bench_futex_requeue_usage[] = { |
| 64 | "perf bench futex requeue <options>", |
| 65 | NULL |
| 66 | }; |
| 67 | |
| 68 | static 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 Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 76 | params.nthreads, |
Arnaldo Carvalho de Melo | 565e691 | 2016-08-08 15:35:21 -0300 | [diff] [blame] | 77 | requeuetime_avg / USEC_PER_MSEC, |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 78 | rel_stddev_stats(requeuetime_stddev, requeuetime_avg)); |
| 79 | } |
| 80 | |
| 81 | static void *workerfn(void *arg __maybe_unused) |
| 82 | { |
Davidlohr Bueso | 6f9661b | 2021-08-08 21:33:00 -0700 | [diff] [blame] | 83 | int ret; |
| 84 | |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 85 | 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 Bueso | 6f9661b | 2021-08-08 21:33:00 -0700 | [diff] [blame] | 92 | while (1) { |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 93 | if (!params.pi) { |
| 94 | ret = futex_wait(&futex1, 0, NULL, futex_flag); |
| 95 | if (!ret) |
| 96 | break; |
Davidlohr Bueso | 6f9661b | 2021-08-08 21:33:00 -0700 | [diff] [blame] | 97 | |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 98 | 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 Bueso | 6f9661b | 2021-08-08 21:33:00 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | static void block_threads(pthread_t *w, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 124 | pthread_attr_t thread_attr, struct perf_cpu_map *cpu) |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 125 | { |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 126 | cpu_set_t cpuset; |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 127 | unsigned int i; |
| 128 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 129 | threads_starting = params.nthreads; |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 130 | |
| 131 | /* create and block all threads */ |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 132 | for (i = 0; i < params.nthreads; i++) { |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 133 | CPU_ZERO(&cpuset); |
Ian Rogers | 6d18804 | 2022-01-04 22:13:51 -0800 | [diff] [blame] | 134 | CPU_SET(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, &cpuset); |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 135 | |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 136 | if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset)) |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 137 | 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 | |
| 144 | static 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 Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 151 | int bench_futex_requeue(int argc, const char **argv) |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 152 | { |
| 153 | int ret = 0; |
| 154 | unsigned int i, j; |
| 155 | struct sigaction act; |
| 156 | pthread_attr_t thread_attr; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 157 | struct perf_cpu_map *cpu; |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 158 | |
| 159 | argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0); |
| 160 | if (argc) |
| 161 | goto err; |
| 162 | |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 163 | cpu = perf_cpu_map__new(NULL); |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 164 | if (!cpu) |
| 165 | err(EXIT_FAILURE, "cpu_map__new"); |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 166 | |
Tommi Rantala | 7b919a5 | 2020-03-05 10:37:14 +0200 | [diff] [blame] | 167 | memset(&act, 0, sizeof(act)); |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 168 | sigfillset(&act.sa_mask); |
| 169 | act.sa_sigaction = toggle_done; |
| 170 | sigaction(SIGINT, &act, NULL); |
| 171 | |
Davidlohr Bueso | 9f9a3ff | 2021-08-08 21:32:58 -0700 | [diff] [blame] | 172 | if (params.mlockall) { |
| 173 | if (mlockall(MCL_CURRENT | MCL_FUTURE)) |
| 174 | err(EXIT_FAILURE, "mlockall"); |
| 175 | } |
| 176 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 177 | if (!params.nthreads) |
| 178 | params.nthreads = cpu->nr; |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 179 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 180 | worker = calloc(params.nthreads, sizeof(*worker)); |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 181 | if (!worker) |
| 182 | err(EXIT_FAILURE, "calloc"); |
| 183 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 184 | if (!params.fshared) |
Davidlohr Bueso | 86c87e1 | 2014-09-29 09:41:07 -0700 | [diff] [blame] | 185 | futex_flag = FUTEX_PRIVATE_FLAG; |
| 186 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 187 | if (params.nrequeue > params.nthreads) |
| 188 | params.nrequeue = params.nthreads; |
Davidlohr Bueso | 052b0f6 | 2015-04-24 10:00:48 -0700 | [diff] [blame] | 189 | |
Davidlohr Bueso | d262e6a | 2021-08-08 21:32:59 -0700 | [diff] [blame] | 190 | if (params.broadcast) |
| 191 | params.nrequeue = params.nthreads; |
| 192 | |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 193 | printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %s%p), " |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 194 | "%d at a time.\n\n", getpid(), params.nthreads, |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 195 | params.fshared ? "shared":"private", &futex1, |
| 196 | params.pi ? "PI ": "", &futex2, params.nrequeue); |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 197 | |
| 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 Bueso | d9de84a | 2014-06-16 11:14:23 -0700 | [diff] [blame] | 205 | for (j = 0; j < bench_repeat && !done; j++) { |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 206 | unsigned int nrequeued = 0, wakeups = 0; |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 207 | struct timeval start, end, runtime; |
| 208 | |
| 209 | /* create, launch & block all threads */ |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 210 | block_threads(worker, thread_attr, cpu); |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 211 | |
| 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 Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 223 | while (nrequeued < params.nthreads) { |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 224 | int r; |
| 225 | |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 226 | /* |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 227 | * 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 Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 231 | */ |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 232 | 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 Bueso | 86c87e1 | 2014-09-29 09:41:07 -0700 | [diff] [blame] | 248 | } |
Davidlohr Bueso | 052b0f6 | 2015-04-24 10:00:48 -0700 | [diff] [blame] | 249 | |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 250 | 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 Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 256 | if (!params.silent) { |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 257 | 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 Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Davidlohr Bueso | 46f81532 | 2021-08-08 21:33:01 -0700 | [diff] [blame] | 273 | 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 Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 280 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 281 | for (i = 0; i < params.nthreads; i++) { |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 282 | ret = pthread_join(worker[i], NULL); |
| 283 | if (ret) |
| 284 | err(EXIT_FAILURE, "pthread_join"); |
| 285 | } |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 286 | } |
| 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 Mohamed | 88e4823 | 2021-11-12 22:11:33 +0200 | [diff] [blame] | 297 | perf_cpu_map__put(cpu); |
Davidlohr Bueso | 0fb298c | 2013-12-14 20:31:57 -0800 | [diff] [blame] | 298 | return ret; |
| 299 | err: |
| 300 | usage_with_options(bench_futex_requeue_usage, options); |
| 301 | exit(EXIT_FAILURE); |
| 302 | } |