perf bench futex, requeue: Add --broadcast option
[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         pthread_mutex_lock(&thread_lock);
81         threads_starting--;
82         if (!threads_starting)
83                 pthread_cond_signal(&thread_parent);
84         pthread_cond_wait(&thread_worker, &thread_lock);
85         pthread_mutex_unlock(&thread_lock);
86
87         futex_wait(&futex1, 0, NULL, futex_flag);
88         return NULL;
89 }
90
91 static void block_threads(pthread_t *w,
92                           pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
93 {
94         cpu_set_t cpuset;
95         unsigned int i;
96
97         threads_starting = params.nthreads;
98
99         /* create and block all threads */
100         for (i = 0; i < params.nthreads; i++) {
101                 CPU_ZERO(&cpuset);
102                 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
103
104                 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
105                         err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
106
107                 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
108                         err(EXIT_FAILURE, "pthread_create");
109         }
110 }
111
112 static void toggle_done(int sig __maybe_unused,
113                         siginfo_t *info __maybe_unused,
114                         void *uc __maybe_unused)
115 {
116         done = true;
117 }
118
119 int bench_futex_requeue(int argc, const char **argv)
120 {
121         int ret = 0;
122         unsigned int i, j;
123         struct sigaction act;
124         pthread_attr_t thread_attr;
125         struct perf_cpu_map *cpu;
126
127         argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
128         if (argc)
129                 goto err;
130
131         cpu = perf_cpu_map__new(NULL);
132         if (!cpu)
133                 err(EXIT_FAILURE, "cpu_map__new");
134
135         memset(&act, 0, sizeof(act));
136         sigfillset(&act.sa_mask);
137         act.sa_sigaction = toggle_done;
138         sigaction(SIGINT, &act, NULL);
139
140         if (params.mlockall) {
141                 if (mlockall(MCL_CURRENT | MCL_FUTURE))
142                         err(EXIT_FAILURE, "mlockall");
143         }
144
145         if (!params.nthreads)
146                 params.nthreads = cpu->nr;
147
148         worker = calloc(params.nthreads, sizeof(*worker));
149         if (!worker)
150                 err(EXIT_FAILURE, "calloc");
151
152         if (!params.fshared)
153                 futex_flag = FUTEX_PRIVATE_FLAG;
154
155         if (params.nrequeue > params.nthreads)
156                 params.nrequeue = params.nthreads;
157
158         if (params.broadcast)
159                 params.nrequeue = params.nthreads;
160
161         printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
162                "%d at a time.\n\n",  getpid(), params.nthreads,
163                params.fshared ? "shared":"private", &futex1, &futex2, params.nrequeue);
164
165         init_stats(&requeued_stats);
166         init_stats(&requeuetime_stats);
167         pthread_attr_init(&thread_attr);
168         pthread_mutex_init(&thread_lock, NULL);
169         pthread_cond_init(&thread_parent, NULL);
170         pthread_cond_init(&thread_worker, NULL);
171
172         for (j = 0; j < bench_repeat && !done; j++) {
173                 unsigned int nrequeued = 0;
174                 struct timeval start, end, runtime;
175
176                 /* create, launch & block all threads */
177                 block_threads(worker, thread_attr, cpu);
178
179                 /* make sure all threads are already blocked */
180                 pthread_mutex_lock(&thread_lock);
181                 while (threads_starting)
182                         pthread_cond_wait(&thread_parent, &thread_lock);
183                 pthread_cond_broadcast(&thread_worker);
184                 pthread_mutex_unlock(&thread_lock);
185
186                 usleep(100000);
187
188                 /* Ok, all threads are patiently blocked, start requeueing */
189                 gettimeofday(&start, NULL);
190                 while (nrequeued < params.nthreads) {
191                         /*
192                          * Do not wakeup any tasks blocked on futex1, allowing
193                          * us to really measure futex_wait functionality.
194                          */
195                         nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0,
196                                                        params.nrequeue,
197                                                        futex_flag);
198                 }
199
200                 gettimeofday(&end, NULL);
201                 timersub(&end, &start, &runtime);
202
203                 update_stats(&requeued_stats, nrequeued);
204                 update_stats(&requeuetime_stats, runtime.tv_usec);
205
206                 if (!params.silent) {
207                         printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
208                                j + 1, nrequeued, params.nthreads,
209                                runtime.tv_usec / (double)USEC_PER_MSEC);
210                 }
211
212                 /* everybody should be blocked on futex2, wake'em up */
213                 nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
214                 if (params.nthreads != nrequeued)
215                         warnx("couldn't wakeup all tasks (%d/%d)",
216                               nrequeued, params.nthreads);
217
218                 for (i = 0; i < params.nthreads; i++) {
219                         ret = pthread_join(worker[i], NULL);
220                         if (ret)
221                                 err(EXIT_FAILURE, "pthread_join");
222                 }
223         }
224
225         /* cleanup & report results */
226         pthread_cond_destroy(&thread_parent);
227         pthread_cond_destroy(&thread_worker);
228         pthread_mutex_destroy(&thread_lock);
229         pthread_attr_destroy(&thread_attr);
230
231         print_summary();
232
233         free(worker);
234         return ret;
235 err:
236         usage_with_options(bench_futex_requeue_usage, options);
237         exit(EXIT_FAILURE);
238 }