blob: d9e5e80bb4d0b7b2b49972057c28a377a40aec89 [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 */
12#include <pthread.h>
13
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030014#include <errno.h>
15#include <signal.h>
16#include <stdlib.h>
Arnaldo Carvalho de Melo86695f52016-07-11 10:05:52 -030017#include <linux/compiler.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030018#include <linux/kernel.h>
19#include <sys/time.h>
20
Davidlohr Buesoa0439712013-12-14 20:31:55 -080021#include "../util/stat.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060022#include <subcmd/parse-options.h>
Davidlohr Buesoa0439712013-12-14 20:31:55 -080023#include "bench.h"
24#include "futex.h"
25
26#include <err.h>
Davidlohr Buesoa0439712013-12-14 20:31:55 -080027#include <sys/time.h>
Davidlohr Buesoa0439712013-12-14 20:31:55 -080028
29static unsigned int nthreads = 0;
30static unsigned int nsecs = 10;
31/* amount of futexes per thread */
32static unsigned int nfutexes = 1024;
33static bool fshared = false, done = false, silent = false;
Davidlohr Bueso86c87e12014-09-29 09:41:07 -070034static int futex_flag = 0;
Davidlohr Buesoa0439712013-12-14 20:31:55 -080035
36struct timeval start, end, runtime;
37static pthread_mutex_t thread_lock;
38static unsigned int threads_starting;
39static struct stats throughput_stats;
40static pthread_cond_t thread_parent, thread_worker;
41
Sebastian Andrzej Siewior34b75302016-10-16 21:08:02 +020042#define SMP_CACHE_BYTES 256
43#define __cacheline_aligned __attribute__ ((aligned (SMP_CACHE_BYTES)))
44
Davidlohr Buesoa0439712013-12-14 20:31:55 -080045struct worker {
46 int tid;
47 u_int32_t *futex;
48 pthread_t thread;
49 unsigned long ops;
Sebastian Andrzej Siewior34b75302016-10-16 21:08:02 +020050} __cacheline_aligned;
Davidlohr Buesoa0439712013-12-14 20:31:55 -080051
52static const struct option options[] = {
53 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
54 OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
55 OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"),
56 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
57 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
58 OPT_END()
59};
60
61static const char * const bench_futex_hash_usage[] = {
62 "perf bench futex hash <options>",
63 NULL
64};
65
66static void *workerfn(void *arg)
67{
68 int ret;
69 unsigned int i;
70 struct worker *w = (struct worker *) arg;
71
72 pthread_mutex_lock(&thread_lock);
73 threads_starting--;
74 if (!threads_starting)
75 pthread_cond_signal(&thread_parent);
76 pthread_cond_wait(&thread_worker, &thread_lock);
77 pthread_mutex_unlock(&thread_lock);
78
79 do {
80 for (i = 0; i < nfutexes; i++, w->ops++) {
81 /*
82 * We want the futex calls to fail in order to stress
83 * the hashing of uaddr and not measure other steps,
84 * such as internal waitqueue handling, thus enlarging
85 * the critical region protected by hb->lock.
86 */
Davidlohr Bueso86c87e12014-09-29 09:41:07 -070087 ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
Davidlohr Buesoa0439712013-12-14 20:31:55 -080088 if (!silent &&
89 (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
90 warn("Non-expected futex return call");
91 }
92 } while (!done);
93
94 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
117int bench_futex_hash(int argc, const char **argv,
118 const char *prefix __maybe_unused)
119{
120 int ret = 0;
121 cpu_set_t cpu;
122 struct sigaction act;
123 unsigned int i, ncpus;
124 pthread_attr_t thread_attr;
125 struct worker *worker = NULL;
126
127 argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
128 if (argc) {
129 usage_with_options(bench_futex_hash_usage, options);
130 exit(EXIT_FAILURE);
131 }
132
133 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
134
135 sigfillset(&act.sa_mask);
136 act.sa_sigaction = toggle_done;
137 sigaction(SIGINT, &act, NULL);
138
139 if (!nthreads) /* default to the number of CPUs */
140 nthreads = ncpus;
141
142 worker = calloc(nthreads, sizeof(*worker));
143 if (!worker)
144 goto errmem;
145
Davidlohr Bueso86c87e12014-09-29 09:41:07 -0700146 if (!fshared)
147 futex_flag = FUTEX_PRIVATE_FLAG;
148
Davidlohr Buesoa0439712013-12-14 20:31:55 -0800149 printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
150 getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
151
152 init_stats(&throughput_stats);
153 pthread_mutex_init(&thread_lock, NULL);
154 pthread_cond_init(&thread_parent, NULL);
155 pthread_cond_init(&thread_worker, NULL);
156
157 threads_starting = nthreads;
158 pthread_attr_init(&thread_attr);
159 gettimeofday(&start, NULL);
160 for (i = 0; i < nthreads; i++) {
161 worker[i].tid = i;
162 worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
163 if (!worker[i].futex)
164 goto errmem;
165
166 CPU_ZERO(&cpu);
167 CPU_SET(i % ncpus, &cpu);
168
169 ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu);
170 if (ret)
171 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
172
173 ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
174 (void *)(struct worker *) &worker[i]);
175 if (ret)
176 err(EXIT_FAILURE, "pthread_create");
177
178 }
179 pthread_attr_destroy(&thread_attr);
180
181 pthread_mutex_lock(&thread_lock);
182 while (threads_starting)
183 pthread_cond_wait(&thread_parent, &thread_lock);
184 pthread_cond_broadcast(&thread_worker);
185 pthread_mutex_unlock(&thread_lock);
186
187 sleep(nsecs);
188 toggle_done(0, NULL, NULL);
189
190 for (i = 0; i < nthreads; i++) {
191 ret = pthread_join(worker[i].thread, NULL);
192 if (ret)
193 err(EXIT_FAILURE, "pthread_join");
194 }
195
196 /* cleanup & report results */
197 pthread_cond_destroy(&thread_parent);
198 pthread_cond_destroy(&thread_worker);
199 pthread_mutex_destroy(&thread_lock);
200
201 for (i = 0; i < nthreads; i++) {
202 unsigned long t = worker[i].ops/runtime.tv_sec;
203 update_stats(&throughput_stats, t);
204 if (!silent) {
205 if (nfutexes == 1)
206 printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
207 worker[i].tid, &worker[i].futex[0], t);
208 else
209 printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
210 worker[i].tid, &worker[i].futex[0],
211 &worker[i].futex[nfutexes-1], t);
212 }
213
214 free(worker[i].futex);
215 }
216
217 print_summary();
218
219 free(worker);
220 return ret;
221errmem:
222 err(EXIT_FAILURE, "calloc");
223}