]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/perf/bench/futex-wake-parallel.c
perf bench futex: Fix build on musl + clang
[karo-tx-linux.git] / tools / perf / bench / futex-wake-parallel.c
1 /*
2  * Copyright (C) 2015 Davidlohr Bueso.
3  *
4  * Block a bunch of threads and let parallel waker threads wakeup an
5  * equal amount of them. The program output reflects the avg latency
6  * for each individual thread to service its share of work. Ultimately
7  * it can be used to measure futex_wake() changes.
8  */
9
10 /* For the CLR_() macros */
11 #include <string.h>
12 #include <pthread.h>
13
14 #include <signal.h>
15 #include "../util/stat.h"
16 #include <subcmd/parse-options.h>
17 #include <linux/compiler.h>
18 #include <linux/kernel.h>
19 #include <linux/time64.h>
20 #include <errno.h>
21 #include "bench.h"
22 #include "futex.h"
23
24 #include <err.h>
25 #include <stdlib.h>
26 #include <sys/time.h>
27
28 struct thread_data {
29         pthread_t worker;
30         unsigned int nwoken;
31         struct timeval runtime;
32 };
33
34 static unsigned int nwakes = 1;
35
36 /* all threads will block on the same futex -- hash bucket chaos ;) */
37 static u_int32_t futex = 0;
38
39 static pthread_t *blocked_worker;
40 static bool done = false, silent = false, fshared = false;
41 static unsigned int nblocked_threads = 0, nwaking_threads = 0;
42 static pthread_mutex_t thread_lock;
43 static pthread_cond_t thread_parent, thread_worker;
44 static struct stats waketime_stats, wakeup_stats;
45 static unsigned int ncpus, threads_starting;
46 static int futex_flag = 0;
47
48 static const struct option options[] = {
49         OPT_UINTEGER('t', "threads", &nblocked_threads, "Specify amount of threads"),
50         OPT_UINTEGER('w', "nwakers", &nwaking_threads, "Specify amount of waking threads"),
51         OPT_BOOLEAN( 's', "silent",  &silent,   "Silent mode: do not display data/details"),
52         OPT_BOOLEAN( 'S', "shared",  &fshared,  "Use shared futexes instead of private ones"),
53         OPT_END()
54 };
55
56 static const char * const bench_futex_wake_parallel_usage[] = {
57         "perf bench futex wake-parallel <options>",
58         NULL
59 };
60
61 static void *waking_workerfn(void *arg)
62 {
63         struct thread_data *waker = (struct thread_data *) arg;
64         struct timeval start, end;
65
66         gettimeofday(&start, NULL);
67
68         waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
69         if (waker->nwoken != nwakes)
70                 warnx("couldn't wakeup all tasks (%d/%d)",
71                       waker->nwoken, nwakes);
72
73         gettimeofday(&end, NULL);
74         timersub(&end, &start, &waker->runtime);
75
76         pthread_exit(NULL);
77         return NULL;
78 }
79
80 static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
81 {
82         unsigned int i;
83
84         pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
85
86         /* create and block all threads */
87         for (i = 0; i < nwaking_threads; i++) {
88                 /*
89                  * Thread creation order will impact per-thread latency
90                  * as it will affect the order to acquire the hb spinlock.
91                  * For now let the scheduler decide.
92                  */
93                 if (pthread_create(&td[i].worker, &thread_attr,
94                                    waking_workerfn, (void *)&td[i]))
95                         err(EXIT_FAILURE, "pthread_create");
96         }
97
98         for (i = 0; i < nwaking_threads; i++)
99                 if (pthread_join(td[i].worker, NULL))
100                         err(EXIT_FAILURE, "pthread_join");
101 }
102
103 static void *blocked_workerfn(void *arg __maybe_unused)
104 {
105         pthread_mutex_lock(&thread_lock);
106         threads_starting--;
107         if (!threads_starting)
108                 pthread_cond_signal(&thread_parent);
109         pthread_cond_wait(&thread_worker, &thread_lock);
110         pthread_mutex_unlock(&thread_lock);
111
112         while (1) { /* handle spurious wakeups */
113                 if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
114                         break;
115         }
116
117         pthread_exit(NULL);
118         return NULL;
119 }
120
121 static void block_threads(pthread_t *w, pthread_attr_t thread_attr)
122 {
123         cpu_set_t cpu;
124         unsigned int i;
125
126         threads_starting = nblocked_threads;
127
128         /* create and block all threads */
129         for (i = 0; i < nblocked_threads; i++) {
130                 CPU_ZERO(&cpu);
131                 CPU_SET(i % ncpus, &cpu);
132
133                 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
134                         err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
135
136                 if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL))
137                         err(EXIT_FAILURE, "pthread_create");
138         }
139 }
140
141 static void print_run(struct thread_data *waking_worker, unsigned int run_num)
142 {
143         unsigned int i, wakeup_avg;
144         double waketime_avg, waketime_stddev;
145         struct stats __waketime_stats, __wakeup_stats;
146
147         init_stats(&__wakeup_stats);
148         init_stats(&__waketime_stats);
149
150         for (i = 0; i < nwaking_threads; i++) {
151                 update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
152                 update_stats(&__wakeup_stats, waking_worker[i].nwoken);
153         }
154
155         waketime_avg = avg_stats(&__waketime_stats);
156         waketime_stddev = stddev_stats(&__waketime_stats);
157         wakeup_avg = avg_stats(&__wakeup_stats);
158
159         printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
160                "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
161                nblocked_threads, waketime_avg / USEC_PER_MSEC,
162                rel_stddev_stats(waketime_stddev, waketime_avg));
163 }
164
165 static void print_summary(void)
166 {
167         unsigned int wakeup_avg;
168         double waketime_avg, waketime_stddev;
169
170         waketime_avg = avg_stats(&waketime_stats);
171         waketime_stddev = stddev_stats(&waketime_stats);
172         wakeup_avg = avg_stats(&wakeup_stats);
173
174         printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
175                wakeup_avg,
176                nblocked_threads,
177                waketime_avg / USEC_PER_MSEC,
178                rel_stddev_stats(waketime_stddev, waketime_avg));
179 }
180
181
182 static void do_run_stats(struct thread_data *waking_worker)
183 {
184         unsigned int i;
185
186         for (i = 0; i < nwaking_threads; i++) {
187                 update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
188                 update_stats(&wakeup_stats, waking_worker[i].nwoken);
189         }
190
191 }
192
193 static void toggle_done(int sig __maybe_unused,
194                         siginfo_t *info __maybe_unused,
195                         void *uc __maybe_unused)
196 {
197         done = true;
198 }
199
200 int bench_futex_wake_parallel(int argc, const char **argv,
201                               const char *prefix __maybe_unused)
202 {
203         int ret = 0;
204         unsigned int i, j;
205         struct sigaction act;
206         pthread_attr_t thread_attr;
207         struct thread_data *waking_worker;
208
209         argc = parse_options(argc, argv, options,
210                              bench_futex_wake_parallel_usage, 0);
211         if (argc) {
212                 usage_with_options(bench_futex_wake_parallel_usage, options);
213                 exit(EXIT_FAILURE);
214         }
215
216         sigfillset(&act.sa_mask);
217         act.sa_sigaction = toggle_done;
218         sigaction(SIGINT, &act, NULL);
219
220         ncpus = sysconf(_SC_NPROCESSORS_ONLN);
221         if (!nblocked_threads)
222                 nblocked_threads = ncpus;
223
224         /* some sanity checks */
225         if (nwaking_threads > nblocked_threads || !nwaking_threads)
226                 nwaking_threads = nblocked_threads;
227
228         if (nblocked_threads % nwaking_threads)
229                 errx(EXIT_FAILURE, "Must be perfectly divisible");
230         /*
231          * Each thread will wakeup nwakes tasks in
232          * a single futex_wait call.
233          */
234         nwakes = nblocked_threads/nwaking_threads;
235
236         blocked_worker = calloc(nblocked_threads, sizeof(*blocked_worker));
237         if (!blocked_worker)
238                 err(EXIT_FAILURE, "calloc");
239
240         if (!fshared)
241                 futex_flag = FUTEX_PRIVATE_FLAG;
242
243         printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
244                "futex %p), %d threads waking up %d at a time.\n\n",
245                getpid(), nblocked_threads, fshared ? "shared":"private",
246                &futex, nwaking_threads, nwakes);
247
248         init_stats(&wakeup_stats);
249         init_stats(&waketime_stats);
250
251         pthread_attr_init(&thread_attr);
252         pthread_mutex_init(&thread_lock, NULL);
253         pthread_cond_init(&thread_parent, NULL);
254         pthread_cond_init(&thread_worker, NULL);
255
256         for (j = 0; j < bench_repeat && !done; j++) {
257                 waking_worker = calloc(nwaking_threads, sizeof(*waking_worker));
258                 if (!waking_worker)
259                         err(EXIT_FAILURE, "calloc");
260
261                 /* create, launch & block all threads */
262                 block_threads(blocked_worker, thread_attr);
263
264                 /* make sure all threads are already blocked */
265                 pthread_mutex_lock(&thread_lock);
266                 while (threads_starting)
267                         pthread_cond_wait(&thread_parent, &thread_lock);
268                 pthread_cond_broadcast(&thread_worker);
269                 pthread_mutex_unlock(&thread_lock);
270
271                 usleep(100000);
272
273                 /* Ok, all threads are patiently blocked, start waking folks up */
274                 wakeup_threads(waking_worker, thread_attr);
275
276                 for (i = 0; i < nblocked_threads; i++) {
277                         ret = pthread_join(blocked_worker[i], NULL);
278                         if (ret)
279                                 err(EXIT_FAILURE, "pthread_join");
280                 }
281
282                 do_run_stats(waking_worker);
283                 if (!silent)
284                         print_run(waking_worker, j);
285
286                 free(waking_worker);
287         }
288
289         /* cleanup & report results */
290         pthread_cond_destroy(&thread_parent);
291         pthread_cond_destroy(&thread_worker);
292         pthread_mutex_destroy(&thread_lock);
293         pthread_attr_destroy(&thread_attr);
294
295         print_summary();
296
297         free(blocked_worker);
298         return ret;
299 }