sample-parsing.c 8.1 KB

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