futex-lock-pi.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 requeue <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. pthread_mutex_lock(&thread_lock);
  68. threads_starting--;
  69. if (!threads_starting)
  70. pthread_cond_signal(&thread_parent);
  71. pthread_cond_wait(&thread_worker, &thread_lock);
  72. pthread_mutex_unlock(&thread_lock);
  73. do {
  74. int ret;
  75. again:
  76. ret = futex_lock_pi(w->futex, NULL, futex_flag);
  77. if (ret) { /* handle lock acquisition */
  78. if (!silent)
  79. warn("thread %d: Could not lock pi-lock for %p (%d)",
  80. w->tid, w->futex, ret);
  81. if (done)
  82. break;
  83. goto again;
  84. }
  85. usleep(1);
  86. ret = futex_unlock_pi(w->futex, futex_flag);
  87. if (ret && !silent)
  88. warn("thread %d: Could not unlock pi-lock for %p (%d)",
  89. w->tid, w->futex, ret);
  90. w->ops++; /* account for thread's share of work */
  91. } while (!done);
  92. return NULL;
  93. }
  94. static void create_threads(struct worker *w, pthread_attr_t thread_attr)
  95. {
  96. cpu_set_t cpu;
  97. unsigned int i;
  98. threads_starting = nthreads;
  99. for (i = 0; i < nthreads; i++) {
  100. worker[i].tid = i;
  101. if (multi) {
  102. worker[i].futex = calloc(1, sizeof(u_int32_t));
  103. if (!worker[i].futex)
  104. err(EXIT_FAILURE, "calloc");
  105. } else
  106. worker[i].futex = &global_futex;
  107. CPU_ZERO(&cpu);
  108. CPU_SET(i % ncpus, &cpu);
  109. if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
  110. err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  111. if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i]))
  112. err(EXIT_FAILURE, "pthread_create");
  113. }
  114. }
  115. int bench_futex_lock_pi(int argc, const char **argv,
  116. const char *prefix __maybe_unused)
  117. {
  118. int ret = 0;
  119. unsigned int i;
  120. struct sigaction act;
  121. pthread_attr_t thread_attr;
  122. argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0);
  123. if (argc)
  124. goto err;
  125. ncpus = sysconf(_SC_NPROCESSORS_ONLN);
  126. sigfillset(&act.sa_mask);
  127. act.sa_sigaction = toggle_done;
  128. sigaction(SIGINT, &act, NULL);
  129. if (!nthreads)
  130. nthreads = ncpus;
  131. worker = calloc(nthreads, sizeof(*worker));
  132. if (!worker)
  133. err(EXIT_FAILURE, "calloc");
  134. if (!fshared)
  135. futex_flag = FUTEX_PRIVATE_FLAG;
  136. printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
  137. getpid(), nthreads, nsecs);
  138. init_stats(&throughput_stats);
  139. pthread_mutex_init(&thread_lock, NULL);
  140. pthread_cond_init(&thread_parent, NULL);
  141. pthread_cond_init(&thread_worker, NULL);
  142. threads_starting = nthreads;
  143. pthread_attr_init(&thread_attr);
  144. gettimeofday(&start, NULL);
  145. create_threads(worker, thread_attr);
  146. pthread_attr_destroy(&thread_attr);
  147. pthread_mutex_lock(&thread_lock);
  148. while (threads_starting)
  149. pthread_cond_wait(&thread_parent, &thread_lock);
  150. pthread_cond_broadcast(&thread_worker);
  151. pthread_mutex_unlock(&thread_lock);
  152. sleep(nsecs);
  153. toggle_done(0, NULL, NULL);
  154. for (i = 0; i < nthreads; i++) {
  155. ret = pthread_join(worker[i].thread, NULL);
  156. if (ret)
  157. err(EXIT_FAILURE, "pthread_join");
  158. }
  159. /* cleanup & report results */
  160. pthread_cond_destroy(&thread_parent);
  161. pthread_cond_destroy(&thread_worker);
  162. pthread_mutex_destroy(&thread_lock);
  163. for (i = 0; i < nthreads; i++) {
  164. unsigned long t = worker[i].ops/runtime.tv_sec;
  165. update_stats(&throughput_stats, t);
  166. if (!silent)
  167. printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
  168. worker[i].tid, worker[i].futex, t);
  169. if (multi)
  170. free(worker[i].futex);
  171. }
  172. print_summary();
  173. free(worker);
  174. return ret;
  175. err:
  176. usage_with_options(bench_futex_lock_pi_usage, options);
  177. exit(EXIT_FAILURE);
  178. }