futex-wake-parallel.c 9.0 KB

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