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