blob: fe16b310097f509042a87ec8d2ee26a0f445c199 [file] [log] [blame]
Davidlohr Buesoa0439712013-12-14 20:31:55 -08001/*
2 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
3 *
4 * futex-hash: Stress the hell out of the Linux kernel futex uaddr hashing.
5 *
6 * This program is particularly useful for measuring the kernel's futex hash
7 * table/function implementation. In order for it to make sense, use with as
8 * many threads and futexes as possible.
9 */
10
Arnaldo Carvalho de Melo8a158582016-07-06 12:14:56 -030011/* For the CLR_() macros */
Arnaldo Carvalho de Meloa0f213e142017-03-02 15:44:19 -030012#include <string.h>
Arnaldo Carvalho de Melo8a158582016-07-06 12:14:56 -030013#include <pthread.h>
14
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030015#include <errno.h>
16#include <signal.h>
17#include <stdlib.h>
Arnaldo Carvalho de Melo86695f52016-07-11 10:05:52 -030018#include <linux/compiler.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030019#include <linux/kernel.h>
20#include <sys/time.h>
21
Davidlohr Buesoa0439712013-12-14 20:31:55 -080022#include "../util/stat.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060023#include <subcmd/parse-options.h>
Davidlohr Buesoa0439712013-12-14 20:31:55 -080024#include "bench.h"
25#include "futex.h"
26
27#include <err.h>
Davidlohr Buesoa0439712013-12-14 20:31:55 -080028#include <sys/time.h>
Davidlohr Buesoa0439712013-12-14 20:31:55 -080029
30static unsigned int nthreads = 0;
31static unsigned int nsecs = 10;
32/* amount of futexes per thread */
33static unsigned int nfutexes = 1024;
34static bool fshared = false, done = false, silent = false;
Davidlohr Bueso86c87e12014-09-29 09:41:07 -070035static int futex_flag = 0;
Davidlohr Buesoa0439712013-12-14 20:31:55 -080036
37struct timeval start, end, runtime;
38static pthread_mutex_t thread_lock;
39static unsigned int threads_starting;
40static struct stats throughput_stats;
41static pthread_cond_t thread_parent, thread_worker;
42
43struct worker {
44 int tid;
45 u_int32_t *futex;
46 pthread_t thread;
47 unsigned long ops;
Davidlohr Buesoe2e16802016-10-24 13:56:52 -070048};
Davidlohr Buesoa0439712013-12-14 20:31:55 -080049
50static const struct option options[] = {
51 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
52 OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
53 OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"),
54 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
55 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
56 OPT_END()
57};
58
59static const char * const bench_futex_hash_usage[] = {
60 "perf bench futex hash <options>",
61 NULL
62};
63
64static void *workerfn(void *arg)
65{
66 int ret;
Davidlohr Buesoa0439712013-12-14 20:31:55 -080067 struct worker *w = (struct worker *) arg;
Davidlohr Buesoe2e16802016-10-24 13:56:52 -070068 unsigned int i;
69 unsigned long ops = w->ops; /* avoid cacheline bouncing */
Davidlohr Buesoa0439712013-12-14 20:31:55 -080070
71 pthread_mutex_lock(&thread_lock);
72 threads_starting--;
73 if (!threads_starting)
74 pthread_cond_signal(&thread_parent);
75 pthread_cond_wait(&thread_worker, &thread_lock);
76 pthread_mutex_unlock(&thread_lock);
77
78 do {
Davidlohr Buesoe2e16802016-10-24 13:56:52 -070079 for (i = 0; i < nfutexes; i++, ops++) {
Davidlohr Buesoa0439712013-12-14 20:31:55 -080080 /*
81 * We want the futex calls to fail in order to stress
82 * the hashing of uaddr and not measure other steps,
83 * such as internal waitqueue handling, thus enlarging
84 * the critical region protected by hb->lock.
85 */
Davidlohr Bueso86c87e12014-09-29 09:41:07 -070086 ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
Davidlohr Buesoa0439712013-12-14 20:31:55 -080087 if (!silent &&
88 (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
89 warn("Non-expected futex return call");
90 }
91 } while (!done);
92
Davidlohr Buesoe2e16802016-10-24 13:56:52 -070093 w->ops = ops;
Davidlohr Buesoa0439712013-12-14 20:31:55 -080094 return NULL;
95}
96
97static void toggle_done(int sig __maybe_unused,
98 siginfo_t *info __maybe_unused,
99 void *uc __maybe_unused)
100{
101 /* inform all threads that we're done for the day */
102 done = true;
103 gettimeofday(&end, NULL);
104 timersub(&end, &start, &runtime);
105}
106
107static void print_summary(void)
108{
109 unsigned long avg = avg_stats(&throughput_stats);
110 double stddev = stddev_stats(&throughput_stats);
111
112 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
113 !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
114 (int) runtime.tv_sec);
115}
116
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300117int bench_futex_hash(int argc, const char **argv)
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800118{
119 int ret = 0;
120 cpu_set_t cpu;
121 struct sigaction act;
122 unsigned int i, ncpus;
123 pthread_attr_t thread_attr;
124 struct worker *worker = NULL;
125
126 argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
127 if (argc) {
128 usage_with_options(bench_futex_hash_usage, options);
129 exit(EXIT_FAILURE);
130 }
131
132 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
133
134 sigfillset(&act.sa_mask);
135 act.sa_sigaction = toggle_done;
136 sigaction(SIGINT, &act, NULL);
137
138 if (!nthreads) /* default to the number of CPUs */
139 nthreads = ncpus;
140
141 worker = calloc(nthreads, sizeof(*worker));
142 if (!worker)
143 goto errmem;
144
Davidlohr Bueso86c87e12014-09-29 09:41:07 -0700145 if (!fshared)
146 futex_flag = FUTEX_PRIVATE_FLAG;
147
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800148 printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
149 getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
150
151 init_stats(&throughput_stats);
152 pthread_mutex_init(&thread_lock, NULL);
153 pthread_cond_init(&thread_parent, NULL);
154 pthread_cond_init(&thread_worker, NULL);
155
156 threads_starting = nthreads;
157 pthread_attr_init(&thread_attr);
158 gettimeofday(&start, NULL);
159 for (i = 0; i < nthreads; i++) {
160 worker[i].tid = i;
161 worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
162 if (!worker[i].futex)
163 goto errmem;
164
165 CPU_ZERO(&cpu);
166 CPU_SET(i % ncpus, &cpu);
167
168 ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu);
169 if (ret)
170 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
171
172 ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
173 (void *)(struct worker *) &worker[i]);
174 if (ret)
175 err(EXIT_FAILURE, "pthread_create");
176
177 }
178 pthread_attr_destroy(&thread_attr);
179
180 pthread_mutex_lock(&thread_lock);
181 while (threads_starting)
182 pthread_cond_wait(&thread_parent, &thread_lock);
183 pthread_cond_broadcast(&thread_worker);
184 pthread_mutex_unlock(&thread_lock);
185
186 sleep(nsecs);
187 toggle_done(0, NULL, NULL);
188
189 for (i = 0; i < nthreads; i++) {
190 ret = pthread_join(worker[i].thread, NULL);
191 if (ret)
192 err(EXIT_FAILURE, "pthread_join");
193 }
194
195 /* cleanup & report results */
196 pthread_cond_destroy(&thread_parent);
197 pthread_cond_destroy(&thread_worker);
198 pthread_mutex_destroy(&thread_lock);
199
200 for (i = 0; i < nthreads; i++) {
201 unsigned long t = worker[i].ops/runtime.tv_sec;
202 update_stats(&throughput_stats, t);
203 if (!silent) {
204 if (nfutexes == 1)
205 printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
206 worker[i].tid, &worker[i].futex[0], t);
207 else
208 printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
209 worker[i].tid, &worker[i].futex[0],
210 &worker[i].futex[nfutexes-1], t);
211 }
212
213 free(worker[i].futex);
214 }
215
216 print_summary();
217
218 free(worker);
219 return ret;
220errmem:
221 err(EXIT_FAILURE, "calloc");
222}