sample-parsing.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. return true;
  101. }
  102. static int do_test(u64 sample_type, u64 sample_regs_user, u64 read_format)
  103. {
  104. struct perf_evsel evsel = {
  105. .needs_swap = false,
  106. .attr = {
  107. .sample_type = sample_type,
  108. .sample_regs_user = sample_regs_user,
  109. .read_format = read_format,
  110. },
  111. };
  112. union perf_event *event;
  113. union {
  114. struct ip_callchain callchain;
  115. u64 data[64];
  116. } callchain = {
  117. /* 3 ips */
  118. .data = {3, 201, 202, 203},
  119. };
  120. union {
  121. struct branch_stack branch_stack;
  122. u64 data[64];
  123. } branch_stack = {
  124. /* 1 branch_entry */
  125. .data = {1, 211, 212, 213},
  126. };
  127. u64 user_regs[64];
  128. const u64 raw_data[] = {0x123456780a0b0c0dULL, 0x1102030405060708ULL};
  129. const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL};
  130. struct perf_sample sample = {
  131. .ip = 101,
  132. .pid = 102,
  133. .tid = 103,
  134. .time = 104,
  135. .addr = 105,
  136. .id = 106,
  137. .stream_id = 107,
  138. .period = 108,
  139. .weight = 109,
  140. .cpu = 110,
  141. .raw_size = sizeof(raw_data),
  142. .data_src = 111,
  143. .raw_data = (void *)raw_data,
  144. .callchain = &callchain.callchain,
  145. .branch_stack = &branch_stack.branch_stack,
  146. .user_regs = {
  147. .abi = PERF_SAMPLE_REGS_ABI_64,
  148. .regs = user_regs,
  149. },
  150. .user_stack = {
  151. .size = sizeof(data),
  152. .data = (void *)data,
  153. },
  154. .read = {
  155. .time_enabled = 0x030a59d664fca7deULL,
  156. .time_running = 0x011b6ae553eb98edULL,
  157. },
  158. };
  159. struct sample_read_value values[] = {{1, 5}, {9, 3}, {2, 7}, {6, 4},};
  160. struct perf_sample sample_out;
  161. size_t i, sz, bufsz;
  162. int err, ret = -1;
  163. for (i = 0; i < sizeof(user_regs); i++)
  164. *(i + (u8 *)user_regs) = i & 0xfe;
  165. if (read_format & PERF_FORMAT_GROUP) {
  166. sample.read.group.nr = 4;
  167. sample.read.group.values = values;
  168. } else {
  169. sample.read.one.value = 0x08789faeb786aa87ULL;
  170. sample.read.one.id = 99;
  171. }
  172. sz = perf_event__sample_event_size(&sample, sample_type,
  173. sample_regs_user, read_format);
  174. bufsz = sz + 4096; /* Add a bit for overrun checking */
  175. event = malloc(bufsz);
  176. if (!event) {
  177. pr_debug("malloc failed\n");
  178. return -1;
  179. }
  180. memset(event, 0xff, bufsz);
  181. event->header.type = PERF_RECORD_SAMPLE;
  182. event->header.misc = 0;
  183. event->header.size = sz;
  184. err = perf_event__synthesize_sample(event, sample_type,
  185. sample_regs_user, read_format,
  186. &sample, false);
  187. if (err) {
  188. pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
  189. "perf_event__synthesize_sample", sample_type, err);
  190. goto out_free;
  191. }
  192. /* The data does not contain 0xff so we use that to check the size */
  193. for (i = bufsz; i > 0; i--) {
  194. if (*(i - 1 + (u8 *)event) != 0xff)
  195. break;
  196. }
  197. if (i != sz) {
  198. pr_debug("Event size mismatch: actual %zu vs expected %zu\n",
  199. i, sz);
  200. goto out_free;
  201. }
  202. evsel.sample_size = __perf_evsel__sample_size(sample_type);
  203. err = perf_evsel__parse_sample(&evsel, event, &sample_out);
  204. if (err) {
  205. pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
  206. "perf_evsel__parse_sample", sample_type, err);
  207. goto out_free;
  208. }
  209. if (!samples_same(&sample, &sample_out, sample_type,
  210. sample_regs_user, read_format)) {
  211. pr_debug("parsing failed for sample_type %#"PRIx64"\n",
  212. sample_type);
  213. goto out_free;
  214. }
  215. ret = 0;
  216. out_free:
  217. free(event);
  218. if (ret && read_format)
  219. pr_debug("read_format %#"PRIx64"\n", read_format);
  220. return ret;
  221. }
  222. /**
  223. * test__sample_parsing - test sample parsing.
  224. *
  225. * This function implements a test that synthesizes a sample event, parses it
  226. * and then checks that the parsed sample matches the original sample. The test
  227. * checks sample format bits separately and together. If the test passes %0 is
  228. * returned, otherwise %-1 is returned.
  229. */
  230. int test__sample_parsing(void)
  231. {
  232. const u64 rf[] = {4, 5, 6, 7, 12, 13, 14, 15};
  233. u64 sample_type;
  234. u64 sample_regs_user;
  235. size_t i;
  236. int err;
  237. /*
  238. * Fail the test if it has not been updated when new sample format bits
  239. * were added.
  240. */
  241. if (PERF_SAMPLE_MAX > PERF_SAMPLE_IDENTIFIER << 1) {
  242. pr_debug("sample format has changed - test needs updating\n");
  243. return -1;
  244. }
  245. /* Test each sample format bit separately */
  246. for (sample_type = 1; sample_type != PERF_SAMPLE_MAX;
  247. sample_type <<= 1) {
  248. /* Test read_format variations */
  249. if (sample_type == PERF_SAMPLE_READ) {
  250. for (i = 0; i < ARRAY_SIZE(rf); i++) {
  251. err = do_test(sample_type, 0, rf[i]);
  252. if (err)
  253. return err;
  254. }
  255. continue;
  256. }
  257. if (sample_type == PERF_SAMPLE_REGS_USER)
  258. sample_regs_user = 0x3fff;
  259. else
  260. sample_regs_user = 0;
  261. err = do_test(sample_type, sample_regs_user, 0);
  262. if (err)
  263. return err;
  264. }
  265. /* Test all sample format bits together */
  266. sample_type = PERF_SAMPLE_MAX - 1;
  267. sample_regs_user = 0x3fff;
  268. for (i = 0; i < ARRAY_SIZE(rf); i++) {
  269. err = do_test(sample_type, sample_regs_user, rf[i]);
  270. if (err)
  271. return err;
  272. }
  273. return 0;
  274. }