futex-wake.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
  4. *
  5. * futex-wake: Block a bunch of threads on a futex and wake'em up, N at a time.
  6. *
  7. * This program is particularly useful to measure the latency of nthread wakeups
  8. * in non-error situations: all waiters are queued and all wake calls wakeup
  9. * one or more tasks, and thus the waitqueue is never empty.
  10. */
  11. /* For the CLR_() macros */
  12. #include <string.h>
  13. #include <pthread.h>
  14. #include <signal.h>
  15. #include "../util/stat.h"
  16. #include <subcmd/parse-options.h>
  17. #include <linux/compiler.h>
  18. #include <linux/kernel.h>
  19. #include <linux/time64.h>
  20. #include <errno.h>
  21. #include "bench.h"
  22. #include "futex.h"
  23. #include "cpumap.h"
  24. #include <err.h>
  25. #include <stdlib.h>
  26. #include <sys/time.h>
  27. /* all threads will block on the same futex */
  28. static u_int32_t futex1 = 0;
  29. /*
  30. * How many wakeups to do at a time.
  31. * Default to 1 in order to make the kernel work more.
  32. */
  33. static unsigned int nwakes = 1;
  34. pthread_t *worker;
  35. static bool done = false, silent = false, fshared = false;
  36. static pthread_mutex_t thread_lock;
  37. static pthread_cond_t thread_parent, thread_worker;
  38. static struct stats waketime_stats, wakeup_stats;
  39. static unsigned int threads_starting, nthreads = 0;
  40. static int futex_flag = 0;
  41. static const struct option options[] = {
  42. OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
  43. OPT_UINTEGER('w', "nwakes", &nwakes, "Specify amount of threads to wake at once"),
  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"),
  46. OPT_END()
  47. };
  48. static const char * const bench_futex_wake_usage[] = {
  49. "perf bench futex wake <options>",
  50. NULL
  51. };
  52. static void *workerfn(void *arg __maybe_unused)
  53. {
  54. pthread_mutex_lock(&thread_lock);
  55. threads_starting--;
  56. if (!threads_starting)
  57. pthread_cond_signal(&thread_parent);
  58. pthread_cond_wait(&thread_worker, &thread_lock);
  59. pthread_mutex_unlock(&thread_lock);
  60. while (1) {
  61. if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
  62. break;
  63. }
  64. pthread_exit(NULL);
  65. return NULL;
  66. }
  67. static void print_summary(void)
  68. {
  69. double waketime_avg = avg_stats(&waketime_stats);
  70. double waketime_stddev = stddev_stats(&waketime_stats);
  71. unsigned int wakeup_avg = avg_stats(&wakeup_stats);
  72. printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
  73. wakeup_avg,
  74. nthreads,
  75. waketime_avg / USEC_PER_MSEC,
  76. rel_stddev_stats(waketime_stddev, waketime_avg));
  77. }
  78. static void block_threads(pthread_t *w,
  79. pthread_attr_t thread_attr, struct cpu_map *cpu)
  80. {
  81. cpu_set_t cpuset;
  82. unsigned int i;
  83. threads_starting = nthreads;
  84. /* create and block all threads */
  85. for (i = 0; i < nthreads; i++) {
  86. CPU_ZERO(&cpuset);
  87. CPU_SET(cpu->map[i % cpu->nr], &cpuset);
  88. if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
  89. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  90. if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
  91. err(EXIT_FAILURE, "pthread_create");
  92. }
  93. }
  94. static void toggle_done(int sig __maybe_unused,
  95. siginfo_t *info __maybe_unused,
  96. void *uc __maybe_unused)
  97. {
  98. done = true;
  99. }
  100. int bench_futex_wake(int argc, const char **argv)
  101. {
  102. int ret = 0;
  103. unsigned int i, j;
  104. struct sigaction act;
  105. pthread_attr_t thread_attr;
  106. struct cpu_map *cpu;
  107. argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
  108. if (argc) {
  109. usage_with_options(bench_futex_wake_usage, options);
  110. exit(EXIT_FAILURE);
  111. }
  112. cpu = cpu_map__new(NULL);
  113. if (!cpu)
  114. err(EXIT_FAILURE, "calloc");
  115. sigfillset(&act.sa_mask);
  116. act.sa_sigaction = toggle_done;
  117. sigaction(SIGINT, &act, NULL);
  118. if (!nthreads)
  119. nthreads = cpu->nr;
  120. worker = calloc(nthreads, sizeof(*worker));
  121. if (!worker)
  122. err(EXIT_FAILURE, "calloc");
  123. if (!fshared)
  124. futex_flag = FUTEX_PRIVATE_FLAG;
  125. printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
  126. "waking up %d at a time.\n\n",
  127. getpid(), nthreads, fshared ? "shared":"private", &futex1, nwakes);
  128. init_stats(&wakeup_stats);
  129. init_stats(&waketime_stats);
  130. pthread_attr_init(&thread_attr);
  131. pthread_mutex_init(&thread_lock, NULL);
  132. pthread_cond_init(&thread_parent, NULL);
  133. pthread_cond_init(&thread_worker, NULL);
  134. for (j = 0; j < bench_repeat && !done; j++) {
  135. unsigned int nwoken = 0;
  136. struct timeval start, end, runtime;
  137. /* create, launch & block all threads */
  138. block_threads(worker, thread_attr, cpu);
  139. /* make sure all threads are already blocked */
  140. pthread_mutex_lock(&thread_lock);
  141. while (threads_starting)
  142. pthread_cond_wait(&thread_parent, &thread_lock);
  143. pthread_cond_broadcast(&thread_worker);
  144. pthread_mutex_unlock(&thread_lock);
  145. usleep(100000);
  146. /* Ok, all threads are patiently blocked, start waking folks up */
  147. gettimeofday(&start, NULL);
  148. while (nwoken != nthreads)
  149. nwoken += futex_wake(&futex1, nwakes, futex_flag);
  150. gettimeofday(&end, NULL);
  151. timersub(&end, &start, &runtime);
  152. update_stats(&wakeup_stats, nwoken);
  153. update_stats(&waketime_stats, runtime.tv_usec);
  154. if (!silent) {
  155. printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
  156. j + 1, nwoken, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
  157. }
  158. for (i = 0; i < nthreads; i++) {
  159. ret = pthread_join(worker[i], NULL);
  160. if (ret)
  161. err(EXIT_FAILURE, "pthread_join");
  162. }
  163. }
  164. /* cleanup & report results */
  165. pthread_cond_destroy(&thread_parent);
  166. pthread_cond_destroy(&thread_worker);
  167. pthread_mutex_destroy(&thread_lock);
  168. pthread_attr_destroy(&thread_attr);
  169. print_summary();
  170. free(worker);
  171. return ret;
  172. }