futex-wake.c 5.3 KB

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