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/kernel.h>
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
;
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"),
49 static const char * const bench_futex_lock_pi_usage
[] = {
50 "perf bench futex requeue <options>",
54 static void print_summary(void)
56 unsigned long avg
= avg_stats(&throughput_stats
);
57 double stddev
= stddev_stats(&throughput_stats
);
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
);
64 static void toggle_done(int sig __maybe_unused
,
65 siginfo_t
*info __maybe_unused
,
66 void *uc __maybe_unused
)
68 /* inform all threads that we're done for the day */
70 gettimeofday(&end
, NULL
);
71 timersub(&end
, &start
, &runtime
);
74 static void *workerfn(void *arg
)
76 struct worker
*w
= (struct worker
*) arg
;
78 pthread_mutex_lock(&thread_lock
);
80 if (!threads_starting
)
81 pthread_cond_signal(&thread_parent
);
82 pthread_cond_wait(&thread_worker
, &thread_lock
);
83 pthread_mutex_unlock(&thread_lock
);
88 ret
= futex_lock_pi(w
->futex
, NULL
, futex_flag
);
90 if (ret
) { /* handle lock acquisition */
92 warn("thread %d: Could not lock pi-lock for %p (%d)",
93 w
->tid
, w
->futex
, ret
);
101 ret
= futex_unlock_pi(w
->futex
, futex_flag
);
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 */
111 static void create_threads(struct worker
*w
, pthread_attr_t thread_attr
)
116 threads_starting
= nthreads
;
118 for (i
= 0; i
< nthreads
; i
++) {
122 worker
[i
].futex
= calloc(1, sizeof(u_int32_t
));
123 if (!worker
[i
].futex
)
124 err(EXIT_FAILURE
, "calloc");
126 worker
[i
].futex
= &global_futex
;
129 CPU_SET(i
% ncpus
, &cpu
);
131 if (pthread_attr_setaffinity_np(&thread_attr
, sizeof(cpu_set_t
), &cpu
))
132 err(EXIT_FAILURE
, "pthread_attr_setaffinity_np");
134 if (pthread_create(&w
[i
].thread
, &thread_attr
, workerfn
, &worker
[i
]))
135 err(EXIT_FAILURE
, "pthread_create");
139 int bench_futex_lock_pi(int argc
, const char **argv
,
140 const char *prefix __maybe_unused
)
144 struct sigaction act
;
145 pthread_attr_t thread_attr
;
147 argc
= parse_options(argc
, argv
, options
, bench_futex_lock_pi_usage
, 0);
151 ncpus
= sysconf(_SC_NPROCESSORS_ONLN
);
153 sigfillset(&act
.sa_mask
);
154 act
.sa_sigaction
= toggle_done
;
155 sigaction(SIGINT
, &act
, NULL
);
160 worker
= calloc(nthreads
, sizeof(*worker
));
162 err(EXIT_FAILURE
, "calloc");
165 futex_flag
= FUTEX_PRIVATE_FLAG
;
167 printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
168 getpid(), nthreads
, nsecs
);
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
);
175 threads_starting
= nthreads
;
176 pthread_attr_init(&thread_attr
);
177 gettimeofday(&start
, NULL
);
179 create_threads(worker
, thread_attr
);
180 pthread_attr_destroy(&thread_attr
);
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
);
189 toggle_done(0, NULL
, NULL
);
191 for (i
= 0; i
< nthreads
; i
++) {
192 ret
= pthread_join(worker
[i
].thread
, NULL
);
194 err(EXIT_FAILURE
, "pthread_join");
197 /* cleanup & report results */
198 pthread_cond_destroy(&thread_parent
);
199 pthread_cond_destroy(&thread_worker
);
200 pthread_mutex_destroy(&thread_lock
);
202 for (i
= 0; i
< nthreads
; i
++) {
203 unsigned long t
= worker
[i
].ops
/runtime
.tv_sec
;
205 update_stats(&throughput_stats
, t
);
207 printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
208 worker
[i
].tid
, worker
[i
].futex
, t
);
211 free(worker
[i
].futex
);
219 usage_with_options(bench_futex_lock_pi_usage
, options
);