xfs: try to avoid blowing out the transaction reservation when bunmaping a shared...
[linux/fpc-iii.git] / tools / perf / bench / futex-lock-pi.c
blob936d89d3048354adea0b182d6223fb6be119d9ce
1 /*
2 * Copyright (C) 2015 Davidlohr Bueso.
3 */
5 /* For the CLR_() macros */
6 #include <pthread.h>
8 #include <signal.h>
9 #include "../util/stat.h"
10 #include <subcmd/parse-options.h>
11 #include <linux/compiler.h>
12 #include <linux/kernel.h>
13 #include <errno.h>
14 #include "bench.h"
15 #include "futex.h"
17 #include <err.h>
18 #include <stdlib.h>
19 #include <sys/time.h>
21 struct worker {
22 int tid;
23 u_int32_t *futex;
24 pthread_t thread;
25 unsigned long ops;
28 static u_int32_t global_futex = 0;
29 static struct worker *worker;
30 static unsigned int nsecs = 10;
31 static bool silent = false, multi = false;
32 static bool done = false, fshared = false;
33 static unsigned int ncpus, nthreads = 0;
34 static int futex_flag = 0;
35 struct timeval start, end, runtime;
36 static pthread_mutex_t thread_lock;
37 static unsigned int threads_starting;
38 static struct stats throughput_stats;
39 static pthread_cond_t thread_parent, thread_worker;
41 static const struct option options[] = {
42 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
43 OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
44 OPT_BOOLEAN( 'M', "multi", &multi, "Use multiple futexes"),
45 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
46 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
47 OPT_END()
50 static const char * const bench_futex_lock_pi_usage[] = {
51 "perf bench futex requeue <options>",
52 NULL
55 static void print_summary(void)
57 unsigned long avg = avg_stats(&throughput_stats);
58 double stddev = stddev_stats(&throughput_stats);
60 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
61 !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
62 (int) runtime.tv_sec);
65 static void toggle_done(int sig __maybe_unused,
66 siginfo_t *info __maybe_unused,
67 void *uc __maybe_unused)
69 /* inform all threads that we're done for the day */
70 done = true;
71 gettimeofday(&end, NULL);
72 timersub(&end, &start, &runtime);
75 static void *workerfn(void *arg)
77 struct worker *w = (struct worker *) arg;
79 pthread_mutex_lock(&thread_lock);
80 threads_starting--;
81 if (!threads_starting)
82 pthread_cond_signal(&thread_parent);
83 pthread_cond_wait(&thread_worker, &thread_lock);
84 pthread_mutex_unlock(&thread_lock);
86 do {
87 int ret;
88 again:
89 ret = futex_lock_pi(w->futex, NULL, futex_flag);
91 if (ret) { /* handle lock acquisition */
92 if (!silent)
93 warn("thread %d: Could not lock pi-lock for %p (%d)",
94 w->tid, w->futex, ret);
95 if (done)
96 break;
98 goto again;
101 usleep(1);
102 ret = futex_unlock_pi(w->futex, futex_flag);
103 if (ret && !silent)
104 warn("thread %d: Could not unlock pi-lock for %p (%d)",
105 w->tid, w->futex, ret);
106 w->ops++; /* account for thread's share of work */
107 } while (!done);
109 return NULL;
112 static void create_threads(struct worker *w, pthread_attr_t thread_attr)
114 cpu_set_t cpu;
115 unsigned int i;
117 threads_starting = nthreads;
119 for (i = 0; i < nthreads; i++) {
120 worker[i].tid = i;
122 if (multi) {
123 worker[i].futex = calloc(1, sizeof(u_int32_t));
124 if (!worker[i].futex)
125 err(EXIT_FAILURE, "calloc");
126 } else
127 worker[i].futex = &global_futex;
129 CPU_ZERO(&cpu);
130 CPU_SET(i % ncpus, &cpu);
132 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
133 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
135 if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i]))
136 err(EXIT_FAILURE, "pthread_create");
140 int bench_futex_lock_pi(int argc, const char **argv,
141 const char *prefix __maybe_unused)
143 int ret = 0;
144 unsigned int i;
145 struct sigaction act;
146 pthread_attr_t thread_attr;
148 argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0);
149 if (argc)
150 goto err;
152 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
154 sigfillset(&act.sa_mask);
155 act.sa_sigaction = toggle_done;
156 sigaction(SIGINT, &act, NULL);
158 if (!nthreads)
159 nthreads = ncpus;
161 worker = calloc(nthreads, sizeof(*worker));
162 if (!worker)
163 err(EXIT_FAILURE, "calloc");
165 if (!fshared)
166 futex_flag = FUTEX_PRIVATE_FLAG;
168 printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
169 getpid(), nthreads, nsecs);
171 init_stats(&throughput_stats);
172 pthread_mutex_init(&thread_lock, NULL);
173 pthread_cond_init(&thread_parent, NULL);
174 pthread_cond_init(&thread_worker, NULL);
176 threads_starting = nthreads;
177 pthread_attr_init(&thread_attr);
178 gettimeofday(&start, NULL);
180 create_threads(worker, thread_attr);
181 pthread_attr_destroy(&thread_attr);
183 pthread_mutex_lock(&thread_lock);
184 while (threads_starting)
185 pthread_cond_wait(&thread_parent, &thread_lock);
186 pthread_cond_broadcast(&thread_worker);
187 pthread_mutex_unlock(&thread_lock);
189 sleep(nsecs);
190 toggle_done(0, NULL, NULL);
192 for (i = 0; i < nthreads; i++) {
193 ret = pthread_join(worker[i].thread, NULL);
194 if (ret)
195 err(EXIT_FAILURE, "pthread_join");
198 /* cleanup & report results */
199 pthread_cond_destroy(&thread_parent);
200 pthread_cond_destroy(&thread_worker);
201 pthread_mutex_destroy(&thread_lock);
203 for (i = 0; i < nthreads; i++) {
204 unsigned long t = worker[i].ops/runtime.tv_sec;
206 update_stats(&throughput_stats, t);
207 if (!silent)
208 printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
209 worker[i].tid, worker[i].futex, t);
211 if (multi)
212 free(worker[i].futex);
215 print_summary();
217 free(worker);
218 return ret;
219 err:
220 usage_with_options(bench_futex_lock_pi_usage, options);
221 exit(EXIT_FAILURE);