futex-hash.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
  4. *
  5. * futex-hash: Stress the hell out of the Linux kernel futex uaddr hashing.
  6. *
  7. * This program is particularly useful for measuring the kernel's futex hash
  8. * table/function implementation. In order for it to make sense, use with as
  9. * many threads and futexes as possible.
  10. */
  11. /* For the CLR_() macros */
  12. #include <string.h>
  13. #include <pthread.h>
  14. #include <errno.h>
  15. #include <signal.h>
  16. #include <stdlib.h>
  17. #include <linux/compiler.h>
  18. #include <linux/kernel.h>
  19. #include <sys/time.h>
  20. #include "../util/stat.h"
  21. #include <subcmd/parse-options.h>
  22. #include "bench.h"
  23. #include "futex.h"
  24. #include "cpumap.h"
  25. #include <err.h>
  26. static unsigned int nthreads = 0;
  27. static unsigned int nsecs = 10;
  28. /* amount of futexes per thread */
  29. static unsigned int nfutexes = 1024;
  30. static bool fshared = false, done = false, silent = false;
  31. static int futex_flag = 0;
  32. struct timeval start, end, runtime;
  33. static pthread_mutex_t thread_lock;
  34. static unsigned int threads_starting;
  35. static struct stats throughput_stats;
  36. static pthread_cond_t thread_parent, thread_worker;
  37. struct worker {
  38. int tid;
  39. u_int32_t *futex;
  40. pthread_t thread;
  41. unsigned long ops;
  42. };
  43. static const struct option options[] = {
  44. OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
  45. OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
  46. OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"),
  47. OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
  48. OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
  49. OPT_END()
  50. };
  51. static const char * const bench_futex_hash_usage[] = {
  52. "perf bench futex hash <options>",
  53. NULL
  54. };
  55. static void *workerfn(void *arg)
  56. {
  57. int ret;
  58. struct worker *w = (struct worker *) arg;
  59. unsigned int i;
  60. unsigned long ops = w->ops; /* avoid cacheline bouncing */
  61. pthread_mutex_lock(&thread_lock);
  62. threads_starting--;
  63. if (!threads_starting)
  64. pthread_cond_signal(&thread_parent);
  65. pthread_cond_wait(&thread_worker, &thread_lock);
  66. pthread_mutex_unlock(&thread_lock);
  67. do {
  68. for (i = 0; i < nfutexes; i++, ops++) {
  69. /*
  70. * We want the futex calls to fail in order to stress
  71. * the hashing of uaddr and not measure other steps,
  72. * such as internal waitqueue handling, thus enlarging
  73. * the critical region protected by hb->lock.
  74. */
  75. ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
  76. if (!silent &&
  77. (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
  78. warn("Non-expected futex return call");
  79. }
  80. } while (!done);
  81. w->ops = ops;
  82. return NULL;
  83. }
  84. static void toggle_done(int sig __maybe_unused,
  85. siginfo_t *info __maybe_unused,
  86. void *uc __maybe_unused)
  87. {
  88. /* inform all threads that we're done for the day */
  89. done = true;
  90. gettimeofday(&end, NULL);
  91. timersub(&end, &start, &runtime);
  92. }
  93. static void print_summary(void)
  94. {
  95. unsigned long avg = avg_stats(&throughput_stats);
  96. double stddev = stddev_stats(&throughput_stats);
  97. printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
  98. !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
  99. (int) runtime.tv_sec);
  100. }
  101. int bench_futex_hash(int argc, const char **argv)
  102. {
  103. int ret = 0;
  104. cpu_set_t cpuset;
  105. struct sigaction act;
  106. unsigned int i;
  107. pthread_attr_t thread_attr;
  108. struct worker *worker = NULL;
  109. struct cpu_map *cpu;
  110. argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
  111. if (argc) {
  112. usage_with_options(bench_futex_hash_usage, options);
  113. exit(EXIT_FAILURE);
  114. }
  115. cpu = cpu_map__new(NULL);
  116. if (!cpu)
  117. goto errmem;
  118. sigfillset(&act.sa_mask);
  119. act.sa_sigaction = toggle_done;
  120. sigaction(SIGINT, &act, NULL);
  121. if (!nthreads) /* default to the number of CPUs */
  122. nthreads = cpu->nr;
  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(&cpuset);
  143. CPU_SET(cpu->map[i % cpu->nr], &cpuset);
  144. ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset);
  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. free(cpu);
  186. return ret;
  187. errmem:
  188. err(EXIT_FAILURE, "calloc");
  189. }