futex-wake-parallel.c 7.9 KB

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