futex-hash.c 5.8 KB

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