perf bench futex, requeue: Robustify futex_wait() handling
[linux-2.6-microblaze.git] / tools / perf / bench / futex-requeue.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2013  Davidlohr Bueso <davidlohr@hp.com>
4  *
5  * futex-requeue: Block a bunch of threads on futex1 and requeue them
6  *                on futex2, N at a time.
7  *
8  * This program is particularly useful to measure the latency of nthread
9  * requeues without waking up any tasks -- thus mimicking a regular futex_wait.
10  */
11
12 /* For the CLR_() macros */
13 #include <string.h>
14 #include <pthread.h>
15
16 #include <signal.h>
17 #include "../util/stat.h"
18 #include <subcmd/parse-options.h>
19 #include <linux/compiler.h>
20 #include <linux/kernel.h>
21 #include <linux/time64.h>
22 #include <errno.h>
23 #include <perf/cpumap.h>
24 #include "bench.h"
25 #include "futex.h"
26
27 #include <err.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30 #include <sys/mman.h>
31
32 static u_int32_t futex1 = 0, futex2 = 0;
33
34 static pthread_t *worker;
35 static bool done = false;
36 static pthread_mutex_t thread_lock;
37 static pthread_cond_t thread_parent, thread_worker;
38 static struct stats requeuetime_stats, requeued_stats;
39 static unsigned int threads_starting;
40 static int futex_flag = 0;
41
42 static struct bench_futex_parameters params = {
43         /*
44          * How many tasks to requeue at a time.
45          * Default to 1 in order to make the kernel work more.
46          */
47         .nrequeue = 1,
48 };
49
50 static const struct option options[] = {
51         OPT_UINTEGER('t', "threads",  &params.nthreads, "Specify amount of threads"),
52         OPT_UINTEGER('q', "nrequeue", &params.nrequeue, "Specify amount of threads to requeue at once"),
53         OPT_BOOLEAN( 's', "silent",   &params.silent, "Silent mode: do not display data/details"),
54         OPT_BOOLEAN( 'S', "shared",   &params.fshared, "Use shared futexes instead of private ones"),
55         OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
56         OPT_BOOLEAN( 'B', "broadcast", &params.broadcast, "Requeue all threads at once"),
57         OPT_END()
58 };
59
60 static const char * const bench_futex_requeue_usage[] = {
61         "perf bench futex requeue <options>",
62         NULL
63 };
64
65 static void print_summary(void)
66 {
67         double requeuetime_avg = avg_stats(&requeuetime_stats);
68         double requeuetime_stddev = stddev_stats(&requeuetime_stats);
69         unsigned int requeued_avg = avg_stats(&requeued_stats);
70
71         printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
72                requeued_avg,
73                params.nthreads,
74                requeuetime_avg / USEC_PER_MSEC,
75                rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
76 }
77
78 static void *workerfn(void *arg __maybe_unused)
79 {
80         int ret;
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         while (1) {
90                 ret = futex_wait(&futex1, 0, NULL, futex_flag);
91                 if (!ret)
92                         break;
93
94                 if (ret && errno != EAGAIN) {
95                         if (!params.silent)
96                                 warn("futex_wait");
97                         break;
98                 }
99         }
100
101         return NULL;
102 }
103
104 static void block_threads(pthread_t *w,
105                           pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
106 {
107         cpu_set_t cpuset;
108         unsigned int i;
109
110         threads_starting = params.nthreads;
111
112         /* create and block all threads */
113         for (i = 0; i < params.nthreads; i++) {
114                 CPU_ZERO(&cpuset);
115                 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
116
117                 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
118                         err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
119
120                 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
121                         err(EXIT_FAILURE, "pthread_create");
122         }
123 }
124
125 static void toggle_done(int sig __maybe_unused,
126                         siginfo_t *info __maybe_unused,
127                         void *uc __maybe_unused)
128 {
129         done = true;
130 }
131
132 int bench_futex_requeue(int argc, const char **argv)
133 {
134         int ret = 0;
135         unsigned int i, j;
136         struct sigaction act;
137         pthread_attr_t thread_attr;
138         struct perf_cpu_map *cpu;
139
140         argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
141         if (argc)
142                 goto err;
143
144         cpu = perf_cpu_map__new(NULL);
145         if (!cpu)
146                 err(EXIT_FAILURE, "cpu_map__new");
147
148         memset(&act, 0, sizeof(act));
149         sigfillset(&act.sa_mask);
150         act.sa_sigaction = toggle_done;
151         sigaction(SIGINT, &act, NULL);
152
153         if (params.mlockall) {
154                 if (mlockall(MCL_CURRENT | MCL_FUTURE))
155                         err(EXIT_FAILURE, "mlockall");
156         }
157
158         if (!params.nthreads)
159                 params.nthreads = cpu->nr;
160
161         worker = calloc(params.nthreads, sizeof(*worker));
162         if (!worker)
163                 err(EXIT_FAILURE, "calloc");
164
165         if (!params.fshared)
166                 futex_flag = FUTEX_PRIVATE_FLAG;
167
168         if (params.nrequeue > params.nthreads)
169                 params.nrequeue = params.nthreads;
170
171         if (params.broadcast)
172                 params.nrequeue = params.nthreads;
173
174         printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
175                "%d at a time.\n\n",  getpid(), params.nthreads,
176                params.fshared ? "shared":"private", &futex1, &futex2, params.nrequeue);
177
178         init_stats(&requeued_stats);
179         init_stats(&requeuetime_stats);
180         pthread_attr_init(&thread_attr);
181         pthread_mutex_init(&thread_lock, NULL);
182         pthread_cond_init(&thread_parent, NULL);
183         pthread_cond_init(&thread_worker, NULL);
184
185         for (j = 0; j < bench_repeat && !done; j++) {
186                 unsigned int nrequeued = 0;
187                 struct timeval start, end, runtime;
188
189                 /* create, launch & block all threads */
190                 block_threads(worker, thread_attr, cpu);
191
192                 /* make sure all threads are already blocked */
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                 usleep(100000);
200
201                 /* Ok, all threads are patiently blocked, start requeueing */
202                 gettimeofday(&start, NULL);
203                 while (nrequeued < params.nthreads) {
204                         /*
205                          * Do not wakeup any tasks blocked on futex1, allowing
206                          * us to really measure futex_wait functionality.
207                          */
208                         nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0,
209                                                        params.nrequeue,
210                                                        futex_flag);
211                 }
212
213                 gettimeofday(&end, NULL);
214                 timersub(&end, &start, &runtime);
215
216                 update_stats(&requeued_stats, nrequeued);
217                 update_stats(&requeuetime_stats, runtime.tv_usec);
218
219                 if (!params.silent) {
220                         printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
221                                j + 1, nrequeued, params.nthreads,
222                                runtime.tv_usec / (double)USEC_PER_MSEC);
223                 }
224
225                 /* everybody should be blocked on futex2, wake'em up */
226                 nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
227                 if (params.nthreads != nrequeued)
228                         warnx("couldn't wakeup all tasks (%d/%d)",
229                               nrequeued, params.nthreads);
230
231                 for (i = 0; i < params.nthreads; i++) {
232                         ret = pthread_join(worker[i], NULL);
233                         if (ret)
234                                 err(EXIT_FAILURE, "pthread_join");
235                 }
236         }
237
238         /* cleanup & report results */
239         pthread_cond_destroy(&thread_parent);
240         pthread_cond_destroy(&thread_worker);
241         pthread_mutex_destroy(&thread_lock);
242         pthread_attr_destroy(&thread_attr);
243
244         print_summary();
245
246         free(worker);
247         return ret;
248 err:
249         usage_with_options(bench_futex_requeue_usage, options);
250         exit(EXIT_FAILURE);
251 }