futex-hash.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. unsigned int i;
  57. struct worker *w = (struct worker *) arg;
  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. do {
  65. for (i = 0; i < nfutexes; i++, w->ops++) {
  66. /*
  67. * We want the futex calls to fail in order to stress
  68. * the hashing of uaddr and not measure other steps,
  69. * such as internal waitqueue handling, thus enlarging
  70. * the critical region protected by hb->lock.
  71. */
  72. ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
  73. if (!silent &&
  74. (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
  75. warn("Non-expected futex return call");
  76. }
  77. } while (!done);
  78. return NULL;
  79. }
  80. static void toggle_done(int sig __maybe_unused,
  81. siginfo_t *info __maybe_unused,
  82. void *uc __maybe_unused)
  83. {
  84. /* inform all threads that we're done for the day */
  85. done = true;
  86. gettimeofday(&end, NULL);
  87. timersub(&end, &start, &runtime);
  88. }
  89. static void print_summary(void)
  90. {
  91. unsigned long avg = avg_stats(&throughput_stats);
  92. double stddev = stddev_stats(&throughput_stats);
  93. printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
  94. !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
  95. (int) runtime.tv_sec);
  96. }
  97. int bench_futex_hash(int argc, const char **argv,
  98. const char *prefix __maybe_unused)
  99. {
  100. int ret = 0;
  101. cpu_set_t cpu;
  102. struct sigaction act;
  103. unsigned int i, ncpus;
  104. pthread_attr_t thread_attr;
  105. struct worker *worker = NULL;
  106. argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
  107. if (argc) {
  108. usage_with_options(bench_futex_hash_usage, options);
  109. exit(EXIT_FAILURE);
  110. }
  111. ncpus = sysconf(_SC_NPROCESSORS_ONLN);
  112. sigfillset(&act.sa_mask);
  113. act.sa_sigaction = toggle_done;
  114. sigaction(SIGINT, &act, NULL);
  115. if (!nthreads) /* default to the number of CPUs */
  116. nthreads = ncpus;
  117. worker = calloc(nthreads, sizeof(*worker));
  118. if (!worker)
  119. goto errmem;
  120. if (!fshared)
  121. futex_flag = FUTEX_PRIVATE_FLAG;
  122. printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
  123. getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
  124. init_stats(&throughput_stats);
  125. pthread_mutex_init(&thread_lock, NULL);
  126. pthread_cond_init(&thread_parent, NULL);
  127. pthread_cond_init(&thread_worker, NULL);
  128. threads_starting = nthreads;
  129. pthread_attr_init(&thread_attr);
  130. gettimeofday(&start, NULL);
  131. for (i = 0; i < nthreads; i++) {
  132. worker[i].tid = i;
  133. worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
  134. if (!worker[i].futex)
  135. goto errmem;
  136. CPU_ZERO(&cpu);
  137. CPU_SET(i % ncpus, &cpu);
  138. ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu);
  139. if (ret)
  140. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  141. ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
  142. (void *)(struct worker *) &worker[i]);
  143. if (ret)
  144. err(EXIT_FAILURE, "pthread_create");
  145. }
  146. pthread_attr_destroy(&thread_attr);
  147. pthread_mutex_lock(&thread_lock);
  148. while (threads_starting)
  149. pthread_cond_wait(&thread_parent, &thread_lock);
  150. pthread_cond_broadcast(&thread_worker);
  151. pthread_mutex_unlock(&thread_lock);
  152. sleep(nsecs);
  153. toggle_done(0, NULL, NULL);
  154. for (i = 0; i < nthreads; i++) {
  155. ret = pthread_join(worker[i].thread, NULL);
  156. if (ret)
  157. err(EXIT_FAILURE, "pthread_join");
  158. }
  159. /* cleanup & report results */
  160. pthread_cond_destroy(&thread_parent);
  161. pthread_cond_destroy(&thread_worker);
  162. pthread_mutex_destroy(&thread_lock);
  163. for (i = 0; i < nthreads; i++) {
  164. unsigned long t = worker[i].ops/runtime.tv_sec;
  165. update_stats(&throughput_stats, t);
  166. if (!silent) {
  167. if (nfutexes == 1)
  168. printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
  169. worker[i].tid, &worker[i].futex[0], t);
  170. else
  171. printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
  172. worker[i].tid, &worker[i].futex[0],
  173. &worker[i].futex[nfutexes-1], t);
  174. }
  175. free(worker[i].futex);
  176. }
  177. print_summary();
  178. free(worker);
  179. return ret;
  180. errmem:
  181. err(EXIT_FAILURE, "calloc");
  182. }