breakpoint.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <subcmd/parse-options.h>
  3. #include <linux/hw_breakpoint.h>
  4. #include <linux/perf_event.h>
  5. #include <linux/time64.h>
  6. #include <sys/syscall.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/time.h>
  9. #include <pthread.h>
  10. #include <stddef.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <stdio.h>
  14. #include <errno.h>
  15. #include "bench.h"
  16. #include "futex.h"
  17. struct {
  18. unsigned int nbreakpoints;
  19. unsigned int nparallel;
  20. unsigned int nthreads;
  21. } thread_params = {
  22. .nbreakpoints = 1,
  23. .nparallel = 1,
  24. .nthreads = 1,
  25. };
  26. static const struct option thread_options[] = {
  27. OPT_UINTEGER('b', "breakpoints", &thread_params.nbreakpoints,
  28. "Specify amount of breakpoints"),
  29. OPT_UINTEGER('p', "parallelism", &thread_params.nparallel, "Specify amount of parallelism"),
  30. OPT_UINTEGER('t', "threads", &thread_params.nthreads, "Specify amount of threads"),
  31. OPT_END()
  32. };
  33. static const char * const thread_usage[] = {
  34. "perf bench breakpoint thread <options>",
  35. NULL
  36. };
  37. struct breakpoint {
  38. int fd;
  39. char watched;
  40. };
  41. static int breakpoint_setup(void *addr)
  42. {
  43. struct perf_event_attr attr = { .size = 0, };
  44. int fd;
  45. attr.type = PERF_TYPE_BREAKPOINT;
  46. attr.size = sizeof(attr);
  47. attr.inherit = 1;
  48. attr.exclude_kernel = 1;
  49. attr.exclude_hv = 1;
  50. attr.bp_addr = (unsigned long)addr;
  51. attr.bp_type = HW_BREAKPOINT_RW;
  52. attr.bp_len = HW_BREAKPOINT_LEN_1;
  53. fd = syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
  54. if (fd < 0)
  55. fd = -errno;
  56. return fd;
  57. }
  58. static void *passive_thread(void *arg)
  59. {
  60. unsigned int *done = (unsigned int *)arg;
  61. while (!__atomic_load_n(done, __ATOMIC_RELAXED))
  62. futex_wait(done, 0, NULL, 0);
  63. return NULL;
  64. }
  65. static void *active_thread(void *arg)
  66. {
  67. unsigned int *done = (unsigned int *)arg;
  68. while (!__atomic_load_n(done, __ATOMIC_RELAXED));
  69. return NULL;
  70. }
  71. static void *breakpoint_thread(void *arg)
  72. {
  73. unsigned int i, done;
  74. int *repeat = (int *)arg;
  75. pthread_t *threads;
  76. threads = calloc(thread_params.nthreads, sizeof(threads[0]));
  77. if (!threads)
  78. exit((perror("calloc"), EXIT_FAILURE));
  79. while (__atomic_fetch_sub(repeat, 1, __ATOMIC_RELAXED) > 0) {
  80. done = 0;
  81. for (i = 0; i < thread_params.nthreads; i++) {
  82. if (pthread_create(&threads[i], NULL, passive_thread, &done))
  83. exit((perror("pthread_create"), EXIT_FAILURE));
  84. }
  85. __atomic_store_n(&done, 1, __ATOMIC_RELAXED);
  86. futex_wake(&done, thread_params.nthreads, 0);
  87. for (i = 0; i < thread_params.nthreads; i++)
  88. pthread_join(threads[i], NULL);
  89. }
  90. free(threads);
  91. return NULL;
  92. }
  93. // The benchmark creates nbreakpoints inheritable breakpoints,
  94. // then starts nparallel threads which create and join bench_repeat batches of nthreads threads.
  95. int bench_breakpoint_thread(int argc, const char **argv)
  96. {
  97. unsigned int i, result_usec;
  98. int repeat = bench_repeat;
  99. struct breakpoint *breakpoints;
  100. pthread_t *parallel;
  101. struct timeval start, stop, diff;
  102. if (parse_options(argc, argv, thread_options, thread_usage, 0)) {
  103. usage_with_options(thread_usage, thread_options);
  104. exit(EXIT_FAILURE);
  105. }
  106. breakpoints = calloc(thread_params.nbreakpoints, sizeof(breakpoints[0]));
  107. parallel = calloc(thread_params.nparallel, sizeof(parallel[0]));
  108. if (!breakpoints || !parallel)
  109. exit((perror("calloc"), EXIT_FAILURE));
  110. for (i = 0; i < thread_params.nbreakpoints; i++) {
  111. breakpoints[i].fd = breakpoint_setup(&breakpoints[i].watched);
  112. if (breakpoints[i].fd < 0) {
  113. if (breakpoints[i].fd == -ENODEV) {
  114. printf("Skipping perf bench breakpoint thread: No hardware support\n");
  115. return 0;
  116. }
  117. exit((perror("perf_event_open"), EXIT_FAILURE));
  118. }
  119. }
  120. gettimeofday(&start, NULL);
  121. for (i = 0; i < thread_params.nparallel; i++) {
  122. if (pthread_create(&parallel[i], NULL, breakpoint_thread, &repeat))
  123. exit((perror("pthread_create"), EXIT_FAILURE));
  124. }
  125. for (i = 0; i < thread_params.nparallel; i++)
  126. pthread_join(parallel[i], NULL);
  127. gettimeofday(&stop, NULL);
  128. timersub(&stop, &start, &diff);
  129. for (i = 0; i < thread_params.nbreakpoints; i++)
  130. close(breakpoints[i].fd);
  131. free(parallel);
  132. free(breakpoints);
  133. switch (bench_format) {
  134. case BENCH_FORMAT_DEFAULT:
  135. printf("# Created/joined %d threads with %d breakpoints and %d parallelism\n",
  136. bench_repeat, thread_params.nbreakpoints, thread_params.nparallel);
  137. printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
  138. (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
  139. result_usec = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
  140. printf(" %14lf usecs/op\n",
  141. (double)result_usec / bench_repeat / thread_params.nthreads);
  142. printf(" %14lf usecs/op/cpu\n",
  143. (double)result_usec / bench_repeat /
  144. thread_params.nthreads * thread_params.nparallel);
  145. break;
  146. case BENCH_FORMAT_SIMPLE:
  147. printf("%lu.%03lu\n", (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
  148. break;
  149. default:
  150. fprintf(stderr, "Unknown format: %d\n", bench_format);
  151. exit(EXIT_FAILURE);
  152. }
  153. return 0;
  154. }
  155. struct {
  156. unsigned int npassive;
  157. unsigned int nactive;
  158. } enable_params = {
  159. .nactive = 0,
  160. .npassive = 0,
  161. };
  162. static const struct option enable_options[] = {
  163. OPT_UINTEGER('p', "passive", &enable_params.npassive, "Specify amount of passive threads"),
  164. OPT_UINTEGER('a', "active", &enable_params.nactive, "Specify amount of active threads"),
  165. OPT_END()
  166. };
  167. static const char * const enable_usage[] = {
  168. "perf bench breakpoint enable <options>",
  169. NULL
  170. };
  171. // The benchmark creates an inheritable breakpoint,
  172. // then starts npassive threads that block and nactive threads that actively spin
  173. // and then disables and enables the breakpoint bench_repeat times.
  174. int bench_breakpoint_enable(int argc, const char **argv)
  175. {
  176. unsigned int i, nthreads, result_usec, done = 0;
  177. char watched;
  178. int fd;
  179. pthread_t *threads;
  180. struct timeval start, stop, diff;
  181. if (parse_options(argc, argv, enable_options, enable_usage, 0)) {
  182. usage_with_options(enable_usage, enable_options);
  183. exit(EXIT_FAILURE);
  184. }
  185. fd = breakpoint_setup(&watched);
  186. if (fd < 0) {
  187. if (fd == -ENODEV) {
  188. printf("Skipping perf bench breakpoint enable: No hardware support\n");
  189. return 0;
  190. }
  191. exit((perror("perf_event_open"), EXIT_FAILURE));
  192. }
  193. nthreads = enable_params.npassive + enable_params.nactive;
  194. threads = calloc(nthreads, sizeof(threads[0]));
  195. if (!threads)
  196. exit((perror("calloc"), EXIT_FAILURE));
  197. for (i = 0; i < nthreads; i++) {
  198. if (pthread_create(&threads[i], NULL,
  199. i < enable_params.npassive ? passive_thread : active_thread, &done))
  200. exit((perror("pthread_create"), EXIT_FAILURE));
  201. }
  202. usleep(10000); // let the threads block
  203. gettimeofday(&start, NULL);
  204. for (i = 0; i < bench_repeat; i++) {
  205. if (ioctl(fd, PERF_EVENT_IOC_DISABLE, 0))
  206. exit((perror("ioctl(PERF_EVENT_IOC_DISABLE)"), EXIT_FAILURE));
  207. if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0))
  208. exit((perror("ioctl(PERF_EVENT_IOC_ENABLE)"), EXIT_FAILURE));
  209. }
  210. gettimeofday(&stop, NULL);
  211. timersub(&stop, &start, &diff);
  212. __atomic_store_n(&done, 1, __ATOMIC_RELAXED);
  213. futex_wake(&done, enable_params.npassive, 0);
  214. for (i = 0; i < nthreads; i++)
  215. pthread_join(threads[i], NULL);
  216. free(threads);
  217. close(fd);
  218. switch (bench_format) {
  219. case BENCH_FORMAT_DEFAULT:
  220. printf("# Enabled/disabled breakpoint %d time with %d passive and %d active threads\n",
  221. bench_repeat, enable_params.npassive, enable_params.nactive);
  222. printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
  223. (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
  224. result_usec = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
  225. printf(" %14lf usecs/op\n", (double)result_usec / bench_repeat);
  226. break;
  227. case BENCH_FORMAT_SIMPLE:
  228. printf("%lu.%03lu\n", (long)diff.tv_sec, (long)(diff.tv_usec / USEC_PER_MSEC));
  229. break;
  230. default:
  231. fprintf(stderr, "Unknown format: %d\n", bench_format);
  232. exit(EXIT_FAILURE);
  233. }
  234. return 0;
  235. }