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>
31 static u_int32_t global_futex
= 0;
32 static struct worker
*worker
;
33 static unsigned int nsecs
= 10;
34 static bool silent
= false, multi
= false;
35 static bool done
= false, fshared
= false;
36 static unsigned int nthreads
= 0;
37 static int futex_flag
= 0;
38 struct timeval start
, end
, runtime
;
39 static pthread_mutex_t thread_lock
;
40 static unsigned int threads_starting
;
41 static struct stats throughput_stats
;
42 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_BOOLEAN( 'M', "multi", &multi
, "Use multiple futexes"),
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_lock_pi_usage
[] = {
54 "perf bench futex lock-pi <options>",
58 static void print_summary(void)
60 unsigned long avg
= avg_stats(&throughput_stats
);
61 double stddev
= stddev_stats(&throughput_stats
);
63 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
64 !silent
? "\n" : "", avg
, rel_stddev_stats(stddev
, avg
),
65 (int) runtime
.tv_sec
);
68 static void toggle_done(int sig __maybe_unused
,
69 siginfo_t
*info __maybe_unused
,
70 void *uc __maybe_unused
)
72 /* inform all threads that we're done for the day */
74 gettimeofday(&end
, NULL
);
75 timersub(&end
, &start
, &runtime
);
78 static void *workerfn(void *arg
)
80 struct worker
*w
= (struct worker
*) arg
;
81 unsigned long ops
= w
->ops
;
83 pthread_mutex_lock(&thread_lock
);
85 if (!threads_starting
)
86 pthread_cond_signal(&thread_parent
);
87 pthread_cond_wait(&thread_worker
, &thread_lock
);
88 pthread_mutex_unlock(&thread_lock
);
93 ret
= futex_lock_pi(w
->futex
, NULL
, futex_flag
);
95 if (ret
) { /* handle lock acquisition */
97 warn("thread %d: Could not lock pi-lock for %p (%d)",
98 w
->tid
, w
->futex
, ret
);
106 ret
= futex_unlock_pi(w
->futex
, futex_flag
);
108 warn("thread %d: Could not unlock pi-lock for %p (%d)",
109 w
->tid
, w
->futex
, ret
);
110 ops
++; /* account for thread's share of work */
117 static void create_threads(struct worker
*w
, pthread_attr_t thread_attr
,
123 threads_starting
= nthreads
;
125 for (i
= 0; i
< nthreads
; i
++) {
129 worker
[i
].futex
= calloc(1, sizeof(u_int32_t
));
130 if (!worker
[i
].futex
)
131 err(EXIT_FAILURE
, "calloc");
133 worker
[i
].futex
= &global_futex
;
136 CPU_SET(cpu
->map
[i
% cpu
->nr
], &cpuset
);
138 if (pthread_attr_setaffinity_np(&thread_attr
, sizeof(cpu_set_t
), &cpuset
))
139 err(EXIT_FAILURE
, "pthread_attr_setaffinity_np");
141 if (pthread_create(&w
[i
].thread
, &thread_attr
, workerfn
, &worker
[i
]))
142 err(EXIT_FAILURE
, "pthread_create");
146 int bench_futex_lock_pi(int argc
, const char **argv
)
150 struct sigaction act
;
151 pthread_attr_t thread_attr
;
154 argc
= parse_options(argc
, argv
, options
, bench_futex_lock_pi_usage
, 0);
158 cpu
= cpu_map__new(NULL
);
160 err(EXIT_FAILURE
, "calloc");
162 sigfillset(&act
.sa_mask
);
163 act
.sa_sigaction
= toggle_done
;
164 sigaction(SIGINT
, &act
, NULL
);
169 worker
= calloc(nthreads
, sizeof(*worker
));
171 err(EXIT_FAILURE
, "calloc");
174 futex_flag
= FUTEX_PRIVATE_FLAG
;
176 printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
177 getpid(), nthreads
, nsecs
);
179 init_stats(&throughput_stats
);
180 pthread_mutex_init(&thread_lock
, NULL
);
181 pthread_cond_init(&thread_parent
, NULL
);
182 pthread_cond_init(&thread_worker
, NULL
);
184 threads_starting
= nthreads
;
185 pthread_attr_init(&thread_attr
);
186 gettimeofday(&start
, NULL
);
188 create_threads(worker
, thread_attr
, cpu
);
189 pthread_attr_destroy(&thread_attr
);
191 pthread_mutex_lock(&thread_lock
);
192 while (threads_starting
)
193 pthread_cond_wait(&thread_parent
, &thread_lock
);
194 pthread_cond_broadcast(&thread_worker
);
195 pthread_mutex_unlock(&thread_lock
);
198 toggle_done(0, NULL
, NULL
);
200 for (i
= 0; i
< nthreads
; i
++) {
201 ret
= pthread_join(worker
[i
].thread
, NULL
);
203 err(EXIT_FAILURE
, "pthread_join");
206 /* cleanup & report results */
207 pthread_cond_destroy(&thread_parent
);
208 pthread_cond_destroy(&thread_worker
);
209 pthread_mutex_destroy(&thread_lock
);
211 for (i
= 0; i
< nthreads
; i
++) {
212 unsigned long t
= worker
[i
].ops
/runtime
.tv_sec
;
214 update_stats(&throughput_stats
, t
);
216 printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
217 worker
[i
].tid
, worker
[i
].futex
, t
);
220 free(worker
[i
].futex
);
228 usage_with_options(bench_futex_lock_pi_usage
, options
);