futex-wake.c 5.0 KB

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