futex-requeue.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
  4. *
  5. * futex-requeue: Block a bunch of threads on futex1 and requeue them
  6. * on futex2, N at a time.
  7. *
  8. * This program is particularly useful to measure the latency of nthread
  9. * requeues without waking up any tasks (in the non-pi case) -- thus
  10. * mimicking a regular futex_wait.
  11. */
  12. /* For the CLR_() macros */
  13. #include <string.h>
  14. #include <pthread.h>
  15. #include <signal.h>
  16. #include "../util/mutex.h"
  17. #include "../util/stat.h"
  18. #include <subcmd/parse-options.h>
  19. #include <linux/compiler.h>
  20. #include <linux/kernel.h>
  21. #include <linux/time64.h>
  22. #include <errno.h>
  23. #include <perf/cpumap.h>
  24. #include "bench.h"
  25. #include "futex.h"
  26. #include <err.h>
  27. #include <stdlib.h>
  28. #include <sys/time.h>
  29. #include <sys/mman.h>
  30. static u_int32_t futex1 = 0, futex2 = 0;
  31. static pthread_t *worker;
  32. static bool done = false;
  33. static struct mutex thread_lock;
  34. static struct cond thread_parent, thread_worker;
  35. static struct stats requeuetime_stats, requeued_stats;
  36. static unsigned int threads_starting;
  37. static int futex_flag = 0;
  38. static struct bench_futex_parameters params = {
  39. /*
  40. * How many tasks to requeue at a time.
  41. * Default to 1 in order to make the kernel work more.
  42. */
  43. .nrequeue = 1,
  44. };
  45. static const struct option options[] = {
  46. OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
  47. OPT_UINTEGER('q', "nrequeue", &params.nrequeue, "Specify amount of threads to requeue at once"),
  48. OPT_BOOLEAN( 's', "silent", &params.silent, "Silent mode: do not display data/details"),
  49. OPT_BOOLEAN( 'S', "shared", &params.fshared, "Use shared futexes instead of private ones"),
  50. OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
  51. OPT_BOOLEAN( 'B', "broadcast", &params.broadcast, "Requeue all threads at once"),
  52. OPT_BOOLEAN( 'p', "pi", &params.pi, "Use PI-aware variants of FUTEX_CMP_REQUEUE"),
  53. OPT_END()
  54. };
  55. static const char * const bench_futex_requeue_usage[] = {
  56. "perf bench futex requeue <options>",
  57. NULL
  58. };
  59. static void print_summary(void)
  60. {
  61. double requeuetime_avg = avg_stats(&requeuetime_stats);
  62. double requeuetime_stddev = stddev_stats(&requeuetime_stats);
  63. unsigned int requeued_avg = avg_stats(&requeued_stats);
  64. printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
  65. requeued_avg,
  66. params.nthreads,
  67. requeuetime_avg / USEC_PER_MSEC,
  68. rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
  69. }
  70. static void *workerfn(void *arg __maybe_unused)
  71. {
  72. int ret;
  73. mutex_lock(&thread_lock);
  74. threads_starting--;
  75. if (!threads_starting)
  76. cond_signal(&thread_parent);
  77. cond_wait(&thread_worker, &thread_lock);
  78. mutex_unlock(&thread_lock);
  79. while (1) {
  80. if (!params.pi) {
  81. ret = futex_wait(&futex1, 0, NULL, futex_flag);
  82. if (!ret)
  83. break;
  84. if (ret && errno != EAGAIN) {
  85. if (!params.silent)
  86. warnx("futex_wait");
  87. break;
  88. }
  89. } else {
  90. ret = futex_wait_requeue_pi(&futex1, 0, &futex2,
  91. NULL, futex_flag);
  92. if (!ret) {
  93. /* got the lock at futex2 */
  94. futex_unlock_pi(&futex2, futex_flag);
  95. break;
  96. }
  97. if (ret && errno != EAGAIN) {
  98. if (!params.silent)
  99. warnx("futex_wait_requeue_pi");
  100. break;
  101. }
  102. }
  103. }
  104. return NULL;
  105. }
  106. static void block_threads(pthread_t *w, struct perf_cpu_map *cpu)
  107. {
  108. cpu_set_t *cpuset;
  109. unsigned int i;
  110. int nrcpus = cpu__max_cpu().cpu;
  111. size_t size;
  112. threads_starting = params.nthreads;
  113. cpuset = CPU_ALLOC(nrcpus);
  114. BUG_ON(!cpuset);
  115. size = CPU_ALLOC_SIZE(nrcpus);
  116. /* create and block all threads */
  117. for (i = 0; i < params.nthreads; i++) {
  118. pthread_attr_t thread_attr;
  119. pthread_attr_init(&thread_attr);
  120. CPU_ZERO_S(size, cpuset);
  121. CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
  122. if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
  123. CPU_FREE(cpuset);
  124. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  125. }
  126. if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) {
  127. CPU_FREE(cpuset);
  128. err(EXIT_FAILURE, "pthread_create");
  129. }
  130. pthread_attr_destroy(&thread_attr);
  131. }
  132. CPU_FREE(cpuset);
  133. }
  134. static void toggle_done(int sig __maybe_unused,
  135. siginfo_t *info __maybe_unused,
  136. void *uc __maybe_unused)
  137. {
  138. done = true;
  139. }
  140. int bench_futex_requeue(int argc, const char **argv)
  141. {
  142. int ret = 0;
  143. unsigned int i, j;
  144. struct sigaction act;
  145. struct perf_cpu_map *cpu;
  146. argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
  147. if (argc)
  148. goto err;
  149. cpu = perf_cpu_map__new_online_cpus();
  150. if (!cpu)
  151. err(EXIT_FAILURE, "cpu_map__new");
  152. memset(&act, 0, sizeof(act));
  153. sigfillset(&act.sa_mask);
  154. act.sa_sigaction = toggle_done;
  155. sigaction(SIGINT, &act, NULL);
  156. if (params.mlockall) {
  157. if (mlockall(MCL_CURRENT | MCL_FUTURE))
  158. err(EXIT_FAILURE, "mlockall");
  159. }
  160. if (!params.nthreads)
  161. params.nthreads = perf_cpu_map__nr(cpu);
  162. worker = calloc(params.nthreads, sizeof(*worker));
  163. if (!worker)
  164. err(EXIT_FAILURE, "calloc");
  165. if (!params.fshared)
  166. futex_flag = FUTEX_PRIVATE_FLAG;
  167. if (params.nrequeue > params.nthreads)
  168. params.nrequeue = params.nthreads;
  169. if (params.broadcast)
  170. params.nrequeue = params.nthreads;
  171. printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %s%p), "
  172. "%d at a time.\n\n", getpid(), params.nthreads,
  173. params.fshared ? "shared":"private", &futex1,
  174. params.pi ? "PI ": "", &futex2, params.nrequeue);
  175. init_stats(&requeued_stats);
  176. init_stats(&requeuetime_stats);
  177. mutex_init(&thread_lock);
  178. cond_init(&thread_parent);
  179. cond_init(&thread_worker);
  180. for (j = 0; j < bench_repeat && !done; j++) {
  181. unsigned int nrequeued = 0, wakeups = 0;
  182. struct timeval start, end, runtime;
  183. /* create, launch & block all threads */
  184. block_threads(worker, cpu);
  185. /* make sure all threads are already blocked */
  186. mutex_lock(&thread_lock);
  187. while (threads_starting)
  188. cond_wait(&thread_parent, &thread_lock);
  189. cond_broadcast(&thread_worker);
  190. mutex_unlock(&thread_lock);
  191. usleep(100000);
  192. /* Ok, all threads are patiently blocked, start requeueing */
  193. gettimeofday(&start, NULL);
  194. while (nrequeued < params.nthreads) {
  195. int r;
  196. /*
  197. * For the regular non-pi case, do not wakeup any tasks
  198. * blocked on futex1, allowing us to really measure
  199. * futex_wait functionality. For the PI case the first
  200. * waiter is always awoken.
  201. */
  202. if (!params.pi) {
  203. r = futex_cmp_requeue(&futex1, 0, &futex2, 0,
  204. params.nrequeue,
  205. futex_flag);
  206. } else {
  207. r = futex_cmp_requeue_pi(&futex1, 0, &futex2,
  208. params.nrequeue,
  209. futex_flag);
  210. wakeups++; /* assume no error */
  211. }
  212. if (r < 0)
  213. err(EXIT_FAILURE, "couldn't requeue from %p to %p",
  214. &futex1, &futex2);
  215. nrequeued += r;
  216. }
  217. gettimeofday(&end, NULL);
  218. timersub(&end, &start, &runtime);
  219. update_stats(&requeued_stats, nrequeued);
  220. update_stats(&requeuetime_stats, runtime.tv_usec);
  221. if (!params.silent) {
  222. if (!params.pi)
  223. printf("[Run %d]: Requeued %d of %d threads in "
  224. "%.4f ms\n", j + 1, nrequeued,
  225. params.nthreads,
  226. runtime.tv_usec / (double)USEC_PER_MSEC);
  227. else {
  228. nrequeued -= wakeups;
  229. printf("[Run %d]: Awoke and Requeued (%d+%d) of "
  230. "%d threads in %.4f ms\n",
  231. j + 1, wakeups, nrequeued,
  232. params.nthreads,
  233. runtime.tv_usec / (double)USEC_PER_MSEC);
  234. }
  235. }
  236. if (!params.pi) {
  237. /* everybody should be blocked on futex2, wake'em up */
  238. nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
  239. if (params.nthreads != nrequeued)
  240. warnx("couldn't wakeup all tasks (%d/%d)",
  241. nrequeued, params.nthreads);
  242. }
  243. for (i = 0; i < params.nthreads; i++) {
  244. ret = pthread_join(worker[i], NULL);
  245. if (ret)
  246. err(EXIT_FAILURE, "pthread_join");
  247. }
  248. }
  249. /* cleanup & report results */
  250. cond_destroy(&thread_parent);
  251. cond_destroy(&thread_worker);
  252. mutex_destroy(&thread_lock);
  253. print_summary();
  254. free(worker);
  255. perf_cpu_map__put(cpu);
  256. return ret;
  257. err:
  258. usage_with_options(bench_futex_requeue_usage, options);
  259. exit(EXIT_FAILURE);
  260. }