blob: 8c5c0b6b5c974c6cbfa396716065817264fd1f12 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
4 *
5 * futex-wake: Block a bunch of threads on a futex and wake'em up, N at a time.
6 *
7 * This program is particularly useful to measure the latency of nthread wakeups
8 * in non-error situations: all waiters are queued and all wake calls wakeup
9 * one or more tasks, and thus the waitqueue is never empty.
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 "bench.h"
24#include "futex.h"
25
26#include <err.h>
27#include <stdlib.h>
28#include <sys/time.h>
29
30/* all threads will block on the same futex */
31static u_int32_t futex1 = 0;
32
33/*
34 * How many wakeups to do at a time.
35 * Default to 1 in order to make the kernel work more.
36 */
37static unsigned int nwakes = 1;
38
39pthread_t *worker;
40static bool done = false, silent = false, fshared = false;
41static pthread_mutex_t thread_lock;
42static pthread_cond_t thread_parent, thread_worker;
43static struct stats waketime_stats, wakeup_stats;
44static unsigned int ncpus, threads_starting, nthreads = 0;
45static int futex_flag = 0;
46
47static const struct option options[] = {
48 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
49 OPT_UINTEGER('w', "nwakes", &nwakes, "Specify amount of threads to wake at once"),
50 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
51 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
52 OPT_END()
53};
54
55static const char * const bench_futex_wake_usage[] = {
56 "perf bench futex wake <options>",
57 NULL
58};
59
60static void *workerfn(void *arg __maybe_unused)
61{
62 pthread_mutex_lock(&thread_lock);
63 threads_starting--;
64 if (!threads_starting)
65 pthread_cond_signal(&thread_parent);
66 pthread_cond_wait(&thread_worker, &thread_lock);
67 pthread_mutex_unlock(&thread_lock);
68
69 while (1) {
70 if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
71 break;
72 }
73
74 pthread_exit(NULL);
75 return NULL;
76}
77
78static void print_summary(void)
79{
80 double waketime_avg = avg_stats(&waketime_stats);
81 double waketime_stddev = stddev_stats(&waketime_stats);
82 unsigned int wakeup_avg = avg_stats(&wakeup_stats);
83
84 printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
85 wakeup_avg,
86 nthreads,
87 waketime_avg / USEC_PER_MSEC,
88 rel_stddev_stats(waketime_stddev, waketime_avg));
89}
90
91static void block_threads(pthread_t *w,
92 pthread_attr_t thread_attr)
93{
94 cpu_set_t cpu;
95 unsigned int i;
96
97 threads_starting = nthreads;
98
99 /* create and block all threads */
100 for (i = 0; i < nthreads; i++) {
101 CPU_ZERO(&cpu);
102 CPU_SET(i % ncpus, &cpu);
103
104 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
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
112static void toggle_done(int sig __maybe_unused,
113 siginfo_t *info __maybe_unused,
114 void *uc __maybe_unused)
115{
116 done = true;
117}
118
119int bench_futex_wake(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
126 argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
127 if (argc) {
128 usage_with_options(bench_futex_wake_usage, options);
129 exit(EXIT_FAILURE);
130 }
131
132 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
133
134 sigfillset(&act.sa_mask);
135 act.sa_sigaction = toggle_done;
136 sigaction(SIGINT, &act, NULL);
137
138 if (!nthreads)
139 nthreads = ncpus;
140
141 worker = calloc(nthreads, sizeof(*worker));
142 if (!worker)
143 err(EXIT_FAILURE, "calloc");
144
145 if (!fshared)
146 futex_flag = FUTEX_PRIVATE_FLAG;
147
148 printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
149 "waking up %d at a time.\n\n",
150 getpid(), nthreads, fshared ? "shared":"private", &futex1, nwakes);
151
152 init_stats(&wakeup_stats);
153 init_stats(&waketime_stats);
154 pthread_attr_init(&thread_attr);
155 pthread_mutex_init(&thread_lock, NULL);
156 pthread_cond_init(&thread_parent, NULL);
157 pthread_cond_init(&thread_worker, NULL);
158
159 for (j = 0; j < bench_repeat && !done; j++) {
160 unsigned int nwoken = 0;
161 struct timeval start, end, runtime;
162
163 /* create, launch & block all threads */
164 block_threads(worker, thread_attr);
165
166 /* make sure all threads are already blocked */
167 pthread_mutex_lock(&thread_lock);
168 while (threads_starting)
169 pthread_cond_wait(&thread_parent, &thread_lock);
170 pthread_cond_broadcast(&thread_worker);
171 pthread_mutex_unlock(&thread_lock);
172
173 usleep(100000);
174
175 /* Ok, all threads are patiently blocked, start waking folks up */
176 gettimeofday(&start, NULL);
177 while (nwoken != nthreads)
178 nwoken += futex_wake(&futex1, nwakes, futex_flag);
179 gettimeofday(&end, NULL);
180 timersub(&end, &start, &runtime);
181
182 update_stats(&wakeup_stats, nwoken);
183 update_stats(&waketime_stats, runtime.tv_usec);
184
185 if (!silent) {
186 printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
187 j + 1, nwoken, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
188 }
189
190 for (i = 0; i < nthreads; i++) {
191 ret = pthread_join(worker[i], NULL);
192 if (ret)
193 err(EXIT_FAILURE, "pthread_join");
194 }
195
196 }
197
198 /* cleanup & report results */
199 pthread_cond_destroy(&thread_parent);
200 pthread_cond_destroy(&thread_worker);
201 pthread_mutex_destroy(&thread_lock);
202 pthread_attr_destroy(&thread_attr);
203
204 print_summary();
205
206 free(worker);
207 return ret;
208}