perf-hwbreak.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * perf events self profiling example test case for hw breakpoints.
  4. *
  5. * This tests perf PERF_TYPE_BREAKPOINT parameters
  6. * 1) tests all variants of the break on read/write flags
  7. * 2) tests exclude_user == 0 and 1
  8. * 3) test array matches (if DAWR is supported))
  9. * 4) test different numbers of breakpoints matches
  10. *
  11. * Configure this breakpoint, then read and write the data a number of
  12. * times. Then check the output count from perf is as expected.
  13. *
  14. * Based on:
  15. * http://ozlabs.org/~anton/junkcode/perf_events_example1.c
  16. *
  17. * Copyright (C) 2018 Michael Neuling, IBM Corporation.
  18. */
  19. #define _GNU_SOURCE
  20. #include <unistd.h>
  21. #include <assert.h>
  22. #include <sched.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <signal.h>
  26. #include <string.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/wait.h>
  29. #include <sys/ptrace.h>
  30. #include <sys/resource.h>
  31. #include <sys/sysinfo.h>
  32. #include <asm/ptrace.h>
  33. #include <elf.h>
  34. #include <pthread.h>
  35. #include <sys/syscall.h>
  36. #include <linux/perf_event.h>
  37. #include <linux/hw_breakpoint.h>
  38. #include "utils.h"
  39. #ifndef PPC_DEBUG_FEATURE_DATA_BP_ARCH_31
  40. #define PPC_DEBUG_FEATURE_DATA_BP_ARCH_31 0x20
  41. #endif
  42. #define MAX_LOOPS 10000
  43. #define DAWR_LENGTH_MAX ((0x3f + 1) * 8)
  44. int nprocs;
  45. static volatile int a = 10;
  46. static volatile int b = 10;
  47. static volatile char c[512 + 8] __attribute__((aligned(512)));
  48. static void perf_event_attr_set(struct perf_event_attr *attr,
  49. __u32 type, __u64 addr, __u64 len,
  50. bool exclude_user)
  51. {
  52. memset(attr, 0, sizeof(struct perf_event_attr));
  53. attr->type = PERF_TYPE_BREAKPOINT;
  54. attr->size = sizeof(struct perf_event_attr);
  55. attr->bp_type = type;
  56. attr->bp_addr = addr;
  57. attr->bp_len = len;
  58. attr->exclude_kernel = 1;
  59. attr->exclude_hv = 1;
  60. attr->exclude_guest = 1;
  61. attr->exclude_user = exclude_user;
  62. attr->disabled = 1;
  63. }
  64. static int
  65. perf_process_event_open_exclude_user(__u32 type, __u64 addr, __u64 len, bool exclude_user)
  66. {
  67. struct perf_event_attr attr;
  68. perf_event_attr_set(&attr, type, addr, len, exclude_user);
  69. return syscall(__NR_perf_event_open, &attr, getpid(), -1, -1, 0);
  70. }
  71. static int perf_process_event_open(__u32 type, __u64 addr, __u64 len)
  72. {
  73. struct perf_event_attr attr;
  74. perf_event_attr_set(&attr, type, addr, len, 0);
  75. return syscall(__NR_perf_event_open, &attr, getpid(), -1, -1, 0);
  76. }
  77. static int perf_cpu_event_open(long cpu, __u32 type, __u64 addr, __u64 len)
  78. {
  79. struct perf_event_attr attr;
  80. perf_event_attr_set(&attr, type, addr, len, 0);
  81. return syscall(__NR_perf_event_open, &attr, -1, cpu, -1, 0);
  82. }
  83. static void close_fds(int *fd, int n)
  84. {
  85. int i;
  86. for (i = 0; i < n; i++)
  87. close(fd[i]);
  88. }
  89. static unsigned long read_fds(int *fd, int n)
  90. {
  91. int i;
  92. unsigned long c = 0;
  93. unsigned long count = 0;
  94. size_t res;
  95. for (i = 0; i < n; i++) {
  96. res = read(fd[i], &c, sizeof(c));
  97. assert(res == sizeof(unsigned long long));
  98. count += c;
  99. }
  100. return count;
  101. }
  102. static void reset_fds(int *fd, int n)
  103. {
  104. int i;
  105. for (i = 0; i < n; i++)
  106. ioctl(fd[i], PERF_EVENT_IOC_RESET);
  107. }
  108. static void enable_fds(int *fd, int n)
  109. {
  110. int i;
  111. for (i = 0; i < n; i++)
  112. ioctl(fd[i], PERF_EVENT_IOC_ENABLE);
  113. }
  114. static void disable_fds(int *fd, int n)
  115. {
  116. int i;
  117. for (i = 0; i < n; i++)
  118. ioctl(fd[i], PERF_EVENT_IOC_DISABLE);
  119. }
  120. static int perf_systemwide_event_open(int *fd, __u32 type, __u64 addr, __u64 len)
  121. {
  122. int i, ncpus, cpu, ret = 0;
  123. struct rlimit rlim;
  124. cpu_set_t *mask;
  125. size_t size;
  126. if (getrlimit(RLIMIT_NOFILE, &rlim)) {
  127. perror("getrlimit");
  128. return -1;
  129. }
  130. rlim.rlim_cur = 65536;
  131. if (setrlimit(RLIMIT_NOFILE, &rlim)) {
  132. perror("setrlimit");
  133. return -1;
  134. }
  135. ncpus = get_nprocs_conf();
  136. size = CPU_ALLOC_SIZE(ncpus);
  137. mask = CPU_ALLOC(ncpus);
  138. if (!mask) {
  139. perror("malloc");
  140. return -1;
  141. }
  142. CPU_ZERO_S(size, mask);
  143. if (sched_getaffinity(0, size, mask)) {
  144. perror("sched_getaffinity");
  145. ret = -1;
  146. goto done;
  147. }
  148. for (i = 0, cpu = 0; i < nprocs && cpu < ncpus; cpu++) {
  149. if (!CPU_ISSET_S(cpu, size, mask))
  150. continue;
  151. fd[i] = perf_cpu_event_open(cpu, type, addr, len);
  152. if (fd[i] < 0) {
  153. perror("perf_systemwide_event_open");
  154. close_fds(fd, i);
  155. ret = fd[i];
  156. goto done;
  157. }
  158. i++;
  159. }
  160. if (i < nprocs) {
  161. printf("Error: Number of online cpus reduced since start of test: %d < %d\n", i, nprocs);
  162. close_fds(fd, i);
  163. ret = -1;
  164. }
  165. done:
  166. CPU_FREE(mask);
  167. return ret;
  168. }
  169. static inline bool breakpoint_test(int len)
  170. {
  171. int fd;
  172. /* bp_addr can point anywhere but needs to be aligned */
  173. fd = perf_process_event_open(HW_BREAKPOINT_R, (__u64)(&fd) & 0xfffffffffffff800, len);
  174. if (fd < 0)
  175. return false;
  176. close(fd);
  177. return true;
  178. }
  179. static inline bool perf_breakpoint_supported(void)
  180. {
  181. return breakpoint_test(4);
  182. }
  183. static inline bool dawr_supported(void)
  184. {
  185. return breakpoint_test(DAWR_LENGTH_MAX);
  186. }
  187. static int runtestsingle(int readwriteflag, int exclude_user, int arraytest)
  188. {
  189. int i,j;
  190. size_t res;
  191. unsigned long long breaks, needed;
  192. int readint;
  193. int readintarraybig[2*DAWR_LENGTH_MAX/sizeof(int)];
  194. int *readintalign;
  195. volatile int *ptr;
  196. int break_fd;
  197. int loop_num = MAX_LOOPS - (rand() % 100); /* provide some variability */
  198. volatile int *k;
  199. __u64 len;
  200. /* align to 0x400 boundary as required by DAWR */
  201. readintalign = (int *)(((unsigned long)readintarraybig + 0x7ff) &
  202. 0xfffffffffffff800);
  203. ptr = &readint;
  204. if (arraytest)
  205. ptr = &readintalign[0];
  206. len = arraytest ? DAWR_LENGTH_MAX : sizeof(int);
  207. break_fd = perf_process_event_open_exclude_user(readwriteflag, (__u64)ptr,
  208. len, exclude_user);
  209. if (break_fd < 0) {
  210. perror("perf_process_event_open_exclude_user");
  211. exit(1);
  212. }
  213. /* start counters */
  214. ioctl(break_fd, PERF_EVENT_IOC_ENABLE);
  215. /* Test a bunch of reads and writes */
  216. k = &readint;
  217. for (i = 0; i < loop_num; i++) {
  218. if (arraytest)
  219. k = &(readintalign[i % (DAWR_LENGTH_MAX/sizeof(int))]);
  220. j = *k;
  221. *k = j;
  222. }
  223. /* stop counters */
  224. ioctl(break_fd, PERF_EVENT_IOC_DISABLE);
  225. /* read and check counters */
  226. res = read(break_fd, &breaks, sizeof(unsigned long long));
  227. assert(res == sizeof(unsigned long long));
  228. /* we read and write each loop, so subtract the ones we are counting */
  229. needed = 0;
  230. if (readwriteflag & HW_BREAKPOINT_R)
  231. needed += loop_num;
  232. if (readwriteflag & HW_BREAKPOINT_W)
  233. needed += loop_num;
  234. needed = needed * (1 - exclude_user);
  235. printf("TESTED: addr:0x%lx brks:% 8lld loops:% 8i rw:%i !user:%i array:%i\n",
  236. (unsigned long int)ptr, breaks, loop_num, readwriteflag, exclude_user, arraytest);
  237. if (breaks != needed) {
  238. printf("FAILED: 0x%lx brks:%lld needed:%lli %i %i %i\n\n",
  239. (unsigned long int)ptr, breaks, needed, loop_num, readwriteflag, exclude_user);
  240. return 1;
  241. }
  242. close(break_fd);
  243. return 0;
  244. }
  245. static int runtest_dar_outside(void)
  246. {
  247. void *target;
  248. volatile __u16 temp16;
  249. volatile __u64 temp64;
  250. int break_fd;
  251. unsigned long long breaks;
  252. int fail = 0;
  253. size_t res;
  254. target = malloc(8);
  255. if (!target) {
  256. perror("malloc failed");
  257. exit(EXIT_FAILURE);
  258. }
  259. /* watch middle half of target array */
  260. break_fd = perf_process_event_open(HW_BREAKPOINT_RW, (__u64)(target + 2), 4);
  261. if (break_fd < 0) {
  262. free(target);
  263. perror("perf_process_event_open");
  264. exit(EXIT_FAILURE);
  265. }
  266. /* Shouldn't hit. */
  267. ioctl(break_fd, PERF_EVENT_IOC_RESET);
  268. ioctl(break_fd, PERF_EVENT_IOC_ENABLE);
  269. temp16 = *((__u16 *)target);
  270. *((__u16 *)target) = temp16;
  271. ioctl(break_fd, PERF_EVENT_IOC_DISABLE);
  272. res = read(break_fd, &breaks, sizeof(unsigned long long));
  273. assert(res == sizeof(unsigned long long));
  274. if (breaks == 0) {
  275. printf("TESTED: No overlap\n");
  276. } else {
  277. printf("FAILED: No overlap: %lld != 0\n", breaks);
  278. fail = 1;
  279. }
  280. /* Hit */
  281. ioctl(break_fd, PERF_EVENT_IOC_RESET);
  282. ioctl(break_fd, PERF_EVENT_IOC_ENABLE);
  283. temp16 = *((__u16 *)(target + 1));
  284. *((__u16 *)(target + 1)) = temp16;
  285. ioctl(break_fd, PERF_EVENT_IOC_DISABLE);
  286. res = read(break_fd, &breaks, sizeof(unsigned long long));
  287. assert(res == sizeof(unsigned long long));
  288. if (breaks == 2) {
  289. printf("TESTED: Partial overlap\n");
  290. } else {
  291. printf("FAILED: Partial overlap: %lld != 2\n", breaks);
  292. fail = 1;
  293. }
  294. /* Hit */
  295. ioctl(break_fd, PERF_EVENT_IOC_RESET);
  296. ioctl(break_fd, PERF_EVENT_IOC_ENABLE);
  297. temp16 = *((__u16 *)(target + 5));
  298. *((__u16 *)(target + 5)) = temp16;
  299. ioctl(break_fd, PERF_EVENT_IOC_DISABLE);
  300. res = read(break_fd, &breaks, sizeof(unsigned long long));
  301. assert(res == sizeof(unsigned long long));
  302. if (breaks == 2) {
  303. printf("TESTED: Partial overlap\n");
  304. } else {
  305. printf("FAILED: Partial overlap: %lld != 2\n", breaks);
  306. fail = 1;
  307. }
  308. /* Shouldn't Hit */
  309. ioctl(break_fd, PERF_EVENT_IOC_RESET);
  310. ioctl(break_fd, PERF_EVENT_IOC_ENABLE);
  311. temp16 = *((__u16 *)(target + 6));
  312. *((__u16 *)(target + 6)) = temp16;
  313. ioctl(break_fd, PERF_EVENT_IOC_DISABLE);
  314. res = read(break_fd, &breaks, sizeof(unsigned long long));
  315. assert(res == sizeof(unsigned long long));
  316. if (breaks == 0) {
  317. printf("TESTED: No overlap\n");
  318. } else {
  319. printf("FAILED: No overlap: %lld != 0\n", breaks);
  320. fail = 1;
  321. }
  322. /* Hit */
  323. ioctl(break_fd, PERF_EVENT_IOC_RESET);
  324. ioctl(break_fd, PERF_EVENT_IOC_ENABLE);
  325. temp64 = *((__u64 *)target);
  326. *((__u64 *)target) = temp64;
  327. ioctl(break_fd, PERF_EVENT_IOC_DISABLE);
  328. res = read(break_fd, &breaks, sizeof(unsigned long long));
  329. assert(res == sizeof(unsigned long long));
  330. if (breaks == 2) {
  331. printf("TESTED: Full overlap\n");
  332. } else {
  333. printf("FAILED: Full overlap: %lld != 2\n", breaks);
  334. fail = 1;
  335. }
  336. free(target);
  337. close(break_fd);
  338. return fail;
  339. }
  340. static void multi_dawr_workload(void)
  341. {
  342. a += 10;
  343. b += 10;
  344. c[512 + 1] += 'a';
  345. }
  346. static int test_process_multi_diff_addr(void)
  347. {
  348. unsigned long long breaks1 = 0, breaks2 = 0;
  349. int fd1, fd2;
  350. char *desc = "Process specific, Two events, diff addr";
  351. size_t res;
  352. fd1 = perf_process_event_open(HW_BREAKPOINT_RW, (__u64)&a, (__u64)sizeof(a));
  353. if (fd1 < 0) {
  354. perror("perf_process_event_open");
  355. exit(EXIT_FAILURE);
  356. }
  357. fd2 = perf_process_event_open(HW_BREAKPOINT_RW, (__u64)&b, (__u64)sizeof(b));
  358. if (fd2 < 0) {
  359. close(fd1);
  360. perror("perf_process_event_open");
  361. exit(EXIT_FAILURE);
  362. }
  363. ioctl(fd1, PERF_EVENT_IOC_RESET);
  364. ioctl(fd2, PERF_EVENT_IOC_RESET);
  365. ioctl(fd1, PERF_EVENT_IOC_ENABLE);
  366. ioctl(fd2, PERF_EVENT_IOC_ENABLE);
  367. multi_dawr_workload();
  368. ioctl(fd1, PERF_EVENT_IOC_DISABLE);
  369. ioctl(fd2, PERF_EVENT_IOC_DISABLE);
  370. res = read(fd1, &breaks1, sizeof(breaks1));
  371. assert(res == sizeof(unsigned long long));
  372. res = read(fd2, &breaks2, sizeof(breaks2));
  373. assert(res == sizeof(unsigned long long));
  374. close(fd1);
  375. close(fd2);
  376. if (breaks1 != 2 || breaks2 != 2) {
  377. printf("FAILED: %s: %lld != 2 || %lld != 2\n", desc, breaks1, breaks2);
  378. return 1;
  379. }
  380. printf("TESTED: %s\n", desc);
  381. return 0;
  382. }
  383. static int test_process_multi_same_addr(void)
  384. {
  385. unsigned long long breaks1 = 0, breaks2 = 0;
  386. int fd1, fd2;
  387. char *desc = "Process specific, Two events, same addr";
  388. size_t res;
  389. fd1 = perf_process_event_open(HW_BREAKPOINT_RW, (__u64)&a, (__u64)sizeof(a));
  390. if (fd1 < 0) {
  391. perror("perf_process_event_open");
  392. exit(EXIT_FAILURE);
  393. }
  394. fd2 = perf_process_event_open(HW_BREAKPOINT_RW, (__u64)&a, (__u64)sizeof(a));
  395. if (fd2 < 0) {
  396. close(fd1);
  397. perror("perf_process_event_open");
  398. exit(EXIT_FAILURE);
  399. }
  400. ioctl(fd1, PERF_EVENT_IOC_RESET);
  401. ioctl(fd2, PERF_EVENT_IOC_RESET);
  402. ioctl(fd1, PERF_EVENT_IOC_ENABLE);
  403. ioctl(fd2, PERF_EVENT_IOC_ENABLE);
  404. multi_dawr_workload();
  405. ioctl(fd1, PERF_EVENT_IOC_DISABLE);
  406. ioctl(fd2, PERF_EVENT_IOC_DISABLE);
  407. res = read(fd1, &breaks1, sizeof(breaks1));
  408. assert(res == sizeof(unsigned long long));
  409. res = read(fd2, &breaks2, sizeof(breaks2));
  410. assert(res == sizeof(unsigned long long));
  411. close(fd1);
  412. close(fd2);
  413. if (breaks1 != 2 || breaks2 != 2) {
  414. printf("FAILED: %s: %lld != 2 || %lld != 2\n", desc, breaks1, breaks2);
  415. return 1;
  416. }
  417. printf("TESTED: %s\n", desc);
  418. return 0;
  419. }
  420. static int test_process_multi_diff_addr_ro_wo(void)
  421. {
  422. unsigned long long breaks1 = 0, breaks2 = 0;
  423. int fd1, fd2;
  424. char *desc = "Process specific, Two events, diff addr, one is RO, other is WO";
  425. size_t res;
  426. fd1 = perf_process_event_open(HW_BREAKPOINT_W, (__u64)&a, (__u64)sizeof(a));
  427. if (fd1 < 0) {
  428. perror("perf_process_event_open");
  429. exit(EXIT_FAILURE);
  430. }
  431. fd2 = perf_process_event_open(HW_BREAKPOINT_R, (__u64)&b, (__u64)sizeof(b));
  432. if (fd2 < 0) {
  433. close(fd1);
  434. perror("perf_process_event_open");
  435. exit(EXIT_FAILURE);
  436. }
  437. ioctl(fd1, PERF_EVENT_IOC_RESET);
  438. ioctl(fd2, PERF_EVENT_IOC_RESET);
  439. ioctl(fd1, PERF_EVENT_IOC_ENABLE);
  440. ioctl(fd2, PERF_EVENT_IOC_ENABLE);
  441. multi_dawr_workload();
  442. ioctl(fd1, PERF_EVENT_IOC_DISABLE);
  443. ioctl(fd2, PERF_EVENT_IOC_DISABLE);
  444. res = read(fd1, &breaks1, sizeof(breaks1));
  445. assert(res == sizeof(unsigned long long));
  446. res = read(fd2, &breaks2, sizeof(breaks2));
  447. assert(res == sizeof(unsigned long long));
  448. close(fd1);
  449. close(fd2);
  450. if (breaks1 != 1 || breaks2 != 1) {
  451. printf("FAILED: %s: %lld != 1 || %lld != 1\n", desc, breaks1, breaks2);
  452. return 1;
  453. }
  454. printf("TESTED: %s\n", desc);
  455. return 0;
  456. }
  457. static int test_process_multi_same_addr_ro_wo(void)
  458. {
  459. unsigned long long breaks1 = 0, breaks2 = 0;
  460. int fd1, fd2;
  461. char *desc = "Process specific, Two events, same addr, one is RO, other is WO";
  462. size_t res;
  463. fd1 = perf_process_event_open(HW_BREAKPOINT_R, (__u64)&a, (__u64)sizeof(a));
  464. if (fd1 < 0) {
  465. perror("perf_process_event_open");
  466. exit(EXIT_FAILURE);
  467. }
  468. fd2 = perf_process_event_open(HW_BREAKPOINT_W, (__u64)&a, (__u64)sizeof(a));
  469. if (fd2 < 0) {
  470. close(fd1);
  471. perror("perf_process_event_open");
  472. exit(EXIT_FAILURE);
  473. }
  474. ioctl(fd1, PERF_EVENT_IOC_RESET);
  475. ioctl(fd2, PERF_EVENT_IOC_RESET);
  476. ioctl(fd1, PERF_EVENT_IOC_ENABLE);
  477. ioctl(fd2, PERF_EVENT_IOC_ENABLE);
  478. multi_dawr_workload();
  479. ioctl(fd1, PERF_EVENT_IOC_DISABLE);
  480. ioctl(fd2, PERF_EVENT_IOC_DISABLE);
  481. res = read(fd1, &breaks1, sizeof(breaks1));
  482. assert(res == sizeof(unsigned long long));
  483. res = read(fd2, &breaks2, sizeof(breaks2));
  484. assert(res == sizeof(unsigned long long));
  485. close(fd1);
  486. close(fd2);
  487. if (breaks1 != 1 || breaks2 != 1) {
  488. printf("FAILED: %s: %lld != 1 || %lld != 1\n", desc, breaks1, breaks2);
  489. return 1;
  490. }
  491. printf("TESTED: %s\n", desc);
  492. return 0;
  493. }
  494. static int test_syswide_multi_diff_addr(void)
  495. {
  496. unsigned long long breaks1 = 0, breaks2 = 0;
  497. int *fd1 = malloc(nprocs * sizeof(int));
  498. int *fd2 = malloc(nprocs * sizeof(int));
  499. char *desc = "Systemwide, Two events, diff addr";
  500. int ret;
  501. ret = perf_systemwide_event_open(fd1, HW_BREAKPOINT_RW, (__u64)&a, (__u64)sizeof(a));
  502. if (ret)
  503. exit(EXIT_FAILURE);
  504. ret = perf_systemwide_event_open(fd2, HW_BREAKPOINT_RW, (__u64)&b, (__u64)sizeof(b));
  505. if (ret) {
  506. close_fds(fd1, nprocs);
  507. exit(EXIT_FAILURE);
  508. }
  509. reset_fds(fd1, nprocs);
  510. reset_fds(fd2, nprocs);
  511. enable_fds(fd1, nprocs);
  512. enable_fds(fd2, nprocs);
  513. multi_dawr_workload();
  514. disable_fds(fd1, nprocs);
  515. disable_fds(fd2, nprocs);
  516. breaks1 = read_fds(fd1, nprocs);
  517. breaks2 = read_fds(fd2, nprocs);
  518. close_fds(fd1, nprocs);
  519. close_fds(fd2, nprocs);
  520. free(fd1);
  521. free(fd2);
  522. if (breaks1 != 2 || breaks2 != 2) {
  523. printf("FAILED: %s: %lld != 2 || %lld != 2\n", desc, breaks1, breaks2);
  524. return 1;
  525. }
  526. printf("TESTED: %s\n", desc);
  527. return 0;
  528. }
  529. static int test_syswide_multi_same_addr(void)
  530. {
  531. unsigned long long breaks1 = 0, breaks2 = 0;
  532. int *fd1 = malloc(nprocs * sizeof(int));
  533. int *fd2 = malloc(nprocs * sizeof(int));
  534. char *desc = "Systemwide, Two events, same addr";
  535. int ret;
  536. ret = perf_systemwide_event_open(fd1, HW_BREAKPOINT_RW, (__u64)&a, (__u64)sizeof(a));
  537. if (ret)
  538. exit(EXIT_FAILURE);
  539. ret = perf_systemwide_event_open(fd2, HW_BREAKPOINT_RW, (__u64)&a, (__u64)sizeof(a));
  540. if (ret) {
  541. close_fds(fd1, nprocs);
  542. exit(EXIT_FAILURE);
  543. }
  544. reset_fds(fd1, nprocs);
  545. reset_fds(fd2, nprocs);
  546. enable_fds(fd1, nprocs);
  547. enable_fds(fd2, nprocs);
  548. multi_dawr_workload();
  549. disable_fds(fd1, nprocs);
  550. disable_fds(fd2, nprocs);
  551. breaks1 = read_fds(fd1, nprocs);
  552. breaks2 = read_fds(fd2, nprocs);
  553. close_fds(fd1, nprocs);
  554. close_fds(fd2, nprocs);
  555. free(fd1);
  556. free(fd2);
  557. if (breaks1 != 2 || breaks2 != 2) {
  558. printf("FAILED: %s: %lld != 2 || %lld != 2\n", desc, breaks1, breaks2);
  559. return 1;
  560. }
  561. printf("TESTED: %s\n", desc);
  562. return 0;
  563. }
  564. static int test_syswide_multi_diff_addr_ro_wo(void)
  565. {
  566. unsigned long long breaks1 = 0, breaks2 = 0;
  567. int *fd1 = malloc(nprocs * sizeof(int));
  568. int *fd2 = malloc(nprocs * sizeof(int));
  569. char *desc = "Systemwide, Two events, diff addr, one is RO, other is WO";
  570. int ret;
  571. ret = perf_systemwide_event_open(fd1, HW_BREAKPOINT_W, (__u64)&a, (__u64)sizeof(a));
  572. if (ret)
  573. exit(EXIT_FAILURE);
  574. ret = perf_systemwide_event_open(fd2, HW_BREAKPOINT_R, (__u64)&b, (__u64)sizeof(b));
  575. if (ret) {
  576. close_fds(fd1, nprocs);
  577. exit(EXIT_FAILURE);
  578. }
  579. reset_fds(fd1, nprocs);
  580. reset_fds(fd2, nprocs);
  581. enable_fds(fd1, nprocs);
  582. enable_fds(fd2, nprocs);
  583. multi_dawr_workload();
  584. disable_fds(fd1, nprocs);
  585. disable_fds(fd2, nprocs);
  586. breaks1 = read_fds(fd1, nprocs);
  587. breaks2 = read_fds(fd2, nprocs);
  588. close_fds(fd1, nprocs);
  589. close_fds(fd2, nprocs);
  590. free(fd1);
  591. free(fd2);
  592. if (breaks1 != 1 || breaks2 != 1) {
  593. printf("FAILED: %s: %lld != 1 || %lld != 1\n", desc, breaks1, breaks2);
  594. return 1;
  595. }
  596. printf("TESTED: %s\n", desc);
  597. return 0;
  598. }
  599. static int test_syswide_multi_same_addr_ro_wo(void)
  600. {
  601. unsigned long long breaks1 = 0, breaks2 = 0;
  602. int *fd1 = malloc(nprocs * sizeof(int));
  603. int *fd2 = malloc(nprocs * sizeof(int));
  604. char *desc = "Systemwide, Two events, same addr, one is RO, other is WO";
  605. int ret;
  606. ret = perf_systemwide_event_open(fd1, HW_BREAKPOINT_W, (__u64)&a, (__u64)sizeof(a));
  607. if (ret)
  608. exit(EXIT_FAILURE);
  609. ret = perf_systemwide_event_open(fd2, HW_BREAKPOINT_R, (__u64)&a, (__u64)sizeof(a));
  610. if (ret) {
  611. close_fds(fd1, nprocs);
  612. exit(EXIT_FAILURE);
  613. }
  614. reset_fds(fd1, nprocs);
  615. reset_fds(fd2, nprocs);
  616. enable_fds(fd1, nprocs);
  617. enable_fds(fd2, nprocs);
  618. multi_dawr_workload();
  619. disable_fds(fd1, nprocs);
  620. disable_fds(fd2, nprocs);
  621. breaks1 = read_fds(fd1, nprocs);
  622. breaks2 = read_fds(fd2, nprocs);
  623. close_fds(fd1, nprocs);
  624. close_fds(fd2, nprocs);
  625. free(fd1);
  626. free(fd2);
  627. if (breaks1 != 1 || breaks2 != 1) {
  628. printf("FAILED: %s: %lld != 1 || %lld != 1\n", desc, breaks1, breaks2);
  629. return 1;
  630. }
  631. printf("TESTED: %s\n", desc);
  632. return 0;
  633. }
  634. static int runtest_multi_dawr(void)
  635. {
  636. int ret = 0;
  637. ret |= test_process_multi_diff_addr();
  638. ret |= test_process_multi_same_addr();
  639. ret |= test_process_multi_diff_addr_ro_wo();
  640. ret |= test_process_multi_same_addr_ro_wo();
  641. ret |= test_syswide_multi_diff_addr();
  642. ret |= test_syswide_multi_same_addr();
  643. ret |= test_syswide_multi_diff_addr_ro_wo();
  644. ret |= test_syswide_multi_same_addr_ro_wo();
  645. return ret;
  646. }
  647. static int runtest_unaligned_512bytes(void)
  648. {
  649. unsigned long long breaks = 0;
  650. int fd;
  651. char *desc = "Process specific, 512 bytes, unaligned";
  652. __u64 addr = (__u64)&c + 8;
  653. size_t res;
  654. fd = perf_process_event_open(HW_BREAKPOINT_RW, addr, 512);
  655. if (fd < 0) {
  656. perror("perf_process_event_open");
  657. exit(EXIT_FAILURE);
  658. }
  659. ioctl(fd, PERF_EVENT_IOC_RESET);
  660. ioctl(fd, PERF_EVENT_IOC_ENABLE);
  661. multi_dawr_workload();
  662. ioctl(fd, PERF_EVENT_IOC_DISABLE);
  663. res = read(fd, &breaks, sizeof(breaks));
  664. assert(res == sizeof(unsigned long long));
  665. close(fd);
  666. if (breaks != 2) {
  667. printf("FAILED: %s: %lld != 2\n", desc, breaks);
  668. return 1;
  669. }
  670. printf("TESTED: %s\n", desc);
  671. return 0;
  672. }
  673. /* There is no perf api to find number of available watchpoints. Use ptrace. */
  674. static int get_nr_wps(bool *arch_31)
  675. {
  676. struct ppc_debug_info dbginfo;
  677. int child_pid;
  678. child_pid = fork();
  679. if (!child_pid) {
  680. int ret = ptrace(PTRACE_TRACEME, 0, NULL, 0);
  681. if (ret) {
  682. perror("PTRACE_TRACEME failed\n");
  683. exit(EXIT_FAILURE);
  684. }
  685. kill(getpid(), SIGUSR1);
  686. sleep(1);
  687. exit(EXIT_SUCCESS);
  688. }
  689. wait(NULL);
  690. if (ptrace(PPC_PTRACE_GETHWDBGINFO, child_pid, NULL, &dbginfo)) {
  691. perror("Can't get breakpoint info");
  692. exit(EXIT_FAILURE);
  693. }
  694. *arch_31 = !!(dbginfo.features & PPC_DEBUG_FEATURE_DATA_BP_ARCH_31);
  695. return dbginfo.num_data_bps;
  696. }
  697. static int runtest(void)
  698. {
  699. int rwflag;
  700. int exclude_user;
  701. int ret;
  702. bool dawr = dawr_supported();
  703. bool arch_31 = false;
  704. int nr_wps = get_nr_wps(&arch_31);
  705. /*
  706. * perf defines rwflag as two bits read and write and at least
  707. * one must be set. So range 1-3.
  708. */
  709. for (rwflag = 1 ; rwflag < 4; rwflag++) {
  710. for (exclude_user = 0 ; exclude_user < 2; exclude_user++) {
  711. ret = runtestsingle(rwflag, exclude_user, 0);
  712. if (ret)
  713. return ret;
  714. /* if we have the dawr, we can do an array test */
  715. if (!dawr)
  716. continue;
  717. ret = runtestsingle(rwflag, exclude_user, 1);
  718. if (ret)
  719. return ret;
  720. }
  721. }
  722. ret = runtest_dar_outside();
  723. if (ret)
  724. return ret;
  725. if (dawr && nr_wps > 1) {
  726. nprocs = get_nprocs();
  727. ret = runtest_multi_dawr();
  728. if (ret)
  729. return ret;
  730. }
  731. if (dawr && arch_31)
  732. ret = runtest_unaligned_512bytes();
  733. return ret;
  734. }
  735. static int perf_hwbreak(void)
  736. {
  737. srand ( time(NULL) );
  738. SKIP_IF_MSG(!perf_breakpoint_supported(), "Perf breakpoints not supported");
  739. return runtest();
  740. }
  741. int main(int argc, char *argv[], char **envp)
  742. {
  743. return test_harness(perf_hwbreak, "perf_hwbreak");
  744. }