futex-wake.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/mutex.h"
  16. #include "../util/stat.h"
  17. #include <subcmd/parse-options.h>
  18. #include <linux/compiler.h>
  19. #include <linux/kernel.h>
  20. #include <linux/time64.h>
  21. #include <errno.h>
  22. #include <perf/cpumap.h>
  23. #include "bench.h"
  24. #include "futex.h"
  25. #include <err.h>
  26. #include <stdlib.h>
  27. #include <sys/time.h>
  28. #include <sys/mman.h>
  29. /* all threads will block on the same futex */
  30. static u_int32_t futex1 = 0;
  31. static pthread_t *worker;
  32. static bool done = false;
  33. static struct mutex thread_lock;
  34. static struct cond thread_parent, thread_worker;
  35. static struct stats waketime_stats, wakeup_stats;
  36. static unsigned int threads_starting;
  37. static int futex_flag = 0;
  38. static struct bench_futex_parameters params = {
  39. /*
  40. * How many wakeups to do at a time.
  41. * Default to 1 in order to make the kernel work more.
  42. */
  43. .nwakes = 1,
  44. };
  45. static const struct option options[] = {
  46. OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
  47. OPT_UINTEGER('w', "nwakes", &params.nwakes, "Specify amount of threads to wake at once"),
  48. OPT_BOOLEAN( 's', "silent", &params.silent, "Silent mode: do not display data/details"),
  49. OPT_BOOLEAN( 'S', "shared", &params.fshared, "Use shared futexes instead of private ones"),
  50. OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
  51. OPT_END()
  52. };
  53. static const char * const bench_futex_wake_usage[] = {
  54. "perf bench futex wake <options>",
  55. NULL
  56. };
  57. static void *workerfn(void *arg __maybe_unused)
  58. {
  59. mutex_lock(&thread_lock);
  60. threads_starting--;
  61. if (!threads_starting)
  62. cond_signal(&thread_parent);
  63. cond_wait(&thread_worker, &thread_lock);
  64. mutex_unlock(&thread_lock);
  65. while (1) {
  66. if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
  67. break;
  68. }
  69. pthread_exit(NULL);
  70. return NULL;
  71. }
  72. static void print_summary(void)
  73. {
  74. double waketime_avg = avg_stats(&waketime_stats);
  75. double waketime_stddev = stddev_stats(&waketime_stats);
  76. unsigned int wakeup_avg = avg_stats(&wakeup_stats);
  77. printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
  78. wakeup_avg,
  79. params.nthreads,
  80. waketime_avg / USEC_PER_MSEC,
  81. rel_stddev_stats(waketime_stddev, waketime_avg));
  82. }
  83. static void block_threads(pthread_t *w, struct perf_cpu_map *cpu)
  84. {
  85. cpu_set_t *cpuset;
  86. unsigned int i;
  87. size_t size;
  88. int nrcpus = cpu__max_cpu().cpu;
  89. threads_starting = params.nthreads;
  90. cpuset = CPU_ALLOC(nrcpus);
  91. BUG_ON(!cpuset);
  92. size = CPU_ALLOC_SIZE(nrcpus);
  93. /* create and block all threads */
  94. for (i = 0; i < params.nthreads; i++) {
  95. pthread_attr_t thread_attr;
  96. pthread_attr_init(&thread_attr);
  97. CPU_ZERO_S(size, cpuset);
  98. CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
  99. if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
  100. CPU_FREE(cpuset);
  101. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  102. }
  103. if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) {
  104. CPU_FREE(cpuset);
  105. err(EXIT_FAILURE, "pthread_create");
  106. }
  107. pthread_attr_destroy(&thread_attr);
  108. }
  109. CPU_FREE(cpuset);
  110. }
  111. static void toggle_done(int sig __maybe_unused,
  112. siginfo_t *info __maybe_unused,
  113. void *uc __maybe_unused)
  114. {
  115. done = true;
  116. }
  117. int bench_futex_wake(int argc, const char **argv)
  118. {
  119. int ret = 0;
  120. unsigned int i, j;
  121. struct sigaction act;
  122. struct perf_cpu_map *cpu;
  123. argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
  124. if (argc) {
  125. usage_with_options(bench_futex_wake_usage, options);
  126. exit(EXIT_FAILURE);
  127. }
  128. cpu = perf_cpu_map__new_online_cpus();
  129. if (!cpu)
  130. err(EXIT_FAILURE, "calloc");
  131. memset(&act, 0, sizeof(act));
  132. sigfillset(&act.sa_mask);
  133. act.sa_sigaction = toggle_done;
  134. sigaction(SIGINT, &act, NULL);
  135. if (params.mlockall) {
  136. if (mlockall(MCL_CURRENT | MCL_FUTURE))
  137. err(EXIT_FAILURE, "mlockall");
  138. }
  139. if (!params.nthreads)
  140. params.nthreads = perf_cpu_map__nr(cpu);
  141. worker = calloc(params.nthreads, sizeof(*worker));
  142. if (!worker)
  143. err(EXIT_FAILURE, "calloc");
  144. if (!params.fshared)
  145. futex_flag = FUTEX_PRIVATE_FLAG;
  146. printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
  147. "waking up %d at a time.\n\n",
  148. getpid(), params.nthreads, params.fshared ? "shared":"private",
  149. &futex1, params.nwakes);
  150. init_stats(&wakeup_stats);
  151. init_stats(&waketime_stats);
  152. mutex_init(&thread_lock);
  153. cond_init(&thread_parent);
  154. cond_init(&thread_worker);
  155. for (j = 0; j < bench_repeat && !done; j++) {
  156. unsigned int nwoken = 0;
  157. struct timeval start, end, runtime;
  158. /* create, launch & block all threads */
  159. block_threads(worker, cpu);
  160. /* make sure all threads are already blocked */
  161. mutex_lock(&thread_lock);
  162. while (threads_starting)
  163. cond_wait(&thread_parent, &thread_lock);
  164. cond_broadcast(&thread_worker);
  165. mutex_unlock(&thread_lock);
  166. usleep(100000);
  167. /* Ok, all threads are patiently blocked, start waking folks up */
  168. gettimeofday(&start, NULL);
  169. while (nwoken != params.nthreads)
  170. nwoken += futex_wake(&futex1,
  171. params.nwakes, futex_flag);
  172. gettimeofday(&end, NULL);
  173. timersub(&end, &start, &runtime);
  174. update_stats(&wakeup_stats, nwoken);
  175. update_stats(&waketime_stats, runtime.tv_usec);
  176. if (!params.silent) {
  177. printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
  178. j + 1, nwoken, params.nthreads,
  179. runtime.tv_usec / (double)USEC_PER_MSEC);
  180. }
  181. for (i = 0; i < params.nthreads; i++) {
  182. ret = pthread_join(worker[i], NULL);
  183. if (ret)
  184. err(EXIT_FAILURE, "pthread_join");
  185. }
  186. }
  187. /* cleanup & report results */
  188. cond_destroy(&thread_parent);
  189. cond_destroy(&thread_worker);
  190. mutex_destroy(&thread_lock);
  191. print_summary();
  192. free(worker);
  193. perf_cpu_map__put(cpu);
  194. return ret;
  195. }