1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015 Davidlohr Bueso.
6 /* For the CLR_() macros */
11 #include "../util/stat.h"
12 #include <subcmd/parse-options.h>
13 #include <linux/compiler.h>
14 #include <linux/kernel.h>
15 #include <linux/zalloc.h>
17 #include <perf/cpumap.h>
33 static u_int32_t global_futex = 0;
34 static struct worker *worker;
35 static bool done = false;
36 static int futex_flag = 0;
37 static pthread_mutex_t thread_lock;
38 static unsigned int threads_starting;
39 static struct stats throughput_stats;
40 static pthread_cond_t thread_parent, thread_worker;
42 static struct bench_futex_parameters params = {
46 static const struct option options[] = {
47 OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"),
48 OPT_UINTEGER('r', "runtime", ¶ms.runtime, "Specify runtime (in seconds)"),
49 OPT_BOOLEAN( 'M', "multi", ¶ms.multi, "Use multiple futexes"),
50 OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"),
51 OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"),
52 OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"),
56 static const char * const bench_futex_lock_pi_usage[] = {
57 "perf bench futex lock-pi <options>",
61 static void print_summary(void)
63 unsigned long avg = avg_stats(&throughput_stats);
64 double stddev = stddev_stats(&throughput_stats);
66 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
67 !params.silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
68 (int)bench__runtime.tv_sec);
71 static void toggle_done(int sig __maybe_unused,
72 siginfo_t *info __maybe_unused,
73 void *uc __maybe_unused)
75 /* inform all threads that we're done for the day */
77 gettimeofday(&bench__end, NULL);
78 timersub(&bench__end, &bench__start, &bench__runtime);
81 static void *workerfn(void *arg)
83 struct worker *w = (struct worker *) arg;
84 unsigned long ops = w->ops;
86 pthread_mutex_lock(&thread_lock);
88 if (!threads_starting)
89 pthread_cond_signal(&thread_parent);
90 pthread_cond_wait(&thread_worker, &thread_lock);
91 pthread_mutex_unlock(&thread_lock);
96 ret = futex_lock_pi(w->futex, NULL, futex_flag);
98 if (ret) { /* handle lock acquisition */
100 warn("thread %d: Could not lock pi-lock for %p (%d)",
101 w->tid, w->futex, ret);
109 ret = futex_unlock_pi(w->futex, futex_flag);
110 if (ret && !params.silent)
111 warn("thread %d: Could not unlock pi-lock for %p (%d)",
112 w->tid, w->futex, ret);
113 ops++; /* account for thread's share of work */
120 static void create_threads(struct worker *w, pthread_attr_t thread_attr,
121 struct perf_cpu_map *cpu)
126 threads_starting = params.nthreads;
128 for (i = 0; i < params.nthreads; i++) {
132 worker[i].futex = calloc(1, sizeof(u_int32_t));
133 if (!worker[i].futex)
134 err(EXIT_FAILURE, "calloc");
136 worker[i].futex = &global_futex;
139 CPU_SET(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, &cpuset);
141 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
142 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
144 if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i]))
145 err(EXIT_FAILURE, "pthread_create");
149 int bench_futex_lock_pi(int argc, const char **argv)
153 struct sigaction act;
154 pthread_attr_t thread_attr;
155 struct perf_cpu_map *cpu;
157 argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0);
161 cpu = perf_cpu_map__new(NULL);
163 err(EXIT_FAILURE, "calloc");
165 memset(&act, 0, sizeof(act));
166 sigfillset(&act.sa_mask);
167 act.sa_sigaction = toggle_done;
168 sigaction(SIGINT, &act, NULL);
170 if (params.mlockall) {
171 if (mlockall(MCL_CURRENT | MCL_FUTURE))
172 err(EXIT_FAILURE, "mlockall");
175 if (!params.nthreads)
176 params.nthreads = cpu->nr;
178 worker = calloc(params.nthreads, sizeof(*worker));
180 err(EXIT_FAILURE, "calloc");
183 futex_flag = FUTEX_PRIVATE_FLAG;
185 printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
186 getpid(), params.nthreads, params.runtime);
188 init_stats(&throughput_stats);
189 pthread_mutex_init(&thread_lock, NULL);
190 pthread_cond_init(&thread_parent, NULL);
191 pthread_cond_init(&thread_worker, NULL);
193 threads_starting = params.nthreads;
194 pthread_attr_init(&thread_attr);
195 gettimeofday(&bench__start, NULL);
197 create_threads(worker, thread_attr, cpu);
198 pthread_attr_destroy(&thread_attr);
200 pthread_mutex_lock(&thread_lock);
201 while (threads_starting)
202 pthread_cond_wait(&thread_parent, &thread_lock);
203 pthread_cond_broadcast(&thread_worker);
204 pthread_mutex_unlock(&thread_lock);
206 sleep(params.runtime);
207 toggle_done(0, NULL, NULL);
209 for (i = 0; i < params.nthreads; i++) {
210 ret = pthread_join(worker[i].thread, NULL);
212 err(EXIT_FAILURE, "pthread_join");
215 /* cleanup & report results */
216 pthread_cond_destroy(&thread_parent);
217 pthread_cond_destroy(&thread_worker);
218 pthread_mutex_destroy(&thread_lock);
220 for (i = 0; i < params.nthreads; i++) {
221 unsigned long t = bench__runtime.tv_sec > 0 ?
222 worker[i].ops / bench__runtime.tv_sec : 0;
224 update_stats(&throughput_stats, t);
226 printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
227 worker[i].tid, worker[i].futex, t);
230 zfree(&worker[i].futex);
236 perf_cpu_map__put(cpu);
239 usage_with_options(bench_futex_lock_pi_usage, options);