futex-lock-pi.c 5.3 KB

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