Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-block.git] / tools / perf / bench / futex-hash.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
a0439711
DB
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
8a158589 12/* For the CLR_() macros */
a0f213e1 13#include <string.h>
8a158589
ACM
14#include <pthread.h>
15
9c304f6c
ACM
16#include <errno.h>
17#include <signal.h>
18#include <stdlib.h>
86695f59 19#include <linux/compiler.h>
9c304f6c 20#include <linux/kernel.h>
d8f9da24 21#include <linux/zalloc.h>
9c304f6c 22#include <sys/time.h>
9f9a3ffe 23#include <sys/mman.h>
9c3516d1 24#include <perf/cpumap.h>
9c304f6c 25
a64d3af5 26#include "../util/mutex.h"
a0439711 27#include "../util/stat.h"
4b6ab94e 28#include <subcmd/parse-options.h>
a0439711
DB
29#include "bench.h"
30#include "futex.h"
31
32#include <err.h>
a0439711 33
09590463 34static bool done = false;
86c87e13 35static int futex_flag = 0;
a0439711 36
e4d9b04b 37struct timeval bench__start, bench__end, bench__runtime;
a64d3af5 38static struct mutex thread_lock;
a0439711
DB
39static unsigned int threads_starting;
40static struct stats throughput_stats;
a64d3af5 41static struct cond thread_parent, thread_worker;
a0439711
DB
42
43struct worker {
44 int tid;
45 u_int32_t *futex;
46 pthread_t thread;
47 unsigned long ops;
e2e1680f 48};
a0439711 49
09590463
DB
50static struct bench_futex_parameters params = {
51 .nfutexes = 1024,
52 .runtime = 10,
53};
54
a0439711 55static const struct option options[] = {
09590463
DB
56 OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
57 OPT_UINTEGER('r', "runtime", &params.runtime, "Specify runtime (in seconds)"),
58 OPT_UINTEGER('f', "futexes", &params.nfutexes, "Specify amount of futexes per threads"),
59 OPT_BOOLEAN( 's', "silent", &params.silent, "Silent mode: do not display data/details"),
60 OPT_BOOLEAN( 'S', "shared", &params.fshared, "Use shared futexes instead of private ones"),
9f9a3ffe 61 OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
a0439711
DB
62 OPT_END()
63};
64
65static const char * const bench_futex_hash_usage[] = {
66 "perf bench futex hash <options>",
67 NULL
68};
69
70static void *workerfn(void *arg)
71{
72 int ret;
a0439711 73 struct worker *w = (struct worker *) arg;
e2e1680f
DB
74 unsigned int i;
75 unsigned long ops = w->ops; /* avoid cacheline bouncing */
a0439711 76
a64d3af5 77 mutex_lock(&thread_lock);
a0439711
DB
78 threads_starting--;
79 if (!threads_starting)
a64d3af5
IR
80 cond_signal(&thread_parent);
81 cond_wait(&thread_worker, &thread_lock);
82 mutex_unlock(&thread_lock);
a0439711
DB
83
84 do {
09590463 85 for (i = 0; i < params.nfutexes; i++, ops++) {
a0439711
DB
86 /*
87 * We want the futex calls to fail in order to stress
88 * the hashing of uaddr and not measure other steps,
89 * such as internal waitqueue handling, thus enlarging
90 * the critical region protected by hb->lock.
91 */
86c87e13 92 ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
09590463 93 if (!params.silent &&
a0439711
DB
94 (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
95 warn("Non-expected futex return call");
96 }
97 } while (!done);
98
e2e1680f 99 w->ops = ops;
a0439711
DB
100 return NULL;
101}
102
103static void toggle_done(int sig __maybe_unused,
104 siginfo_t *info __maybe_unused,
105 void *uc __maybe_unused)
106{
107 /* inform all threads that we're done for the day */
108 done = true;
e4d9b04b
ACM
109 gettimeofday(&bench__end, NULL);
110 timersub(&bench__end, &bench__start, &bench__runtime);
a0439711
DB
111}
112
113static void print_summary(void)
114{
115 unsigned long avg = avg_stats(&throughput_stats);
116 double stddev = stddev_stats(&throughput_stats);
117
118 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
09590463 119 !params.silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
e4d9b04b 120 (int)bench__runtime.tv_sec);
a0439711
DB
121}
122
b0ad8ea6 123int bench_futex_hash(int argc, const char **argv)
a0439711
DB
124{
125 int ret = 0;
c9c2a427 126 cpu_set_t *cpuset;
a0439711 127 struct sigaction act;
3b2323c2 128 unsigned int i;
a0439711
DB
129 pthread_attr_t thread_attr;
130 struct worker *worker = NULL;
f854839b 131 struct perf_cpu_map *cpu;
c9c2a427
AR
132 int nrcpus;
133 size_t size;
a0439711
DB
134
135 argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
136 if (argc) {
137 usage_with_options(bench_futex_hash_usage, options);
138 exit(EXIT_FAILURE);
139 }
140
effe957c 141 cpu = perf_cpu_map__new_online_cpus();
3b2323c2
DB
142 if (!cpu)
143 goto errmem;
a0439711 144
7b919a53 145 memset(&act, 0, sizeof(act));
a0439711
DB
146 sigfillset(&act.sa_mask);
147 act.sa_sigaction = toggle_done;
148 sigaction(SIGINT, &act, NULL);
149
9f9a3ffe
DB
150 if (params.mlockall) {
151 if (mlockall(MCL_CURRENT | MCL_FUTURE))
152 err(EXIT_FAILURE, "mlockall");
153 }
154
09590463 155 if (!params.nthreads) /* default to the number of CPUs */
44028699 156 params.nthreads = perf_cpu_map__nr(cpu);
a0439711 157
09590463 158 worker = calloc(params.nthreads, sizeof(*worker));
a0439711
DB
159 if (!worker)
160 goto errmem;
161
09590463 162 if (!params.fshared)
86c87e13
DB
163 futex_flag = FUTEX_PRIVATE_FLAG;
164
a0439711 165 printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
09590463 166 getpid(), params.nthreads, params.nfutexes, params.fshared ? "shared":"private", params.runtime);
a0439711
DB
167
168 init_stats(&throughput_stats);
a64d3af5
IR
169 mutex_init(&thread_lock);
170 cond_init(&thread_parent);
171 cond_init(&thread_worker);
a0439711 172
09590463 173 threads_starting = params.nthreads;
a0439711 174 pthread_attr_init(&thread_attr);
e4d9b04b 175 gettimeofday(&bench__start, NULL);
c9c2a427 176
18337358 177 nrcpus = cpu__max_cpu().cpu;
c9c2a427
AR
178 cpuset = CPU_ALLOC(nrcpus);
179 BUG_ON(!cpuset);
180 size = CPU_ALLOC_SIZE(nrcpus);
181
09590463 182 for (i = 0; i < params.nthreads; i++) {
a0439711 183 worker[i].tid = i;
09590463 184 worker[i].futex = calloc(params.nfutexes, sizeof(*worker[i].futex));
a0439711
DB
185 if (!worker[i].futex)
186 goto errmem;
187
c9c2a427 188 CPU_ZERO_S(size, cpuset);
a0439711 189
c9c2a427
AR
190 CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
191 ret = pthread_attr_setaffinity_np(&thread_attr, size, cpuset);
192 if (ret) {
193 CPU_FREE(cpuset);
a0439711 194 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
c9c2a427 195 }
a0439711
DB
196 ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
197 (void *)(struct worker *) &worker[i]);
c9c2a427
AR
198 if (ret) {
199 CPU_FREE(cpuset);
a0439711 200 err(EXIT_FAILURE, "pthread_create");
c9c2a427 201 }
a0439711
DB
202
203 }
c9c2a427 204 CPU_FREE(cpuset);
a0439711
DB
205 pthread_attr_destroy(&thread_attr);
206
a64d3af5 207 mutex_lock(&thread_lock);
a0439711 208 while (threads_starting)
a64d3af5
IR
209 cond_wait(&thread_parent, &thread_lock);
210 cond_broadcast(&thread_worker);
211 mutex_unlock(&thread_lock);
a0439711 212
09590463 213 sleep(params.runtime);
a0439711
DB
214 toggle_done(0, NULL, NULL);
215
09590463 216 for (i = 0; i < params.nthreads; i++) {
a0439711
DB
217 ret = pthread_join(worker[i].thread, NULL);
218 if (ret)
219 err(EXIT_FAILURE, "pthread_join");
220 }
221
222 /* cleanup & report results */
a64d3af5
IR
223 cond_destroy(&thread_parent);
224 cond_destroy(&thread_worker);
225 mutex_destroy(&thread_lock);
a0439711 226
09590463 227 for (i = 0; i < params.nthreads; i++) {
41e7c32b
TR
228 unsigned long t = bench__runtime.tv_sec > 0 ?
229 worker[i].ops / bench__runtime.tv_sec : 0;
a0439711 230 update_stats(&throughput_stats, t);
09590463
DB
231 if (!params.silent) {
232 if (params.nfutexes == 1)
a0439711
DB
233 printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
234 worker[i].tid, &worker[i].futex[0], t);
235 else
236 printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
237 worker[i].tid, &worker[i].futex[0],
09590463 238 &worker[i].futex[params.nfutexes-1], t);
a0439711
DB
239 }
240
d8f9da24 241 zfree(&worker[i].futex);
a0439711
DB
242 }
243
244 print_summary();
245
246 free(worker);
3b2323c2 247 free(cpu);
a0439711
DB
248 return ret;
249errmem:
250 err(EXIT_FAILURE, "calloc");
251}