futex-wake.c 5.3 KB

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