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 requeue <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
;
79 pthread_mutex_lock(&thread_lock
);
81 if (!threads_starting
)
82 pthread_cond_signal(&thread_parent
);
83 pthread_cond_wait(&thread_worker
, &thread_lock
);
84 pthread_mutex_unlock(&thread_lock
);
89 ret
= futex_lock_pi(w
->futex
, NULL
, futex_flag
);
91 if (ret
) { /* handle lock acquisition */
93 warn("thread %d: Could not lock pi-lock for %p (%d)",
94 w
->tid
, w
->futex
, ret
);
102 ret
= futex_unlock_pi(w
->futex
, futex_flag
);
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 */
112 static void create_threads(struct worker
*w
, pthread_attr_t thread_attr
)
117 threads_starting
= nthreads
;
119 for (i
= 0; i
< nthreads
; i
++) {
123 worker
[i
].futex
= calloc(1, sizeof(u_int32_t
));
124 if (!worker
[i
].futex
)
125 err(EXIT_FAILURE
, "calloc");
127 worker
[i
].futex
= &global_futex
;
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
)
145 struct sigaction act
;
146 pthread_attr_t thread_attr
;
148 argc
= parse_options(argc
, argv
, options
, bench_futex_lock_pi_usage
, 0);
152 ncpus
= sysconf(_SC_NPROCESSORS_ONLN
);
154 sigfillset(&act
.sa_mask
);
155 act
.sa_sigaction
= toggle_done
;
156 sigaction(SIGINT
, &act
, NULL
);
161 worker
= calloc(nthreads
, sizeof(*worker
));
163 err(EXIT_FAILURE
, "calloc");
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
);
190 toggle_done(0, NULL
, NULL
);
192 for (i
= 0; i
< nthreads
; i
++) {
193 ret
= pthread_join(worker
[i
].thread
, NULL
);
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
);
208 printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
209 worker
[i
].tid
, worker
[i
].futex
, t
);
212 free(worker
[i
].futex
);
220 usage_with_options(bench_futex_lock_pi_usage
, options
);