futex-requeue.c 5.7 KB

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