sample-parsing.c 7.4 KB

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