futex-wake.c 5.4 KB

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