futex-wake.c 5.3 KB

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