sample-parsing.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include <stdbool.h>
  2. #include <inttypes.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, u64 type, u64 regs_user,
  21. 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(regs_user) * sizeof(u64);
  80. COMP(user_regs.abi);
  81. if (s1->user_regs.abi &&
  82. (!s1->user_regs.regs || !s2->user_regs.regs ||
  83. memcmp(s1->user_regs.regs, s2->user_regs.regs, sz))) {
  84. pr_debug("Samples differ at 'user_regs'\n");
  85. return false;
  86. }
  87. }
  88. if (type & PERF_SAMPLE_STACK_USER) {
  89. COMP(user_stack.size);
  90. if (memcmp(s1->user_stack.data, s1->user_stack.data,
  91. s1->user_stack.size)) {
  92. pr_debug("Samples differ at 'user_stack'\n");
  93. return false;
  94. }
  95. }
  96. if (type & PERF_SAMPLE_WEIGHT)
  97. COMP(weight);
  98. if (type & PERF_SAMPLE_DATA_SRC)
  99. COMP(data_src);
  100. if (type & PERF_SAMPLE_TRANSACTION)
  101. COMP(transaction);
  102. return true;
  103. }
  104. static int do_test(u64 sample_type, u64 sample_regs_user, u64 read_format)
  105. {
  106. struct perf_evsel evsel = {
  107. .needs_swap = false,
  108. .attr = {
  109. .sample_type = sample_type,
  110. .sample_regs_user = sample_regs_user,
  111. .read_format = read_format,
  112. },
  113. };
  114. union perf_event *event;
  115. union {
  116. struct ip_callchain callchain;
  117. u64 data[64];
  118. } callchain = {
  119. /* 3 ips */
  120. .data = {3, 201, 202, 203},
  121. };
  122. union {
  123. struct branch_stack branch_stack;
  124. u64 data[64];
  125. } branch_stack = {
  126. /* 1 branch_entry */
  127. .data = {1, 211, 212, 213},
  128. };
  129. u64 user_regs[64];
  130. const u64 raw_data[] = {0x123456780a0b0c0dULL, 0x1102030405060708ULL};
  131. const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL};
  132. struct perf_sample sample = {
  133. .ip = 101,
  134. .pid = 102,
  135. .tid = 103,
  136. .time = 104,
  137. .addr = 105,
  138. .id = 106,
  139. .stream_id = 107,
  140. .period = 108,
  141. .weight = 109,
  142. .cpu = 110,
  143. .raw_size = sizeof(raw_data),
  144. .data_src = 111,
  145. .transaction = 112,
  146. .raw_data = (void *)raw_data,
  147. .callchain = &callchain.callchain,
  148. .branch_stack = &branch_stack.branch_stack,
  149. .user_regs = {
  150. .abi = PERF_SAMPLE_REGS_ABI_64,
  151. .regs = user_regs,
  152. },
  153. .user_stack = {
  154. .size = sizeof(data),
  155. .data = (void *)data,
  156. },
  157. .read = {
  158. .time_enabled = 0x030a59d664fca7deULL,
  159. .time_running = 0x011b6ae553eb98edULL,
  160. },
  161. };
  162. struct sample_read_value values[] = {{1, 5}, {9, 3}, {2, 7}, {6, 4},};
  163. struct perf_sample sample_out;
  164. size_t i, sz, bufsz;
  165. int err, ret = -1;
  166. for (i = 0; i < sizeof(user_regs); i++)
  167. *(i + (u8 *)user_regs) = i & 0xfe;
  168. if (read_format & PERF_FORMAT_GROUP) {
  169. sample.read.group.nr = 4;
  170. sample.read.group.values = values;
  171. } else {
  172. sample.read.one.value = 0x08789faeb786aa87ULL;
  173. sample.read.one.id = 99;
  174. }
  175. sz = perf_event__sample_event_size(&sample, sample_type,
  176. sample_regs_user, read_format);
  177. bufsz = sz + 4096; /* Add a bit for overrun checking */
  178. event = malloc(bufsz);
  179. if (!event) {
  180. pr_debug("malloc failed\n");
  181. return -1;
  182. }
  183. memset(event, 0xff, bufsz);
  184. event->header.type = PERF_RECORD_SAMPLE;
  185. event->header.misc = 0;
  186. event->header.size = sz;
  187. err = perf_event__synthesize_sample(event, sample_type,
  188. sample_regs_user, 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,
  213. sample_regs_user, 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. }