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