1 // SPDX-License-Identifier: GPL-2.0-only
6 #include <linux/bitops.h>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
13 #include "util/sample.h"
14 #include "util/synthetic-events.h"
16 #include "tests/tests.h"
17 #include "arch-tests.h"
19 #define COMP(m) do { \
20 if (s1->m != s2->m) { \
21 pr_debug("Samples differ at '"#m"'\n"); \
26 static bool samples_same(const struct perf_sample *s1,
27 const struct perf_sample *s2,
30 if (type & PERF_SAMPLE_WEIGHT_STRUCT) {
38 static int do_test(u64 sample_type)
40 struct evsel evsel = {
44 .sample_type = sample_type,
49 union perf_event *event;
50 struct perf_sample sample = {
55 struct perf_sample sample_out;
59 sz = perf_event__sample_event_size(&sample, sample_type, 0);
60 bufsz = sz + 4096; /* Add a bit for overrun checking */
61 event = malloc(bufsz);
63 pr_debug("malloc failed\n");
67 memset(event, 0xff, bufsz);
68 event->header.type = PERF_RECORD_SAMPLE;
69 event->header.misc = 0;
70 event->header.size = sz;
72 err = perf_event__synthesize_sample(event, sample_type, 0, &sample);
74 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
75 "perf_event__synthesize_sample", sample_type, err);
79 /* The data does not contain 0xff so we use that to check the size */
80 for (i = bufsz; i > 0; i--) {
81 if (*(i - 1 + (u8 *)event) != 0xff)
85 pr_debug("Event size mismatch: actual %zu vs expected %zu\n",
90 evsel.sample_size = __evsel__sample_size(sample_type);
92 err = evsel__parse_sample(&evsel, event, &sample_out);
94 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
95 "evsel__parse_sample", sample_type, err);
99 if (!samples_same(&sample, &sample_out, sample_type)) {
100 pr_debug("parsing failed for sample_type %#"PRIx64"\n",
113 * test__x86_sample_parsing - test X86 specific sample parsing
115 * This function implements a test that synthesizes a sample event, parses it
116 * and then checks that the parsed sample matches the original sample. If the
117 * test passes %0 is returned, otherwise %-1 is returned.
119 * For now, the PERF_SAMPLE_WEIGHT_STRUCT is the only X86 specific sample type.
120 * The test only checks the PERF_SAMPLE_WEIGHT_STRUCT type.
122 int test__x86_sample_parsing(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
124 return do_test(PERF_SAMPLE_WEIGHT_STRUCT);