]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/bench/futex-hash.c
Merge tag 'asoc-v5.7' of https://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[mirror_ubuntu-hirsute-kernel.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>
87ffb6c6 23#include <internal/cpumap.h>
9c3516d1 24#include <perf/cpumap.h>
9c304f6c 25
a0439711 26#include "../util/stat.h"
4b6ab94e 27#include <subcmd/parse-options.h>
a0439711
DB
28#include "bench.h"
29#include "futex.h"
30
31#include <err.h>
a0439711
DB
32
33static unsigned int nthreads = 0;
34static unsigned int nsecs = 10;
35/* amount of futexes per thread */
36static unsigned int nfutexes = 1024;
37static bool fshared = false, done = false, silent = false;
86c87e13 38static int futex_flag = 0;
a0439711 39
e4d9b04b 40struct timeval bench__start, bench__end, bench__runtime;
a0439711
DB
41static pthread_mutex_t thread_lock;
42static unsigned int threads_starting;
43static struct stats throughput_stats;
44static pthread_cond_t thread_parent, thread_worker;
45
46struct worker {
47 int tid;
48 u_int32_t *futex;
49 pthread_t thread;
50 unsigned long ops;
e2e1680f 51};
a0439711
DB
52
53static const struct option options[] = {
54 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
55 OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
56 OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"),
57 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
58 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
59 OPT_END()
60};
61
62static const char * const bench_futex_hash_usage[] = {
63 "perf bench futex hash <options>",
64 NULL
65};
66
67static void *workerfn(void *arg)
68{
69 int ret;
a0439711 70 struct worker *w = (struct worker *) arg;
e2e1680f
DB
71 unsigned int i;
72 unsigned long ops = w->ops; /* avoid cacheline bouncing */
a0439711
DB
73
74 pthread_mutex_lock(&thread_lock);
75 threads_starting--;
76 if (!threads_starting)
77 pthread_cond_signal(&thread_parent);
78 pthread_cond_wait(&thread_worker, &thread_lock);
79 pthread_mutex_unlock(&thread_lock);
80
81 do {
e2e1680f 82 for (i = 0; i < nfutexes; i++, ops++) {
a0439711
DB
83 /*
84 * We want the futex calls to fail in order to stress
85 * the hashing of uaddr and not measure other steps,
86 * such as internal waitqueue handling, thus enlarging
87 * the critical region protected by hb->lock.
88 */
86c87e13 89 ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
a0439711
DB
90 if (!silent &&
91 (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
92 warn("Non-expected futex return call");
93 }
94 } while (!done);
95
e2e1680f 96 w->ops = ops;
a0439711
DB
97 return NULL;
98}
99
100static void toggle_done(int sig __maybe_unused,
101 siginfo_t *info __maybe_unused,
102 void *uc __maybe_unused)
103{
104 /* inform all threads that we're done for the day */
105 done = true;
e4d9b04b
ACM
106 gettimeofday(&bench__end, NULL);
107 timersub(&bench__end, &bench__start, &bench__runtime);
a0439711
DB
108}
109
110static void print_summary(void)
111{
112 unsigned long avg = avg_stats(&throughput_stats);
113 double stddev = stddev_stats(&throughput_stats);
114
115 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
116 !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
e4d9b04b 117 (int)bench__runtime.tv_sec);
a0439711
DB
118}
119
b0ad8ea6 120int bench_futex_hash(int argc, const char **argv)
a0439711
DB
121{
122 int ret = 0;
3b2323c2 123 cpu_set_t cpuset;
a0439711 124 struct sigaction act;
3b2323c2 125 unsigned int i;
a0439711
DB
126 pthread_attr_t thread_attr;
127 struct worker *worker = NULL;
f854839b 128 struct perf_cpu_map *cpu;
a0439711
DB
129
130 argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
131 if (argc) {
132 usage_with_options(bench_futex_hash_usage, options);
133 exit(EXIT_FAILURE);
134 }
135
9c3516d1 136 cpu = perf_cpu_map__new(NULL);
3b2323c2
DB
137 if (!cpu)
138 goto errmem;
a0439711 139
7b919a53 140 memset(&act, 0, sizeof(act));
a0439711
DB
141 sigfillset(&act.sa_mask);
142 act.sa_sigaction = toggle_done;
143 sigaction(SIGINT, &act, NULL);
144
145 if (!nthreads) /* default to the number of CPUs */
3b2323c2 146 nthreads = cpu->nr;
a0439711
DB
147
148 worker = calloc(nthreads, sizeof(*worker));
149 if (!worker)
150 goto errmem;
151
86c87e13
DB
152 if (!fshared)
153 futex_flag = FUTEX_PRIVATE_FLAG;
154
a0439711
DB
155 printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
156 getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
157
158 init_stats(&throughput_stats);
159 pthread_mutex_init(&thread_lock, NULL);
160 pthread_cond_init(&thread_parent, NULL);
161 pthread_cond_init(&thread_worker, NULL);
162
163 threads_starting = nthreads;
164 pthread_attr_init(&thread_attr);
e4d9b04b 165 gettimeofday(&bench__start, NULL);
a0439711
DB
166 for (i = 0; i < nthreads; i++) {
167 worker[i].tid = i;
168 worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
169 if (!worker[i].futex)
170 goto errmem;
171
3b2323c2
DB
172 CPU_ZERO(&cpuset);
173 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
a0439711 174
3b2323c2 175 ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset);
a0439711
DB
176 if (ret)
177 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
178
179 ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
180 (void *)(struct worker *) &worker[i]);
181 if (ret)
182 err(EXIT_FAILURE, "pthread_create");
183
184 }
185 pthread_attr_destroy(&thread_attr);
186
187 pthread_mutex_lock(&thread_lock);
188 while (threads_starting)
189 pthread_cond_wait(&thread_parent, &thread_lock);
190 pthread_cond_broadcast(&thread_worker);
191 pthread_mutex_unlock(&thread_lock);
192
193 sleep(nsecs);
194 toggle_done(0, NULL, NULL);
195
196 for (i = 0; i < nthreads; i++) {
197 ret = pthread_join(worker[i].thread, NULL);
198 if (ret)
199 err(EXIT_FAILURE, "pthread_join");
200 }
201
202 /* cleanup & report results */
203 pthread_cond_destroy(&thread_parent);
204 pthread_cond_destroy(&thread_worker);
205 pthread_mutex_destroy(&thread_lock);
206
207 for (i = 0; i < nthreads; i++) {
e4d9b04b 208 unsigned long t = worker[i].ops / bench__runtime.tv_sec;
a0439711
DB
209 update_stats(&throughput_stats, t);
210 if (!silent) {
211 if (nfutexes == 1)
212 printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
213 worker[i].tid, &worker[i].futex[0], t);
214 else
215 printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
216 worker[i].tid, &worker[i].futex[0],
217 &worker[i].futex[nfutexes-1], t);
218 }
219
d8f9da24 220 zfree(&worker[i].futex);
a0439711
DB
221 }
222
223 print_summary();
224
225 free(worker);
3b2323c2 226 free(cpu);
a0439711
DB
227 return ret;
228errmem:
229 err(EXIT_FAILURE, "calloc");
230}