futex-requeue.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 -- thus mimicking a regular futex_wait.
  10. */
  11. /* For the CLR_() macros */
  12. #include <string.h>
  13. #include <pthread.h>
  14. #include <signal.h>
  15. #include "../util/stat.h"
  16. #include <subcmd/parse-options.h>
  17. #include <linux/compiler.h>
  18. #include <linux/kernel.h>
  19. #include <linux/time64.h>
  20. #include <errno.h>
  21. #include "bench.h"
  22. #include "futex.h"
  23. #include "cpumap.h"
  24. #include <err.h>
  25. #include <stdlib.h>
  26. #include <sys/time.h>
  27. static u_int32_t futex1 = 0, futex2 = 0;
  28. /*
  29. * How many tasks to requeue at a time.
  30. * Default to 1 in order to make the kernel work more.
  31. */
  32. static unsigned int nrequeue = 1;
  33. static pthread_t *worker;
  34. static bool done = false, silent = false, fshared = false;
  35. static pthread_mutex_t thread_lock;
  36. static pthread_cond_t thread_parent, thread_worker;
  37. static struct stats requeuetime_stats, requeued_stats;
  38. static unsigned int threads_starting, nthreads = 0;
  39. static int futex_flag = 0;
  40. static const struct option options[] = {
  41. OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
  42. OPT_UINTEGER('q', "nrequeue", &nrequeue, "Specify amount of threads to requeue at once"),
  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_requeue_usage[] = {
  48. "perf bench futex requeue <options>",
  49. NULL
  50. };
  51. static void print_summary(void)
  52. {
  53. double requeuetime_avg = avg_stats(&requeuetime_stats);
  54. double requeuetime_stddev = stddev_stats(&requeuetime_stats);
  55. unsigned int requeued_avg = avg_stats(&requeued_stats);
  56. printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
  57. requeued_avg,
  58. nthreads,
  59. requeuetime_avg / USEC_PER_MSEC,
  60. rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
  61. }
  62. static void *workerfn(void *arg __maybe_unused)
  63. {
  64. pthread_mutex_lock(&thread_lock);
  65. threads_starting--;
  66. if (!threads_starting)
  67. pthread_cond_signal(&thread_parent);
  68. pthread_cond_wait(&thread_worker, &thread_lock);
  69. pthread_mutex_unlock(&thread_lock);
  70. futex_wait(&futex1, 0, NULL, futex_flag);
  71. return NULL;
  72. }
  73. static void block_threads(pthread_t *w,
  74. pthread_attr_t thread_attr, struct cpu_map *cpu)
  75. {
  76. cpu_set_t cpuset;
  77. unsigned int i;
  78. threads_starting = nthreads;
  79. /* create and block all threads */
  80. for (i = 0; i < nthreads; i++) {
  81. CPU_ZERO(&cpuset);
  82. CPU_SET(cpu->map[i % cpu->nr], &cpuset);
  83. if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
  84. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  85. if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
  86. err(EXIT_FAILURE, "pthread_create");
  87. }
  88. }
  89. static void toggle_done(int sig __maybe_unused,
  90. siginfo_t *info __maybe_unused,
  91. void *uc __maybe_unused)
  92. {
  93. done = true;
  94. }
  95. int bench_futex_requeue(int argc, const char **argv)
  96. {
  97. int ret = 0;
  98. unsigned int i, j;
  99. struct sigaction act;
  100. pthread_attr_t thread_attr;
  101. struct cpu_map *cpu;
  102. argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
  103. if (argc)
  104. goto err;
  105. cpu = cpu_map__new(NULL);
  106. if (!cpu)
  107. err(EXIT_FAILURE, "cpu_map__new");
  108. sigfillset(&act.sa_mask);
  109. act.sa_sigaction = toggle_done;
  110. sigaction(SIGINT, &act, NULL);
  111. if (!nthreads)
  112. nthreads = cpu->nr;
  113. worker = calloc(nthreads, sizeof(*worker));
  114. if (!worker)
  115. err(EXIT_FAILURE, "calloc");
  116. if (!fshared)
  117. futex_flag = FUTEX_PRIVATE_FLAG;
  118. if (nrequeue > nthreads)
  119. nrequeue = nthreads;
  120. printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
  121. "%d at a time.\n\n", getpid(), nthreads,
  122. fshared ? "shared":"private", &futex1, &futex2, nrequeue);
  123. init_stats(&requeued_stats);
  124. init_stats(&requeuetime_stats);
  125. pthread_attr_init(&thread_attr);
  126. pthread_mutex_init(&thread_lock, NULL);
  127. pthread_cond_init(&thread_parent, NULL);
  128. pthread_cond_init(&thread_worker, NULL);
  129. for (j = 0; j < bench_repeat && !done; j++) {
  130. unsigned int nrequeued = 0;
  131. struct timeval start, end, runtime;
  132. /* create, launch & block all threads */
  133. block_threads(worker, thread_attr, cpu);
  134. /* make sure all threads are already blocked */
  135. pthread_mutex_lock(&thread_lock);
  136. while (threads_starting)
  137. pthread_cond_wait(&thread_parent, &thread_lock);
  138. pthread_cond_broadcast(&thread_worker);
  139. pthread_mutex_unlock(&thread_lock);
  140. usleep(100000);
  141. /* Ok, all threads are patiently blocked, start requeueing */
  142. gettimeofday(&start, NULL);
  143. while (nrequeued < nthreads) {
  144. /*
  145. * Do not wakeup any tasks blocked on futex1, allowing
  146. * us to really measure futex_wait functionality.
  147. */
  148. nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0,
  149. nrequeue, futex_flag);
  150. }
  151. gettimeofday(&end, NULL);
  152. timersub(&end, &start, &runtime);
  153. update_stats(&requeued_stats, nrequeued);
  154. update_stats(&requeuetime_stats, runtime.tv_usec);
  155. if (!silent) {
  156. printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
  157. j + 1, nrequeued, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
  158. }
  159. /* everybody should be blocked on futex2, wake'em up */
  160. nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
  161. if (nthreads != nrequeued)
  162. warnx("couldn't wakeup all tasks (%d/%d)", nrequeued, nthreads);
  163. for (i = 0; i < nthreads; i++) {
  164. ret = pthread_join(worker[i], NULL);
  165. if (ret)
  166. err(EXIT_FAILURE, "pthread_join");
  167. }
  168. }
  169. /* cleanup & report results */
  170. pthread_cond_destroy(&thread_parent);
  171. pthread_cond_destroy(&thread_worker);
  172. pthread_mutex_destroy(&thread_lock);
  173. pthread_attr_destroy(&thread_attr);
  174. print_summary();
  175. free(worker);
  176. return ret;
  177. err:
  178. usage_with_options(bench_futex_requeue_usage, options);
  179. exit(EXIT_FAILURE);
  180. }