2 * Copyright (C) 2015 Davidlohr Bueso.
6 #include "../util/util.h"
7 #include "../util/stat.h"
8 #include "../util/parse-options.h"
9 #include "../util/header.h"
25 static u_int32_t global_futex = 0;
26 static struct worker *worker;
27 static unsigned int nsecs = 10;
28 static bool silent = false, multi = false;
29 static bool done = false, fshared = false;
30 static unsigned int ncpus, nthreads = 0;
31 static int futex_flag = 0;
32 struct timeval start, end, runtime;
33 static pthread_mutex_t thread_lock;
34 static unsigned int threads_starting;
35 static struct stats throughput_stats;
36 static pthread_cond_t thread_parent, thread_worker;
38 static const struct option options[] = {
39 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
40 OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
41 OPT_BOOLEAN( 'M', "multi", &multi, "Use multiple futexes"),
42 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
43 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
47 static const char * const bench_futex_lock_pi_usage[] = {
48 "perf bench futex requeue <options>",
52 static void print_summary(void)
54 unsigned long avg = avg_stats(&throughput_stats);
55 double stddev = stddev_stats(&throughput_stats);
57 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
58 !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
59 (int) runtime.tv_sec);
62 static void toggle_done(int sig __maybe_unused,
63 siginfo_t *info __maybe_unused,
64 void *uc __maybe_unused)
66 /* inform all threads that we're done for the day */
68 gettimeofday(&end, NULL);
69 timersub(&end, &start, &runtime);
72 static void *workerfn(void *arg)
74 struct worker *w = (struct worker *) arg;
76 pthread_mutex_lock(&thread_lock);
78 if (!threads_starting)
79 pthread_cond_signal(&thread_parent);
80 pthread_cond_wait(&thread_worker, &thread_lock);
81 pthread_mutex_unlock(&thread_lock);
86 ret = futex_lock_pi(w->futex, NULL, 0, futex_flag);
88 if (ret) { /* handle lock acquisition */
90 warn("thread %d: Could not lock pi-lock for %p (%d)",
91 w->tid, w->futex, ret);
99 ret = futex_unlock_pi(w->futex, futex_flag);
101 warn("thread %d: Could not unlock pi-lock for %p (%d)",
102 w->tid, w->futex, ret);
103 w->ops++; /* account for thread's share of work */
109 static void create_threads(struct worker *w, pthread_attr_t thread_attr)
114 threads_starting = nthreads;
116 for (i = 0; i < nthreads; i++) {
120 worker[i].futex = calloc(1, sizeof(u_int32_t));
121 if (!worker[i].futex)
122 err(EXIT_FAILURE, "calloc");
124 worker[i].futex = &global_futex;
127 CPU_SET(i % ncpus, &cpu);
129 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
130 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
132 if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i]))
133 err(EXIT_FAILURE, "pthread_create");
137 int bench_futex_lock_pi(int argc, const char **argv,
138 const char *prefix __maybe_unused)
142 struct sigaction act;
143 pthread_attr_t thread_attr;
145 argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0);
149 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
151 sigfillset(&act.sa_mask);
152 act.sa_sigaction = toggle_done;
153 sigaction(SIGINT, &act, NULL);
158 worker = calloc(nthreads, sizeof(*worker));
160 err(EXIT_FAILURE, "calloc");
163 futex_flag = FUTEX_PRIVATE_FLAG;
165 printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
166 getpid(), nthreads, nsecs);
168 init_stats(&throughput_stats);
169 pthread_mutex_init(&thread_lock, NULL);
170 pthread_cond_init(&thread_parent, NULL);
171 pthread_cond_init(&thread_worker, NULL);
173 threads_starting = nthreads;
174 pthread_attr_init(&thread_attr);
175 gettimeofday(&start, NULL);
177 create_threads(worker, thread_attr);
178 pthread_attr_destroy(&thread_attr);
180 pthread_mutex_lock(&thread_lock);
181 while (threads_starting)
182 pthread_cond_wait(&thread_parent, &thread_lock);
183 pthread_cond_broadcast(&thread_worker);
184 pthread_mutex_unlock(&thread_lock);
187 toggle_done(0, NULL, NULL);
189 for (i = 0; i < nthreads; i++) {
190 ret = pthread_join(worker[i].thread, NULL);
192 err(EXIT_FAILURE, "pthread_join");
195 /* cleanup & report results */
196 pthread_cond_destroy(&thread_parent);
197 pthread_cond_destroy(&thread_worker);
198 pthread_mutex_destroy(&thread_lock);
200 for (i = 0; i < nthreads; i++) {
201 unsigned long t = worker[i].ops/runtime.tv_sec;
203 update_stats(&throughput_stats, t);
205 printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
206 worker[i].tid, worker[i].futex, t);
209 free(worker[i].futex);
217 usage_with_options(bench_futex_lock_pi_usage, options);