Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com> |
| 4 | * |
| 5 | * futex-hash: Stress the hell out of the Linux kernel futex uaddr hashing. |
| 6 | * |
| 7 | * This program is particularly useful for measuring the kernel's futex hash |
| 8 | * table/function implementation. In order for it to make sense, use with as |
| 9 | * many threads and futexes as possible. |
| 10 | */ |
| 11 | |
Arnaldo Carvalho de Melo | 8a15858 | 2016-07-06 12:14:56 -0300 | [diff] [blame] | 12 | /* For the CLR_() macros */ |
Arnaldo Carvalho de Melo | a0f213e14 | 2017-03-02 15:44:19 -0300 | [diff] [blame] | 13 | #include <string.h> |
Arnaldo Carvalho de Melo | 8a15858 | 2016-07-06 12:14:56 -0300 | [diff] [blame] | 14 | #include <pthread.h> |
| 15 | |
Arnaldo Carvalho de Melo | 9c304f6 | 2016-07-07 11:01:46 -0300 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | #include <signal.h> |
| 18 | #include <stdlib.h> |
Arnaldo Carvalho de Melo | 86695f5 | 2016-07-11 10:05:52 -0300 | [diff] [blame] | 19 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | 9c304f6 | 2016-07-07 11:01:46 -0300 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 21 | #include <linux/zalloc.h> |
Arnaldo Carvalho de Melo | 9c304f6 | 2016-07-07 11:01:46 -0300 | [diff] [blame] | 22 | #include <sys/time.h> |
Davidlohr Bueso | 9f9a3ff | 2021-08-08 21:32:58 -0700 | [diff] [blame] | 23 | #include <sys/mman.h> |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 24 | #include <perf/cpumap.h> |
Arnaldo Carvalho de Melo | 9c304f6 | 2016-07-07 11:01:46 -0300 | [diff] [blame] | 25 | |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 26 | #include "../util/stat.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 27 | #include <subcmd/parse-options.h> |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 28 | #include "bench.h" |
| 29 | #include "futex.h" |
| 30 | |
| 31 | #include <err.h> |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 32 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 33 | static bool done = false; |
Davidlohr Bueso | 86c87e1 | 2014-09-29 09:41:07 -0700 | [diff] [blame] | 34 | static int futex_flag = 0; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 35 | |
Arnaldo Carvalho de Melo | e4d9b04 | 2020-03-02 12:09:38 -0300 | [diff] [blame] | 36 | struct timeval bench__start, bench__end, bench__runtime; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 37 | static pthread_mutex_t thread_lock; |
| 38 | static unsigned int threads_starting; |
| 39 | static struct stats throughput_stats; |
| 40 | static pthread_cond_t thread_parent, thread_worker; |
| 41 | |
| 42 | struct worker { |
| 43 | int tid; |
| 44 | u_int32_t *futex; |
| 45 | pthread_t thread; |
| 46 | unsigned long ops; |
Davidlohr Bueso | e2e1680 | 2016-10-24 13:56:52 -0700 | [diff] [blame] | 47 | }; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 48 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 49 | static struct bench_futex_parameters params = { |
| 50 | .nfutexes = 1024, |
| 51 | .runtime = 10, |
| 52 | }; |
| 53 | |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 54 | static const struct option options[] = { |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 55 | OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"), |
| 56 | OPT_UINTEGER('r', "runtime", ¶ms.runtime, "Specify runtime (in seconds)"), |
| 57 | OPT_UINTEGER('f', "futexes", ¶ms.nfutexes, "Specify amount of futexes per threads"), |
| 58 | OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"), |
| 59 | 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] | 60 | OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"), |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 61 | OPT_END() |
| 62 | }; |
| 63 | |
| 64 | static const char * const bench_futex_hash_usage[] = { |
| 65 | "perf bench futex hash <options>", |
| 66 | NULL |
| 67 | }; |
| 68 | |
| 69 | static void *workerfn(void *arg) |
| 70 | { |
| 71 | int ret; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 72 | struct worker *w = (struct worker *) arg; |
Davidlohr Bueso | e2e1680 | 2016-10-24 13:56:52 -0700 | [diff] [blame] | 73 | unsigned int i; |
| 74 | unsigned long ops = w->ops; /* avoid cacheline bouncing */ |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 75 | |
| 76 | pthread_mutex_lock(&thread_lock); |
| 77 | threads_starting--; |
| 78 | if (!threads_starting) |
| 79 | pthread_cond_signal(&thread_parent); |
| 80 | pthread_cond_wait(&thread_worker, &thread_lock); |
| 81 | pthread_mutex_unlock(&thread_lock); |
| 82 | |
| 83 | do { |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 84 | for (i = 0; i < params.nfutexes; i++, ops++) { |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 85 | /* |
| 86 | * We want the futex calls to fail in order to stress |
| 87 | * the hashing of uaddr and not measure other steps, |
| 88 | * such as internal waitqueue handling, thus enlarging |
| 89 | * the critical region protected by hb->lock. |
| 90 | */ |
Davidlohr Bueso | 86c87e1 | 2014-09-29 09:41:07 -0700 | [diff] [blame] | 91 | ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag); |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 92 | if (!params.silent && |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 93 | (!ret || errno != EAGAIN || errno != EWOULDBLOCK)) |
| 94 | warn("Non-expected futex return call"); |
| 95 | } |
| 96 | } while (!done); |
| 97 | |
Davidlohr Bueso | e2e1680 | 2016-10-24 13:56:52 -0700 | [diff] [blame] | 98 | w->ops = ops; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | static void toggle_done(int sig __maybe_unused, |
| 103 | siginfo_t *info __maybe_unused, |
| 104 | void *uc __maybe_unused) |
| 105 | { |
| 106 | /* inform all threads that we're done for the day */ |
| 107 | done = true; |
Arnaldo Carvalho de Melo | e4d9b04 | 2020-03-02 12:09:38 -0300 | [diff] [blame] | 108 | gettimeofday(&bench__end, NULL); |
| 109 | timersub(&bench__end, &bench__start, &bench__runtime); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void print_summary(void) |
| 113 | { |
| 114 | unsigned long avg = avg_stats(&throughput_stats); |
| 115 | double stddev = stddev_stats(&throughput_stats); |
| 116 | |
| 117 | printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n", |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 118 | !params.silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg), |
Arnaldo Carvalho de Melo | e4d9b04 | 2020-03-02 12:09:38 -0300 | [diff] [blame] | 119 | (int)bench__runtime.tv_sec); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 122 | int bench_futex_hash(int argc, const char **argv) |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 123 | { |
| 124 | int ret = 0; |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 125 | cpu_set_t cpuset; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 126 | struct sigaction act; |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 127 | unsigned int i; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 128 | pthread_attr_t thread_attr; |
| 129 | struct worker *worker = NULL; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 130 | struct perf_cpu_map *cpu; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 131 | |
| 132 | argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0); |
| 133 | if (argc) { |
| 134 | usage_with_options(bench_futex_hash_usage, options); |
| 135 | exit(EXIT_FAILURE); |
| 136 | } |
| 137 | |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 138 | cpu = perf_cpu_map__new(NULL); |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 139 | if (!cpu) |
| 140 | goto errmem; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 141 | |
Tommi Rantala | 7b919a5 | 2020-03-05 10:37:14 +0200 | [diff] [blame] | 142 | memset(&act, 0, sizeof(act)); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 143 | sigfillset(&act.sa_mask); |
| 144 | act.sa_sigaction = toggle_done; |
| 145 | sigaction(SIGINT, &act, NULL); |
| 146 | |
Davidlohr Bueso | 9f9a3ff | 2021-08-08 21:32:58 -0700 | [diff] [blame] | 147 | if (params.mlockall) { |
| 148 | if (mlockall(MCL_CURRENT | MCL_FUTURE)) |
| 149 | err(EXIT_FAILURE, "mlockall"); |
| 150 | } |
| 151 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 152 | if (!params.nthreads) /* default to the number of CPUs */ |
| 153 | params.nthreads = cpu->nr; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 154 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 155 | worker = calloc(params.nthreads, sizeof(*worker)); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 156 | if (!worker) |
| 157 | goto errmem; |
| 158 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 159 | if (!params.fshared) |
Davidlohr Bueso | 86c87e1 | 2014-09-29 09:41:07 -0700 | [diff] [blame] | 160 | futex_flag = FUTEX_PRIVATE_FLAG; |
| 161 | |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 162 | printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n", |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 163 | getpid(), params.nthreads, params.nfutexes, params.fshared ? "shared":"private", params.runtime); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 164 | |
| 165 | init_stats(&throughput_stats); |
| 166 | pthread_mutex_init(&thread_lock, NULL); |
| 167 | pthread_cond_init(&thread_parent, NULL); |
| 168 | pthread_cond_init(&thread_worker, NULL); |
| 169 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 170 | threads_starting = params.nthreads; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 171 | pthread_attr_init(&thread_attr); |
Arnaldo Carvalho de Melo | e4d9b04 | 2020-03-02 12:09:38 -0300 | [diff] [blame] | 172 | gettimeofday(&bench__start, NULL); |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 173 | for (i = 0; i < params.nthreads; i++) { |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 174 | worker[i].tid = i; |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 175 | worker[i].futex = calloc(params.nfutexes, sizeof(*worker[i].futex)); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 176 | if (!worker[i].futex) |
| 177 | goto errmem; |
| 178 | |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 179 | CPU_ZERO(&cpuset); |
| 180 | CPU_SET(cpu->map[i % cpu->nr], &cpuset); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 181 | |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 182 | ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 183 | if (ret) |
| 184 | err(EXIT_FAILURE, "pthread_attr_setaffinity_np"); |
| 185 | |
| 186 | ret = pthread_create(&worker[i].thread, &thread_attr, workerfn, |
| 187 | (void *)(struct worker *) &worker[i]); |
| 188 | if (ret) |
| 189 | err(EXIT_FAILURE, "pthread_create"); |
| 190 | |
| 191 | } |
| 192 | pthread_attr_destroy(&thread_attr); |
| 193 | |
| 194 | pthread_mutex_lock(&thread_lock); |
| 195 | while (threads_starting) |
| 196 | pthread_cond_wait(&thread_parent, &thread_lock); |
| 197 | pthread_cond_broadcast(&thread_worker); |
| 198 | pthread_mutex_unlock(&thread_lock); |
| 199 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 200 | sleep(params.runtime); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 201 | toggle_done(0, NULL, NULL); |
| 202 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 203 | for (i = 0; i < params.nthreads; i++) { |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 204 | ret = pthread_join(worker[i].thread, NULL); |
| 205 | if (ret) |
| 206 | err(EXIT_FAILURE, "pthread_join"); |
| 207 | } |
| 208 | |
| 209 | /* cleanup & report results */ |
| 210 | pthread_cond_destroy(&thread_parent); |
| 211 | pthread_cond_destroy(&thread_worker); |
| 212 | pthread_mutex_destroy(&thread_lock); |
| 213 | |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 214 | for (i = 0; i < params.nthreads; i++) { |
Tommi Rantala | 41e7c32 | 2020-04-17 16:23:29 +0300 | [diff] [blame] | 215 | unsigned long t = bench__runtime.tv_sec > 0 ? |
| 216 | worker[i].ops / bench__runtime.tv_sec : 0; |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 217 | update_stats(&throughput_stats, t); |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 218 | if (!params.silent) { |
| 219 | if (params.nfutexes == 1) |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 220 | printf("[thread %2d] futex: %p [ %ld ops/sec ]\n", |
| 221 | worker[i].tid, &worker[i].futex[0], t); |
| 222 | else |
| 223 | printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n", |
| 224 | worker[i].tid, &worker[i].futex[0], |
Davidlohr Bueso | 0959046 | 2021-08-08 21:32:55 -0700 | [diff] [blame] | 225 | &worker[i].futex[params.nfutexes-1], t); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 228 | zfree(&worker[i].futex); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | print_summary(); |
| 232 | |
| 233 | free(worker); |
Davidlohr Bueso | 3b2323c | 2017-11-26 20:20:59 -0800 | [diff] [blame] | 234 | free(cpu); |
Davidlohr Bueso | a043971 | 2013-12-14 20:31:55 -0800 | [diff] [blame] | 235 | return ret; |
| 236 | errmem: |
| 237 | err(EXIT_FAILURE, "calloc"); |
| 238 | } |