0001
0002 #include <stdbool.h>
0003 #include <inttypes.h>
0004 #include <stdlib.h>
0005 #include <string.h>
0006 #include <linux/bitops.h>
0007 #include <linux/kernel.h>
0008 #include <linux/types.h>
0009
0010 #include "map_symbol.h"
0011 #include "branch.h"
0012 #include "event.h"
0013 #include "evsel.h"
0014 #include "debug.h"
0015 #include "util/synthetic-events.h"
0016 #include "util/trace-event.h"
0017
0018 #include "tests.h"
0019
0020 #define COMP(m) do { \
0021 if (s1->m != s2->m) { \
0022 pr_debug("Samples differ at '"#m"'\n"); \
0023 return false; \
0024 } \
0025 } while (0)
0026
0027 #define MCOMP(m) do { \
0028 if (memcmp(&s1->m, &s2->m, sizeof(s1->m))) { \
0029 pr_debug("Samples differ at '"#m"'\n"); \
0030 return false; \
0031 } \
0032 } while (0)
0033
0034
0035
0036
0037
0038
0039 #define BS_EXPECTED_BE 0xa000d00000000000
0040 #define BS_EXPECTED_LE 0xd5000000
0041 #define FLAG(s) s->branch_stack->entries[i].flags
0042
0043 static bool samples_same(const struct perf_sample *s1,
0044 const struct perf_sample *s2,
0045 u64 type, u64 read_format, bool needs_swap)
0046 {
0047 size_t i;
0048
0049 if (type & PERF_SAMPLE_IDENTIFIER)
0050 COMP(id);
0051
0052 if (type & PERF_SAMPLE_IP)
0053 COMP(ip);
0054
0055 if (type & PERF_SAMPLE_TID) {
0056 COMP(pid);
0057 COMP(tid);
0058 }
0059
0060 if (type & PERF_SAMPLE_TIME)
0061 COMP(time);
0062
0063 if (type & PERF_SAMPLE_ADDR)
0064 COMP(addr);
0065
0066 if (type & PERF_SAMPLE_ID)
0067 COMP(id);
0068
0069 if (type & PERF_SAMPLE_STREAM_ID)
0070 COMP(stream_id);
0071
0072 if (type & PERF_SAMPLE_CPU)
0073 COMP(cpu);
0074
0075 if (type & PERF_SAMPLE_PERIOD)
0076 COMP(period);
0077
0078 if (type & PERF_SAMPLE_READ) {
0079 if (read_format & PERF_FORMAT_GROUP)
0080 COMP(read.group.nr);
0081 else
0082 COMP(read.one.value);
0083 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
0084 COMP(read.time_enabled);
0085 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
0086 COMP(read.time_running);
0087
0088 if (read_format & PERF_FORMAT_GROUP) {
0089 for (i = 0; i < s1->read.group.nr; i++) {
0090
0091 if (read_format & PERF_FORMAT_LOST)
0092 MCOMP(read.group.values[i]);
0093 }
0094 } else {
0095 COMP(read.one.id);
0096 if (read_format & PERF_FORMAT_LOST)
0097 COMP(read.one.lost);
0098 }
0099 }
0100
0101 if (type & PERF_SAMPLE_CALLCHAIN) {
0102 COMP(callchain->nr);
0103 for (i = 0; i < s1->callchain->nr; i++)
0104 COMP(callchain->ips[i]);
0105 }
0106
0107 if (type & PERF_SAMPLE_RAW) {
0108 COMP(raw_size);
0109 if (memcmp(s1->raw_data, s2->raw_data, s1->raw_size)) {
0110 pr_debug("Samples differ at 'raw_data'\n");
0111 return false;
0112 }
0113 }
0114
0115 if (type & PERF_SAMPLE_BRANCH_STACK) {
0116 COMP(branch_stack->nr);
0117 COMP(branch_stack->hw_idx);
0118 for (i = 0; i < s1->branch_stack->nr; i++) {
0119 if (needs_swap)
0120 return ((tep_is_bigendian()) ?
0121 (FLAG(s2).value == BS_EXPECTED_BE) :
0122 (FLAG(s2).value == BS_EXPECTED_LE));
0123 else
0124 MCOMP(branch_stack->entries[i]);
0125 }
0126 }
0127
0128 if (type & PERF_SAMPLE_REGS_USER) {
0129 size_t sz = hweight_long(s1->user_regs.mask) * sizeof(u64);
0130
0131 COMP(user_regs.mask);
0132 COMP(user_regs.abi);
0133 if (s1->user_regs.abi &&
0134 (!s1->user_regs.regs || !s2->user_regs.regs ||
0135 memcmp(s1->user_regs.regs, s2->user_regs.regs, sz))) {
0136 pr_debug("Samples differ at 'user_regs'\n");
0137 return false;
0138 }
0139 }
0140
0141 if (type & PERF_SAMPLE_STACK_USER) {
0142 COMP(user_stack.size);
0143 if (memcmp(s1->user_stack.data, s2->user_stack.data,
0144 s1->user_stack.size)) {
0145 pr_debug("Samples differ at 'user_stack'\n");
0146 return false;
0147 }
0148 }
0149
0150 if (type & PERF_SAMPLE_WEIGHT)
0151 COMP(weight);
0152
0153 if (type & PERF_SAMPLE_DATA_SRC)
0154 COMP(data_src);
0155
0156 if (type & PERF_SAMPLE_TRANSACTION)
0157 COMP(transaction);
0158
0159 if (type & PERF_SAMPLE_REGS_INTR) {
0160 size_t sz = hweight_long(s1->intr_regs.mask) * sizeof(u64);
0161
0162 COMP(intr_regs.mask);
0163 COMP(intr_regs.abi);
0164 if (s1->intr_regs.abi &&
0165 (!s1->intr_regs.regs || !s2->intr_regs.regs ||
0166 memcmp(s1->intr_regs.regs, s2->intr_regs.regs, sz))) {
0167 pr_debug("Samples differ at 'intr_regs'\n");
0168 return false;
0169 }
0170 }
0171
0172 if (type & PERF_SAMPLE_PHYS_ADDR)
0173 COMP(phys_addr);
0174
0175 if (type & PERF_SAMPLE_CGROUP)
0176 COMP(cgroup);
0177
0178 if (type & PERF_SAMPLE_DATA_PAGE_SIZE)
0179 COMP(data_page_size);
0180
0181 if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
0182 COMP(code_page_size);
0183
0184 if (type & PERF_SAMPLE_AUX) {
0185 COMP(aux_sample.size);
0186 if (memcmp(s1->aux_sample.data, s2->aux_sample.data,
0187 s1->aux_sample.size)) {
0188 pr_debug("Samples differ at 'aux_sample'\n");
0189 return false;
0190 }
0191 }
0192
0193 return true;
0194 }
0195
0196 static int do_test(u64 sample_type, u64 sample_regs, u64 read_format)
0197 {
0198 struct evsel evsel = {
0199 .needs_swap = false,
0200 .core = {
0201 . attr = {
0202 .sample_type = sample_type,
0203 .read_format = read_format,
0204 },
0205 },
0206 };
0207 union perf_event *event;
0208 union {
0209 struct ip_callchain callchain;
0210 u64 data[64];
0211 } callchain = {
0212
0213 .data = {3, 201, 202, 203},
0214 };
0215 union {
0216 struct branch_stack branch_stack;
0217 u64 data[64];
0218 } branch_stack = {
0219
0220 .data = {1, -1ULL, 211, 212, 213},
0221 };
0222 u64 regs[64];
0223 const u32 raw_data[] = {0x12345678, 0x0a0b0c0d, 0x11020304, 0x05060708, 0 };
0224 const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL};
0225 const u64 aux_data[] = {0xa55a, 0, 0xeeddee, 0x0282028202820282};
0226 struct perf_sample sample = {
0227 .ip = 101,
0228 .pid = 102,
0229 .tid = 103,
0230 .time = 104,
0231 .addr = 105,
0232 .id = 106,
0233 .stream_id = 107,
0234 .period = 108,
0235 .weight = 109,
0236 .cpu = 110,
0237 .raw_size = sizeof(raw_data),
0238 .data_src = 111,
0239 .transaction = 112,
0240 .raw_data = (void *)raw_data,
0241 .callchain = &callchain.callchain,
0242 .no_hw_idx = false,
0243 .branch_stack = &branch_stack.branch_stack,
0244 .user_regs = {
0245 .abi = PERF_SAMPLE_REGS_ABI_64,
0246 .mask = sample_regs,
0247 .regs = regs,
0248 },
0249 .user_stack = {
0250 .size = sizeof(data),
0251 .data = (void *)data,
0252 },
0253 .read = {
0254 .time_enabled = 0x030a59d664fca7deULL,
0255 .time_running = 0x011b6ae553eb98edULL,
0256 },
0257 .intr_regs = {
0258 .abi = PERF_SAMPLE_REGS_ABI_64,
0259 .mask = sample_regs,
0260 .regs = regs,
0261 },
0262 .phys_addr = 113,
0263 .cgroup = 114,
0264 .data_page_size = 115,
0265 .code_page_size = 116,
0266 .aux_sample = {
0267 .size = sizeof(aux_data),
0268 .data = (void *)aux_data,
0269 },
0270 };
0271 struct sample_read_value values[] = {{1, 5, 0}, {9, 3, 0}, {2, 7, 0}, {6, 4, 1},};
0272 struct perf_sample sample_out, sample_out_endian;
0273 size_t i, sz, bufsz;
0274 int err, ret = -1;
0275
0276 if (sample_type & PERF_SAMPLE_REGS_USER)
0277 evsel.core.attr.sample_regs_user = sample_regs;
0278
0279 if (sample_type & PERF_SAMPLE_REGS_INTR)
0280 evsel.core.attr.sample_regs_intr = sample_regs;
0281
0282 if (sample_type & PERF_SAMPLE_BRANCH_STACK)
0283 evsel.core.attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
0284
0285 for (i = 0; i < sizeof(regs); i++)
0286 *(i + (u8 *)regs) = i & 0xfe;
0287
0288 if (read_format & PERF_FORMAT_GROUP) {
0289 sample.read.group.nr = 4;
0290 sample.read.group.values = values;
0291 } else {
0292 sample.read.one.value = 0x08789faeb786aa87ULL;
0293 sample.read.one.id = 99;
0294 sample.read.one.lost = 1;
0295 }
0296
0297 sz = perf_event__sample_event_size(&sample, sample_type, read_format);
0298 bufsz = sz + 4096;
0299 event = malloc(bufsz);
0300 if (!event) {
0301 pr_debug("malloc failed\n");
0302 return -1;
0303 }
0304
0305 memset(event, 0xff, bufsz);
0306 event->header.type = PERF_RECORD_SAMPLE;
0307 event->header.misc = 0;
0308 event->header.size = sz;
0309
0310 err = perf_event__synthesize_sample(event, sample_type, read_format,
0311 &sample);
0312 if (err) {
0313 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
0314 "perf_event__synthesize_sample", sample_type, err);
0315 goto out_free;
0316 }
0317
0318
0319 for (i = bufsz; i > 0; i--) {
0320 if (*(i - 1 + (u8 *)event) != 0xff)
0321 break;
0322 }
0323 if (i != sz) {
0324 pr_debug("Event size mismatch: actual %zu vs expected %zu\n",
0325 i, sz);
0326 goto out_free;
0327 }
0328
0329 evsel.sample_size = __evsel__sample_size(sample_type);
0330
0331 err = evsel__parse_sample(&evsel, event, &sample_out);
0332 if (err) {
0333 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
0334 "evsel__parse_sample", sample_type, err);
0335 goto out_free;
0336 }
0337
0338 if (!samples_same(&sample, &sample_out, sample_type, read_format, evsel.needs_swap)) {
0339 pr_debug("parsing failed for sample_type %#"PRIx64"\n",
0340 sample_type);
0341 goto out_free;
0342 }
0343
0344 if (sample_type == PERF_SAMPLE_BRANCH_STACK) {
0345 evsel.needs_swap = true;
0346 evsel.sample_size = __evsel__sample_size(sample_type);
0347 err = evsel__parse_sample(&evsel, event, &sample_out_endian);
0348 if (err) {
0349 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
0350 "evsel__parse_sample", sample_type, err);
0351 goto out_free;
0352 }
0353
0354 if (!samples_same(&sample, &sample_out_endian, sample_type, read_format, evsel.needs_swap)) {
0355 pr_debug("parsing failed for sample_type %#"PRIx64"\n",
0356 sample_type);
0357 goto out_free;
0358 }
0359 }
0360
0361 ret = 0;
0362 out_free:
0363 free(event);
0364 if (ret && read_format)
0365 pr_debug("read_format %#"PRIx64"\n", read_format);
0366 return ret;
0367 }
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377 static int test__sample_parsing(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0378 {
0379 const u64 rf[] = {4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 28, 29, 30, 31};
0380 u64 sample_type;
0381 u64 sample_regs;
0382 size_t i;
0383 int err;
0384
0385
0386
0387
0388
0389
0390 if (PERF_SAMPLE_MAX > PERF_SAMPLE_WEIGHT_STRUCT << 1) {
0391 pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n");
0392 return -1;
0393 }
0394
0395
0396 for (sample_type = 1; sample_type != PERF_SAMPLE_MAX;
0397 sample_type <<= 1) {
0398
0399 if (sample_type == PERF_SAMPLE_READ) {
0400 for (i = 0; i < ARRAY_SIZE(rf); i++) {
0401 err = do_test(sample_type, 0, rf[i]);
0402 if (err)
0403 return err;
0404 }
0405 continue;
0406 }
0407 sample_regs = 0;
0408
0409 if (sample_type == PERF_SAMPLE_REGS_USER)
0410 sample_regs = 0x3fff;
0411
0412 if (sample_type == PERF_SAMPLE_REGS_INTR)
0413 sample_regs = 0xff0fff;
0414
0415 err = do_test(sample_type, sample_regs, 0);
0416 if (err)
0417 return err;
0418 }
0419
0420
0421
0422
0423
0424
0425 sample_type = (PERF_SAMPLE_MAX - 1) & ~PERF_SAMPLE_WEIGHT;
0426 sample_regs = 0x3fff;
0427 for (i = 0; i < ARRAY_SIZE(rf); i++) {
0428 err = do_test(sample_type, sample_regs, rf[i]);
0429 if (err)
0430 return err;
0431 }
0432
0433 return 0;
0434 }
0435
0436 DEFINE_SUITE("Sample parsing", sample_parsing);