futex-lock-pi.c 5.3 KB

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