Merge branch 'core-objtool-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / tools / perf / bench / futex-lock-pi.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
d2f3f5d2
DB
2/*
3 * Copyright (C) 2015 Davidlohr Bueso.
4 */
5
8a158589 6/* For the CLR_() macros */
a0f213e1 7#include <string.h>
8a158589
ACM
8#include <pthread.h>
9
9c304f6c 10#include <signal.h>
d2f3f5d2 11#include "../util/stat.h"
4b6ab94e 12#include <subcmd/parse-options.h>
86695f59 13#include <linux/compiler.h>
9c304f6c 14#include <linux/kernel.h>
d8f9da24 15#include <linux/zalloc.h>
9c304f6c 16#include <errno.h>
87ffb6c6 17#include <internal/cpumap.h>
9c3516d1 18#include <perf/cpumap.h>
d2f3f5d2
DB
19#include "bench.h"
20#include "futex.h"
21
22#include <err.h>
23#include <stdlib.h>
24#include <sys/time.h>
d2f3f5d2
DB
25
26struct worker {
27 int tid;
28 u_int32_t *futex;
29 pthread_t thread;
30 unsigned long ops;
31};
32
33static u_int32_t global_futex = 0;
34static struct worker *worker;
35static unsigned int nsecs = 10;
36static bool silent = false, multi = false;
37static bool done = false, fshared = false;
3b2323c2 38static unsigned int nthreads = 0;
d2f3f5d2 39static int futex_flag = 0;
d2f3f5d2
DB
40static pthread_mutex_t thread_lock;
41static unsigned int threads_starting;
42static struct stats throughput_stats;
43static pthread_cond_t thread_parent, thread_worker;
44
45static const struct option options[] = {
46 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
47 OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
48 OPT_BOOLEAN( 'M', "multi", &multi, "Use multiple futexes"),
49 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
50 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
51 OPT_END()
52};
53
54static const char * const bench_futex_lock_pi_usage[] = {
9de3ffa1 55 "perf bench futex lock-pi <options>",
d2f3f5d2
DB
56 NULL
57};
58
59static void print_summary(void)
60{
61 unsigned long avg = avg_stats(&throughput_stats);
62 double stddev = stddev_stats(&throughput_stats);
63
64 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
65 !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
e4d9b04b 66 (int)bench__runtime.tv_sec);
d2f3f5d2
DB
67}
68
69static void toggle_done(int sig __maybe_unused,
70 siginfo_t *info __maybe_unused,
71 void *uc __maybe_unused)
72{
73 /* inform all threads that we're done for the day */
74 done = true;
e4d9b04b
ACM
75 gettimeofday(&bench__end, NULL);
76 timersub(&bench__end, &bench__start, &bench__runtime);
d2f3f5d2
DB
77}
78
79static void *workerfn(void *arg)
80{
81 struct worker *w = (struct worker *) arg;
e2e1680f 82 unsigned long ops = w->ops;
d2f3f5d2
DB
83
84 pthread_mutex_lock(&thread_lock);
85 threads_starting--;
86 if (!threads_starting)
87 pthread_cond_signal(&thread_parent);
88 pthread_cond_wait(&thread_worker, &thread_lock);
89 pthread_mutex_unlock(&thread_lock);
90
91 do {
92 int ret;
93 again:
73b1794e 94 ret = futex_lock_pi(w->futex, NULL, futex_flag);
d2f3f5d2
DB
95
96 if (ret) { /* handle lock acquisition */
97 if (!silent)
98 warn("thread %d: Could not lock pi-lock for %p (%d)",
99 w->tid, w->futex, ret);
100 if (done)
101 break;
102
103 goto again;
104 }
105
106 usleep(1);
107 ret = futex_unlock_pi(w->futex, futex_flag);
108 if (ret && !silent)
109 warn("thread %d: Could not unlock pi-lock for %p (%d)",
110 w->tid, w->futex, ret);
e2e1680f 111 ops++; /* account for thread's share of work */
d2f3f5d2
DB
112 } while (!done);
113
e2e1680f 114 w->ops = ops;
d2f3f5d2
DB
115 return NULL;
116}
117
3b2323c2 118static void create_threads(struct worker *w, pthread_attr_t thread_attr,
f854839b 119 struct perf_cpu_map *cpu)
d2f3f5d2 120{
3b2323c2 121 cpu_set_t cpuset;
d2f3f5d2
DB
122 unsigned int i;
123
124 threads_starting = nthreads;
125
126 for (i = 0; i < nthreads; i++) {
127 worker[i].tid = i;
128
129 if (multi) {
130 worker[i].futex = calloc(1, sizeof(u_int32_t));
131 if (!worker[i].futex)
132 err(EXIT_FAILURE, "calloc");
133 } else
134 worker[i].futex = &global_futex;
135
3b2323c2
DB
136 CPU_ZERO(&cpuset);
137 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
d2f3f5d2 138
3b2323c2 139 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
d2f3f5d2
DB
140 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
141
142 if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i]))
143 err(EXIT_FAILURE, "pthread_create");
144 }
145}
146
b0ad8ea6 147int bench_futex_lock_pi(int argc, const char **argv)
d2f3f5d2
DB
148{
149 int ret = 0;
150 unsigned int i;
151 struct sigaction act;
152 pthread_attr_t thread_attr;
f854839b 153 struct perf_cpu_map *cpu;
d2f3f5d2
DB
154
155 argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0);
156 if (argc)
157 goto err;
158
9c3516d1 159 cpu = perf_cpu_map__new(NULL);
3b2323c2
DB
160 if (!cpu)
161 err(EXIT_FAILURE, "calloc");
d2f3f5d2 162
7b919a53 163 memset(&act, 0, sizeof(act));
d2f3f5d2
DB
164 sigfillset(&act.sa_mask);
165 act.sa_sigaction = toggle_done;
166 sigaction(SIGINT, &act, NULL);
167
168 if (!nthreads)
3b2323c2 169 nthreads = cpu->nr;
d2f3f5d2
DB
170
171 worker = calloc(nthreads, sizeof(*worker));
172 if (!worker)
173 err(EXIT_FAILURE, "calloc");
174
175 if (!fshared)
176 futex_flag = FUTEX_PRIVATE_FLAG;
177
178 printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
179 getpid(), nthreads, nsecs);
180
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);
185
186 threads_starting = nthreads;
187 pthread_attr_init(&thread_attr);
e4d9b04b 188 gettimeofday(&bench__start, NULL);
d2f3f5d2 189
3b2323c2 190 create_threads(worker, thread_attr, cpu);
d2f3f5d2
DB
191 pthread_attr_destroy(&thread_attr);
192
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);
198
199 sleep(nsecs);
200 toggle_done(0, NULL, NULL);
201
202 for (i = 0; i < nthreads; i++) {
203 ret = pthread_join(worker[i].thread, NULL);
204 if (ret)
205 err(EXIT_FAILURE, "pthread_join");
206 }
207
208 /* cleanup & report results */
209 pthread_cond_destroy(&thread_parent);
210 pthread_cond_destroy(&thread_worker);
211 pthread_mutex_destroy(&thread_lock);
212
213 for (i = 0; i < nthreads; i++) {
e4d9b04b 214 unsigned long t = worker[i].ops / bench__runtime.tv_sec;
d2f3f5d2
DB
215
216 update_stats(&throughput_stats, t);
217 if (!silent)
218 printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
219 worker[i].tid, worker[i].futex, t);
220
221 if (multi)
d8f9da24 222 zfree(&worker[i].futex);
d2f3f5d2
DB
223 }
224
225 print_summary();
226
227 free(worker);
228 return ret;
229err:
230 usage_with_options(bench_futex_lock_pi_usage, options);
231 exit(EXIT_FAILURE);
232}