blob: fcdea3e44937896277c80cb3e9a6967392a756b7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Davidlohr Buesoa0439712013-12-14 20:31:55 -08002/*
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 Melo8a158582016-07-06 12:14:56 -030012/* For the CLR_() macros */
Arnaldo Carvalho de Meloa0f213e142017-03-02 15:44:19 -030013#include <string.h>
Arnaldo Carvalho de Melo8a158582016-07-06 12:14:56 -030014#include <pthread.h>
15
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030016#include <errno.h>
17#include <signal.h>
18#include <stdlib.h>
Arnaldo Carvalho de Melo86695f52016-07-11 10:05:52 -030019#include <linux/compiler.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030020#include <linux/kernel.h>
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -030021#include <linux/zalloc.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030022#include <sys/time.h>
Davidlohr Bueso9f9a3ff2021-08-08 21:32:58 -070023#include <sys/mman.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +020024#include <perf/cpumap.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030025
Davidlohr Buesoa0439712013-12-14 20:31:55 -080026#include "../util/stat.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060027#include <subcmd/parse-options.h>
Davidlohr Buesoa0439712013-12-14 20:31:55 -080028#include "bench.h"
29#include "futex.h"
30
31#include <err.h>
Davidlohr Buesoa0439712013-12-14 20:31:55 -080032
Davidlohr Bueso09590462021-08-08 21:32:55 -070033static bool done = false;
Davidlohr Bueso86c87e12014-09-29 09:41:07 -070034static int futex_flag = 0;
Davidlohr Buesoa0439712013-12-14 20:31:55 -080035
Arnaldo Carvalho de Meloe4d9b042020-03-02 12:09:38 -030036struct timeval bench__start, bench__end, bench__runtime;
Davidlohr Buesoa0439712013-12-14 20:31:55 -080037static pthread_mutex_t thread_lock;
38static unsigned int threads_starting;
39static struct stats throughput_stats;
40static pthread_cond_t thread_parent, thread_worker;
41
42struct worker {
43 int tid;
44 u_int32_t *futex;
45 pthread_t thread;
46 unsigned long ops;
Davidlohr Buesoe2e16802016-10-24 13:56:52 -070047};
Davidlohr Buesoa0439712013-12-14 20:31:55 -080048
Davidlohr Bueso09590462021-08-08 21:32:55 -070049static struct bench_futex_parameters params = {
50 .nfutexes = 1024,
51 .runtime = 10,
52};
53
Davidlohr Buesoa0439712013-12-14 20:31:55 -080054static const struct option options[] = {
Davidlohr Bueso09590462021-08-08 21:32:55 -070055 OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
56 OPT_UINTEGER('r', "runtime", &params.runtime, "Specify runtime (in seconds)"),
57 OPT_UINTEGER('f', "futexes", &params.nfutexes, "Specify amount of futexes per threads"),
58 OPT_BOOLEAN( 's', "silent", &params.silent, "Silent mode: do not display data/details"),
59 OPT_BOOLEAN( 'S', "shared", &params.fshared, "Use shared futexes instead of private ones"),
Davidlohr Bueso9f9a3ff2021-08-08 21:32:58 -070060 OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
Davidlohr Buesoa0439712013-12-14 20:31:55 -080061 OPT_END()
62};
63
64static const char * const bench_futex_hash_usage[] = {
65 "perf bench futex hash <options>",
66 NULL
67};
68
69static void *workerfn(void *arg)
70{
71 int ret;
Davidlohr Buesoa0439712013-12-14 20:31:55 -080072 struct worker *w = (struct worker *) arg;
Davidlohr Buesoe2e16802016-10-24 13:56:52 -070073 unsigned int i;
74 unsigned long ops = w->ops; /* avoid cacheline bouncing */
Davidlohr Buesoa0439712013-12-14 20:31:55 -080075
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 Bueso09590462021-08-08 21:32:55 -070084 for (i = 0; i < params.nfutexes; i++, ops++) {
Davidlohr Buesoa0439712013-12-14 20:31:55 -080085 /*
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 Bueso86c87e12014-09-29 09:41:07 -070091 ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
Davidlohr Bueso09590462021-08-08 21:32:55 -070092 if (!params.silent &&
Davidlohr Buesoa0439712013-12-14 20:31:55 -080093 (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
94 warn("Non-expected futex return call");
95 }
96 } while (!done);
97
Davidlohr Buesoe2e16802016-10-24 13:56:52 -070098 w->ops = ops;
Davidlohr Buesoa0439712013-12-14 20:31:55 -080099 return NULL;
100}
101
102static 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 Meloe4d9b042020-03-02 12:09:38 -0300108 gettimeofday(&bench__end, NULL);
109 timersub(&bench__end, &bench__start, &bench__runtime);
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800110}
111
112static 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 Bueso09590462021-08-08 21:32:55 -0700118 !params.silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
Arnaldo Carvalho de Meloe4d9b042020-03-02 12:09:38 -0300119 (int)bench__runtime.tv_sec);
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800120}
121
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300122int bench_futex_hash(int argc, const char **argv)
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800123{
124 int ret = 0;
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800125 cpu_set_t cpuset;
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800126 struct sigaction act;
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800127 unsigned int i;
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800128 pthread_attr_t thread_attr;
129 struct worker *worker = NULL;
Jiri Olsaf8548392019-07-21 13:23:49 +0200130 struct perf_cpu_map *cpu;
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800131
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 Olsa9c3516d2019-07-21 13:24:30 +0200138 cpu = perf_cpu_map__new(NULL);
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800139 if (!cpu)
140 goto errmem;
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800141
Tommi Rantala7b919a52020-03-05 10:37:14 +0200142 memset(&act, 0, sizeof(act));
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800143 sigfillset(&act.sa_mask);
144 act.sa_sigaction = toggle_done;
145 sigaction(SIGINT, &act, NULL);
146
Davidlohr Bueso9f9a3ff2021-08-08 21:32:58 -0700147 if (params.mlockall) {
148 if (mlockall(MCL_CURRENT | MCL_FUTURE))
149 err(EXIT_FAILURE, "mlockall");
150 }
151
Davidlohr Bueso09590462021-08-08 21:32:55 -0700152 if (!params.nthreads) /* default to the number of CPUs */
153 params.nthreads = cpu->nr;
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800154
Davidlohr Bueso09590462021-08-08 21:32:55 -0700155 worker = calloc(params.nthreads, sizeof(*worker));
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800156 if (!worker)
157 goto errmem;
158
Davidlohr Bueso09590462021-08-08 21:32:55 -0700159 if (!params.fshared)
Davidlohr Bueso86c87e12014-09-29 09:41:07 -0700160 futex_flag = FUTEX_PRIVATE_FLAG;
161
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800162 printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
Davidlohr Bueso09590462021-08-08 21:32:55 -0700163 getpid(), params.nthreads, params.nfutexes, params.fshared ? "shared":"private", params.runtime);
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800164
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 Bueso09590462021-08-08 21:32:55 -0700170 threads_starting = params.nthreads;
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800171 pthread_attr_init(&thread_attr);
Arnaldo Carvalho de Meloe4d9b042020-03-02 12:09:38 -0300172 gettimeofday(&bench__start, NULL);
Davidlohr Bueso09590462021-08-08 21:32:55 -0700173 for (i = 0; i < params.nthreads; i++) {
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800174 worker[i].tid = i;
Davidlohr Bueso09590462021-08-08 21:32:55 -0700175 worker[i].futex = calloc(params.nfutexes, sizeof(*worker[i].futex));
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800176 if (!worker[i].futex)
177 goto errmem;
178
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800179 CPU_ZERO(&cpuset);
180 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800181
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800182 ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset);
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800183 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 Bueso09590462021-08-08 21:32:55 -0700200 sleep(params.runtime);
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800201 toggle_done(0, NULL, NULL);
202
Davidlohr Bueso09590462021-08-08 21:32:55 -0700203 for (i = 0; i < params.nthreads; i++) {
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800204 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 Bueso09590462021-08-08 21:32:55 -0700214 for (i = 0; i < params.nthreads; i++) {
Tommi Rantala41e7c322020-04-17 16:23:29 +0300215 unsigned long t = bench__runtime.tv_sec > 0 ?
216 worker[i].ops / bench__runtime.tv_sec : 0;
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800217 update_stats(&throughput_stats, t);
Davidlohr Bueso09590462021-08-08 21:32:55 -0700218 if (!params.silent) {
219 if (params.nfutexes == 1)
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800220 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 Bueso09590462021-08-08 21:32:55 -0700225 &worker[i].futex[params.nfutexes-1], t);
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800226 }
227
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300228 zfree(&worker[i].futex);
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800229 }
230
231 print_summary();
232
233 free(worker);
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800234 free(cpu);
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800235 return ret;
236errmem:
237 err(EXIT_FAILURE, "calloc");
238}