futex-lock-pi.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2015 Davidlohr Bueso.
  3. */
  4. /* For the CLR_() macros */
  5. #include <pthread.h>
  6. #include <signal.h>
  7. #include "../util/stat.h"
  8. #include <subcmd/parse-options.h>
  9. #include <linux/compiler.h>
  10. #include <linux/kernel.h>
  11. #include <errno.h>
  12. #include "bench.h"
  13. #include "futex.h"
  14. #include <err.h>
  15. #include <stdlib.h>
  16. #include <sys/time.h>
  17. struct worker {
  18. int tid;
  19. u_int32_t *futex;
  20. pthread_t thread;
  21. unsigned long ops;
  22. };
  23. static u_int32_t global_futex = 0;
  24. static struct worker *worker;
  25. static unsigned int nsecs = 10;
  26. static bool silent = false, multi = false;
  27. static bool done = false, fshared = false;
  28. static unsigned int ncpus, nthreads = 0;
  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. static const struct option options[] = {
  36. OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
  37. OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
  38. OPT_BOOLEAN( 'M', "multi", &multi, "Use multiple futexes"),
  39. OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
  40. OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
  41. OPT_END()
  42. };
  43. static const char * const bench_futex_lock_pi_usage[] = {
  44. "perf bench futex lock-pi <options>",
  45. NULL
  46. };
  47. static void print_summary(void)
  48. {
  49. unsigned long avg = avg_stats(&throughput_stats);
  50. double stddev = stddev_stats(&throughput_stats);
  51. printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
  52. !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
  53. (int) runtime.tv_sec);
  54. }
  55. static void toggle_done(int sig __maybe_unused,
  56. siginfo_t *info __maybe_unused,
  57. void *uc __maybe_unused)
  58. {
  59. /* inform all threads that we're done for the day */
  60. done = true;
  61. gettimeofday(&end, NULL);
  62. timersub(&end, &start, &runtime);
  63. }
  64. static void *workerfn(void *arg)
  65. {
  66. struct worker *w = (struct worker *) arg;
  67. unsigned long ops = w->ops;
  68. pthread_mutex_lock(&thread_lock);
  69. threads_starting--;
  70. if (!threads_starting)
  71. pthread_cond_signal(&thread_parent);
  72. pthread_cond_wait(&thread_worker, &thread_lock);
  73. pthread_mutex_unlock(&thread_lock);
  74. do {
  75. int ret;
  76. again:
  77. ret = futex_lock_pi(w->futex, NULL, futex_flag);
  78. if (ret) { /* handle lock acquisition */
  79. if (!silent)
  80. warn("thread %d: Could not lock pi-lock for %p (%d)",
  81. w->tid, w->futex, ret);
  82. if (done)
  83. break;
  84. goto again;
  85. }
  86. usleep(1);
  87. ret = futex_unlock_pi(w->futex, futex_flag);
  88. if (ret && !silent)
  89. warn("thread %d: Could not unlock pi-lock for %p (%d)",
  90. w->tid, w->futex, ret);
  91. ops++; /* account for thread's share of work */
  92. } while (!done);
  93. w->ops = ops;
  94. return NULL;
  95. }
  96. static void create_threads(struct worker *w, pthread_attr_t thread_attr)
  97. {
  98. cpu_set_t cpu;
  99. unsigned int i;
  100. threads_starting = nthreads;
  101. for (i = 0; i < nthreads; i++) {
  102. worker[i].tid = i;
  103. if (multi) {
  104. worker[i].futex = calloc(1, sizeof(u_int32_t));
  105. if (!worker[i].futex)
  106. err(EXIT_FAILURE, "calloc");
  107. } else
  108. worker[i].futex = &global_futex;
  109. CPU_ZERO(&cpu);
  110. CPU_SET(i % ncpus, &cpu);
  111. if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
  112. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  113. if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i]))
  114. err(EXIT_FAILURE, "pthread_create");
  115. }
  116. }
  117. int bench_futex_lock_pi(int argc, const char **argv,
  118. const char *prefix __maybe_unused)
  119. {
  120. int ret = 0;
  121. unsigned int i;
  122. struct sigaction act;
  123. pthread_attr_t thread_attr;
  124. argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0);
  125. if (argc)
  126. goto err;
  127. ncpus = sysconf(_SC_NPROCESSORS_ONLN);
  128. nsecs = futexbench_sanitize_numeric(nsecs);
  129. sigfillset(&act.sa_mask);
  130. act.sa_sigaction = toggle_done;
  131. sigaction(SIGINT, &act, NULL);
  132. if (!nthreads)
  133. nthreads = ncpus;
  134. else
  135. nthreads = futexbench_sanitize_numeric(nthreads);
  136. worker = calloc(nthreads, sizeof(*worker));
  137. if (!worker)
  138. err(EXIT_FAILURE, "calloc");
  139. if (!fshared)
  140. futex_flag = FUTEX_PRIVATE_FLAG;
  141. printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
  142. getpid(), nthreads, nsecs);
  143. init_stats(&throughput_stats);
  144. pthread_mutex_init(&thread_lock, NULL);
  145. pthread_cond_init(&thread_parent, NULL);
  146. pthread_cond_init(&thread_worker, NULL);
  147. threads_starting = nthreads;
  148. pthread_attr_init(&thread_attr);
  149. gettimeofday(&start, NULL);
  150. create_threads(worker, thread_attr);
  151. pthread_attr_destroy(&thread_attr);
  152. pthread_mutex_lock(&thread_lock);
  153. while (threads_starting)
  154. pthread_cond_wait(&thread_parent, &thread_lock);
  155. pthread_cond_broadcast(&thread_worker);
  156. pthread_mutex_unlock(&thread_lock);
  157. sleep(nsecs);
  158. toggle_done(0, NULL, NULL);
  159. for (i = 0; i < nthreads; i++) {
  160. ret = pthread_join(worker[i].thread, NULL);
  161. if (ret)
  162. err(EXIT_FAILURE, "pthread_join");
  163. }
  164. /* cleanup & report results */
  165. pthread_cond_destroy(&thread_parent);
  166. pthread_cond_destroy(&thread_worker);
  167. pthread_mutex_destroy(&thread_lock);
  168. for (i = 0; i < nthreads; i++) {
  169. unsigned long t = worker[i].ops/runtime.tv_sec;
  170. update_stats(&throughput_stats, t);
  171. if (!silent)
  172. printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
  173. worker[i].tid, worker[i].futex, t);
  174. if (multi)
  175. free(worker[i].futex);
  176. }
  177. print_summary();
  178. free(worker);
  179. return ret;
  180. err:
  181. usage_with_options(bench_futex_lock_pi_usage, options);
  182. exit(EXIT_FAILURE);
  183. }