sample-parsing.c 8.0 KB

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