futex-hash.c 6.0 KB

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