futex-hash.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 <linux/zalloc.h>
  20. #include <sys/time.h>
  21. #include <sys/mman.h>
  22. #include <perf/cpumap.h>
  23. #include "../util/mutex.h"
  24. #include "../util/stat.h"
  25. #include <subcmd/parse-options.h>
  26. #include "bench.h"
  27. #include "futex.h"
  28. #include <err.h>
  29. static bool done = false;
  30. static int futex_flag = 0;
  31. struct timeval bench__start, bench__end, bench__runtime;
  32. static struct mutex thread_lock;
  33. static unsigned int threads_starting;
  34. static struct stats throughput_stats;
  35. static struct cond 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 struct bench_futex_parameters params = {
  43. .nfutexes = 1024,
  44. .runtime = 10,
  45. };
  46. static const struct option options[] = {
  47. OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
  48. OPT_UINTEGER('r', "runtime", &params.runtime, "Specify runtime (in seconds)"),
  49. OPT_UINTEGER('f', "futexes", &params.nfutexes, "Specify amount of futexes per threads"),
  50. OPT_BOOLEAN( 's', "silent", &params.silent, "Silent mode: do not display data/details"),
  51. OPT_BOOLEAN( 'S', "shared", &params.fshared, "Use shared futexes instead of private ones"),
  52. OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
  53. OPT_END()
  54. };
  55. static const char * const bench_futex_hash_usage[] = {
  56. "perf bench futex hash <options>",
  57. NULL
  58. };
  59. static void *workerfn(void *arg)
  60. {
  61. int ret;
  62. struct worker *w = (struct worker *) arg;
  63. unsigned int i;
  64. unsigned long ops = w->ops; /* avoid cacheline bouncing */
  65. mutex_lock(&thread_lock);
  66. threads_starting--;
  67. if (!threads_starting)
  68. cond_signal(&thread_parent);
  69. cond_wait(&thread_worker, &thread_lock);
  70. mutex_unlock(&thread_lock);
  71. do {
  72. for (i = 0; i < params.nfutexes; i++, ops++) {
  73. /*
  74. * We want the futex calls to fail in order to stress
  75. * the hashing of uaddr and not measure other steps,
  76. * such as internal waitqueue handling, thus enlarging
  77. * the critical region protected by hb->lock.
  78. */
  79. ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
  80. if (!params.silent &&
  81. (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
  82. warn("Non-expected futex return call");
  83. }
  84. } while (!done);
  85. w->ops = ops;
  86. return NULL;
  87. }
  88. static void toggle_done(int sig __maybe_unused,
  89. siginfo_t *info __maybe_unused,
  90. void *uc __maybe_unused)
  91. {
  92. /* inform all threads that we're done for the day */
  93. done = true;
  94. gettimeofday(&bench__end, NULL);
  95. timersub(&bench__end, &bench__start, &bench__runtime);
  96. }
  97. static void print_summary(void)
  98. {
  99. unsigned long avg = avg_stats(&throughput_stats);
  100. double stddev = stddev_stats(&throughput_stats);
  101. printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
  102. !params.silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
  103. (int)bench__runtime.tv_sec);
  104. }
  105. int bench_futex_hash(int argc, const char **argv)
  106. {
  107. int ret = 0;
  108. cpu_set_t *cpuset;
  109. struct sigaction act;
  110. unsigned int i;
  111. pthread_attr_t thread_attr;
  112. struct worker *worker = NULL;
  113. struct perf_cpu_map *cpu;
  114. int nrcpus;
  115. size_t size;
  116. argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
  117. if (argc) {
  118. usage_with_options(bench_futex_hash_usage, options);
  119. exit(EXIT_FAILURE);
  120. }
  121. cpu = perf_cpu_map__new_online_cpus();
  122. if (!cpu)
  123. goto errmem;
  124. memset(&act, 0, sizeof(act));
  125. sigfillset(&act.sa_mask);
  126. act.sa_sigaction = toggle_done;
  127. sigaction(SIGINT, &act, NULL);
  128. if (params.mlockall) {
  129. if (mlockall(MCL_CURRENT | MCL_FUTURE))
  130. err(EXIT_FAILURE, "mlockall");
  131. }
  132. if (!params.nthreads) /* default to the number of CPUs */
  133. params.nthreads = perf_cpu_map__nr(cpu);
  134. worker = calloc(params.nthreads, sizeof(*worker));
  135. if (!worker)
  136. goto errmem;
  137. if (!params.fshared)
  138. futex_flag = FUTEX_PRIVATE_FLAG;
  139. printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
  140. getpid(), params.nthreads, params.nfutexes, params.fshared ? "shared":"private", params.runtime);
  141. init_stats(&throughput_stats);
  142. mutex_init(&thread_lock);
  143. cond_init(&thread_parent);
  144. cond_init(&thread_worker);
  145. threads_starting = params.nthreads;
  146. pthread_attr_init(&thread_attr);
  147. gettimeofday(&bench__start, NULL);
  148. nrcpus = cpu__max_cpu().cpu;
  149. cpuset = CPU_ALLOC(nrcpus);
  150. BUG_ON(!cpuset);
  151. size = CPU_ALLOC_SIZE(nrcpus);
  152. for (i = 0; i < params.nthreads; i++) {
  153. worker[i].tid = i;
  154. worker[i].futex = calloc(params.nfutexes, sizeof(*worker[i].futex));
  155. if (!worker[i].futex)
  156. goto errmem;
  157. CPU_ZERO_S(size, cpuset);
  158. CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
  159. ret = pthread_attr_setaffinity_np(&thread_attr, size, cpuset);
  160. if (ret) {
  161. CPU_FREE(cpuset);
  162. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  163. }
  164. ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
  165. (void *)(struct worker *) &worker[i]);
  166. if (ret) {
  167. CPU_FREE(cpuset);
  168. err(EXIT_FAILURE, "pthread_create");
  169. }
  170. }
  171. CPU_FREE(cpuset);
  172. pthread_attr_destroy(&thread_attr);
  173. mutex_lock(&thread_lock);
  174. while (threads_starting)
  175. cond_wait(&thread_parent, &thread_lock);
  176. cond_broadcast(&thread_worker);
  177. mutex_unlock(&thread_lock);
  178. sleep(params.runtime);
  179. toggle_done(0, NULL, NULL);
  180. for (i = 0; i < params.nthreads; i++) {
  181. ret = pthread_join(worker[i].thread, NULL);
  182. if (ret)
  183. err(EXIT_FAILURE, "pthread_join");
  184. }
  185. /* cleanup & report results */
  186. cond_destroy(&thread_parent);
  187. cond_destroy(&thread_worker);
  188. mutex_destroy(&thread_lock);
  189. for (i = 0; i < params.nthreads; i++) {
  190. unsigned long t = bench__runtime.tv_sec > 0 ?
  191. worker[i].ops / bench__runtime.tv_sec : 0;
  192. update_stats(&throughput_stats, t);
  193. if (!params.silent) {
  194. if (params.nfutexes == 1)
  195. printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
  196. worker[i].tid, &worker[i].futex[0], t);
  197. else
  198. printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
  199. worker[i].tid, &worker[i].futex[0],
  200. &worker[i].futex[params.nfutexes-1], t);
  201. }
  202. zfree(&worker[i].futex);
  203. }
  204. print_summary();
  205. free(worker);
  206. free(cpu);
  207. return ret;
  208. errmem:
  209. err(EXIT_FAILURE, "calloc");
  210. }