futex-requeue.c 5.7 KB

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