2 * Copyright (C) 2015 Davidlohr Bueso.
5 /* For the CLR_() macros */
9 #include "../util/stat.h"
10 #include <subcmd/parse-options.h>
11 #include <linux/compiler.h>
12 #include <linux/kernel.h>
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"),
50 static const char * const bench_futex_lock_pi_usage
[] = {
51 "perf bench futex lock-pi <options>",
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 */
71 gettimeofday(&end
, NULL
);
72 timersub(&end
, &start
, &runtime
);
75 static void *workerfn(void *arg
)
77 struct worker
*w
= (struct worker
*) arg
;
78 unsigned long ops
= w
->ops
;
80 pthread_mutex_lock(&thread_lock
);
82 if (!threads_starting
)
83 pthread_cond_signal(&thread_parent
);
84 pthread_cond_wait(&thread_worker
, &thread_lock
);
85 pthread_mutex_unlock(&thread_lock
);
90 ret
= futex_lock_pi(w
->futex
, NULL
, futex_flag
);
92 if (ret
) { /* handle lock acquisition */
94 warn("thread %d: Could not lock pi-lock for %p (%d)",
95 w
->tid
, w
->futex
, ret
);
103 ret
= futex_unlock_pi(w
->futex
, futex_flag
);
105 warn("thread %d: Could not unlock pi-lock for %p (%d)",
106 w
->tid
, w
->futex
, ret
);
107 ops
++; /* account for thread's share of work */
114 static void create_threads(struct worker
*w
, pthread_attr_t thread_attr
)
119 threads_starting
= nthreads
;
121 for (i
= 0; i
< nthreads
; i
++) {
125 worker
[i
].futex
= calloc(1, sizeof(u_int32_t
));
126 if (!worker
[i
].futex
)
127 err(EXIT_FAILURE
, "calloc");
129 worker
[i
].futex
= &global_futex
;
132 CPU_SET(i
% ncpus
, &cpu
);
134 if (pthread_attr_setaffinity_np(&thread_attr
, sizeof(cpu_set_t
), &cpu
))
135 err(EXIT_FAILURE
, "pthread_attr_setaffinity_np");
137 if (pthread_create(&w
[i
].thread
, &thread_attr
, workerfn
, &worker
[i
]))
138 err(EXIT_FAILURE
, "pthread_create");
142 int bench_futex_lock_pi(int argc
, const char **argv
,
143 const char *prefix __maybe_unused
)
147 struct sigaction act
;
148 pthread_attr_t thread_attr
;
150 argc
= parse_options(argc
, argv
, options
, bench_futex_lock_pi_usage
, 0);
154 ncpus
= sysconf(_SC_NPROCESSORS_ONLN
);
156 sigfillset(&act
.sa_mask
);
157 act
.sa_sigaction
= toggle_done
;
158 sigaction(SIGINT
, &act
, NULL
);
163 worker
= calloc(nthreads
, sizeof(*worker
));
165 err(EXIT_FAILURE
, "calloc");
168 futex_flag
= FUTEX_PRIVATE_FLAG
;
170 printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
171 getpid(), nthreads
, nsecs
);
173 init_stats(&throughput_stats
);
174 pthread_mutex_init(&thread_lock
, NULL
);
175 pthread_cond_init(&thread_parent
, NULL
);
176 pthread_cond_init(&thread_worker
, NULL
);
178 threads_starting
= nthreads
;
179 pthread_attr_init(&thread_attr
);
180 gettimeofday(&start
, NULL
);
182 create_threads(worker
, thread_attr
);
183 pthread_attr_destroy(&thread_attr
);
185 pthread_mutex_lock(&thread_lock
);
186 while (threads_starting
)
187 pthread_cond_wait(&thread_parent
, &thread_lock
);
188 pthread_cond_broadcast(&thread_worker
);
189 pthread_mutex_unlock(&thread_lock
);
192 toggle_done(0, NULL
, NULL
);
194 for (i
= 0; i
< nthreads
; i
++) {
195 ret
= pthread_join(worker
[i
].thread
, NULL
);
197 err(EXIT_FAILURE
, "pthread_join");
200 /* cleanup & report results */
201 pthread_cond_destroy(&thread_parent
);
202 pthread_cond_destroy(&thread_worker
);
203 pthread_mutex_destroy(&thread_lock
);
205 for (i
= 0; i
< nthreads
; i
++) {
206 unsigned long t
= worker
[i
].ops
/runtime
.tv_sec
;
208 update_stats(&throughput_stats
, t
);
210 printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
211 worker
[i
].tid
, worker
[i
].futex
, t
);
214 free(worker
[i
].futex
);
222 usage_with_options(bench_futex_lock_pi_usage
, options
);