blob: a512a320df74b3093e139d5cf9dd3de9b9424994 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -07002/*
3 * Copyright (C) 2015 Davidlohr Bueso.
4 */
5
Arnaldo Carvalho de Melo8a158582016-07-06 12:14:56 -03006/* For the CLR_() macros */
Arnaldo Carvalho de Meloa0f213e142017-03-02 15:44:19 -03007#include <string.h>
Arnaldo Carvalho de Melo8a158582016-07-06 12:14:56 -03008#include <pthread.h>
9
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030010#include <signal.h>
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070011#include "../util/stat.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060012#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo86695f52016-07-11 10:05:52 -030013#include <linux/compiler.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030014#include <linux/kernel.h>
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -030015#include <linux/zalloc.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030016#include <errno.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +020017#include <perf/cpumap.h>
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070018#include "bench.h"
19#include "futex.h"
20
21#include <err.h>
22#include <stdlib.h>
23#include <sys/time.h>
Davidlohr Bueso9f9a3ff2021-08-08 21:32:58 -070024#include <sys/mman.h>
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070025
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;
Davidlohr Bueso09590462021-08-08 21:32:55 -070035static bool done = false;
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070036static int futex_flag = 0;
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070037static pthread_mutex_t thread_lock;
38static unsigned int threads_starting;
39static struct stats throughput_stats;
40static pthread_cond_t thread_parent, thread_worker;
41
Davidlohr Bueso09590462021-08-08 21:32:55 -070042static struct bench_futex_parameters params = {
43 .runtime = 10,
44};
45
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070046static const struct option options[] = {
Davidlohr Bueso09590462021-08-08 21:32:55 -070047 OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
48 OPT_UINTEGER('r', "runtime", &params.runtime, "Specify runtime (in seconds)"),
49 OPT_BOOLEAN( 'M', "multi", &params.multi, "Use multiple futexes"),
50 OPT_BOOLEAN( 's', "silent", &params.silent, "Silent mode: do not display data/details"),
51 OPT_BOOLEAN( 'S', "shared", &params.fshared, "Use shared futexes instead of private ones"),
Davidlohr Bueso9f9a3ff2021-08-08 21:32:58 -070052 OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070053 OPT_END()
54};
55
56static const char * const bench_futex_lock_pi_usage[] = {
Davidlohr Bueso9de3ffa2016-12-15 11:36:24 -080057 "perf bench futex lock-pi <options>",
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070058 NULL
59};
60
61static void print_summary(void)
62{
63 unsigned long avg = avg_stats(&throughput_stats);
64 double stddev = stddev_stats(&throughput_stats);
65
66 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
Davidlohr Bueso09590462021-08-08 21:32:55 -070067 !params.silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
Arnaldo Carvalho de Meloe4d9b042020-03-02 12:09:38 -030068 (int)bench__runtime.tv_sec);
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070069}
70
71static void toggle_done(int sig __maybe_unused,
72 siginfo_t *info __maybe_unused,
73 void *uc __maybe_unused)
74{
75 /* inform all threads that we're done for the day */
76 done = true;
Arnaldo Carvalho de Meloe4d9b042020-03-02 12:09:38 -030077 gettimeofday(&bench__end, NULL);
78 timersub(&bench__end, &bench__start, &bench__runtime);
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070079}
80
81static void *workerfn(void *arg)
82{
83 struct worker *w = (struct worker *) arg;
Davidlohr Buesoe2e16802016-10-24 13:56:52 -070084 unsigned long ops = w->ops;
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070085
86 pthread_mutex_lock(&thread_lock);
87 threads_starting--;
88 if (!threads_starting)
89 pthread_cond_signal(&thread_parent);
90 pthread_cond_wait(&thread_worker, &thread_lock);
91 pthread_mutex_unlock(&thread_lock);
92
93 do {
94 int ret;
95 again:
Davidlohr Bueso73b17942016-04-20 20:14:07 -070096 ret = futex_lock_pi(w->futex, NULL, futex_flag);
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -070097
98 if (ret) { /* handle lock acquisition */
Davidlohr Bueso09590462021-08-08 21:32:55 -070099 if (!params.silent)
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700100 warn("thread %d: Could not lock pi-lock for %p (%d)",
101 w->tid, w->futex, ret);
102 if (done)
103 break;
104
105 goto again;
106 }
107
108 usleep(1);
109 ret = futex_unlock_pi(w->futex, futex_flag);
Davidlohr Bueso09590462021-08-08 21:32:55 -0700110 if (ret && !params.silent)
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700111 warn("thread %d: Could not unlock pi-lock for %p (%d)",
112 w->tid, w->futex, ret);
Davidlohr Buesoe2e16802016-10-24 13:56:52 -0700113 ops++; /* account for thread's share of work */
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700114 } while (!done);
115
Davidlohr Buesoe2e16802016-10-24 13:56:52 -0700116 w->ops = ops;
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700117 return NULL;
118}
119
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800120static void create_threads(struct worker *w, pthread_attr_t thread_attr,
Jiri Olsaf8548392019-07-21 13:23:49 +0200121 struct perf_cpu_map *cpu)
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700122{
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800123 cpu_set_t cpuset;
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700124 unsigned int i;
125
Davidlohr Bueso09590462021-08-08 21:32:55 -0700126 threads_starting = params.nthreads;
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700127
Davidlohr Bueso09590462021-08-08 21:32:55 -0700128 for (i = 0; i < params.nthreads; i++) {
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700129 worker[i].tid = i;
130
Davidlohr Bueso09590462021-08-08 21:32:55 -0700131 if (params.multi) {
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700132 worker[i].futex = calloc(1, sizeof(u_int32_t));
133 if (!worker[i].futex)
134 err(EXIT_FAILURE, "calloc");
135 } else
136 worker[i].futex = &global_futex;
137
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800138 CPU_ZERO(&cpuset);
Ian Rogers6d188042022-01-04 22:13:51 -0800139 CPU_SET(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, &cpuset);
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700140
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800141 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700142 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
143
144 if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i]))
145 err(EXIT_FAILURE, "pthread_create");
146 }
147}
148
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300149int bench_futex_lock_pi(int argc, const char **argv)
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700150{
151 int ret = 0;
152 unsigned int i;
153 struct sigaction act;
154 pthread_attr_t thread_attr;
Jiri Olsaf8548392019-07-21 13:23:49 +0200155 struct perf_cpu_map *cpu;
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700156
157 argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0);
158 if (argc)
159 goto err;
160
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200161 cpu = perf_cpu_map__new(NULL);
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800162 if (!cpu)
163 err(EXIT_FAILURE, "calloc");
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700164
Tommi Rantala7b919a52020-03-05 10:37:14 +0200165 memset(&act, 0, sizeof(act));
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700166 sigfillset(&act.sa_mask);
167 act.sa_sigaction = toggle_done;
168 sigaction(SIGINT, &act, NULL);
169
Davidlohr Bueso9f9a3ff2021-08-08 21:32:58 -0700170 if (params.mlockall) {
171 if (mlockall(MCL_CURRENT | MCL_FUTURE))
172 err(EXIT_FAILURE, "mlockall");
173 }
174
Davidlohr Bueso09590462021-08-08 21:32:55 -0700175 if (!params.nthreads)
176 params.nthreads = cpu->nr;
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700177
Davidlohr Bueso09590462021-08-08 21:32:55 -0700178 worker = calloc(params.nthreads, sizeof(*worker));
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700179 if (!worker)
180 err(EXIT_FAILURE, "calloc");
181
Davidlohr Bueso09590462021-08-08 21:32:55 -0700182 if (!params.fshared)
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700183 futex_flag = FUTEX_PRIVATE_FLAG;
184
185 printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
Davidlohr Bueso09590462021-08-08 21:32:55 -0700186 getpid(), params.nthreads, params.runtime);
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700187
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);
192
Davidlohr Bueso09590462021-08-08 21:32:55 -0700193 threads_starting = params.nthreads;
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700194 pthread_attr_init(&thread_attr);
Arnaldo Carvalho de Meloe4d9b042020-03-02 12:09:38 -0300195 gettimeofday(&bench__start, NULL);
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700196
Davidlohr Bueso3b2323c2017-11-26 20:20:59 -0800197 create_threads(worker, thread_attr, cpu);
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700198 pthread_attr_destroy(&thread_attr);
199
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);
205
Davidlohr Bueso09590462021-08-08 21:32:55 -0700206 sleep(params.runtime);
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700207 toggle_done(0, NULL, NULL);
208
Davidlohr Bueso09590462021-08-08 21:32:55 -0700209 for (i = 0; i < params.nthreads; i++) {
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700210 ret = pthread_join(worker[i].thread, NULL);
211 if (ret)
212 err(EXIT_FAILURE, "pthread_join");
213 }
214
215 /* cleanup & report results */
216 pthread_cond_destroy(&thread_parent);
217 pthread_cond_destroy(&thread_worker);
218 pthread_mutex_destroy(&thread_lock);
219
Davidlohr Bueso09590462021-08-08 21:32:55 -0700220 for (i = 0; i < params.nthreads; i++) {
Tommi Rantala41e7c322020-04-17 16:23:29 +0300221 unsigned long t = bench__runtime.tv_sec > 0 ?
222 worker[i].ops / bench__runtime.tv_sec : 0;
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700223
224 update_stats(&throughput_stats, t);
Davidlohr Bueso09590462021-08-08 21:32:55 -0700225 if (!params.silent)
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700226 printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
227 worker[i].tid, worker[i].futex, t);
228
Davidlohr Bueso09590462021-08-08 21:32:55 -0700229 if (params.multi)
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300230 zfree(&worker[i].futex);
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700231 }
232
233 print_summary();
234
235 free(worker);
Sohaib Mohamed88e48232021-11-12 22:11:33 +0200236 perf_cpu_map__put(cpu);
Davidlohr Buesod2f3f5d2015-07-07 01:55:53 -0700237 return ret;
238err:
239 usage_with_options(bench_futex_lock_pi_usage, options);
240 exit(EXIT_FAILURE);
241}