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