1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d2f3f5d2SDavidlohr Bueso /*
3d2f3f5d2SDavidlohr Bueso * Copyright (C) 2015 Davidlohr Bueso.
4d2f3f5d2SDavidlohr Bueso */
5d2f3f5d2SDavidlohr Bueso
68a158589SArnaldo Carvalho de Melo /* For the CLR_() macros */
7a0f213e1SArnaldo Carvalho de Melo #include <string.h>
88a158589SArnaldo Carvalho de Melo #include <pthread.h>
98a158589SArnaldo Carvalho de Melo
109c304f6cSArnaldo Carvalho de Melo #include <signal.h>
11a64d3af5SIan Rogers #include "../util/mutex.h"
12d2f3f5d2SDavidlohr Bueso #include "../util/stat.h"
134b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1486695f59SArnaldo Carvalho de Melo #include <linux/compiler.h>
159c304f6cSArnaldo Carvalho de Melo #include <linux/kernel.h>
16d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
179c304f6cSArnaldo Carvalho de Melo #include <errno.h>
189c3516d1SJiri Olsa #include <perf/cpumap.h>
19d2f3f5d2SDavidlohr Bueso #include "bench.h"
20d2f3f5d2SDavidlohr Bueso #include "futex.h"
21d2f3f5d2SDavidlohr Bueso
22d2f3f5d2SDavidlohr Bueso #include <err.h>
23d2f3f5d2SDavidlohr Bueso #include <stdlib.h>
24d2f3f5d2SDavidlohr Bueso #include <sys/time.h>
259f9a3ffeSDavidlohr Bueso #include <sys/mman.h>
26d2f3f5d2SDavidlohr Bueso
27d2f3f5d2SDavidlohr Bueso struct worker {
28d2f3f5d2SDavidlohr Bueso int tid;
29d2f3f5d2SDavidlohr Bueso u_int32_t *futex;
30d2f3f5d2SDavidlohr Bueso pthread_t thread;
31d2f3f5d2SDavidlohr Bueso unsigned long ops;
32d2f3f5d2SDavidlohr Bueso };
33d2f3f5d2SDavidlohr Bueso
34d2f3f5d2SDavidlohr Bueso static u_int32_t global_futex = 0;
35d2f3f5d2SDavidlohr Bueso static struct worker *worker;
3609590463SDavidlohr Bueso static bool done = false;
37d2f3f5d2SDavidlohr Bueso static int futex_flag = 0;
38a64d3af5SIan Rogers static struct mutex thread_lock;
39d2f3f5d2SDavidlohr Bueso static unsigned int threads_starting;
40d2f3f5d2SDavidlohr Bueso static struct stats throughput_stats;
41a64d3af5SIan Rogers static struct cond thread_parent, thread_worker;
42d2f3f5d2SDavidlohr Bueso
4309590463SDavidlohr Bueso static struct bench_futex_parameters params = {
4409590463SDavidlohr Bueso .runtime = 10,
4509590463SDavidlohr Bueso };
4609590463SDavidlohr Bueso
47d2f3f5d2SDavidlohr Bueso static const struct option options[] = {
4809590463SDavidlohr Bueso OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"),
4909590463SDavidlohr Bueso OPT_UINTEGER('r', "runtime", ¶ms.runtime, "Specify runtime (in seconds)"),
5009590463SDavidlohr Bueso OPT_BOOLEAN( 'M', "multi", ¶ms.multi, "Use multiple futexes"),
5109590463SDavidlohr Bueso OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"),
5209590463SDavidlohr Bueso OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"),
539f9a3ffeSDavidlohr Bueso OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"),
54d2f3f5d2SDavidlohr Bueso OPT_END()
55d2f3f5d2SDavidlohr Bueso };
56d2f3f5d2SDavidlohr Bueso
57d2f3f5d2SDavidlohr Bueso static const char * const bench_futex_lock_pi_usage[] = {
589de3ffa1SDavidlohr Bueso "perf bench futex lock-pi <options>",
59d2f3f5d2SDavidlohr Bueso NULL
60d2f3f5d2SDavidlohr Bueso };
61d2f3f5d2SDavidlohr Bueso
print_summary(void)62d2f3f5d2SDavidlohr Bueso static void print_summary(void)
63d2f3f5d2SDavidlohr Bueso {
64d2f3f5d2SDavidlohr Bueso unsigned long avg = avg_stats(&throughput_stats);
65d2f3f5d2SDavidlohr Bueso double stddev = stddev_stats(&throughput_stats);
66d2f3f5d2SDavidlohr Bueso
67d2f3f5d2SDavidlohr Bueso printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
6809590463SDavidlohr Bueso !params.silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
69e4d9b04bSArnaldo Carvalho de Melo (int)bench__runtime.tv_sec);
70d2f3f5d2SDavidlohr Bueso }
71d2f3f5d2SDavidlohr Bueso
toggle_done(int sig __maybe_unused,siginfo_t * info __maybe_unused,void * uc __maybe_unused)72d2f3f5d2SDavidlohr Bueso static void toggle_done(int sig __maybe_unused,
73d2f3f5d2SDavidlohr Bueso siginfo_t *info __maybe_unused,
74d2f3f5d2SDavidlohr Bueso void *uc __maybe_unused)
75d2f3f5d2SDavidlohr Bueso {
76d2f3f5d2SDavidlohr Bueso /* inform all threads that we're done for the day */
77d2f3f5d2SDavidlohr Bueso done = true;
78e4d9b04bSArnaldo Carvalho de Melo gettimeofday(&bench__end, NULL);
79e4d9b04bSArnaldo Carvalho de Melo timersub(&bench__end, &bench__start, &bench__runtime);
80d2f3f5d2SDavidlohr Bueso }
81d2f3f5d2SDavidlohr Bueso
workerfn(void * arg)82d2f3f5d2SDavidlohr Bueso static void *workerfn(void *arg)
83d2f3f5d2SDavidlohr Bueso {
84d2f3f5d2SDavidlohr Bueso struct worker *w = (struct worker *) arg;
85e2e1680fSDavidlohr Bueso unsigned long ops = w->ops;
86d2f3f5d2SDavidlohr Bueso
87a64d3af5SIan Rogers mutex_lock(&thread_lock);
88d2f3f5d2SDavidlohr Bueso threads_starting--;
89d2f3f5d2SDavidlohr Bueso if (!threads_starting)
90a64d3af5SIan Rogers cond_signal(&thread_parent);
91a64d3af5SIan Rogers cond_wait(&thread_worker, &thread_lock);
92a64d3af5SIan Rogers mutex_unlock(&thread_lock);
93d2f3f5d2SDavidlohr Bueso
94d2f3f5d2SDavidlohr Bueso do {
95d2f3f5d2SDavidlohr Bueso int ret;
96d2f3f5d2SDavidlohr Bueso again:
9773b1794eSDavidlohr Bueso ret = futex_lock_pi(w->futex, NULL, futex_flag);
98d2f3f5d2SDavidlohr Bueso
99d2f3f5d2SDavidlohr Bueso if (ret) { /* handle lock acquisition */
10009590463SDavidlohr Bueso if (!params.silent)
101d2f3f5d2SDavidlohr Bueso warn("thread %d: Could not lock pi-lock for %p (%d)",
102d2f3f5d2SDavidlohr Bueso w->tid, w->futex, ret);
103d2f3f5d2SDavidlohr Bueso if (done)
104d2f3f5d2SDavidlohr Bueso break;
105d2f3f5d2SDavidlohr Bueso
106d2f3f5d2SDavidlohr Bueso goto again;
107d2f3f5d2SDavidlohr Bueso }
108d2f3f5d2SDavidlohr Bueso
109d2f3f5d2SDavidlohr Bueso usleep(1);
110d2f3f5d2SDavidlohr Bueso ret = futex_unlock_pi(w->futex, futex_flag);
11109590463SDavidlohr Bueso if (ret && !params.silent)
112d2f3f5d2SDavidlohr Bueso warn("thread %d: Could not unlock pi-lock for %p (%d)",
113d2f3f5d2SDavidlohr Bueso w->tid, w->futex, ret);
114e2e1680fSDavidlohr Bueso ops++; /* account for thread's share of work */
115d2f3f5d2SDavidlohr Bueso } while (!done);
116d2f3f5d2SDavidlohr Bueso
117e2e1680fSDavidlohr Bueso w->ops = ops;
118d2f3f5d2SDavidlohr Bueso return NULL;
119d2f3f5d2SDavidlohr Bueso }
120d2f3f5d2SDavidlohr Bueso
create_threads(struct worker * w,struct perf_cpu_map * cpu)121*8351498dSIan Rogers static void create_threads(struct worker *w, struct perf_cpu_map *cpu)
122d2f3f5d2SDavidlohr Bueso {
123c9c2a427SAthira Rajeev cpu_set_t *cpuset;
124d2f3f5d2SDavidlohr Bueso unsigned int i;
125c9c2a427SAthira Rajeev int nrcpus = perf_cpu_map__nr(cpu);
126c9c2a427SAthira Rajeev size_t size;
127d2f3f5d2SDavidlohr Bueso
12809590463SDavidlohr Bueso threads_starting = params.nthreads;
129d2f3f5d2SDavidlohr Bueso
130c9c2a427SAthira Rajeev cpuset = CPU_ALLOC(nrcpus);
131c9c2a427SAthira Rajeev BUG_ON(!cpuset);
132c9c2a427SAthira Rajeev size = CPU_ALLOC_SIZE(nrcpus);
133c9c2a427SAthira Rajeev
13409590463SDavidlohr Bueso for (i = 0; i < params.nthreads; i++) {
135*8351498dSIan Rogers pthread_attr_t thread_attr;
136*8351498dSIan Rogers
137*8351498dSIan Rogers pthread_attr_init(&thread_attr);
138d2f3f5d2SDavidlohr Bueso worker[i].tid = i;
139d2f3f5d2SDavidlohr Bueso
14009590463SDavidlohr Bueso if (params.multi) {
141d2f3f5d2SDavidlohr Bueso worker[i].futex = calloc(1, sizeof(u_int32_t));
142d2f3f5d2SDavidlohr Bueso if (!worker[i].futex)
143d2f3f5d2SDavidlohr Bueso err(EXIT_FAILURE, "calloc");
144d2f3f5d2SDavidlohr Bueso } else
145d2f3f5d2SDavidlohr Bueso worker[i].futex = &global_futex;
146d2f3f5d2SDavidlohr Bueso
147c9c2a427SAthira Rajeev CPU_ZERO_S(size, cpuset);
148c9c2a427SAthira Rajeev CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
149d2f3f5d2SDavidlohr Bueso
150c9c2a427SAthira Rajeev if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
151c9c2a427SAthira Rajeev CPU_FREE(cpuset);
152d2f3f5d2SDavidlohr Bueso err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
153c9c2a427SAthira Rajeev }
154d2f3f5d2SDavidlohr Bueso
155c9c2a427SAthira Rajeev if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i])) {
156c9c2a427SAthira Rajeev CPU_FREE(cpuset);
157d2f3f5d2SDavidlohr Bueso err(EXIT_FAILURE, "pthread_create");
158d2f3f5d2SDavidlohr Bueso }
159*8351498dSIan Rogers pthread_attr_destroy(&thread_attr);
160d2f3f5d2SDavidlohr Bueso }
161c9c2a427SAthira Rajeev CPU_FREE(cpuset);
162c9c2a427SAthira Rajeev }
163d2f3f5d2SDavidlohr Bueso
bench_futex_lock_pi(int argc,const char ** argv)164b0ad8ea6SArnaldo Carvalho de Melo int bench_futex_lock_pi(int argc, const char **argv)
165d2f3f5d2SDavidlohr Bueso {
166d2f3f5d2SDavidlohr Bueso int ret = 0;
167d2f3f5d2SDavidlohr Bueso unsigned int i;
168d2f3f5d2SDavidlohr Bueso struct sigaction act;
169f854839bSJiri Olsa struct perf_cpu_map *cpu;
170d2f3f5d2SDavidlohr Bueso
171d2f3f5d2SDavidlohr Bueso argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0);
172d2f3f5d2SDavidlohr Bueso if (argc)
173d2f3f5d2SDavidlohr Bueso goto err;
174d2f3f5d2SDavidlohr Bueso
1759c3516d1SJiri Olsa cpu = perf_cpu_map__new(NULL);
1763b2323c2SDavidlohr Bueso if (!cpu)
1773b2323c2SDavidlohr Bueso err(EXIT_FAILURE, "calloc");
178d2f3f5d2SDavidlohr Bueso
1797b919a53STommi Rantala memset(&act, 0, sizeof(act));
180d2f3f5d2SDavidlohr Bueso sigfillset(&act.sa_mask);
181d2f3f5d2SDavidlohr Bueso act.sa_sigaction = toggle_done;
182d2f3f5d2SDavidlohr Bueso sigaction(SIGINT, &act, NULL);
183d2f3f5d2SDavidlohr Bueso
1849f9a3ffeSDavidlohr Bueso if (params.mlockall) {
1859f9a3ffeSDavidlohr Bueso if (mlockall(MCL_CURRENT | MCL_FUTURE))
1869f9a3ffeSDavidlohr Bueso err(EXIT_FAILURE, "mlockall");
1879f9a3ffeSDavidlohr Bueso }
1889f9a3ffeSDavidlohr Bueso
18909590463SDavidlohr Bueso if (!params.nthreads)
19044028699SIan Rogers params.nthreads = perf_cpu_map__nr(cpu);
191d2f3f5d2SDavidlohr Bueso
19209590463SDavidlohr Bueso worker = calloc(params.nthreads, sizeof(*worker));
193d2f3f5d2SDavidlohr Bueso if (!worker)
194d2f3f5d2SDavidlohr Bueso err(EXIT_FAILURE, "calloc");
195d2f3f5d2SDavidlohr Bueso
19609590463SDavidlohr Bueso if (!params.fshared)
197d2f3f5d2SDavidlohr Bueso futex_flag = FUTEX_PRIVATE_FLAG;
198d2f3f5d2SDavidlohr Bueso
199d2f3f5d2SDavidlohr Bueso printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
20009590463SDavidlohr Bueso getpid(), params.nthreads, params.runtime);
201d2f3f5d2SDavidlohr Bueso
202d2f3f5d2SDavidlohr Bueso init_stats(&throughput_stats);
203a64d3af5SIan Rogers mutex_init(&thread_lock);
204a64d3af5SIan Rogers cond_init(&thread_parent);
205a64d3af5SIan Rogers cond_init(&thread_worker);
206d2f3f5d2SDavidlohr Bueso
20709590463SDavidlohr Bueso threads_starting = params.nthreads;
208e4d9b04bSArnaldo Carvalho de Melo gettimeofday(&bench__start, NULL);
209d2f3f5d2SDavidlohr Bueso
210*8351498dSIan Rogers create_threads(worker, cpu);
211d2f3f5d2SDavidlohr Bueso
212a64d3af5SIan Rogers mutex_lock(&thread_lock);
213d2f3f5d2SDavidlohr Bueso while (threads_starting)
214a64d3af5SIan Rogers cond_wait(&thread_parent, &thread_lock);
215a64d3af5SIan Rogers cond_broadcast(&thread_worker);
216a64d3af5SIan Rogers mutex_unlock(&thread_lock);
217d2f3f5d2SDavidlohr Bueso
21809590463SDavidlohr Bueso sleep(params.runtime);
219d2f3f5d2SDavidlohr Bueso toggle_done(0, NULL, NULL);
220d2f3f5d2SDavidlohr Bueso
22109590463SDavidlohr Bueso for (i = 0; i < params.nthreads; i++) {
222d2f3f5d2SDavidlohr Bueso ret = pthread_join(worker[i].thread, NULL);
223d2f3f5d2SDavidlohr Bueso if (ret)
224d2f3f5d2SDavidlohr Bueso err(EXIT_FAILURE, "pthread_join");
225d2f3f5d2SDavidlohr Bueso }
226d2f3f5d2SDavidlohr Bueso
227d2f3f5d2SDavidlohr Bueso /* cleanup & report results */
228a64d3af5SIan Rogers cond_destroy(&thread_parent);
229a64d3af5SIan Rogers cond_destroy(&thread_worker);
230a64d3af5SIan Rogers mutex_destroy(&thread_lock);
231d2f3f5d2SDavidlohr Bueso
23209590463SDavidlohr Bueso for (i = 0; i < params.nthreads; i++) {
23341e7c32bSTommi Rantala unsigned long t = bench__runtime.tv_sec > 0 ?
23441e7c32bSTommi Rantala worker[i].ops / bench__runtime.tv_sec : 0;
235d2f3f5d2SDavidlohr Bueso
236d2f3f5d2SDavidlohr Bueso update_stats(&throughput_stats, t);
23709590463SDavidlohr Bueso if (!params.silent)
238d2f3f5d2SDavidlohr Bueso printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
239d2f3f5d2SDavidlohr Bueso worker[i].tid, worker[i].futex, t);
240d2f3f5d2SDavidlohr Bueso
24109590463SDavidlohr Bueso if (params.multi)
242d8f9da24SArnaldo Carvalho de Melo zfree(&worker[i].futex);
243d2f3f5d2SDavidlohr Bueso }
244d2f3f5d2SDavidlohr Bueso
245d2f3f5d2SDavidlohr Bueso print_summary();
246d2f3f5d2SDavidlohr Bueso
247d2f3f5d2SDavidlohr Bueso free(worker);
24888e48238SSohaib Mohamed perf_cpu_map__put(cpu);
249d2f3f5d2SDavidlohr Bueso return ret;
250d2f3f5d2SDavidlohr Bueso err:
251d2f3f5d2SDavidlohr Bueso usage_with_options(bench_futex_lock_pi_usage, options);
252d2f3f5d2SDavidlohr Bueso exit(EXIT_FAILURE);
253d2f3f5d2SDavidlohr Bueso }
254