futex-wake-parallel.c 8.6 KB

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