futex-requeue.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. static pthread_t *worker;
  28. static bool done = false, silent = false, fshared = false;
  29. static pthread_mutex_t thread_lock;
  30. static pthread_cond_t thread_parent, thread_worker;
  31. static struct stats requeuetime_stats, requeued_stats;
  32. static unsigned int ncpus, threads_starting, nthreads = 0;
  33. static int futex_flag = 0;
  34. static const struct option options[] = {
  35. OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
  36. OPT_UINTEGER('q', "nrequeue", &nrequeue, "Specify amount of threads to requeue at once"),
  37. OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
  38. OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
  39. OPT_END()
  40. };
  41. static const char * const bench_futex_requeue_usage[] = {
  42. "perf bench futex requeue <options>",
  43. NULL
  44. };
  45. static void print_summary(void)
  46. {
  47. double requeuetime_avg = avg_stats(&requeuetime_stats);
  48. double requeuetime_stddev = stddev_stats(&requeuetime_stats);
  49. unsigned int requeued_avg = avg_stats(&requeued_stats);
  50. printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
  51. requeued_avg,
  52. nthreads,
  53. requeuetime_avg/1e3,
  54. rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
  55. }
  56. static void *workerfn(void *arg __maybe_unused)
  57. {
  58. pthread_mutex_lock(&thread_lock);
  59. threads_starting--;
  60. if (!threads_starting)
  61. pthread_cond_signal(&thread_parent);
  62. pthread_cond_wait(&thread_worker, &thread_lock);
  63. pthread_mutex_unlock(&thread_lock);
  64. futex_wait(&futex1, 0, NULL, futex_flag);
  65. return NULL;
  66. }
  67. static void block_threads(pthread_t *w,
  68. pthread_attr_t thread_attr)
  69. {
  70. cpu_set_t cpu;
  71. unsigned int i;
  72. threads_starting = nthreads;
  73. /* create and block all threads */
  74. for (i = 0; i < nthreads; i++) {
  75. CPU_ZERO(&cpu);
  76. CPU_SET(i % ncpus, &cpu);
  77. if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
  78. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  79. if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
  80. err(EXIT_FAILURE, "pthread_create");
  81. }
  82. }
  83. static void toggle_done(int sig __maybe_unused,
  84. siginfo_t *info __maybe_unused,
  85. void *uc __maybe_unused)
  86. {
  87. done = true;
  88. }
  89. int bench_futex_requeue(int argc, const char **argv,
  90. const char *prefix __maybe_unused)
  91. {
  92. int ret = 0;
  93. unsigned int i, j;
  94. struct sigaction act;
  95. pthread_attr_t thread_attr;
  96. argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
  97. if (argc)
  98. goto err;
  99. ncpus = sysconf(_SC_NPROCESSORS_ONLN);
  100. sigfillset(&act.sa_mask);
  101. act.sa_sigaction = toggle_done;
  102. sigaction(SIGINT, &act, NULL);
  103. if (!nthreads)
  104. nthreads = ncpus;
  105. worker = calloc(nthreads, sizeof(*worker));
  106. if (!worker)
  107. err(EXIT_FAILURE, "calloc");
  108. if (!fshared)
  109. futex_flag = FUTEX_PRIVATE_FLAG;
  110. if (nrequeue > nthreads)
  111. nrequeue = nthreads;
  112. printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
  113. "%d at a time.\n\n", getpid(), nthreads,
  114. fshared ? "shared":"private", &futex1, &futex2, nrequeue);
  115. init_stats(&requeued_stats);
  116. init_stats(&requeuetime_stats);
  117. pthread_attr_init(&thread_attr);
  118. pthread_mutex_init(&thread_lock, NULL);
  119. pthread_cond_init(&thread_parent, NULL);
  120. pthread_cond_init(&thread_worker, NULL);
  121. for (j = 0; j < bench_repeat && !done; j++) {
  122. unsigned int nrequeued = 0;
  123. struct timeval start, end, runtime;
  124. /* create, launch & block all threads */
  125. block_threads(worker, thread_attr);
  126. /* make sure all threads are already blocked */
  127. pthread_mutex_lock(&thread_lock);
  128. while (threads_starting)
  129. pthread_cond_wait(&thread_parent, &thread_lock);
  130. pthread_cond_broadcast(&thread_worker);
  131. pthread_mutex_unlock(&thread_lock);
  132. usleep(100000);
  133. /* Ok, all threads are patiently blocked, start requeueing */
  134. gettimeofday(&start, NULL);
  135. while (nrequeued < nthreads) {
  136. /*
  137. * Do not wakeup any tasks blocked on futex1, allowing
  138. * us to really measure futex_wait functionality.
  139. */
  140. nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0,
  141. nrequeue, futex_flag);
  142. }
  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, nrequeued, futex_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. }