futex-wake-parallel.c 7.9 KB

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