futex-wake.c 5.2 KB

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