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