futex-hash.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
  3. *
  4. * futex-hash: Stress the hell out of the Linux kernel futex uaddr hashing.
  5. *
  6. * This program is particularly useful for measuring the kernel's futex hash
  7. * table/function implementation. In order for it to make sense, use with as
  8. * many threads and futexes as possible.
  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 unsigned int nthreads = 0;
  22. static unsigned int nsecs = 10;
  23. /* amount of futexes per thread */
  24. static unsigned int nfutexes = 1024;
  25. static bool fshared = false, done = false, silent = false;
  26. struct timeval start, end, runtime;
  27. static pthread_mutex_t thread_lock;
  28. static unsigned int threads_starting;
  29. static struct stats throughput_stats;
  30. static pthread_cond_t thread_parent, thread_worker;
  31. struct worker {
  32. int tid;
  33. u_int32_t *futex;
  34. pthread_t thread;
  35. unsigned long ops;
  36. };
  37. static const struct option options[] = {
  38. OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
  39. OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
  40. OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"),
  41. OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
  42. OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
  43. OPT_END()
  44. };
  45. static const char * const bench_futex_hash_usage[] = {
  46. "perf bench futex hash <options>",
  47. NULL
  48. };
  49. static void *workerfn(void *arg)
  50. {
  51. int ret;
  52. unsigned int i;
  53. struct worker *w = (struct worker *) arg;
  54. pthread_mutex_lock(&thread_lock);
  55. threads_starting--;
  56. if (!threads_starting)
  57. pthread_cond_signal(&thread_parent);
  58. pthread_cond_wait(&thread_worker, &thread_lock);
  59. pthread_mutex_unlock(&thread_lock);
  60. do {
  61. for (i = 0; i < nfutexes; i++, w->ops++) {
  62. /*
  63. * We want the futex calls to fail in order to stress
  64. * the hashing of uaddr and not measure other steps,
  65. * such as internal waitqueue handling, thus enlarging
  66. * the critical region protected by hb->lock.
  67. */
  68. ret = futex_wait(&w->futex[i], 1234, NULL,
  69. fshared ? 0 : FUTEX_PRIVATE_FLAG);
  70. if (!silent &&
  71. (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
  72. warn("Non-expected futex return call");
  73. }
  74. } while (!done);
  75. return NULL;
  76. }
  77. static void toggle_done(int sig __maybe_unused,
  78. siginfo_t *info __maybe_unused,
  79. void *uc __maybe_unused)
  80. {
  81. /* inform all threads that we're done for the day */
  82. done = true;
  83. gettimeofday(&end, NULL);
  84. timersub(&end, &start, &runtime);
  85. }
  86. static void print_summary(void)
  87. {
  88. unsigned long avg = avg_stats(&throughput_stats);
  89. double stddev = stddev_stats(&throughput_stats);
  90. printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
  91. !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
  92. (int) runtime.tv_sec);
  93. }
  94. int bench_futex_hash(int argc, const char **argv,
  95. const char *prefix __maybe_unused)
  96. {
  97. int ret = 0;
  98. cpu_set_t cpu;
  99. struct sigaction act;
  100. unsigned int i, ncpus;
  101. pthread_attr_t thread_attr;
  102. struct worker *worker = NULL;
  103. argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
  104. if (argc) {
  105. usage_with_options(bench_futex_hash_usage, options);
  106. exit(EXIT_FAILURE);
  107. }
  108. ncpus = sysconf(_SC_NPROCESSORS_ONLN);
  109. sigfillset(&act.sa_mask);
  110. act.sa_sigaction = toggle_done;
  111. sigaction(SIGINT, &act, NULL);
  112. if (!nthreads) /* default to the number of CPUs */
  113. nthreads = ncpus;
  114. worker = calloc(nthreads, sizeof(*worker));
  115. if (!worker)
  116. goto errmem;
  117. printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
  118. getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
  119. init_stats(&throughput_stats);
  120. pthread_mutex_init(&thread_lock, NULL);
  121. pthread_cond_init(&thread_parent, NULL);
  122. pthread_cond_init(&thread_worker, NULL);
  123. threads_starting = nthreads;
  124. pthread_attr_init(&thread_attr);
  125. gettimeofday(&start, NULL);
  126. for (i = 0; i < nthreads; i++) {
  127. worker[i].tid = i;
  128. worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
  129. if (!worker[i].futex)
  130. goto errmem;
  131. CPU_ZERO(&cpu);
  132. CPU_SET(i % ncpus, &cpu);
  133. ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu);
  134. if (ret)
  135. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  136. ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
  137. (void *)(struct worker *) &worker[i]);
  138. if (ret)
  139. err(EXIT_FAILURE, "pthread_create");
  140. }
  141. pthread_attr_destroy(&thread_attr);
  142. pthread_mutex_lock(&thread_lock);
  143. while (threads_starting)
  144. pthread_cond_wait(&thread_parent, &thread_lock);
  145. pthread_cond_broadcast(&thread_worker);
  146. pthread_mutex_unlock(&thread_lock);
  147. sleep(nsecs);
  148. toggle_done(0, NULL, NULL);
  149. for (i = 0; i < nthreads; i++) {
  150. ret = pthread_join(worker[i].thread, NULL);
  151. if (ret)
  152. err(EXIT_FAILURE, "pthread_join");
  153. }
  154. /* cleanup & report results */
  155. pthread_cond_destroy(&thread_parent);
  156. pthread_cond_destroy(&thread_worker);
  157. pthread_mutex_destroy(&thread_lock);
  158. for (i = 0; i < nthreads; i++) {
  159. unsigned long t = worker[i].ops/runtime.tv_sec;
  160. update_stats(&throughput_stats, t);
  161. if (!silent) {
  162. if (nfutexes == 1)
  163. printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
  164. worker[i].tid, &worker[i].futex[0], t);
  165. else
  166. printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
  167. worker[i].tid, &worker[i].futex[0],
  168. &worker[i].futex[nfutexes-1], t);
  169. }
  170. free(worker[i].futex);
  171. }
  172. print_summary();
  173. free(worker);
  174. return ret;
  175. errmem:
  176. err(EXIT_FAILURE, "calloc");
  177. }