sample-parsing.c 8.2 KB

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