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