Commit | Line | Data |
---|---|---|
045f8cd8 | 1 | #include <stdbool.h> |
d944c4ee | 2 | #include <linux/types.h> |
045f8cd8 AH |
3 | |
4 | #include "util.h" | |
5 | #include "event.h" | |
6 | #include "evsel.h" | |
84f5d36f | 7 | #include "debug.h" |
045f8cd8 AH |
8 | |
9 | #include "tests.h" | |
10 | ||
11 | #define COMP(m) do { \ | |
12 | if (s1->m != s2->m) { \ | |
13 | pr_debug("Samples differ at '"#m"'\n"); \ | |
14 | return false; \ | |
15 | } \ | |
16 | } while (0) | |
17 | ||
18 | #define MCOMP(m) do { \ | |
19 | if (memcmp(&s1->m, &s2->m, sizeof(s1->m))) { \ | |
20 | pr_debug("Samples differ at '"#m"'\n"); \ | |
21 | return false; \ | |
22 | } \ | |
23 | } while (0) | |
24 | ||
25 | static bool samples_same(const struct perf_sample *s1, | |
352ea45a JO |
26 | const struct perf_sample *s2, |
27 | u64 type, u64 read_format) | |
045f8cd8 AH |
28 | { |
29 | size_t i; | |
30 | ||
31 | if (type & PERF_SAMPLE_IDENTIFIER) | |
32 | COMP(id); | |
33 | ||
34 | if (type & PERF_SAMPLE_IP) | |
35 | COMP(ip); | |
36 | ||
37 | if (type & PERF_SAMPLE_TID) { | |
38 | COMP(pid); | |
39 | COMP(tid); | |
40 | } | |
41 | ||
42 | if (type & PERF_SAMPLE_TIME) | |
43 | COMP(time); | |
44 | ||
45 | if (type & PERF_SAMPLE_ADDR) | |
46 | COMP(addr); | |
47 | ||
48 | if (type & PERF_SAMPLE_ID) | |
49 | COMP(id); | |
50 | ||
51 | if (type & PERF_SAMPLE_STREAM_ID) | |
52 | COMP(stream_id); | |
53 | ||
54 | if (type & PERF_SAMPLE_CPU) | |
55 | COMP(cpu); | |
56 | ||
57 | if (type & PERF_SAMPLE_PERIOD) | |
58 | COMP(period); | |
59 | ||
60 | if (type & PERF_SAMPLE_READ) { | |
61 | if (read_format & PERF_FORMAT_GROUP) | |
62 | COMP(read.group.nr); | |
63 | else | |
64 | COMP(read.one.value); | |
65 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
66 | COMP(read.time_enabled); | |
67 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
68 | COMP(read.time_running); | |
69 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ | |
70 | if (read_format & PERF_FORMAT_GROUP) { | |
71 | for (i = 0; i < s1->read.group.nr; i++) | |
72 | MCOMP(read.group.values[i]); | |
73 | } else { | |
74 | COMP(read.one.id); | |
75 | } | |
76 | } | |
77 | ||
78 | if (type & PERF_SAMPLE_CALLCHAIN) { | |
79 | COMP(callchain->nr); | |
80 | for (i = 0; i < s1->callchain->nr; i++) | |
81 | COMP(callchain->ips[i]); | |
82 | } | |
83 | ||
84 | if (type & PERF_SAMPLE_RAW) { | |
85 | COMP(raw_size); | |
86 | if (memcmp(s1->raw_data, s2->raw_data, s1->raw_size)) { | |
87 | pr_debug("Samples differ at 'raw_data'\n"); | |
88 | return false; | |
89 | } | |
90 | } | |
91 | ||
92 | if (type & PERF_SAMPLE_BRANCH_STACK) { | |
93 | COMP(branch_stack->nr); | |
94 | for (i = 0; i < s1->branch_stack->nr; i++) | |
95 | MCOMP(branch_stack->entries[i]); | |
96 | } | |
97 | ||
98 | if (type & PERF_SAMPLE_REGS_USER) { | |
352ea45a | 99 | size_t sz = hweight_long(s1->user_regs.mask) * sizeof(u64); |
045f8cd8 | 100 | |
352ea45a | 101 | COMP(user_regs.mask); |
045f8cd8 AH |
102 | COMP(user_regs.abi); |
103 | if (s1->user_regs.abi && | |
104 | (!s1->user_regs.regs || !s2->user_regs.regs || | |
105 | memcmp(s1->user_regs.regs, s2->user_regs.regs, sz))) { | |
106 | pr_debug("Samples differ at 'user_regs'\n"); | |
107 | return false; | |
108 | } | |
109 | } | |
110 | ||
111 | if (type & PERF_SAMPLE_STACK_USER) { | |
112 | COMP(user_stack.size); | |
113 | if (memcmp(s1->user_stack.data, s1->user_stack.data, | |
114 | s1->user_stack.size)) { | |
115 | pr_debug("Samples differ at 'user_stack'\n"); | |
116 | return false; | |
117 | } | |
118 | } | |
119 | ||
120 | if (type & PERF_SAMPLE_WEIGHT) | |
121 | COMP(weight); | |
122 | ||
123 | if (type & PERF_SAMPLE_DATA_SRC) | |
124 | COMP(data_src); | |
125 | ||
091a4ef5 AH |
126 | if (type & PERF_SAMPLE_TRANSACTION) |
127 | COMP(transaction); | |
128 | ||
045f8cd8 AH |
129 | return true; |
130 | } | |
131 | ||
132 | static int do_test(u64 sample_type, u64 sample_regs_user, u64 read_format) | |
133 | { | |
134 | struct perf_evsel evsel = { | |
135 | .needs_swap = false, | |
136 | .attr = { | |
137 | .sample_type = sample_type, | |
138 | .sample_regs_user = sample_regs_user, | |
139 | .read_format = read_format, | |
140 | }, | |
141 | }; | |
142 | union perf_event *event; | |
143 | union { | |
144 | struct ip_callchain callchain; | |
145 | u64 data[64]; | |
146 | } callchain = { | |
147 | /* 3 ips */ | |
148 | .data = {3, 201, 202, 203}, | |
149 | }; | |
150 | union { | |
151 | struct branch_stack branch_stack; | |
152 | u64 data[64]; | |
153 | } branch_stack = { | |
154 | /* 1 branch_entry */ | |
155 | .data = {1, 211, 212, 213}, | |
156 | }; | |
157 | u64 user_regs[64]; | |
158 | const u64 raw_data[] = {0x123456780a0b0c0dULL, 0x1102030405060708ULL}; | |
159 | const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL}; | |
160 | struct perf_sample sample = { | |
161 | .ip = 101, | |
162 | .pid = 102, | |
163 | .tid = 103, | |
164 | .time = 104, | |
165 | .addr = 105, | |
166 | .id = 106, | |
167 | .stream_id = 107, | |
168 | .period = 108, | |
169 | .weight = 109, | |
170 | .cpu = 110, | |
171 | .raw_size = sizeof(raw_data), | |
172 | .data_src = 111, | |
091a4ef5 | 173 | .transaction = 112, |
045f8cd8 AH |
174 | .raw_data = (void *)raw_data, |
175 | .callchain = &callchain.callchain, | |
176 | .branch_stack = &branch_stack.branch_stack, | |
177 | .user_regs = { | |
178 | .abi = PERF_SAMPLE_REGS_ABI_64, | |
352ea45a | 179 | .mask = sample_regs_user, |
045f8cd8 AH |
180 | .regs = user_regs, |
181 | }, | |
182 | .user_stack = { | |
183 | .size = sizeof(data), | |
184 | .data = (void *)data, | |
185 | }, | |
186 | .read = { | |
187 | .time_enabled = 0x030a59d664fca7deULL, | |
188 | .time_running = 0x011b6ae553eb98edULL, | |
189 | }, | |
190 | }; | |
191 | struct sample_read_value values[] = {{1, 5}, {9, 3}, {2, 7}, {6, 4},}; | |
192 | struct perf_sample sample_out; | |
193 | size_t i, sz, bufsz; | |
194 | int err, ret = -1; | |
195 | ||
196 | for (i = 0; i < sizeof(user_regs); i++) | |
197 | *(i + (u8 *)user_regs) = i & 0xfe; | |
198 | ||
199 | if (read_format & PERF_FORMAT_GROUP) { | |
200 | sample.read.group.nr = 4; | |
201 | sample.read.group.values = values; | |
202 | } else { | |
203 | sample.read.one.value = 0x08789faeb786aa87ULL; | |
204 | sample.read.one.id = 99; | |
205 | } | |
206 | ||
352ea45a | 207 | sz = perf_event__sample_event_size(&sample, sample_type, read_format); |
045f8cd8 AH |
208 | bufsz = sz + 4096; /* Add a bit for overrun checking */ |
209 | event = malloc(bufsz); | |
210 | if (!event) { | |
211 | pr_debug("malloc failed\n"); | |
212 | return -1; | |
213 | } | |
214 | ||
215 | memset(event, 0xff, bufsz); | |
216 | event->header.type = PERF_RECORD_SAMPLE; | |
217 | event->header.misc = 0; | |
218 | event->header.size = sz; | |
219 | ||
352ea45a | 220 | err = perf_event__synthesize_sample(event, sample_type, read_format, |
045f8cd8 AH |
221 | &sample, false); |
222 | if (err) { | |
223 | pr_debug("%s failed for sample_type %#"PRIx64", error %d\n", | |
224 | "perf_event__synthesize_sample", sample_type, err); | |
225 | goto out_free; | |
226 | } | |
227 | ||
228 | /* The data does not contain 0xff so we use that to check the size */ | |
229 | for (i = bufsz; i > 0; i--) { | |
230 | if (*(i - 1 + (u8 *)event) != 0xff) | |
231 | break; | |
232 | } | |
233 | if (i != sz) { | |
234 | pr_debug("Event size mismatch: actual %zu vs expected %zu\n", | |
235 | i, sz); | |
236 | goto out_free; | |
237 | } | |
238 | ||
239 | evsel.sample_size = __perf_evsel__sample_size(sample_type); | |
240 | ||
241 | err = perf_evsel__parse_sample(&evsel, event, &sample_out); | |
242 | if (err) { | |
243 | pr_debug("%s failed for sample_type %#"PRIx64", error %d\n", | |
244 | "perf_evsel__parse_sample", sample_type, err); | |
245 | goto out_free; | |
246 | } | |
247 | ||
352ea45a | 248 | if (!samples_same(&sample, &sample_out, sample_type, read_format)) { |
045f8cd8 AH |
249 | pr_debug("parsing failed for sample_type %#"PRIx64"\n", |
250 | sample_type); | |
251 | goto out_free; | |
252 | } | |
253 | ||
254 | ret = 0; | |
255 | out_free: | |
256 | free(event); | |
257 | if (ret && read_format) | |
258 | pr_debug("read_format %#"PRIx64"\n", read_format); | |
259 | return ret; | |
260 | } | |
261 | ||
262 | /** | |
263 | * test__sample_parsing - test sample parsing. | |
264 | * | |
265 | * This function implements a test that synthesizes a sample event, parses it | |
266 | * and then checks that the parsed sample matches the original sample. The test | |
267 | * checks sample format bits separately and together. If the test passes %0 is | |
268 | * returned, otherwise %-1 is returned. | |
269 | */ | |
270 | int test__sample_parsing(void) | |
271 | { | |
272 | const u64 rf[] = {4, 5, 6, 7, 12, 13, 14, 15}; | |
273 | u64 sample_type; | |
274 | u64 sample_regs_user; | |
275 | size_t i; | |
276 | int err; | |
277 | ||
278 | /* | |
279 | * Fail the test if it has not been updated when new sample format bits | |
091a4ef5 AH |
280 | * were added. Please actually update the test rather than just change |
281 | * the condition below. | |
045f8cd8 | 282 | */ |
4ac2f1c1 | 283 | if (PERF_SAMPLE_MAX > PERF_SAMPLE_TRANSACTION << 1) { |
11a4d435 | 284 | pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n"); |
045f8cd8 AH |
285 | return -1; |
286 | } | |
287 | ||
288 | /* Test each sample format bit separately */ | |
289 | for (sample_type = 1; sample_type != PERF_SAMPLE_MAX; | |
290 | sample_type <<= 1) { | |
291 | /* Test read_format variations */ | |
292 | if (sample_type == PERF_SAMPLE_READ) { | |
293 | for (i = 0; i < ARRAY_SIZE(rf); i++) { | |
294 | err = do_test(sample_type, 0, rf[i]); | |
295 | if (err) | |
296 | return err; | |
297 | } | |
298 | continue; | |
299 | } | |
300 | ||
301 | if (sample_type == PERF_SAMPLE_REGS_USER) | |
302 | sample_regs_user = 0x3fff; | |
303 | else | |
304 | sample_regs_user = 0; | |
305 | ||
306 | err = do_test(sample_type, sample_regs_user, 0); | |
307 | if (err) | |
308 | return err; | |
309 | } | |
310 | ||
311 | /* Test all sample format bits together */ | |
312 | sample_type = PERF_SAMPLE_MAX - 1; | |
313 | sample_regs_user = 0x3fff; | |
314 | for (i = 0; i < ARRAY_SIZE(rf); i++) { | |
315 | err = do_test(sample_type, sample_regs_user, rf[i]); | |
316 | if (err) | |
317 | return err; | |
318 | } | |
319 | ||
320 | return 0; | |
321 | } |