sample-parsing.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdbool.h>
  3. #include <inttypes.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <linux/bitops.h>
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include "map_symbol.h"
  10. #include "branch.h"
  11. #include "event.h"
  12. #include "evsel.h"
  13. #include "debug.h"
  14. #include "util/synthetic-events.h"
  15. #include "tests.h"
  16. #define COMP(m) do { \
  17. if (s1->m != s2->m) { \
  18. pr_debug("Samples differ at '"#m"'\n"); \
  19. return false; \
  20. } \
  21. } while (0)
  22. #define MCOMP(m) do { \
  23. if (memcmp(&s1->m, &s2->m, sizeof(s1->m))) { \
  24. pr_debug("Samples differ at '"#m"'\n"); \
  25. return false; \
  26. } \
  27. } while (0)
  28. static bool samples_same(const struct perf_sample *s1,
  29. const struct perf_sample *s2,
  30. u64 type, u64 read_format)
  31. {
  32. size_t i;
  33. if (type & PERF_SAMPLE_IDENTIFIER)
  34. COMP(id);
  35. if (type & PERF_SAMPLE_IP)
  36. COMP(ip);
  37. if (type & PERF_SAMPLE_TID) {
  38. COMP(pid);
  39. COMP(tid);
  40. }
  41. if (type & PERF_SAMPLE_TIME)
  42. COMP(time);
  43. if (type & PERF_SAMPLE_ADDR)
  44. COMP(addr);
  45. if (type & PERF_SAMPLE_ID)
  46. COMP(id);
  47. if (type & PERF_SAMPLE_STREAM_ID)
  48. COMP(stream_id);
  49. if (type & PERF_SAMPLE_CPU)
  50. COMP(cpu);
  51. if (type & PERF_SAMPLE_PERIOD)
  52. COMP(period);
  53. if (type & PERF_SAMPLE_READ) {
  54. if (read_format & PERF_FORMAT_GROUP)
  55. COMP(read.group.nr);
  56. else
  57. COMP(read.one.value);
  58. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  59. COMP(read.time_enabled);
  60. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  61. COMP(read.time_running);
  62. /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
  63. if (read_format & PERF_FORMAT_GROUP) {
  64. for (i = 0; i < s1->read.group.nr; i++)
  65. MCOMP(read.group.values[i]);
  66. } else {
  67. COMP(read.one.id);
  68. }
  69. }
  70. if (type & PERF_SAMPLE_CALLCHAIN) {
  71. COMP(callchain->nr);
  72. for (i = 0; i < s1->callchain->nr; i++)
  73. COMP(callchain->ips[i]);
  74. }
  75. if (type & PERF_SAMPLE_RAW) {
  76. COMP(raw_size);
  77. if (memcmp(s1->raw_data, s2->raw_data, s1->raw_size)) {
  78. pr_debug("Samples differ at 'raw_data'\n");
  79. return false;
  80. }
  81. }
  82. if (type & PERF_SAMPLE_BRANCH_STACK) {
  83. COMP(branch_stack->nr);
  84. COMP(branch_stack->hw_idx);
  85. for (i = 0; i < s1->branch_stack->nr; i++)
  86. MCOMP(branch_stack->entries[i]);
  87. }
  88. if (type & PERF_SAMPLE_REGS_USER) {
  89. size_t sz = hweight_long(s1->user_regs.mask) * sizeof(u64);
  90. COMP(user_regs.mask);
  91. COMP(user_regs.abi);
  92. if (s1->user_regs.abi &&
  93. (!s1->user_regs.regs || !s2->user_regs.regs ||
  94. memcmp(s1->user_regs.regs, s2->user_regs.regs, sz))) {
  95. pr_debug("Samples differ at 'user_regs'\n");
  96. return false;
  97. }
  98. }
  99. if (type & PERF_SAMPLE_STACK_USER) {
  100. COMP(user_stack.size);
  101. if (memcmp(s1->user_stack.data, s2->user_stack.data,
  102. s1->user_stack.size)) {
  103. pr_debug("Samples differ at 'user_stack'\n");
  104. return false;
  105. }
  106. }
  107. if (type & PERF_SAMPLE_WEIGHT)
  108. COMP(weight);
  109. if (type & PERF_SAMPLE_DATA_SRC)
  110. COMP(data_src);
  111. if (type & PERF_SAMPLE_TRANSACTION)
  112. COMP(transaction);
  113. if (type & PERF_SAMPLE_REGS_INTR) {
  114. size_t sz = hweight_long(s1->intr_regs.mask) * sizeof(u64);
  115. COMP(intr_regs.mask);
  116. COMP(intr_regs.abi);
  117. if (s1->intr_regs.abi &&
  118. (!s1->intr_regs.regs || !s2->intr_regs.regs ||
  119. memcmp(s1->intr_regs.regs, s2->intr_regs.regs, sz))) {
  120. pr_debug("Samples differ at 'intr_regs'\n");
  121. return false;
  122. }
  123. }
  124. if (type & PERF_SAMPLE_PHYS_ADDR)
  125. COMP(phys_addr);
  126. if (type & PERF_SAMPLE_CGROUP)
  127. COMP(cgroup);
  128. if (type & PERF_SAMPLE_AUX) {
  129. COMP(aux_sample.size);
  130. if (memcmp(s1->aux_sample.data, s2->aux_sample.data,
  131. s1->aux_sample.size)) {
  132. pr_debug("Samples differ at 'aux_sample'\n");
  133. return false;
  134. }
  135. }
  136. return true;
  137. }
  138. static int do_test(u64 sample_type, u64 sample_regs, u64 read_format)
  139. {
  140. struct evsel evsel = {
  141. .needs_swap = false,
  142. .core = {
  143. . attr = {
  144. .sample_type = sample_type,
  145. .read_format = read_format,
  146. },
  147. },
  148. };
  149. union perf_event *event;
  150. union {
  151. struct ip_callchain callchain;
  152. u64 data[64];
  153. } callchain = {
  154. /* 3 ips */
  155. .data = {3, 201, 202, 203},
  156. };
  157. union {
  158. struct branch_stack branch_stack;
  159. u64 data[64];
  160. } branch_stack = {
  161. /* 1 branch_entry */
  162. .data = {1, -1ULL, 211, 212, 213},
  163. };
  164. u64 regs[64];
  165. const u32 raw_data[] = {0x12345678, 0x0a0b0c0d, 0x11020304, 0x05060708, 0 };
  166. const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL};
  167. const u64 aux_data[] = {0xa55a, 0, 0xeeddee, 0x0282028202820282};
  168. struct perf_sample sample = {
  169. .ip = 101,
  170. .pid = 102,
  171. .tid = 103,
  172. .time = 104,
  173. .addr = 105,
  174. .id = 106,
  175. .stream_id = 107,
  176. .period = 108,
  177. .weight = 109,
  178. .cpu = 110,
  179. .raw_size = sizeof(raw_data),
  180. .data_src = 111,
  181. .transaction = 112,
  182. .raw_data = (void *)raw_data,
  183. .callchain = &callchain.callchain,
  184. .no_hw_idx = false,
  185. .branch_stack = &branch_stack.branch_stack,
  186. .user_regs = {
  187. .abi = PERF_SAMPLE_REGS_ABI_64,
  188. .mask = sample_regs,
  189. .regs = regs,
  190. },
  191. .user_stack = {
  192. .size = sizeof(data),
  193. .data = (void *)data,
  194. },
  195. .read = {
  196. .time_enabled = 0x030a59d664fca7deULL,
  197. .time_running = 0x011b6ae553eb98edULL,
  198. },
  199. .intr_regs = {
  200. .abi = PERF_SAMPLE_REGS_ABI_64,
  201. .mask = sample_regs,
  202. .regs = regs,
  203. },
  204. .phys_addr = 113,
  205. .cgroup = 114,
  206. .aux_sample = {
  207. .size = sizeof(aux_data),
  208. .data = (void *)aux_data,
  209. },
  210. };
  211. struct sample_read_value values[] = {{1, 5}, {9, 3}, {2, 7}, {6, 4},};
  212. struct perf_sample sample_out;
  213. size_t i, sz, bufsz;
  214. int err, ret = -1;
  215. if (sample_type & PERF_SAMPLE_REGS_USER)
  216. evsel.core.attr.sample_regs_user = sample_regs;
  217. if (sample_type & PERF_SAMPLE_REGS_INTR)
  218. evsel.core.attr.sample_regs_intr = sample_regs;
  219. if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  220. evsel.core.attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
  221. for (i = 0; i < sizeof(regs); i++)
  222. *(i + (u8 *)regs) = i & 0xfe;
  223. if (read_format & PERF_FORMAT_GROUP) {
  224. sample.read.group.nr = 4;
  225. sample.read.group.values = values;
  226. } else {
  227. sample.read.one.value = 0x08789faeb786aa87ULL;
  228. sample.read.one.id = 99;
  229. }
  230. sz = perf_event__sample_event_size(&sample, sample_type, read_format);
  231. bufsz = sz + 4096; /* Add a bit for overrun checking */
  232. event = malloc(bufsz);
  233. if (!event) {
  234. pr_debug("malloc failed\n");
  235. return -1;
  236. }
  237. memset(event, 0xff, bufsz);
  238. event->header.type = PERF_RECORD_SAMPLE;
  239. event->header.misc = 0;
  240. event->header.size = sz;
  241. err = perf_event__synthesize_sample(event, sample_type, read_format,
  242. &sample);
  243. if (err) {
  244. pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
  245. "perf_event__synthesize_sample", sample_type, err);
  246. goto out_free;
  247. }
  248. /* The data does not contain 0xff so we use that to check the size */
  249. for (i = bufsz; i > 0; i--) {
  250. if (*(i - 1 + (u8 *)event) != 0xff)
  251. break;
  252. }
  253. if (i != sz) {
  254. pr_debug("Event size mismatch: actual %zu vs expected %zu\n",
  255. i, sz);
  256. goto out_free;
  257. }
  258. evsel.sample_size = __evsel__sample_size(sample_type);
  259. err = evsel__parse_sample(&evsel, event, &sample_out);
  260. if (err) {
  261. pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
  262. "evsel__parse_sample", sample_type, err);
  263. goto out_free;
  264. }
  265. if (!samples_same(&sample, &sample_out, sample_type, read_format)) {
  266. pr_debug("parsing failed for sample_type %#"PRIx64"\n",
  267. sample_type);
  268. goto out_free;
  269. }
  270. ret = 0;
  271. out_free:
  272. free(event);
  273. if (ret && read_format)
  274. pr_debug("read_format %#"PRIx64"\n", read_format);
  275. return ret;
  276. }
  277. /**
  278. * test__sample_parsing - test sample parsing.
  279. *
  280. * This function implements a test that synthesizes a sample event, parses it
  281. * and then checks that the parsed sample matches the original sample. The test
  282. * checks sample format bits separately and together. If the test passes %0 is
  283. * returned, otherwise %-1 is returned.
  284. */
  285. int test__sample_parsing(struct test *test __maybe_unused, int subtest __maybe_unused)
  286. {
  287. const u64 rf[] = {4, 5, 6, 7, 12, 13, 14, 15};
  288. u64 sample_type;
  289. u64 sample_regs;
  290. size_t i;
  291. int err;
  292. /*
  293. * Fail the test if it has not been updated when new sample format bits
  294. * were added. Please actually update the test rather than just change
  295. * the condition below.
  296. */
  297. if (PERF_SAMPLE_MAX > PERF_SAMPLE_CGROUP << 1) {
  298. pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n");
  299. return -1;
  300. }
  301. /* Test each sample format bit separately */
  302. for (sample_type = 1; sample_type != PERF_SAMPLE_MAX;
  303. sample_type <<= 1) {
  304. /* Test read_format variations */
  305. if (sample_type == PERF_SAMPLE_READ) {
  306. for (i = 0; i < ARRAY_SIZE(rf); i++) {
  307. err = do_test(sample_type, 0, rf[i]);
  308. if (err)
  309. return err;
  310. }
  311. continue;
  312. }
  313. sample_regs = 0;
  314. if (sample_type == PERF_SAMPLE_REGS_USER)
  315. sample_regs = 0x3fff;
  316. if (sample_type == PERF_SAMPLE_REGS_INTR)
  317. sample_regs = 0xff0fff;
  318. err = do_test(sample_type, sample_regs, 0);
  319. if (err)
  320. return err;
  321. }
  322. /* Test all sample format bits together */
  323. sample_type = PERF_SAMPLE_MAX - 1;
  324. sample_regs = 0x3fff; /* shared yb intr and user regs */
  325. for (i = 0; i < ARRAY_SIZE(rf); i++) {
  326. err = do_test(sample_type, sample_regs, rf[i]);
  327. if (err)
  328. return err;
  329. }
  330. return 0;
  331. }