Linux Perf
futex-wake.c
Go to the documentation of this file.
1 // 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 #include "cpumap.h"
26 
27 #include <err.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30 
31 /* all threads will block on the same futex */
32 static u_int32_t futex1 = 0;
33 
34 /*
35  * How many wakeups to do at a time.
36  * Default to 1 in order to make the kernel work more.
37  */
38 static unsigned int nwakes = 1;
39 
40 pthread_t *worker;
41 static bool done = false, silent = false, fshared = false;
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, nthreads = 0;
46 static int futex_flag = 0;
47 
48 static const struct option options[] = {
49  OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
50  OPT_UINTEGER('w', "nwakes", &nwakes, "Specify amount of threads to wake at once"),
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_usage[] = {
57  "perf bench futex wake <options>",
58  NULL
59 };
60 
61 static void *workerfn(void *arg __maybe_unused)
62 {
63  pthread_mutex_lock(&thread_lock);
65  if (!threads_starting)
66  pthread_cond_signal(&thread_parent);
67  pthread_cond_wait(&thread_worker, &thread_lock);
68  pthread_mutex_unlock(&thread_lock);
69 
70  while (1) {
71  if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
72  break;
73  }
74 
75  pthread_exit(NULL);
76  return NULL;
77 }
78 
79 static void print_summary(void)
80 {
81  double waketime_avg = avg_stats(&waketime_stats);
82  double waketime_stddev = stddev_stats(&waketime_stats);
83  unsigned int wakeup_avg = avg_stats(&wakeup_stats);
84 
85  printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
86  wakeup_avg,
87  nthreads,
88  waketime_avg / USEC_PER_MSEC,
89  rel_stddev_stats(waketime_stddev, waketime_avg));
90 }
91 
92 static void block_threads(pthread_t *w,
93  pthread_attr_t thread_attr, struct cpu_map *cpu)
94 {
95  cpu_set_t cpuset;
96  unsigned int i;
97 
99 
100  /* create and block all threads */
101  for (i = 0; i < nthreads; i++) {
102  CPU_ZERO(&cpuset);
103  CPU_SET(cpu->map[i % cpu->nr], &cpuset);
104 
105  if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
106  err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
107 
108  if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
109  err(EXIT_FAILURE, "pthread_create");
110  }
111 }
112 
113 static void toggle_done(int sig __maybe_unused,
114  siginfo_t *info __maybe_unused,
115  void *uc __maybe_unused)
116 {
117  done = true;
118 }
119 
120 int bench_futex_wake(int argc, const char **argv)
121 {
122  int ret = 0;
123  unsigned int i, j;
124  struct sigaction act;
125  pthread_attr_t thread_attr;
126  struct cpu_map *cpu;
127 
128  argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
129  if (argc) {
130  usage_with_options(bench_futex_wake_usage, options);
131  exit(EXIT_FAILURE);
132  }
133 
134  cpu = cpu_map__new(NULL);
135  if (!cpu)
136  err(EXIT_FAILURE, "calloc");
137 
138  sigfillset(&act.sa_mask);
139  act.sa_sigaction = toggle_done;
140  sigaction(SIGINT, &act, NULL);
141 
142  if (!nthreads)
143  nthreads = ncpus;
144 
145  worker = calloc(nthreads, sizeof(*worker));
146  if (!worker)
147  err(EXIT_FAILURE, "calloc");
148 
149  if (!fshared)
151 
152  printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
153  "waking up %d at a time.\n\n",
154  getpid(), nthreads, fshared ? "shared":"private", &futex1, nwakes);
155 
157  init_stats(&waketime_stats);
158  pthread_attr_init(&thread_attr);
159  pthread_mutex_init(&thread_lock, NULL);
160  pthread_cond_init(&thread_parent, NULL);
161  pthread_cond_init(&thread_worker, NULL);
162 
163  for (j = 0; j < bench_repeat && !done; j++) {
164  unsigned int nwoken = 0;
165  struct timeval start, end, runtime;
166 
167  /* create, launch & block all threads */
168  block_threads(worker, thread_attr, cpu);
169 
170  /* make sure all threads are already blocked */
171  pthread_mutex_lock(&thread_lock);
172  while (threads_starting)
173  pthread_cond_wait(&thread_parent, &thread_lock);
174  pthread_cond_broadcast(&thread_worker);
175  pthread_mutex_unlock(&thread_lock);
176 
177  usleep(100000);
178 
179  /* Ok, all threads are patiently blocked, start waking folks up */
180  gettimeofday(&start, NULL);
181  while (nwoken != nthreads)
182  nwoken += futex_wake(&futex1, nwakes, futex_flag);
183  gettimeofday(&end, NULL);
184  timersub(&end, &start, &runtime);
185 
186  update_stats(&wakeup_stats, nwoken);
187  update_stats(&waketime_stats, runtime.tv_usec);
188 
189  if (!silent) {
190  printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
191  j + 1, nwoken, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
192  }
193 
194  for (i = 0; i < nthreads; i++) {
195  ret = pthread_join(worker[i], NULL);
196  if (ret)
197  err(EXIT_FAILURE, "pthread_join");
198  }
199 
200  }
201 
202  /* cleanup & report results */
203  pthread_cond_destroy(&thread_parent);
204  pthread_cond_destroy(&thread_worker);
205  pthread_mutex_destroy(&thread_lock);
206  pthread_attr_destroy(&thread_attr);
207 
208  print_summary();
209 
210  free(worker);
211  return ret;
212 }
static void block_threads(pthread_t *w, pthread_attr_t thread_attr, struct cpu_map *cpu)
Definition: futex-wake.c:92
int nr
Definition: cpumap.h:14
int bench_futex_wake(int argc, const char **argv)
Definition: futex-wake.c:120
double avg_stats(struct stats *stats)
Definition: stat.c:26
static struct stats waketime_stats wakeup_stats
Definition: futex-wake.c:44
static const char *const bench_futex_wake_usage[]
Definition: futex-wake.c:56
Definition: genelf.c:61
static bool fshared
Definition: futex-wake.c:41
int int err
Definition: 5sec.c:44
Definition: cpumap.h:12
double stddev_stats(struct stats *stats)
Definition: stat.c:47
static void init_stats(struct stats *stats)
Definition: stat.h:103
static u_int32_t futex1
Definition: futex-wake.c:32
void update_stats(struct stats *stats, u64 val)
Definition: stat.c:10
static int futex_flag
Definition: futex-wake.c:46
static void * workerfn(void *arg __maybe_unused)
Definition: futex-wake.c:61
static int pthread_attr_setaffinity_np(pthread_attr_t *attr __maybe_unused, size_t cpusetsize __maybe_unused, cpu_set_t *cpuset __maybe_unused)
Definition: futex.h:93
double rel_stddev_stats(double stddev, double avg)
Definition: stat.c:60
static unsigned int threads_starting
Definition: futex-wake.c:45
int map[]
Definition: cpumap.h:15
static unsigned int nwakes
Definition: futex-wake.c:38
static pthread_mutex_t thread_lock
Definition: futex-wake.c:42
int FUTEX_PRIVATE_FLAG
Definition: Util.py:13
static bool done
Definition: futex-wake.c:41
static pthread_cond_t thread_parent
Definition: futex-wake.c:43
Definition: stat.h:10
static int futex_wait(u_int32_t *uaddr, u_int32_t val, struct timespec *timeout, int opflags)
Definition: futex.h:44
static unsigned int ncpus
Definition: futex-wake.c:45
pthread_t * worker
Definition: futex-wake.c:40
void free(void *)
static bool silent
Definition: futex-wake.c:41
struct cpu_map * cpu_map__new(const char *cpu_list)
Definition: cpumap.c:125
static void toggle_done(int sig __maybe_unused, siginfo_t *info __maybe_unused, void *uc __maybe_unused)
Definition: futex-wake.c:113
unsigned int bench_repeat
static int futex_wake(u_int32_t *uaddr, int nr_wake, int opflags)
Definition: futex.h:54
static void print_summary(void)
Definition: futex-wake.c:79
static unsigned int nthreads
Definition: futex-wake.c:45
static pthread_cond_t thread_worker
Definition: futex-wake.c:43