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 <internal/cpumap.h>
18 #include <perf/cpumap.h>
33 static u_int32_t global_futex
= 0;
34 static struct worker
*worker
;
35 static unsigned int nsecs
= 10;
36 static bool silent
= false, multi
= false;
37 static bool done
= false, fshared
= false;
38 static unsigned int nthreads
= 0;
39 static int futex_flag
= 0;
40 struct timeval start
, end
, runtime
;
41 static pthread_mutex_t thread_lock
;
42 static unsigned int threads_starting
;
43 static struct stats throughput_stats
;
44 static pthread_cond_t thread_parent
, thread_worker
;
46 static const struct option options
[] = {
47 OPT_UINTEGER('t', "threads", &nthreads
, "Specify amount of threads"),
48 OPT_UINTEGER('r', "runtime", &nsecs
, "Specify runtime (in seconds)"),
49 OPT_BOOLEAN( 'M', "multi", &multi
, "Use multiple futexes"),
50 OPT_BOOLEAN( 's', "silent", &silent
, "Silent mode: do not display data/details"),
51 OPT_BOOLEAN( 'S', "shared", &fshared
, "Use shared futexes instead of private ones"),
55 static const char * const bench_futex_lock_pi_usage
[] = {
56 "perf bench futex lock-pi <options>",
60 static void print_summary(void)
62 unsigned long avg
= avg_stats(&throughput_stats
);
63 double stddev
= stddev_stats(&throughput_stats
);
65 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
66 !silent
? "\n" : "", avg
, rel_stddev_stats(stddev
, avg
),
67 (int) runtime
.tv_sec
);
70 static void toggle_done(int sig __maybe_unused
,
71 siginfo_t
*info __maybe_unused
,
72 void *uc __maybe_unused
)
74 /* inform all threads that we're done for the day */
76 gettimeofday(&end
, NULL
);
77 timersub(&end
, &start
, &runtime
);
80 static void *workerfn(void *arg
)
82 struct worker
*w
= (struct worker
*) arg
;
83 unsigned long ops
= w
->ops
;
85 pthread_mutex_lock(&thread_lock
);
87 if (!threads_starting
)
88 pthread_cond_signal(&thread_parent
);
89 pthread_cond_wait(&thread_worker
, &thread_lock
);
90 pthread_mutex_unlock(&thread_lock
);
95 ret
= futex_lock_pi(w
->futex
, NULL
, futex_flag
);
97 if (ret
) { /* handle lock acquisition */
99 warn("thread %d: Could not lock pi-lock for %p (%d)",
100 w
->tid
, w
->futex
, ret
);
108 ret
= futex_unlock_pi(w
->futex
, futex_flag
);
110 warn("thread %d: Could not unlock pi-lock for %p (%d)",
111 w
->tid
, w
->futex
, ret
);
112 ops
++; /* account for thread's share of work */
119 static void create_threads(struct worker
*w
, pthread_attr_t thread_attr
,
120 struct perf_cpu_map
*cpu
)
125 threads_starting
= nthreads
;
127 for (i
= 0; i
< nthreads
; i
++) {
131 worker
[i
].futex
= calloc(1, sizeof(u_int32_t
));
132 if (!worker
[i
].futex
)
133 err(EXIT_FAILURE
, "calloc");
135 worker
[i
].futex
= &global_futex
;
138 CPU_SET(cpu
->map
[i
% cpu
->nr
], &cpuset
);
140 if (pthread_attr_setaffinity_np(&thread_attr
, sizeof(cpu_set_t
), &cpuset
))
141 err(EXIT_FAILURE
, "pthread_attr_setaffinity_np");
143 if (pthread_create(&w
[i
].thread
, &thread_attr
, workerfn
, &worker
[i
]))
144 err(EXIT_FAILURE
, "pthread_create");
148 int bench_futex_lock_pi(int argc
, const char **argv
)
152 struct sigaction act
;
153 pthread_attr_t thread_attr
;
154 struct perf_cpu_map
*cpu
;
156 argc
= parse_options(argc
, argv
, options
, bench_futex_lock_pi_usage
, 0);
160 cpu
= perf_cpu_map__new(NULL
);
162 err(EXIT_FAILURE
, "calloc");
164 sigfillset(&act
.sa_mask
);
165 act
.sa_sigaction
= toggle_done
;
166 sigaction(SIGINT
, &act
, NULL
);
171 worker
= calloc(nthreads
, sizeof(*worker
));
173 err(EXIT_FAILURE
, "calloc");
176 futex_flag
= FUTEX_PRIVATE_FLAG
;
178 printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
179 getpid(), nthreads
, nsecs
);
181 init_stats(&throughput_stats
);
182 pthread_mutex_init(&thread_lock
, NULL
);
183 pthread_cond_init(&thread_parent
, NULL
);
184 pthread_cond_init(&thread_worker
, NULL
);
186 threads_starting
= nthreads
;
187 pthread_attr_init(&thread_attr
);
188 gettimeofday(&start
, NULL
);
190 create_threads(worker
, thread_attr
, cpu
);
191 pthread_attr_destroy(&thread_attr
);
193 pthread_mutex_lock(&thread_lock
);
194 while (threads_starting
)
195 pthread_cond_wait(&thread_parent
, &thread_lock
);
196 pthread_cond_broadcast(&thread_worker
);
197 pthread_mutex_unlock(&thread_lock
);
200 toggle_done(0, NULL
, NULL
);
202 for (i
= 0; i
< nthreads
; i
++) {
203 ret
= pthread_join(worker
[i
].thread
, NULL
);
205 err(EXIT_FAILURE
, "pthread_join");
208 /* cleanup & report results */
209 pthread_cond_destroy(&thread_parent
);
210 pthread_cond_destroy(&thread_worker
);
211 pthread_mutex_destroy(&thread_lock
);
213 for (i
= 0; i
< nthreads
; i
++) {
214 unsigned long t
= worker
[i
].ops
/runtime
.tv_sec
;
216 update_stats(&throughput_stats
, t
);
218 printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
219 worker
[i
].tid
, worker
[i
].futex
, t
);
222 zfree(&worker
[i
].futex
);
230 usage_with_options(bench_futex_lock_pi_usage
, options
);