1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
20fb298cfSDavidlohr Bueso /*
30fb298cfSDavidlohr Bueso * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
40fb298cfSDavidlohr Bueso *
50fb298cfSDavidlohr Bueso * futex-requeue: Block a bunch of threads on futex1 and requeue them
60fb298cfSDavidlohr Bueso * on futex2, N at a time.
70fb298cfSDavidlohr Bueso *
80fb298cfSDavidlohr Bueso * This program is particularly useful to measure the latency of nthread
946f81532SDavidlohr Bueso * requeues without waking up any tasks (in the non-pi case) -- thus
1046f81532SDavidlohr Bueso * mimicking a regular futex_wait.
110fb298cfSDavidlohr Bueso */
120fb298cfSDavidlohr Bueso
138a158589SArnaldo Carvalho de Melo /* For the CLR_() macros */
14a0f213e1SArnaldo Carvalho de Melo #include <string.h>
158a158589SArnaldo Carvalho de Melo #include <pthread.h>
168a158589SArnaldo Carvalho de Melo
179c304f6cSArnaldo Carvalho de Melo #include <signal.h>
18a64d3af5SIan Rogers #include "../util/mutex.h"
190fb298cfSDavidlohr Bueso #include "../util/stat.h"
204b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
2186695f59SArnaldo Carvalho de Melo #include <linux/compiler.h>
229c304f6cSArnaldo Carvalho de Melo #include <linux/kernel.h>
23565e6911SArnaldo Carvalho de Melo #include <linux/time64.h>
249c304f6cSArnaldo Carvalho de Melo #include <errno.h>
259c3516d1SJiri Olsa #include <perf/cpumap.h>
260fb298cfSDavidlohr Bueso #include "bench.h"
270fb298cfSDavidlohr Bueso #include "futex.h"
280fb298cfSDavidlohr Bueso
290fb298cfSDavidlohr Bueso #include <err.h>
300fb298cfSDavidlohr Bueso #include <stdlib.h>
310fb298cfSDavidlohr Bueso #include <sys/time.h>
329f9a3ffeSDavidlohr Bueso #include <sys/mman.h>
330fb298cfSDavidlohr Bueso
340fb298cfSDavidlohr Bueso static u_int32_t futex1 = 0, futex2 = 0;
350fb298cfSDavidlohr Bueso
3609590463SDavidlohr Bueso static pthread_t *worker;
3709590463SDavidlohr Bueso static bool done = false;
38a64d3af5SIan Rogers static struct mutex thread_lock;
39a64d3af5SIan Rogers static struct cond thread_parent, thread_worker;
4009590463SDavidlohr Bueso static struct stats requeuetime_stats, requeued_stats;
4109590463SDavidlohr Bueso static unsigned int threads_starting;
4209590463SDavidlohr Bueso static int futex_flag = 0;
4309590463SDavidlohr Bueso
4409590463SDavidlohr Bueso static struct bench_futex_parameters params = {
450fb298cfSDavidlohr Bueso /*
460fb298cfSDavidlohr Bueso * How many tasks to requeue at a time.
470fb298cfSDavidlohr Bueso * Default to 1 in order to make the kernel work more.
480fb298cfSDavidlohr Bueso */
4909590463SDavidlohr Bueso .nrequeue = 1,
5009590463SDavidlohr Bueso };
510fb298cfSDavidlohr Bueso
520fb298cfSDavidlohr Bueso static const struct option options[] = {
5309590463SDavidlohr Bueso OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"),
5409590463SDavidlohr Bueso OPT_UINTEGER('q', "nrequeue", ¶ms.nrequeue, "Specify amount of threads to requeue at once"),
5509590463SDavidlohr Bueso OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"),
5609590463SDavidlohr Bueso OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"),
579f9a3ffeSDavidlohr Bueso OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"),
58d262e6a9SDavidlohr Bueso OPT_BOOLEAN( 'B', "broadcast", ¶ms.broadcast, "Requeue all threads at once"),
5946f81532SDavidlohr Bueso OPT_BOOLEAN( 'p', "pi", ¶ms.pi, "Use PI-aware variants of FUTEX_CMP_REQUEUE"),
6046f81532SDavidlohr Bueso
610fb298cfSDavidlohr Bueso OPT_END()
620fb298cfSDavidlohr Bueso };
630fb298cfSDavidlohr Bueso
640fb298cfSDavidlohr Bueso static const char * const bench_futex_requeue_usage[] = {
650fb298cfSDavidlohr Bueso "perf bench futex requeue <options>",
660fb298cfSDavidlohr Bueso NULL
670fb298cfSDavidlohr Bueso };
680fb298cfSDavidlohr Bueso
print_summary(void)690fb298cfSDavidlohr Bueso static void print_summary(void)
700fb298cfSDavidlohr Bueso {
710fb298cfSDavidlohr Bueso double requeuetime_avg = avg_stats(&requeuetime_stats);
720fb298cfSDavidlohr Bueso double requeuetime_stddev = stddev_stats(&requeuetime_stats);
730fb298cfSDavidlohr Bueso unsigned int requeued_avg = avg_stats(&requeued_stats);
740fb298cfSDavidlohr Bueso
750fb298cfSDavidlohr Bueso printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
760fb298cfSDavidlohr Bueso requeued_avg,
7709590463SDavidlohr Bueso params.nthreads,
78565e6911SArnaldo Carvalho de Melo requeuetime_avg / USEC_PER_MSEC,
790fb298cfSDavidlohr Bueso rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
800fb298cfSDavidlohr Bueso }
810fb298cfSDavidlohr Bueso
workerfn(void * arg __maybe_unused)820fb298cfSDavidlohr Bueso static void *workerfn(void *arg __maybe_unused)
830fb298cfSDavidlohr Bueso {
846f9661b2SDavidlohr Bueso int ret;
856f9661b2SDavidlohr Bueso
86a64d3af5SIan Rogers mutex_lock(&thread_lock);
870fb298cfSDavidlohr Bueso threads_starting--;
880fb298cfSDavidlohr Bueso if (!threads_starting)
89a64d3af5SIan Rogers cond_signal(&thread_parent);
90a64d3af5SIan Rogers cond_wait(&thread_worker, &thread_lock);
91a64d3af5SIan Rogers mutex_unlock(&thread_lock);
920fb298cfSDavidlohr Bueso
936f9661b2SDavidlohr Bueso while (1) {
9446f81532SDavidlohr Bueso if (!params.pi) {
956f9661b2SDavidlohr Bueso ret = futex_wait(&futex1, 0, NULL, futex_flag);
966f9661b2SDavidlohr Bueso if (!ret)
976f9661b2SDavidlohr Bueso break;
986f9661b2SDavidlohr Bueso
996f9661b2SDavidlohr Bueso if (ret && errno != EAGAIN) {
1006f9661b2SDavidlohr Bueso if (!params.silent)
10146f81532SDavidlohr Bueso warnx("futex_wait");
1026f9661b2SDavidlohr Bueso break;
1036f9661b2SDavidlohr Bueso }
10446f81532SDavidlohr Bueso } else {
10546f81532SDavidlohr Bueso ret = futex_wait_requeue_pi(&futex1, 0, &futex2,
10646f81532SDavidlohr Bueso NULL, futex_flag);
10746f81532SDavidlohr Bueso if (!ret) {
10846f81532SDavidlohr Bueso /* got the lock at futex2 */
10946f81532SDavidlohr Bueso futex_unlock_pi(&futex2, futex_flag);
11046f81532SDavidlohr Bueso break;
11146f81532SDavidlohr Bueso }
11246f81532SDavidlohr Bueso
11346f81532SDavidlohr Bueso if (ret && errno != EAGAIN) {
11446f81532SDavidlohr Bueso if (!params.silent)
11546f81532SDavidlohr Bueso warnx("futex_wait_requeue_pi");
11646f81532SDavidlohr Bueso break;
11746f81532SDavidlohr Bueso }
11846f81532SDavidlohr Bueso }
1196f9661b2SDavidlohr Bueso }
1206f9661b2SDavidlohr Bueso
1210fb298cfSDavidlohr Bueso return NULL;
1220fb298cfSDavidlohr Bueso }
1230fb298cfSDavidlohr Bueso
block_threads(pthread_t * w,struct perf_cpu_map * cpu)124*8351498dSIan Rogers static void block_threads(pthread_t *w, struct perf_cpu_map *cpu)
1250fb298cfSDavidlohr Bueso {
126c9c2a427SAthira Rajeev cpu_set_t *cpuset;
1270fb298cfSDavidlohr Bueso unsigned int i;
128c9c2a427SAthira Rajeev int nrcpus = perf_cpu_map__nr(cpu);
129c9c2a427SAthira Rajeev size_t size;
1300fb298cfSDavidlohr Bueso
13109590463SDavidlohr Bueso threads_starting = params.nthreads;
1320fb298cfSDavidlohr Bueso
133c9c2a427SAthira Rajeev cpuset = CPU_ALLOC(nrcpus);
134c9c2a427SAthira Rajeev BUG_ON(!cpuset);
135c9c2a427SAthira Rajeev size = CPU_ALLOC_SIZE(nrcpus);
136c9c2a427SAthira Rajeev
1370fb298cfSDavidlohr Bueso /* create and block all threads */
13809590463SDavidlohr Bueso for (i = 0; i < params.nthreads; i++) {
139*8351498dSIan Rogers pthread_attr_t thread_attr;
140*8351498dSIan Rogers
141*8351498dSIan Rogers pthread_attr_init(&thread_attr);
142c9c2a427SAthira Rajeev CPU_ZERO_S(size, cpuset);
143c9c2a427SAthira Rajeev CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
1440fb298cfSDavidlohr Bueso
145c9c2a427SAthira Rajeev if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
146c9c2a427SAthira Rajeev CPU_FREE(cpuset);
1470fb298cfSDavidlohr Bueso err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
148c9c2a427SAthira Rajeev }
1490fb298cfSDavidlohr Bueso
150c9c2a427SAthira Rajeev if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) {
151c9c2a427SAthira Rajeev CPU_FREE(cpuset);
1520fb298cfSDavidlohr Bueso err(EXIT_FAILURE, "pthread_create");
1530fb298cfSDavidlohr Bueso }
154*8351498dSIan Rogers pthread_attr_destroy(&thread_attr);
1550fb298cfSDavidlohr Bueso }
156c9c2a427SAthira Rajeev CPU_FREE(cpuset);
157c9c2a427SAthira Rajeev }
1580fb298cfSDavidlohr Bueso
toggle_done(int sig __maybe_unused,siginfo_t * info __maybe_unused,void * uc __maybe_unused)1590fb298cfSDavidlohr Bueso static void toggle_done(int sig __maybe_unused,
1600fb298cfSDavidlohr Bueso siginfo_t *info __maybe_unused,
1610fb298cfSDavidlohr Bueso void *uc __maybe_unused)
1620fb298cfSDavidlohr Bueso {
1630fb298cfSDavidlohr Bueso done = true;
1640fb298cfSDavidlohr Bueso }
1650fb298cfSDavidlohr Bueso
bench_futex_requeue(int argc,const char ** argv)166b0ad8ea6SArnaldo Carvalho de Melo int bench_futex_requeue(int argc, const char **argv)
1670fb298cfSDavidlohr Bueso {
1680fb298cfSDavidlohr Bueso int ret = 0;
1690fb298cfSDavidlohr Bueso unsigned int i, j;
1700fb298cfSDavidlohr Bueso struct sigaction act;
171f854839bSJiri Olsa struct perf_cpu_map *cpu;
1720fb298cfSDavidlohr Bueso
1730fb298cfSDavidlohr Bueso argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
1740fb298cfSDavidlohr Bueso if (argc)
1750fb298cfSDavidlohr Bueso goto err;
1760fb298cfSDavidlohr Bueso
1779c3516d1SJiri Olsa cpu = perf_cpu_map__new(NULL);
1783b2323c2SDavidlohr Bueso if (!cpu)
1793b2323c2SDavidlohr Bueso err(EXIT_FAILURE, "cpu_map__new");
1800fb298cfSDavidlohr Bueso
1817b919a53STommi Rantala memset(&act, 0, sizeof(act));
1820fb298cfSDavidlohr Bueso sigfillset(&act.sa_mask);
1830fb298cfSDavidlohr Bueso act.sa_sigaction = toggle_done;
1840fb298cfSDavidlohr Bueso sigaction(SIGINT, &act, NULL);
1850fb298cfSDavidlohr Bueso
1869f9a3ffeSDavidlohr Bueso if (params.mlockall) {
1879f9a3ffeSDavidlohr Bueso if (mlockall(MCL_CURRENT | MCL_FUTURE))
1889f9a3ffeSDavidlohr Bueso err(EXIT_FAILURE, "mlockall");
1899f9a3ffeSDavidlohr Bueso }
1909f9a3ffeSDavidlohr Bueso
19109590463SDavidlohr Bueso if (!params.nthreads)
19244028699SIan Rogers params.nthreads = perf_cpu_map__nr(cpu);
1930fb298cfSDavidlohr Bueso
19409590463SDavidlohr Bueso worker = calloc(params.nthreads, sizeof(*worker));
1950fb298cfSDavidlohr Bueso if (!worker)
1960fb298cfSDavidlohr Bueso err(EXIT_FAILURE, "calloc");
1970fb298cfSDavidlohr Bueso
19809590463SDavidlohr Bueso if (!params.fshared)
19986c87e13SDavidlohr Bueso futex_flag = FUTEX_PRIVATE_FLAG;
20086c87e13SDavidlohr Bueso
20109590463SDavidlohr Bueso if (params.nrequeue > params.nthreads)
20209590463SDavidlohr Bueso params.nrequeue = params.nthreads;
203052b0f6eSDavidlohr Bueso
204d262e6a9SDavidlohr Bueso if (params.broadcast)
205d262e6a9SDavidlohr Bueso params.nrequeue = params.nthreads;
206d262e6a9SDavidlohr Bueso
20746f81532SDavidlohr Bueso printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %s%p), "
20809590463SDavidlohr Bueso "%d at a time.\n\n", getpid(), params.nthreads,
20946f81532SDavidlohr Bueso params.fshared ? "shared":"private", &futex1,
21046f81532SDavidlohr Bueso params.pi ? "PI ": "", &futex2, params.nrequeue);
2110fb298cfSDavidlohr Bueso
2120fb298cfSDavidlohr Bueso init_stats(&requeued_stats);
2130fb298cfSDavidlohr Bueso init_stats(&requeuetime_stats);
214a64d3af5SIan Rogers mutex_init(&thread_lock);
215a64d3af5SIan Rogers cond_init(&thread_parent);
216a64d3af5SIan Rogers cond_init(&thread_worker);
2170fb298cfSDavidlohr Bueso
218d9de84afSDavidlohr Bueso for (j = 0; j < bench_repeat && !done; j++) {
21946f81532SDavidlohr Bueso unsigned int nrequeued = 0, wakeups = 0;
2200fb298cfSDavidlohr Bueso struct timeval start, end, runtime;
2210fb298cfSDavidlohr Bueso
2220fb298cfSDavidlohr Bueso /* create, launch & block all threads */
223*8351498dSIan Rogers block_threads(worker, cpu);
2240fb298cfSDavidlohr Bueso
2250fb298cfSDavidlohr Bueso /* make sure all threads are already blocked */
226a64d3af5SIan Rogers mutex_lock(&thread_lock);
2270fb298cfSDavidlohr Bueso while (threads_starting)
228a64d3af5SIan Rogers cond_wait(&thread_parent, &thread_lock);
229a64d3af5SIan Rogers cond_broadcast(&thread_worker);
230a64d3af5SIan Rogers mutex_unlock(&thread_lock);
2310fb298cfSDavidlohr Bueso
2320fb298cfSDavidlohr Bueso usleep(100000);
2330fb298cfSDavidlohr Bueso
2340fb298cfSDavidlohr Bueso /* Ok, all threads are patiently blocked, start requeueing */
2350fb298cfSDavidlohr Bueso gettimeofday(&start, NULL);
23609590463SDavidlohr Bueso while (nrequeued < params.nthreads) {
23746f81532SDavidlohr Bueso int r;
23846f81532SDavidlohr Bueso
2390fb298cfSDavidlohr Bueso /*
24046f81532SDavidlohr Bueso * For the regular non-pi case, do not wakeup any tasks
24146f81532SDavidlohr Bueso * blocked on futex1, allowing us to really measure
24246f81532SDavidlohr Bueso * futex_wait functionality. For the PI case the first
24346f81532SDavidlohr Bueso * waiter is always awoken.
2440fb298cfSDavidlohr Bueso */
24546f81532SDavidlohr Bueso if (!params.pi) {
24646f81532SDavidlohr Bueso r = futex_cmp_requeue(&futex1, 0, &futex2, 0,
24709590463SDavidlohr Bueso params.nrequeue,
24809590463SDavidlohr Bueso futex_flag);
24946f81532SDavidlohr Bueso } else {
25046f81532SDavidlohr Bueso r = futex_cmp_requeue_pi(&futex1, 0, &futex2,
25146f81532SDavidlohr Bueso params.nrequeue,
25246f81532SDavidlohr Bueso futex_flag);
25346f81532SDavidlohr Bueso wakeups++; /* assume no error */
25446f81532SDavidlohr Bueso }
25546f81532SDavidlohr Bueso
25646f81532SDavidlohr Bueso if (r < 0)
25746f81532SDavidlohr Bueso err(EXIT_FAILURE, "couldn't requeue from %p to %p",
25846f81532SDavidlohr Bueso &futex1, &futex2);
25946f81532SDavidlohr Bueso
26046f81532SDavidlohr Bueso nrequeued += r;
26186c87e13SDavidlohr Bueso }
262052b0f6eSDavidlohr Bueso
2630fb298cfSDavidlohr Bueso gettimeofday(&end, NULL);
2640fb298cfSDavidlohr Bueso timersub(&end, &start, &runtime);
2650fb298cfSDavidlohr Bueso
2660fb298cfSDavidlohr Bueso update_stats(&requeued_stats, nrequeued);
2670fb298cfSDavidlohr Bueso update_stats(&requeuetime_stats, runtime.tv_usec);
2680fb298cfSDavidlohr Bueso
26909590463SDavidlohr Bueso if (!params.silent) {
27046f81532SDavidlohr Bueso if (!params.pi)
27146f81532SDavidlohr Bueso printf("[Run %d]: Requeued %d of %d threads in "
27246f81532SDavidlohr Bueso "%.4f ms\n", j + 1, nrequeued,
27346f81532SDavidlohr Bueso params.nthreads,
27446f81532SDavidlohr Bueso runtime.tv_usec / (double)USEC_PER_MSEC);
27546f81532SDavidlohr Bueso else {
27646f81532SDavidlohr Bueso nrequeued -= wakeups;
27746f81532SDavidlohr Bueso printf("[Run %d]: Awoke and Requeued (%d+%d) of "
27846f81532SDavidlohr Bueso "%d threads in %.4f ms\n",
27946f81532SDavidlohr Bueso j + 1, wakeups, nrequeued,
28046f81532SDavidlohr Bueso params.nthreads,
28109590463SDavidlohr Bueso runtime.tv_usec / (double)USEC_PER_MSEC);
2820fb298cfSDavidlohr Bueso }
2830fb298cfSDavidlohr Bueso
28446f81532SDavidlohr Bueso }
28546f81532SDavidlohr Bueso
28646f81532SDavidlohr Bueso if (!params.pi) {
2870fb298cfSDavidlohr Bueso /* everybody should be blocked on futex2, wake'em up */
288052b0f6eSDavidlohr Bueso nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
28909590463SDavidlohr Bueso if (params.nthreads != nrequeued)
29009590463SDavidlohr Bueso warnx("couldn't wakeup all tasks (%d/%d)",
29109590463SDavidlohr Bueso nrequeued, params.nthreads);
29246f81532SDavidlohr Bueso }
2930fb298cfSDavidlohr Bueso
29409590463SDavidlohr Bueso for (i = 0; i < params.nthreads; i++) {
2950fb298cfSDavidlohr Bueso ret = pthread_join(worker[i], NULL);
2960fb298cfSDavidlohr Bueso if (ret)
2970fb298cfSDavidlohr Bueso err(EXIT_FAILURE, "pthread_join");
2980fb298cfSDavidlohr Bueso }
2990fb298cfSDavidlohr Bueso }
3000fb298cfSDavidlohr Bueso
3010fb298cfSDavidlohr Bueso /* cleanup & report results */
302a64d3af5SIan Rogers cond_destroy(&thread_parent);
303a64d3af5SIan Rogers cond_destroy(&thread_worker);
304a64d3af5SIan Rogers mutex_destroy(&thread_lock);
3050fb298cfSDavidlohr Bueso
3060fb298cfSDavidlohr Bueso print_summary();
3070fb298cfSDavidlohr Bueso
3080fb298cfSDavidlohr Bueso free(worker);
30988e48238SSohaib Mohamed perf_cpu_map__put(cpu);
3100fb298cfSDavidlohr Bueso return ret;
3110fb298cfSDavidlohr Bueso err:
3120fb298cfSDavidlohr Bueso usage_with_options(bench_futex_requeue_usage, options);
3130fb298cfSDavidlohr Bueso exit(EXIT_FAILURE);
3140fb298cfSDavidlohr Bueso }
315