futex-wake-parallel.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C) 2015 Davidlohr Bueso.
  3. *
  4. * Block a bunch of threads and let parallel waker threads wakeup an
  5. * equal amount of them. The program output reflects the avg latency
  6. * for each individual thread to service its share of work. Ultimately
  7. * it can be used to measure futex_wake() changes.
  8. */
  9. #include "../perf.h"
  10. #include "../util/util.h"
  11. #include "../util/stat.h"
  12. #include "../util/parse-options.h"
  13. #include "../util/header.h"
  14. #include "bench.h"
  15. #include "futex.h"
  16. #include <err.h>
  17. #include <stdlib.h>
  18. #include <sys/time.h>
  19. #include <pthread.h>
  20. struct thread_data {
  21. pthread_t worker;
  22. unsigned int nwoken;
  23. struct timeval runtime;
  24. };
  25. static unsigned int nwakes = 1;
  26. /* all threads will block on the same futex -- hash bucket chaos ;) */
  27. static u_int32_t futex = 0;
  28. static pthread_t *blocked_worker;
  29. static bool done = false, silent = false, fshared = false;
  30. static unsigned int nblocked_threads = 0, nwaking_threads = 0;
  31. static pthread_mutex_t thread_lock;
  32. static pthread_cond_t thread_parent, thread_worker;
  33. static struct stats waketime_stats, wakeup_stats;
  34. static unsigned int ncpus, threads_starting;
  35. static int futex_flag = 0;
  36. static const struct option options[] = {
  37. OPT_UINTEGER('t', "threads", &nblocked_threads, "Specify amount of threads"),
  38. OPT_UINTEGER('w', "nwakers", &nwaking_threads, "Specify amount of waking threads"),
  39. OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
  40. OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
  41. OPT_END()
  42. };
  43. static const char * const bench_futex_wake_parallel_usage[] = {
  44. "perf bench futex wake-parallel <options>",
  45. NULL
  46. };
  47. static void *waking_workerfn(void *arg)
  48. {
  49. struct thread_data *waker = (struct thread_data *) arg;
  50. struct timeval start, end;
  51. gettimeofday(&start, NULL);
  52. waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
  53. if (waker->nwoken != nwakes)
  54. warnx("couldn't wakeup all tasks (%d/%d)",
  55. waker->nwoken, nwakes);
  56. gettimeofday(&end, NULL);
  57. timersub(&end, &start, &waker->runtime);
  58. pthread_exit(NULL);
  59. return NULL;
  60. }
  61. static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
  62. {
  63. unsigned int i;
  64. pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
  65. /* create and block all threads */
  66. for (i = 0; i < nwaking_threads; i++) {
  67. /*
  68. * Thread creation order will impact per-thread latency
  69. * as it will affect the order to acquire the hb spinlock.
  70. * For now let the scheduler decide.
  71. */
  72. if (pthread_create(&td[i].worker, &thread_attr,
  73. waking_workerfn, (void *)&td[i]))
  74. err(EXIT_FAILURE, "pthread_create");
  75. }
  76. for (i = 0; i < nwaking_threads; i++)
  77. if (pthread_join(td[i].worker, NULL))
  78. err(EXIT_FAILURE, "pthread_join");
  79. }
  80. static void *blocked_workerfn(void *arg __maybe_unused)
  81. {
  82. pthread_mutex_lock(&thread_lock);
  83. threads_starting--;
  84. if (!threads_starting)
  85. pthread_cond_signal(&thread_parent);
  86. pthread_cond_wait(&thread_worker, &thread_lock);
  87. pthread_mutex_unlock(&thread_lock);
  88. while (1) { /* handle spurious wakeups */
  89. if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
  90. break;
  91. }
  92. pthread_exit(NULL);
  93. return NULL;
  94. }
  95. static void block_threads(pthread_t *w, pthread_attr_t thread_attr)
  96. {
  97. cpu_set_t cpu;
  98. unsigned int i;
  99. threads_starting = nblocked_threads;
  100. /* create and block all threads */
  101. for (i = 0; i < nblocked_threads; i++) {
  102. CPU_ZERO(&cpu);
  103. CPU_SET(i % ncpus, &cpu);
  104. if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
  105. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  106. if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL))
  107. err(EXIT_FAILURE, "pthread_create");
  108. }
  109. }
  110. static void print_run(struct thread_data *waking_worker, unsigned int run_num)
  111. {
  112. unsigned int i, wakeup_avg;
  113. double waketime_avg, waketime_stddev;
  114. struct stats __waketime_stats, __wakeup_stats;
  115. init_stats(&__wakeup_stats);
  116. init_stats(&__waketime_stats);
  117. for (i = 0; i < nwaking_threads; i++) {
  118. update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
  119. update_stats(&__wakeup_stats, waking_worker[i].nwoken);
  120. }
  121. waketime_avg = avg_stats(&__waketime_stats);
  122. waketime_stddev = stddev_stats(&__waketime_stats);
  123. wakeup_avg = avg_stats(&__wakeup_stats);
  124. printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
  125. "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
  126. nblocked_threads, waketime_avg/1e3,
  127. rel_stddev_stats(waketime_stddev, waketime_avg));
  128. }
  129. static void print_summary(void)
  130. {
  131. unsigned int wakeup_avg;
  132. double waketime_avg, waketime_stddev;
  133. waketime_avg = avg_stats(&waketime_stats);
  134. waketime_stddev = stddev_stats(&waketime_stats);
  135. wakeup_avg = avg_stats(&wakeup_stats);
  136. printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
  137. wakeup_avg,
  138. nblocked_threads,
  139. waketime_avg/1e3,
  140. rel_stddev_stats(waketime_stddev, waketime_avg));
  141. }
  142. static void do_run_stats(struct thread_data *waking_worker)
  143. {
  144. unsigned int i;
  145. for (i = 0; i < nwaking_threads; i++) {
  146. update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
  147. update_stats(&wakeup_stats, waking_worker[i].nwoken);
  148. }
  149. }
  150. static void toggle_done(int sig __maybe_unused,
  151. siginfo_t *info __maybe_unused,
  152. void *uc __maybe_unused)
  153. {
  154. done = true;
  155. }
  156. int bench_futex_wake_parallel(int argc, const char **argv,
  157. const char *prefix __maybe_unused)
  158. {
  159. int ret = 0;
  160. unsigned int i, j;
  161. struct sigaction act;
  162. pthread_attr_t thread_attr;
  163. struct thread_data *waking_worker;
  164. argc = parse_options(argc, argv, options,
  165. bench_futex_wake_parallel_usage, 0);
  166. if (argc) {
  167. usage_with_options(bench_futex_wake_parallel_usage, options);
  168. exit(EXIT_FAILURE);
  169. }
  170. sigfillset(&act.sa_mask);
  171. act.sa_sigaction = toggle_done;
  172. sigaction(SIGINT, &act, NULL);
  173. ncpus = sysconf(_SC_NPROCESSORS_ONLN);
  174. if (!nblocked_threads)
  175. nblocked_threads = ncpus;
  176. /* some sanity checks */
  177. if (nwaking_threads > nblocked_threads || !nwaking_threads)
  178. nwaking_threads = nblocked_threads;
  179. if (nblocked_threads % nwaking_threads)
  180. errx(EXIT_FAILURE, "Must be perfectly divisible");
  181. /*
  182. * Each thread will wakeup nwakes tasks in
  183. * a single futex_wait call.
  184. */
  185. nwakes = nblocked_threads/nwaking_threads;
  186. blocked_worker = calloc(nblocked_threads, sizeof(*blocked_worker));
  187. if (!blocked_worker)
  188. err(EXIT_FAILURE, "calloc");
  189. if (!fshared)
  190. futex_flag = FUTEX_PRIVATE_FLAG;
  191. printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
  192. "futex %p), %d threads waking up %d at a time.\n\n",
  193. getpid(), nblocked_threads, fshared ? "shared":"private",
  194. &futex, nwaking_threads, nwakes);
  195. init_stats(&wakeup_stats);
  196. init_stats(&waketime_stats);
  197. pthread_attr_init(&thread_attr);
  198. pthread_mutex_init(&thread_lock, NULL);
  199. pthread_cond_init(&thread_parent, NULL);
  200. pthread_cond_init(&thread_worker, NULL);
  201. for (j = 0; j < bench_repeat && !done; j++) {
  202. waking_worker = calloc(nwaking_threads, sizeof(*waking_worker));
  203. if (!waking_worker)
  204. err(EXIT_FAILURE, "calloc");
  205. /* create, launch & block all threads */
  206. block_threads(blocked_worker, thread_attr);
  207. /* make sure all threads are already blocked */
  208. pthread_mutex_lock(&thread_lock);
  209. while (threads_starting)
  210. pthread_cond_wait(&thread_parent, &thread_lock);
  211. pthread_cond_broadcast(&thread_worker);
  212. pthread_mutex_unlock(&thread_lock);
  213. usleep(100000);
  214. /* Ok, all threads are patiently blocked, start waking folks up */
  215. wakeup_threads(waking_worker, thread_attr);
  216. for (i = 0; i < nblocked_threads; i++) {
  217. ret = pthread_join(blocked_worker[i], NULL);
  218. if (ret)
  219. err(EXIT_FAILURE, "pthread_join");
  220. }
  221. do_run_stats(waking_worker);
  222. if (!silent)
  223. print_run(waking_worker, j);
  224. free(waking_worker);
  225. }
  226. /* cleanup & report results */
  227. pthread_cond_destroy(&thread_parent);
  228. pthread_cond_destroy(&thread_worker);
  229. pthread_mutex_destroy(&thread_lock);
  230. pthread_attr_destroy(&thread_attr);
  231. print_summary();
  232. free(blocked_worker);
  233. return ret;
  234. }