2 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
4 * futex-requeue: Block a bunch of threads on futex1 and requeue them
5 * on futex2, N at a time.
7 * This program is particularly useful to measure the latency of nthread
8 * requeues without waking up any tasks -- thus mimicking a regular futex_wait.
12 #include "../util/util.h"
13 #include "../util/stat.h"
14 #include "../util/parse-options.h"
15 #include "../util/header.h"
24 static u_int32_t futex1 = 0, futex2 = 0;
27 * How many tasks to requeue at a time.
28 * Default to 1 in order to make the kernel work more.
30 static unsigned int nrequeue = 1;
33 * There can be significant variance from run to run,
34 * the more repeats, the more exact the overall avg and
35 * the better idea of the futex latency.
37 static unsigned int repeat = 10;
39 static pthread_t *worker;
40 static bool done = 0, silent = 0;
41 static pthread_mutex_t thread_lock;
42 static pthread_cond_t thread_parent, thread_worker;
43 static struct stats requeuetime_stats, requeued_stats;
44 static unsigned int ncpus, threads_starting, nthreads = 0;
46 static const struct option options[] = {
47 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
48 OPT_UINTEGER('q', "nrequeue", &nrequeue, "Specify amount of threads to requeue at once"),
49 OPT_UINTEGER('r', "repeat", &repeat, "Specify amount of times to repeat the run"),
50 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
54 static const char * const bench_futex_requeue_usage[] = {
55 "perf bench futex requeue <options>",
59 static void print_summary(void)
61 double requeuetime_avg = avg_stats(&requeuetime_stats);
62 double requeuetime_stddev = stddev_stats(&requeuetime_stats);
63 unsigned int requeued_avg = avg_stats(&requeued_stats);
65 printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
69 rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
72 static void *workerfn(void *arg __maybe_unused)
74 pthread_mutex_lock(&thread_lock);
76 if (!threads_starting)
77 pthread_cond_signal(&thread_parent);
78 pthread_cond_wait(&thread_worker, &thread_lock);
79 pthread_mutex_unlock(&thread_lock);
81 futex_wait(&futex1, 0, NULL, FUTEX_PRIVATE_FLAG);
85 static void block_threads(pthread_t *w,
86 pthread_attr_t thread_attr)
91 threads_starting = nthreads;
93 /* create and block all threads */
94 for (i = 0; i < nthreads; i++) {
96 CPU_SET(i % ncpus, &cpu);
98 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
99 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
101 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
102 err(EXIT_FAILURE, "pthread_create");
106 static void toggle_done(int sig __maybe_unused,
107 siginfo_t *info __maybe_unused,
108 void *uc __maybe_unused)
113 int bench_futex_requeue(int argc, const char **argv,
114 const char *prefix __maybe_unused)
118 struct sigaction act;
119 pthread_attr_t thread_attr;
121 argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
125 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
127 sigfillset(&act.sa_mask);
128 act.sa_sigaction = toggle_done;
129 sigaction(SIGINT, &act, NULL);
134 worker = calloc(nthreads, sizeof(*worker));
136 err(EXIT_FAILURE, "calloc");
138 printf("Run summary [PID %d]: Requeuing %d threads (from %p to %p), "
140 getpid(), nthreads, &futex1, &futex2, nrequeue);
142 init_stats(&requeued_stats);
143 init_stats(&requeuetime_stats);
144 pthread_attr_init(&thread_attr);
145 pthread_mutex_init(&thread_lock, NULL);
146 pthread_cond_init(&thread_parent, NULL);
147 pthread_cond_init(&thread_worker, NULL);
149 for (j = 0; j < repeat && !done; j++) {
150 unsigned int nrequeued = 0;
151 struct timeval start, end, runtime;
153 /* create, launch & block all threads */
154 block_threads(worker, thread_attr);
156 /* make sure all threads are already blocked */
157 pthread_mutex_lock(&thread_lock);
158 while (threads_starting)
159 pthread_cond_wait(&thread_parent, &thread_lock);
160 pthread_cond_broadcast(&thread_worker);
161 pthread_mutex_unlock(&thread_lock);
165 /* Ok, all threads are patiently blocked, start requeueing */
166 gettimeofday(&start, NULL);
167 for (nrequeued = 0; nrequeued < nthreads; nrequeued += nrequeue)
169 * Do not wakeup any tasks blocked on futex1, allowing
170 * us to really measure futex_wait functionality.
172 futex_cmp_requeue(&futex1, 0, &futex2, 0, nrequeue,
174 gettimeofday(&end, NULL);
175 timersub(&end, &start, &runtime);
177 update_stats(&requeued_stats, nrequeued);
178 update_stats(&requeuetime_stats, runtime.tv_usec);
181 printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
182 j + 1, nrequeued, nthreads, runtime.tv_usec/1e3);
185 /* everybody should be blocked on futex2, wake'em up */
186 nrequeued = futex_wake(&futex2, nthreads, FUTEX_PRIVATE_FLAG);
187 if (nthreads != nrequeued)
188 warnx("couldn't wakeup all tasks (%d/%d)", nrequeued, nthreads);
190 for (i = 0; i < nthreads; i++) {
191 ret = pthread_join(worker[i], NULL);
193 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);
202 pthread_attr_destroy(&thread_attr);
209 usage_with_options(bench_futex_requeue_usage, options);