sample-parsing.c 7.4 KB

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