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/synthetic-events.h"
15 #include "tests/tests.h"
16 #include "arch-tests.h"
18 #define COMP(m) do { \
19 if (s1->m != s2->m) { \
20 pr_debug("Samples differ at '"#m"'\n"); \
25 static bool samples_same(const struct perf_sample *s1,
26 const struct perf_sample *s2,
29 if (type & PERF_SAMPLE_WEIGHT_STRUCT)
35 static int do_test(u64 sample_type)
37 struct evsel evsel = {
41 .sample_type = sample_type,
46 union perf_event *event;
47 struct perf_sample sample = {
51 struct perf_sample sample_out;
55 sz = perf_event__sample_event_size(&sample, sample_type, 0);
56 bufsz = sz + 4096; /* Add a bit for overrun checking */
57 event = malloc(bufsz);
59 pr_debug("malloc failed\n");
63 memset(event, 0xff, bufsz);
64 event->header.type = PERF_RECORD_SAMPLE;
65 event->header.misc = 0;
66 event->header.size = sz;
68 err = perf_event__synthesize_sample(event, sample_type, 0, &sample);
70 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
71 "perf_event__synthesize_sample", sample_type, err);
75 /* The data does not contain 0xff so we use that to check the size */
76 for (i = bufsz; i > 0; i--) {
77 if (*(i - 1 + (u8 *)event) != 0xff)
81 pr_debug("Event size mismatch: actual %zu vs expected %zu\n",
86 evsel.sample_size = __evsel__sample_size(sample_type);
88 err = evsel__parse_sample(&evsel, event, &sample_out);
90 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
91 "evsel__parse_sample", sample_type, err);
95 if (!samples_same(&sample, &sample_out, sample_type)) {
96 pr_debug("parsing failed for sample_type %#"PRIx64"\n",
109 * test__x86_sample_parsing - test X86 specific sample parsing
111 * This function implements a test that synthesizes a sample event, parses it
112 * and then checks that the parsed sample matches the original sample. If the
113 * test passes %0 is returned, otherwise %-1 is returned.
115 * For now, the PERF_SAMPLE_WEIGHT_STRUCT is the only X86 specific sample type.
116 * The test only checks the PERF_SAMPLE_WEIGHT_STRUCT type.
118 int test__x86_sample_parsing(struct test *test __maybe_unused, int subtest __maybe_unused)
120 return do_test(PERF_SAMPLE_WEIGHT_STRUCT);