2 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
4 * futex-hash: Stress the hell out of the Linux kernel futex uaddr hashing.
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.
12 #include "../util/util.h"
13 #include "../util/stat.h"
14 #include "../util/parse-options.h"
15 #include "../util/header.h"
24 static unsigned int nthreads
= 0;
25 static unsigned int nsecs
= 10;
26 /* amount of futexes per thread */
27 static unsigned int nfutexes
= 1024;
28 static bool fshared
= false, done
= false, silent
= false;
29 static int futex_flag
= 0;
31 struct timeval start
, end
, runtime
;
32 static pthread_mutex_t thread_lock
;
33 static unsigned int threads_starting
;
34 static struct stats throughput_stats
;
35 static pthread_cond_t thread_parent
, thread_worker
;
44 static const struct option options
[] = {
45 OPT_UINTEGER('t', "threads", &nthreads
, "Specify amount of threads"),
46 OPT_UINTEGER('r', "runtime", &nsecs
, "Specify runtime (in seconds)"),
47 OPT_UINTEGER('f', "futexes", &nfutexes
, "Specify amount of futexes per threads"),
48 OPT_BOOLEAN( 's', "silent", &silent
, "Silent mode: do not display data/details"),
49 OPT_BOOLEAN( 'S', "shared", &fshared
, "Use shared futexes instead of private ones"),
53 static const char * const bench_futex_hash_usage
[] = {
54 "perf bench futex hash <options>",
58 static void *workerfn(void *arg
)
62 struct worker
*w
= (struct worker
*) arg
;
64 pthread_mutex_lock(&thread_lock
);
66 if (!threads_starting
)
67 pthread_cond_signal(&thread_parent
);
68 pthread_cond_wait(&thread_worker
, &thread_lock
);
69 pthread_mutex_unlock(&thread_lock
);
72 for (i
= 0; i
< nfutexes
; i
++, w
->ops
++) {
74 * We want the futex calls to fail in order to stress
75 * the hashing of uaddr and not measure other steps,
76 * such as internal waitqueue handling, thus enlarging
77 * the critical region protected by hb->lock.
79 ret
= futex_wait(&w
->futex
[i
], 1234, NULL
, futex_flag
);
81 (!ret
|| errno
!= EAGAIN
|| errno
!= EWOULDBLOCK
))
82 warn("Non-expected futex return call");
89 static void toggle_done(int sig __maybe_unused
,
90 siginfo_t
*info __maybe_unused
,
91 void *uc __maybe_unused
)
93 /* inform all threads that we're done for the day */
95 gettimeofday(&end
, NULL
);
96 timersub(&end
, &start
, &runtime
);
99 static void print_summary(void)
101 unsigned long avg
= avg_stats(&throughput_stats
);
102 double stddev
= stddev_stats(&throughput_stats
);
104 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
105 !silent
? "\n" : "", avg
, rel_stddev_stats(stddev
, avg
),
106 (int) runtime
.tv_sec
);
109 int bench_futex_hash(int argc
, const char **argv
,
110 const char *prefix __maybe_unused
)
114 struct sigaction act
;
115 unsigned int i
, ncpus
;
116 pthread_attr_t thread_attr
;
117 struct worker
*worker
= NULL
;
119 argc
= parse_options(argc
, argv
, options
, bench_futex_hash_usage
, 0);
121 usage_with_options(bench_futex_hash_usage
, options
);
125 ncpus
= sysconf(_SC_NPROCESSORS_ONLN
);
127 sigfillset(&act
.sa_mask
);
128 act
.sa_sigaction
= toggle_done
;
129 sigaction(SIGINT
, &act
, NULL
);
131 if (!nthreads
) /* default to the number of CPUs */
134 worker
= calloc(nthreads
, sizeof(*worker
));
139 futex_flag
= FUTEX_PRIVATE_FLAG
;
141 printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
142 getpid(), nthreads
, nfutexes
, fshared
? "shared":"private", nsecs
);
144 init_stats(&throughput_stats
);
145 pthread_mutex_init(&thread_lock
, NULL
);
146 pthread_cond_init(&thread_parent
, NULL
);
147 pthread_cond_init(&thread_worker
, NULL
);
149 threads_starting
= nthreads
;
150 pthread_attr_init(&thread_attr
);
151 gettimeofday(&start
, NULL
);
152 for (i
= 0; i
< nthreads
; i
++) {
154 worker
[i
].futex
= calloc(nfutexes
, sizeof(*worker
[i
].futex
));
155 if (!worker
[i
].futex
)
159 CPU_SET(i
% ncpus
, &cpu
);
161 ret
= pthread_attr_setaffinity_np(&thread_attr
, sizeof(cpu_set_t
), &cpu
);
163 err(EXIT_FAILURE
, "pthread_attr_setaffinity_np");
165 ret
= pthread_create(&worker
[i
].thread
, &thread_attr
, workerfn
,
166 (void *)(struct worker
*) &worker
[i
]);
168 err(EXIT_FAILURE
, "pthread_create");
171 pthread_attr_destroy(&thread_attr
);
173 pthread_mutex_lock(&thread_lock
);
174 while (threads_starting
)
175 pthread_cond_wait(&thread_parent
, &thread_lock
);
176 pthread_cond_broadcast(&thread_worker
);
177 pthread_mutex_unlock(&thread_lock
);
180 toggle_done(0, NULL
, NULL
);
182 for (i
= 0; i
< nthreads
; i
++) {
183 ret
= pthread_join(worker
[i
].thread
, NULL
);
185 err(EXIT_FAILURE
, "pthread_join");
188 /* cleanup & report results */
189 pthread_cond_destroy(&thread_parent
);
190 pthread_cond_destroy(&thread_worker
);
191 pthread_mutex_destroy(&thread_lock
);
193 for (i
= 0; i
< nthreads
; i
++) {
194 unsigned long t
= worker
[i
].ops
/runtime
.tv_sec
;
195 update_stats(&throughput_stats
, t
);
198 printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
199 worker
[i
].tid
, &worker
[i
].futex
[0], t
);
201 printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
202 worker
[i
].tid
, &worker
[i
].futex
[0],
203 &worker
[i
].futex
[nfutexes
-1], t
);
206 free(worker
[i
].futex
);
214 err(EXIT_FAILURE
, "calloc");