futex-wake-parallel.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <linux/time64.h>
  17. #include <errno.h>
  18. #include "bench.h"
  19. #include "futex.h"
  20. #include <err.h>
  21. #include <stdlib.h>
  22. #include <sys/time.h>
  23. struct thread_data {
  24. pthread_t worker;
  25. unsigned int nwoken;
  26. struct timeval runtime;
  27. };
  28. static unsigned int nwakes = 1;
  29. /* all threads will block on the same futex -- hash bucket chaos ;) */
  30. static u_int32_t futex = 0;
  31. static pthread_t *blocked_worker;
  32. static bool done = false, silent = false, fshared = false;
  33. static unsigned int nblocked_threads = 0, nwaking_threads = 0;
  34. static pthread_mutex_t thread_lock;
  35. static pthread_cond_t thread_parent, thread_worker;
  36. static struct stats waketime_stats, wakeup_stats;
  37. static unsigned int ncpus, threads_starting;
  38. static int futex_flag = 0;
  39. static const struct option options[] = {
  40. OPT_UINTEGER('t', "threads", &nblocked_threads, "Specify amount of threads"),
  41. OPT_UINTEGER('w', "nwakers", &nwaking_threads, "Specify amount of waking threads"),
  42. OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
  43. OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
  44. OPT_END()
  45. };
  46. static const char * const bench_futex_wake_parallel_usage[] = {
  47. "perf bench futex wake-parallel <options>",
  48. NULL
  49. };
  50. static void *waking_workerfn(void *arg)
  51. {
  52. struct thread_data *waker = (struct thread_data *) arg;
  53. struct timeval start, end;
  54. gettimeofday(&start, NULL);
  55. waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
  56. if (waker->nwoken != nwakes)
  57. warnx("couldn't wakeup all tasks (%d/%d)",
  58. waker->nwoken, nwakes);
  59. gettimeofday(&end, NULL);
  60. timersub(&end, &start, &waker->runtime);
  61. pthread_exit(NULL);
  62. return NULL;
  63. }
  64. static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
  65. {
  66. unsigned int i;
  67. pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
  68. /* create and block all threads */
  69. for (i = 0; i < nwaking_threads; i++) {
  70. /*
  71. * Thread creation order will impact per-thread latency
  72. * as it will affect the order to acquire the hb spinlock.
  73. * For now let the scheduler decide.
  74. */
  75. if (pthread_create(&td[i].worker, &thread_attr,
  76. waking_workerfn, (void *)&td[i]))
  77. err(EXIT_FAILURE, "pthread_create");
  78. }
  79. for (i = 0; i < nwaking_threads; i++)
  80. if (pthread_join(td[i].worker, NULL))
  81. err(EXIT_FAILURE, "pthread_join");
  82. }
  83. static void *blocked_workerfn(void *arg __maybe_unused)
  84. {
  85. pthread_mutex_lock(&thread_lock);
  86. threads_starting--;
  87. if (!threads_starting)
  88. pthread_cond_signal(&thread_parent);
  89. pthread_cond_wait(&thread_worker, &thread_lock);
  90. pthread_mutex_unlock(&thread_lock);
  91. while (1) { /* handle spurious wakeups */
  92. if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
  93. break;
  94. }
  95. pthread_exit(NULL);
  96. return NULL;
  97. }
  98. static void block_threads(pthread_t *w, pthread_attr_t thread_attr)
  99. {
  100. cpu_set_t cpu;
  101. unsigned int i;
  102. threads_starting = nblocked_threads;
  103. /* create and block all threads */
  104. for (i = 0; i < nblocked_threads; i++) {
  105. CPU_ZERO(&cpu);
  106. CPU_SET(i % ncpus, &cpu);
  107. if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
  108. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  109. if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL))
  110. err(EXIT_FAILURE, "pthread_create");
  111. }
  112. }
  113. static void print_run(struct thread_data *waking_worker, unsigned int run_num)
  114. {
  115. unsigned int i, wakeup_avg;
  116. double waketime_avg, waketime_stddev;
  117. struct stats __waketime_stats, __wakeup_stats;
  118. init_stats(&__wakeup_stats);
  119. init_stats(&__waketime_stats);
  120. for (i = 0; i < nwaking_threads; i++) {
  121. update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
  122. update_stats(&__wakeup_stats, waking_worker[i].nwoken);
  123. }
  124. waketime_avg = avg_stats(&__waketime_stats);
  125. waketime_stddev = stddev_stats(&__waketime_stats);
  126. wakeup_avg = avg_stats(&__wakeup_stats);
  127. printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
  128. "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
  129. nblocked_threads, waketime_avg / USEC_PER_MSEC,
  130. rel_stddev_stats(waketime_stddev, waketime_avg));
  131. }
  132. static void print_summary(void)
  133. {
  134. unsigned int wakeup_avg;
  135. double waketime_avg, waketime_stddev;
  136. waketime_avg = avg_stats(&waketime_stats);
  137. waketime_stddev = stddev_stats(&waketime_stats);
  138. wakeup_avg = avg_stats(&wakeup_stats);
  139. printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
  140. wakeup_avg,
  141. nblocked_threads,
  142. waketime_avg / USEC_PER_MSEC,
  143. rel_stddev_stats(waketime_stddev, waketime_avg));
  144. }
  145. static void do_run_stats(struct thread_data *waking_worker)
  146. {
  147. unsigned int i;
  148. for (i = 0; i < nwaking_threads; i++) {
  149. update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
  150. update_stats(&wakeup_stats, waking_worker[i].nwoken);
  151. }
  152. }
  153. static void toggle_done(int sig __maybe_unused,
  154. siginfo_t *info __maybe_unused,
  155. void *uc __maybe_unused)
  156. {
  157. done = true;
  158. }
  159. int bench_futex_wake_parallel(int argc, const char **argv,
  160. const char *prefix __maybe_unused)
  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. }