futex-hash.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. static int futex_flag = 0;
  27. struct timeval start, end, runtime;
  28. static pthread_mutex_t thread_lock;
  29. static unsigned int threads_starting;
  30. static struct stats throughput_stats;
  31. static pthread_cond_t thread_parent, thread_worker;
  32. struct worker {
  33. int tid;
  34. u_int32_t *futex;
  35. pthread_t thread;
  36. unsigned long ops;
  37. };
  38. static const struct option options[] = {
  39. OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
  40. OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
  41. OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"),
  42. OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
  43. OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
  44. OPT_END()
  45. };
  46. static const char * const bench_futex_hash_usage[] = {
  47. "perf bench futex hash <options>",
  48. NULL
  49. };
  50. static void *workerfn(void *arg)
  51. {
  52. int ret;
  53. unsigned int i;
  54. struct worker *w = (struct worker *) arg;
  55. pthread_mutex_lock(&thread_lock);
  56. threads_starting--;
  57. if (!threads_starting)
  58. pthread_cond_signal(&thread_parent);
  59. pthread_cond_wait(&thread_worker, &thread_lock);
  60. pthread_mutex_unlock(&thread_lock);
  61. do {
  62. for (i = 0; i < nfutexes; i++, w->ops++) {
  63. /*
  64. * We want the futex calls to fail in order to stress
  65. * the hashing of uaddr and not measure other steps,
  66. * such as internal waitqueue handling, thus enlarging
  67. * the critical region protected by hb->lock.
  68. */
  69. ret = futex_wait(&w->futex[i], 1234, NULL, futex_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. if (!fshared)
  118. futex_flag = FUTEX_PRIVATE_FLAG;
  119. printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
  120. getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
  121. init_stats(&throughput_stats);
  122. pthread_mutex_init(&thread_lock, NULL);
  123. pthread_cond_init(&thread_parent, NULL);
  124. pthread_cond_init(&thread_worker, NULL);
  125. threads_starting = nthreads;
  126. pthread_attr_init(&thread_attr);
  127. gettimeofday(&start, NULL);
  128. for (i = 0; i < nthreads; i++) {
  129. worker[i].tid = i;
  130. worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
  131. if (!worker[i].futex)
  132. goto errmem;
  133. CPU_ZERO(&cpu);
  134. CPU_SET(i % ncpus, &cpu);
  135. ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu);
  136. if (ret)
  137. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  138. ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
  139. (void *)(struct worker *) &worker[i]);
  140. if (ret)
  141. err(EXIT_FAILURE, "pthread_create");
  142. }
  143. pthread_attr_destroy(&thread_attr);
  144. pthread_mutex_lock(&thread_lock);
  145. while (threads_starting)
  146. pthread_cond_wait(&thread_parent, &thread_lock);
  147. pthread_cond_broadcast(&thread_worker);
  148. pthread_mutex_unlock(&thread_lock);
  149. sleep(nsecs);
  150. toggle_done(0, NULL, NULL);
  151. for (i = 0; i < nthreads; i++) {
  152. ret = pthread_join(worker[i].thread, NULL);
  153. if (ret)
  154. err(EXIT_FAILURE, "pthread_join");
  155. }
  156. /* cleanup & report results */
  157. pthread_cond_destroy(&thread_parent);
  158. pthread_cond_destroy(&thread_worker);
  159. pthread_mutex_destroy(&thread_lock);
  160. for (i = 0; i < nthreads; i++) {
  161. unsigned long t = worker[i].ops/runtime.tv_sec;
  162. update_stats(&throughput_stats, t);
  163. if (!silent) {
  164. if (nfutexes == 1)
  165. printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
  166. worker[i].tid, &worker[i].futex[0], t);
  167. else
  168. printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
  169. worker[i].tid, &worker[i].futex[0],
  170. &worker[i].futex[nfutexes-1], t);
  171. }
  172. free(worker[i].futex);
  173. }
  174. print_summary();
  175. free(worker);
  176. return ret;
  177. errmem:
  178. err(EXIT_FAILURE, "calloc");
  179. }