futex-requeue.c 5.4 KB

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