1 // SPDX-License-Identifier: GPL-2.0
2 #include "parse-events.h"
3 #include "evsel.h"
4 #include "evlist.h"
5 #include <api/fs/fs.h>
6 #include "tests.h"
7 #include "debug.h"
8 #include "pmu.h"
9 #include "pmus.h"
10 #include <dirent.h>
11 #include <errno.h>
12 #include "fncache.h"
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <linux/kernel.h>
17 #include <linux/hw_breakpoint.h>
18 #include <api/fs/tracing_path.h>
19
20 #define PERF_TP_SAMPLE_TYPE (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | \
21 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD)
22
num_core_entries(void)23 static int num_core_entries(void)
24 {
25 /*
26 * If the kernel supports extended type, expect events to be
27 * opened once for each core PMU type. Otherwise fall back to the legacy
28 * behavior of opening only one event even though there are multiple
29 * PMUs
30 */
31 if (perf_pmus__supports_extended_type())
32 return perf_pmus__num_core_pmus();
33
34 return 1;
35 }
36
test_config(const struct evsel * evsel,__u64 expected_config)37 static bool test_config(const struct evsel *evsel, __u64 expected_config)
38 {
39 __u32 type = evsel->core.attr.type;
40 __u64 config = evsel->core.attr.config;
41
42 if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) {
43 /*
44 * HARDWARE and HW_CACHE events encode the PMU's extended type
45 * in the top 32-bits. Mask in order to ignore.
46 */
47 config &= PERF_HW_EVENT_MASK;
48 }
49 return config == expected_config;
50 }
51
test_perf_config(const struct perf_evsel * evsel,__u64 expected_config)52 static bool test_perf_config(const struct perf_evsel *evsel, __u64 expected_config)
53 {
54 return (evsel->attr.config & PERF_HW_EVENT_MASK) == expected_config;
55 }
56
57 #if defined(__s390x__)
58 /* Return true if kvm module is available and loaded. Test this
59 * and return success when trace point kvm_s390_create_vm
60 * exists. Otherwise this test always fails.
61 */
kvm_s390_create_vm_valid(void)62 static bool kvm_s390_create_vm_valid(void)
63 {
64 char *eventfile;
65 bool rc = false;
66
67 eventfile = get_events_file("kvm-s390");
68
69 if (eventfile) {
70 DIR *mydir = opendir(eventfile);
71
72 if (mydir) {
73 rc = true;
74 closedir(mydir);
75 }
76 put_events_file(eventfile);
77 }
78
79 return rc;
80 }
81 #endif
82
test__checkevent_tracepoint(struct evlist * evlist)83 static int test__checkevent_tracepoint(struct evlist *evlist)
84 {
85 struct evsel *evsel = evlist__first(evlist);
86
87 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
88 TEST_ASSERT_VAL("wrong number of groups", 0 == evlist__nr_groups(evlist));
89 TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
90 TEST_ASSERT_VAL("wrong sample_type",
91 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
92 TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period);
93 return TEST_OK;
94 }
95
test__checkevent_tracepoint_multi(struct evlist * evlist)96 static int test__checkevent_tracepoint_multi(struct evlist *evlist)
97 {
98 struct evsel *evsel;
99
100 TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries > 1);
101 TEST_ASSERT_VAL("wrong number of groups", 0 == evlist__nr_groups(evlist));
102
103 evlist__for_each_entry(evlist, evsel) {
104 TEST_ASSERT_VAL("wrong type",
105 PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
106 TEST_ASSERT_VAL("wrong sample_type",
107 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
108 TEST_ASSERT_VAL("wrong sample_period",
109 1 == evsel->core.attr.sample_period);
110 }
111 return TEST_OK;
112 }
113
test__checkevent_raw(struct evlist * evlist)114 static int test__checkevent_raw(struct evlist *evlist)
115 {
116 struct perf_evsel *evsel;
117 bool raw_type_match = false;
118
119 TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries);
120
121 perf_evlist__for_each_evsel(&evlist->core, evsel) {
122 struct perf_pmu *pmu __maybe_unused = NULL;
123 bool type_matched = false;
124
125 TEST_ASSERT_VAL("wrong config", test_perf_config(evsel, 0x1a));
126 TEST_ASSERT_VAL("event not parsed as raw type",
127 evsel->attr.type == PERF_TYPE_RAW);
128 #if defined(__aarch64__)
129 /*
130 * Arm doesn't have a real raw type PMU in sysfs, so raw events
131 * would never match any PMU. However, RAW events on Arm will
132 * always successfully open on the first available core PMU
133 * so no need to test for a matching type here.
134 */
135 type_matched = raw_type_match = true;
136 #else
137 while ((pmu = perf_pmus__scan(pmu)) != NULL) {
138 if (pmu->type == evsel->attr.type) {
139 TEST_ASSERT_VAL("PMU type expected once", !type_matched);
140 type_matched = true;
141 if (pmu->type == PERF_TYPE_RAW)
142 raw_type_match = true;
143 }
144 }
145 #endif
146 TEST_ASSERT_VAL("No PMU found for type", type_matched);
147 }
148 TEST_ASSERT_VAL("Raw PMU not matched", raw_type_match);
149 return TEST_OK;
150 }
151
test__checkevent_numeric(struct evlist * evlist)152 static int test__checkevent_numeric(struct evlist *evlist)
153 {
154 struct evsel *evsel = evlist__first(evlist);
155
156 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
157 TEST_ASSERT_VAL("wrong type", 1 == evsel->core.attr.type);
158 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1));
159 return TEST_OK;
160 }
161
162
assert_hw(struct perf_evsel * evsel,enum perf_hw_id id,const char * name)163 static int assert_hw(struct perf_evsel *evsel, enum perf_hw_id id, const char *name)
164 {
165 struct perf_pmu *pmu;
166
167 if (evsel->attr.type == PERF_TYPE_HARDWARE) {
168 TEST_ASSERT_VAL("wrong config", test_perf_config(evsel, id));
169 return 0;
170 }
171 pmu = perf_pmus__find_by_type(evsel->attr.type);
172
173 TEST_ASSERT_VAL("unexpected PMU type", pmu);
174 TEST_ASSERT_VAL("PMU missing event", perf_pmu__have_event(pmu, name));
175 return 0;
176 }
177
test__checkevent_symbolic_name(struct evlist * evlist)178 static int test__checkevent_symbolic_name(struct evlist *evlist)
179 {
180 struct perf_evsel *evsel;
181
182 TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries);
183
184 perf_evlist__for_each_evsel(&evlist->core, evsel) {
185 int ret = assert_hw(evsel, PERF_COUNT_HW_INSTRUCTIONS, "instructions");
186
187 if (ret)
188 return ret;
189 }
190
191 return TEST_OK;
192 }
193
test__checkevent_symbolic_name_config(struct evlist * evlist)194 static int test__checkevent_symbolic_name_config(struct evlist *evlist)
195 {
196 struct perf_evsel *evsel;
197
198 TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries);
199
200 perf_evlist__for_each_evsel(&evlist->core, evsel) {
201 int ret = assert_hw(evsel, PERF_COUNT_HW_CPU_CYCLES, "cycles");
202
203 if (ret)
204 return ret;
205 /*
206 * The period value gets configured within evlist__config,
207 * while this test executes only parse events method.
208 */
209 TEST_ASSERT_VAL("wrong period", 0 == evsel->attr.sample_period);
210 TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
211 TEST_ASSERT_VAL("wrong config2", 1 == evsel->attr.config2);
212 }
213 return TEST_OK;
214 }
215
test__checkevent_symbolic_alias(struct evlist * evlist)216 static int test__checkevent_symbolic_alias(struct evlist *evlist)
217 {
218 struct evsel *evsel = evlist__first(evlist);
219
220 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
221 TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type);
222 TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_SW_PAGE_FAULTS));
223 return TEST_OK;
224 }
225
test__checkevent_genhw(struct evlist * evlist)226 static int test__checkevent_genhw(struct evlist *evlist)
227 {
228 struct perf_evsel *evsel;
229
230 TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries);
231
232 perf_evlist__for_each_entry(&evlist->core, evsel) {
233 TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->attr.type);
234 TEST_ASSERT_VAL("wrong config", test_perf_config(evsel, 1 << 16));
235 }
236 return TEST_OK;
237 }
238
test__checkevent_breakpoint(struct evlist * evlist)239 static int test__checkevent_breakpoint(struct evlist *evlist)
240 {
241 struct evsel *evsel = evlist__first(evlist);
242
243 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
244 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
245 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
246 TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
247 evsel->core.attr.bp_type);
248 TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 ==
249 evsel->core.attr.bp_len);
250 return TEST_OK;
251 }
252
test__checkevent_breakpoint_x(struct evlist * evlist)253 static int test__checkevent_breakpoint_x(struct evlist *evlist)
254 {
255 struct evsel *evsel = evlist__first(evlist);
256
257 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
258 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
259 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
260 TEST_ASSERT_VAL("wrong bp_type",
261 HW_BREAKPOINT_X == evsel->core.attr.bp_type);
262 TEST_ASSERT_VAL("wrong bp_len", default_breakpoint_len() == evsel->core.attr.bp_len);
263 return TEST_OK;
264 }
265
test__checkevent_breakpoint_r(struct evlist * evlist)266 static int test__checkevent_breakpoint_r(struct evlist *evlist)
267 {
268 struct evsel *evsel = evlist__first(evlist);
269
270 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
271 TEST_ASSERT_VAL("wrong type",
272 PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
273 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
274 TEST_ASSERT_VAL("wrong bp_type",
275 HW_BREAKPOINT_R == evsel->core.attr.bp_type);
276 TEST_ASSERT_VAL("wrong bp_len",
277 HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len);
278 return TEST_OK;
279 }
280
test__checkevent_breakpoint_w(struct evlist * evlist)281 static int test__checkevent_breakpoint_w(struct evlist *evlist)
282 {
283 struct evsel *evsel = evlist__first(evlist);
284
285 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
286 TEST_ASSERT_VAL("wrong type",
287 PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
288 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
289 TEST_ASSERT_VAL("wrong bp_type",
290 HW_BREAKPOINT_W == evsel->core.attr.bp_type);
291 TEST_ASSERT_VAL("wrong bp_len",
292 HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len);
293 return TEST_OK;
294 }
295
test__checkevent_breakpoint_rw(struct evlist * evlist)296 static int test__checkevent_breakpoint_rw(struct evlist *evlist)
297 {
298 struct evsel *evsel = evlist__first(evlist);
299
300 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
301 TEST_ASSERT_VAL("wrong type",
302 PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
303 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
304 TEST_ASSERT_VAL("wrong bp_type",
305 (HW_BREAKPOINT_R|HW_BREAKPOINT_W) == evsel->core.attr.bp_type);
306 TEST_ASSERT_VAL("wrong bp_len",
307 HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len);
308 return TEST_OK;
309 }
310
test__checkevent_tracepoint_modifier(struct evlist * evlist)311 static int test__checkevent_tracepoint_modifier(struct evlist *evlist)
312 {
313 struct evsel *evsel = evlist__first(evlist);
314
315 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
316 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
317 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
318 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
319
320 return test__checkevent_tracepoint(evlist);
321 }
322
323 static int
test__checkevent_tracepoint_multi_modifier(struct evlist * evlist)324 test__checkevent_tracepoint_multi_modifier(struct evlist *evlist)
325 {
326 struct perf_evsel *evsel;
327
328 TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries > 1);
329
330 perf_evlist__for_each_entry(&evlist->core, evsel) {
331 TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
332 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
333 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
334 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
335 }
336
337 return test__checkevent_tracepoint_multi(evlist);
338 }
339
test__checkevent_raw_modifier(struct evlist * evlist)340 static int test__checkevent_raw_modifier(struct evlist *evlist)
341 {
342 struct perf_evsel *evsel;
343
344 perf_evlist__for_each_entry(&evlist->core, evsel) {
345 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
346 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
347 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
348 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
349 }
350 return test__checkevent_raw(evlist);
351 }
352
test__checkevent_numeric_modifier(struct evlist * evlist)353 static int test__checkevent_numeric_modifier(struct evlist *evlist)
354 {
355 struct perf_evsel *evsel;
356
357 perf_evlist__for_each_entry(&evlist->core, evsel) {
358 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
359 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
360 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
361 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
362 }
363 return test__checkevent_numeric(evlist);
364 }
365
test__checkevent_symbolic_name_modifier(struct evlist * evlist)366 static int test__checkevent_symbolic_name_modifier(struct evlist *evlist)
367 {
368 struct perf_evsel *evsel;
369
370 TEST_ASSERT_VAL("wrong number of entries",
371 evlist->core.nr_entries == num_core_entries());
372
373 perf_evlist__for_each_entry(&evlist->core, evsel) {
374 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
375 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
376 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
377 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
378 }
379 return test__checkevent_symbolic_name(evlist);
380 }
381
test__checkevent_exclude_host_modifier(struct evlist * evlist)382 static int test__checkevent_exclude_host_modifier(struct evlist *evlist)
383 {
384 struct perf_evsel *evsel;
385
386 perf_evlist__for_each_entry(&evlist->core, evsel) {
387 TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
388 TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
389 }
390 return test__checkevent_symbolic_name(evlist);
391 }
392
test__checkevent_exclude_guest_modifier(struct evlist * evlist)393 static int test__checkevent_exclude_guest_modifier(struct evlist *evlist)
394 {
395 struct perf_evsel *evsel;
396
397 perf_evlist__for_each_entry(&evlist->core, evsel) {
398 TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest);
399 TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
400 }
401 return test__checkevent_symbolic_name(evlist);
402 }
403
test__checkevent_symbolic_alias_modifier(struct evlist * evlist)404 static int test__checkevent_symbolic_alias_modifier(struct evlist *evlist)
405 {
406 struct evsel *evsel = evlist__first(evlist);
407
408 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
409 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
410 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
411 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
412
413 return test__checkevent_symbolic_alias(evlist);
414 }
415
test__checkevent_genhw_modifier(struct evlist * evlist)416 static int test__checkevent_genhw_modifier(struct evlist *evlist)
417 {
418 struct perf_evsel *evsel;
419
420 perf_evlist__for_each_entry(&evlist->core, evsel) {
421 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
422 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
423 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
424 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
425 }
426 return test__checkevent_genhw(evlist);
427 }
428
test__checkevent_exclude_idle_modifier(struct evlist * evlist)429 static int test__checkevent_exclude_idle_modifier(struct evlist *evlist)
430 {
431 struct evsel *evsel = evlist__first(evlist);
432
433 TEST_ASSERT_VAL("wrong exclude idle", evsel->core.attr.exclude_idle);
434 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
435 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
436 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
437 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
438 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
439 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
440
441 return test__checkevent_symbolic_name(evlist);
442 }
443
test__checkevent_exclude_idle_modifier_1(struct evlist * evlist)444 static int test__checkevent_exclude_idle_modifier_1(struct evlist *evlist)
445 {
446 struct evsel *evsel = evlist__first(evlist);
447
448 TEST_ASSERT_VAL("wrong exclude idle", evsel->core.attr.exclude_idle);
449 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
450 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
451 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
452 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
453 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
454 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
455
456 return test__checkevent_symbolic_name(evlist);
457 }
458
test__checkevent_breakpoint_modifier(struct evlist * evlist)459 static int test__checkevent_breakpoint_modifier(struct evlist *evlist)
460 {
461 struct evsel *evsel = evlist__first(evlist);
462
463
464 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
465 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
466 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
467 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
468 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "mem:0:u"));
469
470 return test__checkevent_breakpoint(evlist);
471 }
472
test__checkevent_breakpoint_x_modifier(struct evlist * evlist)473 static int test__checkevent_breakpoint_x_modifier(struct evlist *evlist)
474 {
475 struct evsel *evsel = evlist__first(evlist);
476
477 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
478 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
479 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
480 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
481 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "mem:0:x:k"));
482
483 return test__checkevent_breakpoint_x(evlist);
484 }
485
test__checkevent_breakpoint_r_modifier(struct evlist * evlist)486 static int test__checkevent_breakpoint_r_modifier(struct evlist *evlist)
487 {
488 struct evsel *evsel = evlist__first(evlist);
489
490 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
491 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
492 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
493 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
494 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "mem:0:r:hp"));
495
496 return test__checkevent_breakpoint_r(evlist);
497 }
498
test__checkevent_breakpoint_w_modifier(struct evlist * evlist)499 static int test__checkevent_breakpoint_w_modifier(struct evlist *evlist)
500 {
501 struct evsel *evsel = evlist__first(evlist);
502
503 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
504 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
505 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
506 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
507 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "mem:0:w:up"));
508
509 return test__checkevent_breakpoint_w(evlist);
510 }
511
test__checkevent_breakpoint_rw_modifier(struct evlist * evlist)512 static int test__checkevent_breakpoint_rw_modifier(struct evlist *evlist)
513 {
514 struct evsel *evsel = evlist__first(evlist);
515
516 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
517 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
518 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
519 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
520 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "mem:0:rw:kp"));
521
522 return test__checkevent_breakpoint_rw(evlist);
523 }
524
test__checkevent_breakpoint_modifier_name(struct evlist * evlist)525 static int test__checkevent_breakpoint_modifier_name(struct evlist *evlist)
526 {
527 struct evsel *evsel = evlist__first(evlist);
528
529 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
530 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
531 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
532 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
533 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint"));
534
535 return test__checkevent_breakpoint(evlist);
536 }
537
test__checkevent_breakpoint_x_modifier_name(struct evlist * evlist)538 static int test__checkevent_breakpoint_x_modifier_name(struct evlist *evlist)
539 {
540 struct evsel *evsel = evlist__first(evlist);
541
542 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
543 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
544 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
545 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
546 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint"));
547
548 return test__checkevent_breakpoint_x(evlist);
549 }
550
test__checkevent_breakpoint_r_modifier_name(struct evlist * evlist)551 static int test__checkevent_breakpoint_r_modifier_name(struct evlist *evlist)
552 {
553 struct evsel *evsel = evlist__first(evlist);
554
555 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
556 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
557 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
558 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
559 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint"));
560
561 return test__checkevent_breakpoint_r(evlist);
562 }
563
test__checkevent_breakpoint_w_modifier_name(struct evlist * evlist)564 static int test__checkevent_breakpoint_w_modifier_name(struct evlist *evlist)
565 {
566 struct evsel *evsel = evlist__first(evlist);
567
568 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
569 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
570 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
571 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
572 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint"));
573
574 return test__checkevent_breakpoint_w(evlist);
575 }
576
test__checkevent_breakpoint_rw_modifier_name(struct evlist * evlist)577 static int test__checkevent_breakpoint_rw_modifier_name(struct evlist *evlist)
578 {
579 struct evsel *evsel = evlist__first(evlist);
580
581 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
582 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
583 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
584 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
585 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint"));
586
587 return test__checkevent_breakpoint_rw(evlist);
588 }
589
test__checkevent_breakpoint_2_events(struct evlist * evlist)590 static int test__checkevent_breakpoint_2_events(struct evlist *evlist)
591 {
592 struct evsel *evsel = evlist__first(evlist);
593
594 TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
595
596 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
597 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint1"));
598
599 evsel = evsel__next(evsel);
600
601 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
602 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "breakpoint2"));
603
604 return TEST_OK;
605 }
606
test__checkevent_pmu(struct evlist * evlist)607 static int test__checkevent_pmu(struct evlist *evlist)
608 {
609
610 struct evsel *evsel = evlist__first(evlist);
611
612 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
613 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
614 TEST_ASSERT_VAL("wrong config", test_config(evsel, 10));
615 TEST_ASSERT_VAL("wrong config1", 1 == evsel->core.attr.config1);
616 TEST_ASSERT_VAL("wrong config2", 3 == evsel->core.attr.config2);
617 TEST_ASSERT_VAL("wrong config3", 0 == evsel->core.attr.config3);
618 /*
619 * The period value gets configured within evlist__config,
620 * while this test executes only parse events method.
621 */
622 TEST_ASSERT_VAL("wrong period", 0 == evsel->core.attr.sample_period);
623
624 return TEST_OK;
625 }
626
test__checkevent_list(struct evlist * evlist)627 static int test__checkevent_list(struct evlist *evlist)
628 {
629 struct evsel *evsel = evlist__first(evlist);
630
631 TEST_ASSERT_VAL("wrong number of entries", 3 <= evlist->core.nr_entries);
632
633 /* r1 */
634 TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT != evsel->core.attr.type);
635 while (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
636 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1));
637 TEST_ASSERT_VAL("wrong config1", 0 == evsel->core.attr.config1);
638 TEST_ASSERT_VAL("wrong config2", 0 == evsel->core.attr.config2);
639 TEST_ASSERT_VAL("wrong config3", 0 == evsel->core.attr.config3);
640 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
641 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
642 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
643 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
644 evsel = evsel__next(evsel);
645 }
646
647 /* syscalls:sys_enter_openat:k */
648 TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
649 TEST_ASSERT_VAL("wrong sample_type",
650 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
651 TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period);
652 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
653 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
654 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
655 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
656
657 /* 1:1:hp */
658 evsel = evsel__next(evsel);
659 TEST_ASSERT_VAL("wrong type", 1 == evsel->core.attr.type);
660 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1));
661 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
662 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
663 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
664 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
665
666 return TEST_OK;
667 }
668
test__checkevent_pmu_name(struct evlist * evlist)669 static int test__checkevent_pmu_name(struct evlist *evlist)
670 {
671 struct evsel *evsel = evlist__first(evlist);
672
673 /* cpu/config=1,name=krava/u */
674 TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
675 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
676 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1));
677 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "krava"));
678
679 /* cpu/config=2/u" */
680 evsel = evsel__next(evsel);
681 TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
682 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
683 TEST_ASSERT_VAL("wrong config", test_config(evsel, 2));
684 TEST_ASSERT_VAL("wrong name", evsel__name_is(evsel, "cpu/config=2/u"));
685
686 return TEST_OK;
687 }
688
test__checkevent_pmu_partial_time_callgraph(struct evlist * evlist)689 static int test__checkevent_pmu_partial_time_callgraph(struct evlist *evlist)
690 {
691 struct evsel *evsel = evlist__first(evlist);
692
693 /* cpu/config=1,call-graph=fp,time,period=100000/ */
694 TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
695 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
696 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1));
697 /*
698 * The period, time and callgraph value gets configured within evlist__config,
699 * while this test executes only parse events method.
700 */
701 TEST_ASSERT_VAL("wrong period", 0 == evsel->core.attr.sample_period);
702 TEST_ASSERT_VAL("wrong callgraph", !evsel__has_callchain(evsel));
703 TEST_ASSERT_VAL("wrong time", !(PERF_SAMPLE_TIME & evsel->core.attr.sample_type));
704
705 /* cpu/config=2,call-graph=no,time=0,period=2000/ */
706 evsel = evsel__next(evsel);
707 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
708 TEST_ASSERT_VAL("wrong config", test_config(evsel, 2));
709 /*
710 * The period, time and callgraph value gets configured within evlist__config,
711 * while this test executes only parse events method.
712 */
713 TEST_ASSERT_VAL("wrong period", 0 == evsel->core.attr.sample_period);
714 TEST_ASSERT_VAL("wrong callgraph", !evsel__has_callchain(evsel));
715 TEST_ASSERT_VAL("wrong time", !(PERF_SAMPLE_TIME & evsel->core.attr.sample_type));
716
717 return TEST_OK;
718 }
719
test__checkevent_pmu_events(struct evlist * evlist)720 static int test__checkevent_pmu_events(struct evlist *evlist)
721 {
722 struct evsel *evsel = evlist__first(evlist);
723
724 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
725 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type ||
726 strcmp(evsel->pmu->name, "cpu"));
727 TEST_ASSERT_VAL("wrong exclude_user",
728 !evsel->core.attr.exclude_user);
729 TEST_ASSERT_VAL("wrong exclude_kernel",
730 evsel->core.attr.exclude_kernel);
731 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
732 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
733 TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
734 TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive);
735
736 return TEST_OK;
737 }
738
739
test__checkevent_pmu_events_mix(struct evlist * evlist)740 static int test__checkevent_pmu_events_mix(struct evlist *evlist)
741 {
742 struct evsel *evsel = NULL;
743
744 /*
745 * The wild card event will be opened at least once, but it may be
746 * opened on each core PMU.
747 */
748 TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries >= 2);
749 for (int i = 0; i < evlist->core.nr_entries - 1; i++) {
750 evsel = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
751 /* pmu-event:u */
752 TEST_ASSERT_VAL("wrong exclude_user",
753 !evsel->core.attr.exclude_user);
754 TEST_ASSERT_VAL("wrong exclude_kernel",
755 evsel->core.attr.exclude_kernel);
756 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
757 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
758 TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
759 TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive);
760 }
761 /* cpu/pmu-event/u*/
762 evsel = evsel__next(evsel);
763 TEST_ASSERT_VAL("wrong type", evsel__find_pmu(evsel)->is_core);
764 TEST_ASSERT_VAL("wrong exclude_user",
765 !evsel->core.attr.exclude_user);
766 TEST_ASSERT_VAL("wrong exclude_kernel",
767 evsel->core.attr.exclude_kernel);
768 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
769 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
770 TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
771 TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.pinned);
772
773 return TEST_OK;
774 }
775
test__checkterms_simple(struct parse_events_terms * terms)776 static int test__checkterms_simple(struct parse_events_terms *terms)
777 {
778 struct parse_events_term *term;
779
780 /* config=10 */
781 term = list_entry(terms->terms.next, struct parse_events_term, list);
782 TEST_ASSERT_VAL("wrong type term",
783 term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG);
784 TEST_ASSERT_VAL("wrong type val",
785 term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
786 TEST_ASSERT_VAL("wrong val", term->val.num == 10);
787 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config"));
788
789 /* config1 */
790 term = list_entry(term->list.next, struct parse_events_term, list);
791 TEST_ASSERT_VAL("wrong type term",
792 term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG1);
793 TEST_ASSERT_VAL("wrong type val",
794 term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
795 TEST_ASSERT_VAL("wrong val", term->val.num == 1);
796 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config1"));
797
798 /* config2=3 */
799 term = list_entry(term->list.next, struct parse_events_term, list);
800 TEST_ASSERT_VAL("wrong type term",
801 term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG2);
802 TEST_ASSERT_VAL("wrong type val",
803 term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
804 TEST_ASSERT_VAL("wrong val", term->val.num == 3);
805 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config2"));
806
807 /* config3=4 */
808 term = list_entry(term->list.next, struct parse_events_term, list);
809 TEST_ASSERT_VAL("wrong type term",
810 term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG3);
811 TEST_ASSERT_VAL("wrong type val",
812 term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
813 TEST_ASSERT_VAL("wrong val", term->val.num == 4);
814 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config3"));
815
816 /* umask=1*/
817 term = list_entry(term->list.next, struct parse_events_term, list);
818 TEST_ASSERT_VAL("wrong type term",
819 term->type_term == PARSE_EVENTS__TERM_TYPE_USER);
820 TEST_ASSERT_VAL("wrong type val",
821 term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
822 TEST_ASSERT_VAL("wrong val", term->val.num == 1);
823 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "umask"));
824
825 /*
826 * read
827 *
828 * The perf_pmu__test_parse_init injects 'read' term into
829 * perf_pmu_events_list, so 'read' is evaluated as read term
830 * and not as raw event with 'ead' hex value.
831 */
832 term = list_entry(term->list.next, struct parse_events_term, list);
833 TEST_ASSERT_VAL("wrong type term",
834 term->type_term == PARSE_EVENTS__TERM_TYPE_RAW);
835 TEST_ASSERT_VAL("wrong type val",
836 term->type_val == PARSE_EVENTS__TERM_TYPE_STR);
837 TEST_ASSERT_VAL("wrong val", !strcmp(term->val.str, "read"));
838 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "raw"));
839
840 /*
841 * r0xead
842 *
843 * To be still able to pass 'ead' value with 'r' syntax,
844 * we added support to parse 'r0xHEX' event.
845 */
846 term = list_entry(term->list.next, struct parse_events_term, list);
847 TEST_ASSERT_VAL("wrong type term",
848 term->type_term == PARSE_EVENTS__TERM_TYPE_RAW);
849 TEST_ASSERT_VAL("wrong type val",
850 term->type_val == PARSE_EVENTS__TERM_TYPE_STR);
851 TEST_ASSERT_VAL("wrong val", !strcmp(term->val.str, "r0xead"));
852 TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "raw"));
853 return TEST_OK;
854 }
855
test__group1(struct evlist * evlist)856 static int test__group1(struct evlist *evlist)
857 {
858 struct evsel *evsel, *leader;
859
860 TEST_ASSERT_VAL("wrong number of entries",
861 evlist->core.nr_entries == (num_core_entries() * 2));
862 TEST_ASSERT_VAL("wrong number of groups",
863 evlist__nr_groups(evlist) == num_core_entries());
864
865 for (int i = 0; i < num_core_entries(); i++) {
866 int ret;
867
868 /* instructions:k */
869 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
870 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions");
871 if (ret)
872 return ret;
873
874 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
875 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
876 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
877 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
878 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
879 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
880 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
881 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
882 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
883 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
884
885 /* cycles:upp */
886 evsel = evsel__next(evsel);
887 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
888 if (ret)
889 return ret;
890
891 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
892 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
893 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
894 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
895 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
896 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 2);
897 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
898 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
899 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
900 }
901 return TEST_OK;
902 }
903
test__group2(struct evlist * evlist)904 static int test__group2(struct evlist *evlist)
905 {
906 struct evsel *evsel, *leader = NULL;
907
908 TEST_ASSERT_VAL("wrong number of entries",
909 evlist->core.nr_entries == (2 * num_core_entries() + 1));
910 /*
911 * TODO: Currently the software event won't be grouped with the hardware
912 * event except for 1 PMU.
913 */
914 TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
915
916 evlist__for_each_entry(evlist, evsel) {
917 int ret;
918
919 if (evsel->core.attr.type == PERF_TYPE_SOFTWARE) {
920 /* faults + :ku modifier */
921 leader = evsel;
922 TEST_ASSERT_VAL("wrong config",
923 test_config(evsel, PERF_COUNT_SW_PAGE_FAULTS));
924 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
925 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
926 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
927 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
928 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
929 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
930 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
931 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
932 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
933 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
934 continue;
935 }
936 if (evsel->core.attr.type == PERF_TYPE_HARDWARE &&
937 test_config(evsel, PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) {
938 /* branches + :u modifier */
939 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
940 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
941 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
942 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
943 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
944 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
945 if (evsel__has_leader(evsel, leader))
946 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
947 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
948 continue;
949 }
950 /* cycles:k */
951 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
952 if (ret)
953 return ret;
954
955 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
956 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
957 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
958 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
959 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
960 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
961 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
962 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
963 }
964 return TEST_OK;
965 }
966
test__group3(struct evlist * evlist __maybe_unused)967 static int test__group3(struct evlist *evlist __maybe_unused)
968 {
969 struct evsel *evsel, *group1_leader = NULL, *group2_leader = NULL;
970 int ret;
971
972 TEST_ASSERT_VAL("wrong number of entries",
973 evlist->core.nr_entries == (3 * perf_pmus__num_core_pmus() + 2));
974 /*
975 * Currently the software event won't be grouped with the hardware event
976 * except for 1 PMU. This means there are always just 2 groups
977 * regardless of the number of core PMUs.
978 */
979 TEST_ASSERT_VAL("wrong number of groups", 2 == evlist__nr_groups(evlist));
980
981 evlist__for_each_entry(evlist, evsel) {
982 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
983 /* group1 syscalls:sys_enter_openat:H */
984 group1_leader = evsel;
985 TEST_ASSERT_VAL("wrong sample_type",
986 evsel->core.attr.sample_type == PERF_TP_SAMPLE_TYPE);
987 TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period);
988 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
989 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
990 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
991 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
992 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
993 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
994 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
995 TEST_ASSERT_VAL("wrong group name", !strcmp(evsel->group_name, "group1"));
996 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
997 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
998 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
999 continue;
1000 }
1001 if (evsel->core.attr.type == PERF_TYPE_HARDWARE &&
1002 test_config(evsel, PERF_COUNT_HW_CPU_CYCLES)) {
1003 if (evsel->core.attr.exclude_user) {
1004 /* group1 cycles:kppp */
1005 TEST_ASSERT_VAL("wrong exclude_user",
1006 evsel->core.attr.exclude_user);
1007 TEST_ASSERT_VAL("wrong exclude_kernel",
1008 !evsel->core.attr.exclude_kernel);
1009 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1010 TEST_ASSERT_VAL("wrong exclude guest",
1011 !evsel->core.attr.exclude_guest);
1012 TEST_ASSERT_VAL("wrong exclude host",
1013 !evsel->core.attr.exclude_host);
1014 TEST_ASSERT_VAL("wrong precise_ip",
1015 evsel->core.attr.precise_ip == 3);
1016 if (evsel__has_leader(evsel, group1_leader)) {
1017 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1018 TEST_ASSERT_VAL("wrong group_idx",
1019 evsel__group_idx(evsel) == 1);
1020 }
1021 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
1022 } else {
1023 /* group2 cycles + G modifier */
1024 group2_leader = evsel;
1025 TEST_ASSERT_VAL("wrong exclude_kernel",
1026 !evsel->core.attr.exclude_kernel);
1027 TEST_ASSERT_VAL("wrong exclude_hv",
1028 !evsel->core.attr.exclude_hv);
1029 TEST_ASSERT_VAL("wrong exclude guest",
1030 !evsel->core.attr.exclude_guest);
1031 TEST_ASSERT_VAL("wrong exclude host",
1032 evsel->core.attr.exclude_host);
1033 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1034 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1035 if (evsel->core.nr_members == 2) {
1036 TEST_ASSERT_VAL("wrong group_idx",
1037 evsel__group_idx(evsel) == 0);
1038 }
1039 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
1040 }
1041 continue;
1042 }
1043 if (evsel->core.attr.type == 1) {
1044 /* group2 1:3 + G modifier */
1045 TEST_ASSERT_VAL("wrong config", test_config(evsel, 3));
1046 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1047 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1048 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1049 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1050 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1051 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1052 if (evsel__has_leader(evsel, group2_leader))
1053 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1054 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
1055 continue;
1056 }
1057 /* instructions:u */
1058 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions");
1059 if (ret)
1060 return ret;
1061
1062 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1063 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1064 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1065 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1066 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1067 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1068 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1069 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
1070 }
1071 return TEST_OK;
1072 }
1073
test__group4(struct evlist * evlist __maybe_unused)1074 static int test__group4(struct evlist *evlist __maybe_unused)
1075 {
1076 struct evsel *evsel, *leader;
1077
1078 TEST_ASSERT_VAL("wrong number of entries",
1079 evlist->core.nr_entries == (num_core_entries() * 2));
1080 TEST_ASSERT_VAL("wrong number of groups",
1081 num_core_entries() == evlist__nr_groups(evlist));
1082
1083 for (int i = 0; i < num_core_entries(); i++) {
1084 int ret;
1085
1086 /* cycles:u + p */
1087 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1088 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1089 if (ret)
1090 return ret;
1091
1092 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1093 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1094 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1095 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1096 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1097 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 1);
1098 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1099 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1100 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1101 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1102 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
1103
1104 /* instructions:kp + p */
1105 evsel = evsel__next(evsel);
1106 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions");
1107 if (ret)
1108 return ret;
1109
1110 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
1111 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1112 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1113 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1114 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1115 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 2);
1116 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1117 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1118 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
1119 }
1120 return TEST_OK;
1121 }
1122
test__group5(struct evlist * evlist __maybe_unused)1123 static int test__group5(struct evlist *evlist __maybe_unused)
1124 {
1125 struct evsel *evsel = NULL, *leader;
1126 int ret;
1127
1128 TEST_ASSERT_VAL("wrong number of entries",
1129 evlist->core.nr_entries == (5 * num_core_entries()));
1130 TEST_ASSERT_VAL("wrong number of groups",
1131 evlist__nr_groups(evlist) == (2 * num_core_entries()));
1132
1133 for (int i = 0; i < num_core_entries(); i++) {
1134 /* cycles + G */
1135 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1136 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1137 if (ret)
1138 return ret;
1139
1140 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1141 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1142 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1143 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1144 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1145 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1146 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1147 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1148 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1149 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1150 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
1151
1152 /* instructions + G */
1153 evsel = evsel__next(evsel);
1154 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions");
1155 if (ret)
1156 return ret;
1157
1158 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1159 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1160 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1161 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1162 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1163 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1164 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1165 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1166 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
1167 }
1168 for (int i = 0; i < num_core_entries(); i++) {
1169 /* cycles:G */
1170 evsel = leader = evsel__next(evsel);
1171 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1172 if (ret)
1173 return ret;
1174
1175 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1176 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1177 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1178 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1179 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1180 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1181 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1182 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1183 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1184 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1185 TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
1186
1187 /* instructions:G */
1188 evsel = evsel__next(evsel);
1189 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions");
1190 if (ret)
1191 return ret;
1192
1193 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1194 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1195 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1196 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1197 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1198 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1199 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1200 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1201 }
1202 for (int i = 0; i < num_core_entries(); i++) {
1203 /* cycles */
1204 evsel = evsel__next(evsel);
1205 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1206 if (ret)
1207 return ret;
1208
1209 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1210 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1211 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1212 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1213 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1214 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1215 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1216 }
1217 return TEST_OK;
1218 }
1219
test__group_gh1(struct evlist * evlist)1220 static int test__group_gh1(struct evlist *evlist)
1221 {
1222 struct evsel *evsel = NULL, *leader;
1223
1224 TEST_ASSERT_VAL("wrong number of entries",
1225 evlist->core.nr_entries == (2 * num_core_entries()));
1226 TEST_ASSERT_VAL("wrong number of groups",
1227 evlist__nr_groups(evlist) == num_core_entries());
1228
1229 for (int i = 0; i < num_core_entries(); i++) {
1230 int ret;
1231
1232 /* cycles + :H group modifier */
1233 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1234 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1235 if (ret)
1236 return ret;
1237
1238 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1239 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1240 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1241 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1242 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1243 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1244 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1245 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1246 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1247 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1248
1249 /* cache-misses:G + :H group modifier */
1250 evsel = evsel__next(evsel);
1251 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses");
1252 if (ret)
1253 return ret;
1254
1255 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1256 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1257 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1258 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1259 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1260 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1261 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1262 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1263 }
1264 return TEST_OK;
1265 }
1266
test__group_gh2(struct evlist * evlist)1267 static int test__group_gh2(struct evlist *evlist)
1268 {
1269 struct evsel *evsel = NULL, *leader;
1270
1271 TEST_ASSERT_VAL("wrong number of entries",
1272 evlist->core.nr_entries == (2 * num_core_entries()));
1273 TEST_ASSERT_VAL("wrong number of groups",
1274 evlist__nr_groups(evlist) == num_core_entries());
1275
1276 for (int i = 0; i < num_core_entries(); i++) {
1277 int ret;
1278
1279 /* cycles + :G group modifier */
1280 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1281 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1282 if (ret)
1283 return ret;
1284
1285 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1286 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1287 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1288 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1289 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1290 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1291 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1292 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1293 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1294 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1295
1296 /* cache-misses:H + :G group modifier */
1297 evsel = evsel__next(evsel);
1298 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses");
1299 if (ret)
1300 return ret;
1301
1302 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1303 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1304 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1305 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1306 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1307 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1308 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1309 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1310 }
1311 return TEST_OK;
1312 }
1313
test__group_gh3(struct evlist * evlist)1314 static int test__group_gh3(struct evlist *evlist)
1315 {
1316 struct evsel *evsel = NULL, *leader;
1317
1318 TEST_ASSERT_VAL("wrong number of entries",
1319 evlist->core.nr_entries == (2 * num_core_entries()));
1320 TEST_ASSERT_VAL("wrong number of groups",
1321 evlist__nr_groups(evlist) == num_core_entries());
1322
1323 for (int i = 0; i < num_core_entries(); i++) {
1324 int ret;
1325
1326 /* cycles:G + :u group modifier */
1327 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1328 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1329 if (ret)
1330 return ret;
1331
1332 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1333 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1334 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1335 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1336 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1337 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1338 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1339 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1340 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1341 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1342
1343 /* cache-misses:H + :u group modifier */
1344 evsel = evsel__next(evsel);
1345 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses");
1346 if (ret)
1347 return ret;
1348
1349 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1350 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1351 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1352 TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1353 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1354 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1355 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1356 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1357 }
1358 return TEST_OK;
1359 }
1360
test__group_gh4(struct evlist * evlist)1361 static int test__group_gh4(struct evlist *evlist)
1362 {
1363 struct evsel *evsel = NULL, *leader;
1364
1365 TEST_ASSERT_VAL("wrong number of entries",
1366 evlist->core.nr_entries == (2 * num_core_entries()));
1367 TEST_ASSERT_VAL("wrong number of groups",
1368 evlist__nr_groups(evlist) == num_core_entries());
1369
1370 for (int i = 0; i < num_core_entries(); i++) {
1371 int ret;
1372
1373 /* cycles:G + :uG group modifier */
1374 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1375 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1376 if (ret)
1377 return ret;
1378
1379 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1380 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1381 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1382 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1383 TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1384 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1385 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1386 TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1387 TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1388 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1389
1390 /* cache-misses:H + :uG group modifier */
1391 evsel = evsel__next(evsel);
1392 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses");
1393 if (ret)
1394 return ret;
1395
1396 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1397 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1398 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1399 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1400 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1401 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1402 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1403 TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1404 }
1405 return TEST_OK;
1406 }
1407
test__leader_sample1(struct evlist * evlist)1408 static int test__leader_sample1(struct evlist *evlist)
1409 {
1410 struct evsel *evsel = NULL, *leader;
1411
1412 TEST_ASSERT_VAL("wrong number of entries",
1413 evlist->core.nr_entries == (3 * num_core_entries()));
1414
1415 for (int i = 0; i < num_core_entries(); i++) {
1416 int ret;
1417
1418 /* cycles - sampling group leader */
1419 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1420 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1421 if (ret)
1422 return ret;
1423
1424 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1425 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1426 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1427 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1428 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1429 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1430 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1431 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1432 TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1433
1434 /* cache-misses - not sampling */
1435 evsel = evsel__next(evsel);
1436 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses");
1437 if (ret)
1438 return ret;
1439
1440 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1441 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1442 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1443 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1444 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1445 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1446 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1447 TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1448
1449 /* branch-misses - not sampling */
1450 evsel = evsel__next(evsel);
1451 ret = assert_hw(&evsel->core, PERF_COUNT_HW_BRANCH_MISSES, "branch-misses");
1452 if (ret)
1453 return ret;
1454
1455 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1456 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1457 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1458 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1459 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1460 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1461 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1462 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1463 TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1464 }
1465 return TEST_OK;
1466 }
1467
test__leader_sample2(struct evlist * evlist __maybe_unused)1468 static int test__leader_sample2(struct evlist *evlist __maybe_unused)
1469 {
1470 struct evsel *evsel = NULL, *leader;
1471
1472 TEST_ASSERT_VAL("wrong number of entries",
1473 evlist->core.nr_entries == (2 * num_core_entries()));
1474
1475 for (int i = 0; i < num_core_entries(); i++) {
1476 int ret;
1477
1478 /* instructions - sampling group leader */
1479 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1480 ret = assert_hw(&evsel->core, PERF_COUNT_HW_INSTRUCTIONS, "instructions");
1481 if (ret)
1482 return ret;
1483
1484 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1485 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1486 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1487 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1488 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1489 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1490 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1491 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1492 TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1493
1494 /* branch-misses - not sampling */
1495 evsel = evsel__next(evsel);
1496 ret = assert_hw(&evsel->core, PERF_COUNT_HW_BRANCH_MISSES, "branch-misses");
1497 if (ret)
1498 return ret;
1499
1500 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1501 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1502 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1503 TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1504 TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1505 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1506 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1507 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1508 TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1509 }
1510 return TEST_OK;
1511 }
1512
test__checkevent_pinned_modifier(struct evlist * evlist)1513 static int test__checkevent_pinned_modifier(struct evlist *evlist)
1514 {
1515 struct evsel *evsel = NULL;
1516
1517 TEST_ASSERT_VAL("wrong number of entries",
1518 evlist->core.nr_entries == num_core_entries());
1519
1520 for (int i = 0; i < num_core_entries(); i++) {
1521 evsel = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1522 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1523 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1524 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1525 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
1526 TEST_ASSERT_VAL("wrong pinned", evsel->core.attr.pinned);
1527 }
1528 return test__checkevent_symbolic_name(evlist);
1529 }
1530
test__pinned_group(struct evlist * evlist)1531 static int test__pinned_group(struct evlist *evlist)
1532 {
1533 struct evsel *evsel = NULL, *leader;
1534
1535 TEST_ASSERT_VAL("wrong number of entries",
1536 evlist->core.nr_entries == (3 * num_core_entries()));
1537
1538 for (int i = 0; i < num_core_entries(); i++) {
1539 int ret;
1540
1541 /* cycles - group leader */
1542 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1543 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1544 if (ret)
1545 return ret;
1546
1547 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1548 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1549 /* TODO: The group modifier is not copied to the split group leader. */
1550 if (perf_pmus__num_core_pmus() == 1)
1551 TEST_ASSERT_VAL("wrong pinned", evsel->core.attr.pinned);
1552
1553 /* cache-misses - can not be pinned, but will go on with the leader */
1554 evsel = evsel__next(evsel);
1555 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses");
1556 if (ret)
1557 return ret;
1558
1559 TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
1560
1561 /* branch-misses - ditto */
1562 evsel = evsel__next(evsel);
1563 ret = assert_hw(&evsel->core, PERF_COUNT_HW_BRANCH_MISSES, "branch-misses");
1564 if (ret)
1565 return ret;
1566
1567 TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
1568 }
1569 return TEST_OK;
1570 }
1571
test__checkevent_exclusive_modifier(struct evlist * evlist)1572 static int test__checkevent_exclusive_modifier(struct evlist *evlist)
1573 {
1574 struct evsel *evsel = evlist__first(evlist);
1575
1576 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1577 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1578 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1579 TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
1580 TEST_ASSERT_VAL("wrong exclusive", evsel->core.attr.exclusive);
1581
1582 return test__checkevent_symbolic_name(evlist);
1583 }
1584
test__exclusive_group(struct evlist * evlist)1585 static int test__exclusive_group(struct evlist *evlist)
1586 {
1587 struct evsel *evsel = NULL, *leader;
1588
1589 TEST_ASSERT_VAL("wrong number of entries",
1590 evlist->core.nr_entries == 3 * num_core_entries());
1591
1592 for (int i = 0; i < num_core_entries(); i++) {
1593 int ret;
1594
1595 /* cycles - group leader */
1596 evsel = leader = (i == 0 ? evlist__first(evlist) : evsel__next(evsel));
1597 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1598 if (ret)
1599 return ret;
1600
1601 TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1602 TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1603 /* TODO: The group modifier is not copied to the split group leader. */
1604 if (perf_pmus__num_core_pmus() == 1)
1605 TEST_ASSERT_VAL("wrong exclusive", evsel->core.attr.exclusive);
1606
1607 /* cache-misses - can not be pinned, but will go on with the leader */
1608 evsel = evsel__next(evsel);
1609 ret = assert_hw(&evsel->core, PERF_COUNT_HW_CACHE_MISSES, "cache-misses");
1610 if (ret)
1611 return ret;
1612
1613 TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive);
1614
1615 /* branch-misses - ditto */
1616 evsel = evsel__next(evsel);
1617 ret = assert_hw(&evsel->core, PERF_COUNT_HW_BRANCH_MISSES, "branch-misses");
1618 if (ret)
1619 return ret;
1620
1621 TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive);
1622 }
1623 return TEST_OK;
1624 }
test__checkevent_breakpoint_len(struct evlist * evlist)1625 static int test__checkevent_breakpoint_len(struct evlist *evlist)
1626 {
1627 struct evsel *evsel = evlist__first(evlist);
1628
1629 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
1630 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
1631 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
1632 TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
1633 evsel->core.attr.bp_type);
1634 TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_1 ==
1635 evsel->core.attr.bp_len);
1636
1637 return TEST_OK;
1638 }
1639
test__checkevent_breakpoint_len_w(struct evlist * evlist)1640 static int test__checkevent_breakpoint_len_w(struct evlist *evlist)
1641 {
1642 struct evsel *evsel = evlist__first(evlist);
1643
1644 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
1645 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
1646 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
1647 TEST_ASSERT_VAL("wrong bp_type", HW_BREAKPOINT_W ==
1648 evsel->core.attr.bp_type);
1649 TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_2 ==
1650 evsel->core.attr.bp_len);
1651
1652 return TEST_OK;
1653 }
1654
1655 static int
test__checkevent_breakpoint_len_rw_modifier(struct evlist * evlist)1656 test__checkevent_breakpoint_len_rw_modifier(struct evlist *evlist)
1657 {
1658 struct evsel *evsel = evlist__first(evlist);
1659
1660 TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1661 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1662 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1663 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1664
1665 return test__checkevent_breakpoint_rw(evlist);
1666 }
1667
test__checkevent_precise_max_modifier(struct evlist * evlist)1668 static int test__checkevent_precise_max_modifier(struct evlist *evlist)
1669 {
1670 struct evsel *evsel = evlist__first(evlist);
1671
1672 TEST_ASSERT_VAL("wrong number of entries",
1673 evlist->core.nr_entries == 1 + num_core_entries());
1674 TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type);
1675 TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_SW_TASK_CLOCK));
1676 return TEST_OK;
1677 }
1678
test__checkevent_config_symbol(struct evlist * evlist)1679 static int test__checkevent_config_symbol(struct evlist *evlist)
1680 {
1681 struct evsel *evsel = evlist__first(evlist);
1682
1683 TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "insn"));
1684 return TEST_OK;
1685 }
1686
test__checkevent_config_raw(struct evlist * evlist)1687 static int test__checkevent_config_raw(struct evlist *evlist)
1688 {
1689 struct evsel *evsel = evlist__first(evlist);
1690
1691 TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "rawpmu"));
1692 return TEST_OK;
1693 }
1694
test__checkevent_config_num(struct evlist * evlist)1695 static int test__checkevent_config_num(struct evlist *evlist)
1696 {
1697 struct evsel *evsel = evlist__first(evlist);
1698
1699 TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "numpmu"));
1700 return TEST_OK;
1701 }
1702
test__checkevent_config_cache(struct evlist * evlist)1703 static int test__checkevent_config_cache(struct evlist *evlist)
1704 {
1705 struct evsel *evsel = evlist__first(evlist);
1706
1707 TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "cachepmu"));
1708 return test__checkevent_genhw(evlist);
1709 }
1710
test__pmu_cpu_valid(void)1711 static bool test__pmu_cpu_valid(void)
1712 {
1713 return !!perf_pmus__find("cpu");
1714 }
1715
test__pmu_cpu_event_valid(void)1716 static bool test__pmu_cpu_event_valid(void)
1717 {
1718 struct perf_pmu *pmu = perf_pmus__find("cpu");
1719
1720 if (!pmu)
1721 return false;
1722
1723 return perf_pmu__has_format(pmu, "event");
1724 }
1725
test__intel_pt_valid(void)1726 static bool test__intel_pt_valid(void)
1727 {
1728 return !!perf_pmus__find("intel_pt");
1729 }
1730
test__intel_pt(struct evlist * evlist)1731 static int test__intel_pt(struct evlist *evlist)
1732 {
1733 struct evsel *evsel = evlist__first(evlist);
1734
1735 TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "intel_pt//u"));
1736 return TEST_OK;
1737 }
1738
test__checkevent_complex_name(struct evlist * evlist)1739 static int test__checkevent_complex_name(struct evlist *evlist)
1740 {
1741 struct evsel *evsel = evlist__first(evlist);
1742
1743 TEST_ASSERT_VAL("wrong complex name parsing",
1744 evsel__name_is(evsel,
1745 "COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks"));
1746 return TEST_OK;
1747 }
1748
test__checkevent_raw_pmu(struct evlist * evlist)1749 static int test__checkevent_raw_pmu(struct evlist *evlist)
1750 {
1751 struct evsel *evsel = evlist__first(evlist);
1752
1753 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
1754 TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type);
1755 TEST_ASSERT_VAL("wrong config", test_config(evsel, 0x1a));
1756 return TEST_OK;
1757 }
1758
test__sym_event_slash(struct evlist * evlist)1759 static int test__sym_event_slash(struct evlist *evlist)
1760 {
1761 struct evsel *evsel = evlist__first(evlist);
1762 int ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1763
1764 if (ret)
1765 return ret;
1766
1767 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1768 return TEST_OK;
1769 }
1770
test__sym_event_dc(struct evlist * evlist)1771 static int test__sym_event_dc(struct evlist *evlist)
1772 {
1773 struct evsel *evsel = evlist__first(evlist);
1774 int ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1775
1776 if (ret)
1777 return ret;
1778
1779 TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
1780 return TEST_OK;
1781 }
1782
test__term_equal_term(struct evlist * evlist)1783 static int test__term_equal_term(struct evlist *evlist)
1784 {
1785 struct evsel *evsel = evlist__first(evlist);
1786 int ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1787
1788 if (ret)
1789 return ret;
1790
1791 TEST_ASSERT_VAL("wrong name setting", strcmp(evsel->name, "name") == 0);
1792 return TEST_OK;
1793 }
1794
test__term_equal_legacy(struct evlist * evlist)1795 static int test__term_equal_legacy(struct evlist *evlist)
1796 {
1797 struct evsel *evsel = evlist__first(evlist);
1798 int ret = assert_hw(&evsel->core, PERF_COUNT_HW_CPU_CYCLES, "cycles");
1799
1800 if (ret)
1801 return ret;
1802
1803 TEST_ASSERT_VAL("wrong name setting", strcmp(evsel->name, "l1d") == 0);
1804 return TEST_OK;
1805 }
1806
count_tracepoints(void)1807 static int count_tracepoints(void)
1808 {
1809 struct dirent *events_ent;
1810 DIR *events_dir;
1811 int cnt = 0;
1812
1813 events_dir = tracing_events__opendir();
1814
1815 TEST_ASSERT_VAL("Can't open events dir", events_dir);
1816
1817 while ((events_ent = readdir(events_dir))) {
1818 char *sys_path;
1819 struct dirent *sys_ent;
1820 DIR *sys_dir;
1821
1822 if (!strcmp(events_ent->d_name, ".")
1823 || !strcmp(events_ent->d_name, "..")
1824 || !strcmp(events_ent->d_name, "enable")
1825 || !strcmp(events_ent->d_name, "header_event")
1826 || !strcmp(events_ent->d_name, "header_page"))
1827 continue;
1828
1829 sys_path = get_events_file(events_ent->d_name);
1830 TEST_ASSERT_VAL("Can't get sys path", sys_path);
1831
1832 sys_dir = opendir(sys_path);
1833 TEST_ASSERT_VAL("Can't open sys dir", sys_dir);
1834
1835 while ((sys_ent = readdir(sys_dir))) {
1836 if (!strcmp(sys_ent->d_name, ".")
1837 || !strcmp(sys_ent->d_name, "..")
1838 || !strcmp(sys_ent->d_name, "enable")
1839 || !strcmp(sys_ent->d_name, "filter"))
1840 continue;
1841
1842 cnt++;
1843 }
1844
1845 closedir(sys_dir);
1846 put_events_file(sys_path);
1847 }
1848
1849 closedir(events_dir);
1850 return cnt;
1851 }
1852
test__all_tracepoints(struct evlist * evlist)1853 static int test__all_tracepoints(struct evlist *evlist)
1854 {
1855 TEST_ASSERT_VAL("wrong events count",
1856 count_tracepoints() == evlist->core.nr_entries);
1857
1858 return test__checkevent_tracepoint_multi(evlist);
1859 }
1860
1861 struct evlist_test {
1862 const char *name;
1863 bool (*valid)(void);
1864 int (*check)(struct evlist *evlist);
1865 };
1866
1867 static const struct evlist_test test__events[] = {
1868 {
1869 .name = "syscalls:sys_enter_openat",
1870 .check = test__checkevent_tracepoint,
1871 /* 0 */
1872 },
1873 {
1874 .name = "syscalls:*",
1875 .check = test__checkevent_tracepoint_multi,
1876 /* 1 */
1877 },
1878 {
1879 .name = "r1a",
1880 .check = test__checkevent_raw,
1881 /* 2 */
1882 },
1883 {
1884 .name = "1:1",
1885 .check = test__checkevent_numeric,
1886 /* 3 */
1887 },
1888 {
1889 .name = "instructions",
1890 .check = test__checkevent_symbolic_name,
1891 /* 4 */
1892 },
1893 {
1894 .name = "cycles/period=100000,config2/",
1895 .check = test__checkevent_symbolic_name_config,
1896 /* 5 */
1897 },
1898 {
1899 .name = "faults",
1900 .check = test__checkevent_symbolic_alias,
1901 /* 6 */
1902 },
1903 {
1904 .name = "L1-dcache-load-miss",
1905 .check = test__checkevent_genhw,
1906 /* 7 */
1907 },
1908 {
1909 .name = "mem:0",
1910 .check = test__checkevent_breakpoint,
1911 /* 8 */
1912 },
1913 {
1914 .name = "mem:0:x",
1915 .check = test__checkevent_breakpoint_x,
1916 /* 9 */
1917 },
1918 {
1919 .name = "mem:0:r",
1920 .check = test__checkevent_breakpoint_r,
1921 /* 0 */
1922 },
1923 {
1924 .name = "mem:0:w",
1925 .check = test__checkevent_breakpoint_w,
1926 /* 1 */
1927 },
1928 {
1929 .name = "syscalls:sys_enter_openat:k",
1930 .check = test__checkevent_tracepoint_modifier,
1931 /* 2 */
1932 },
1933 {
1934 .name = "syscalls:*:u",
1935 .check = test__checkevent_tracepoint_multi_modifier,
1936 /* 3 */
1937 },
1938 {
1939 .name = "r1a:kp",
1940 .check = test__checkevent_raw_modifier,
1941 /* 4 */
1942 },
1943 {
1944 .name = "1:1:hp",
1945 .check = test__checkevent_numeric_modifier,
1946 /* 5 */
1947 },
1948 {
1949 .name = "instructions:h",
1950 .check = test__checkevent_symbolic_name_modifier,
1951 /* 6 */
1952 },
1953 {
1954 .name = "faults:u",
1955 .check = test__checkevent_symbolic_alias_modifier,
1956 /* 7 */
1957 },
1958 {
1959 .name = "L1-dcache-load-miss:kp",
1960 .check = test__checkevent_genhw_modifier,
1961 /* 8 */
1962 },
1963 {
1964 .name = "mem:0:u",
1965 .check = test__checkevent_breakpoint_modifier,
1966 /* 9 */
1967 },
1968 {
1969 .name = "mem:0:x:k",
1970 .check = test__checkevent_breakpoint_x_modifier,
1971 /* 0 */
1972 },
1973 {
1974 .name = "mem:0:r:hp",
1975 .check = test__checkevent_breakpoint_r_modifier,
1976 /* 1 */
1977 },
1978 {
1979 .name = "mem:0:w:up",
1980 .check = test__checkevent_breakpoint_w_modifier,
1981 /* 2 */
1982 },
1983 {
1984 .name = "r1,syscalls:sys_enter_openat:k,1:1:hp",
1985 .check = test__checkevent_list,
1986 /* 3 */
1987 },
1988 {
1989 .name = "instructions:G",
1990 .check = test__checkevent_exclude_host_modifier,
1991 /* 4 */
1992 },
1993 {
1994 .name = "instructions:H",
1995 .check = test__checkevent_exclude_guest_modifier,
1996 /* 5 */
1997 },
1998 {
1999 .name = "mem:0:rw",
2000 .check = test__checkevent_breakpoint_rw,
2001 /* 6 */
2002 },
2003 {
2004 .name = "mem:0:rw:kp",
2005 .check = test__checkevent_breakpoint_rw_modifier,
2006 /* 7 */
2007 },
2008 {
2009 .name = "{instructions:k,cycles:upp}",
2010 .check = test__group1,
2011 /* 8 */
2012 },
2013 {
2014 .name = "{faults:k,branches}:u,cycles:k",
2015 .check = test__group2,
2016 /* 9 */
2017 },
2018 {
2019 .name = "group1{syscalls:sys_enter_openat:H,cycles:kppp},group2{cycles,1:3}:G,instructions:u",
2020 .check = test__group3,
2021 /* 0 */
2022 },
2023 {
2024 .name = "{cycles:u,instructions:kp}:p",
2025 .check = test__group4,
2026 /* 1 */
2027 },
2028 {
2029 .name = "{cycles,instructions}:G,{cycles:G,instructions:G},cycles",
2030 .check = test__group5,
2031 /* 2 */
2032 },
2033 {
2034 .name = "*:*",
2035 .check = test__all_tracepoints,
2036 /* 3 */
2037 },
2038 {
2039 .name = "{cycles,cache-misses:G}:H",
2040 .check = test__group_gh1,
2041 /* 4 */
2042 },
2043 {
2044 .name = "{cycles,cache-misses:H}:G",
2045 .check = test__group_gh2,
2046 /* 5 */
2047 },
2048 {
2049 .name = "{cycles:G,cache-misses:H}:u",
2050 .check = test__group_gh3,
2051 /* 6 */
2052 },
2053 {
2054 .name = "{cycles:G,cache-misses:H}:uG",
2055 .check = test__group_gh4,
2056 /* 7 */
2057 },
2058 {
2059 .name = "{cycles,cache-misses,branch-misses}:S",
2060 .check = test__leader_sample1,
2061 /* 8 */
2062 },
2063 {
2064 .name = "{instructions,branch-misses}:Su",
2065 .check = test__leader_sample2,
2066 /* 9 */
2067 },
2068 {
2069 .name = "instructions:uDp",
2070 .check = test__checkevent_pinned_modifier,
2071 /* 0 */
2072 },
2073 {
2074 .name = "{cycles,cache-misses,branch-misses}:D",
2075 .check = test__pinned_group,
2076 /* 1 */
2077 },
2078 {
2079 .name = "mem:0/1",
2080 .check = test__checkevent_breakpoint_len,
2081 /* 2 */
2082 },
2083 {
2084 .name = "mem:0/2:w",
2085 .check = test__checkevent_breakpoint_len_w,
2086 /* 3 */
2087 },
2088 {
2089 .name = "mem:0/4:rw:u",
2090 .check = test__checkevent_breakpoint_len_rw_modifier,
2091 /* 4 */
2092 },
2093 #if defined(__s390x__)
2094 {
2095 .name = "kvm-s390:kvm_s390_create_vm",
2096 .check = test__checkevent_tracepoint,
2097 .valid = kvm_s390_create_vm_valid,
2098 /* 0 */
2099 },
2100 #endif
2101 {
2102 .name = "instructions:I",
2103 .check = test__checkevent_exclude_idle_modifier,
2104 /* 5 */
2105 },
2106 {
2107 .name = "instructions:kIG",
2108 .check = test__checkevent_exclude_idle_modifier_1,
2109 /* 6 */
2110 },
2111 {
2112 .name = "task-clock:P,cycles",
2113 .check = test__checkevent_precise_max_modifier,
2114 /* 7 */
2115 },
2116 {
2117 .name = "instructions/name=insn/",
2118 .check = test__checkevent_config_symbol,
2119 /* 8 */
2120 },
2121 {
2122 .name = "r1234/name=rawpmu/",
2123 .check = test__checkevent_config_raw,
2124 /* 9 */
2125 },
2126 {
2127 .name = "4:0x6530160/name=numpmu/",
2128 .check = test__checkevent_config_num,
2129 /* 0 */
2130 },
2131 {
2132 .name = "L1-dcache-misses/name=cachepmu/",
2133 .check = test__checkevent_config_cache,
2134 /* 1 */
2135 },
2136 {
2137 .name = "intel_pt//u",
2138 .valid = test__intel_pt_valid,
2139 .check = test__intel_pt,
2140 /* 2 */
2141 },
2142 {
2143 .name = "cycles/name='COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks'/Duk",
2144 .check = test__checkevent_complex_name,
2145 /* 3 */
2146 },
2147 {
2148 .name = "cycles//u",
2149 .check = test__sym_event_slash,
2150 /* 4 */
2151 },
2152 {
2153 .name = "cycles:k",
2154 .check = test__sym_event_dc,
2155 /* 5 */
2156 },
2157 {
2158 .name = "instructions:uep",
2159 .check = test__checkevent_exclusive_modifier,
2160 /* 6 */
2161 },
2162 {
2163 .name = "{cycles,cache-misses,branch-misses}:e",
2164 .check = test__exclusive_group,
2165 /* 7 */
2166 },
2167 {
2168 .name = "cycles/name=name/",
2169 .check = test__term_equal_term,
2170 /* 8 */
2171 },
2172 {
2173 .name = "cycles/name=l1d/",
2174 .check = test__term_equal_legacy,
2175 /* 9 */
2176 },
2177 {
2178 .name = "mem:0/name=breakpoint/",
2179 .check = test__checkevent_breakpoint,
2180 /* 0 */
2181 },
2182 {
2183 .name = "mem:0:x/name=breakpoint/",
2184 .check = test__checkevent_breakpoint_x,
2185 /* 1 */
2186 },
2187 {
2188 .name = "mem:0:r/name=breakpoint/",
2189 .check = test__checkevent_breakpoint_r,
2190 /* 2 */
2191 },
2192 {
2193 .name = "mem:0:w/name=breakpoint/",
2194 .check = test__checkevent_breakpoint_w,
2195 /* 3 */
2196 },
2197 {
2198 .name = "mem:0/name=breakpoint/u",
2199 .check = test__checkevent_breakpoint_modifier_name,
2200 /* 4 */
2201 },
2202 {
2203 .name = "mem:0:x/name=breakpoint/k",
2204 .check = test__checkevent_breakpoint_x_modifier_name,
2205 /* 5 */
2206 },
2207 {
2208 .name = "mem:0:r/name=breakpoint/hp",
2209 .check = test__checkevent_breakpoint_r_modifier_name,
2210 /* 6 */
2211 },
2212 {
2213 .name = "mem:0:w/name=breakpoint/up",
2214 .check = test__checkevent_breakpoint_w_modifier_name,
2215 /* 7 */
2216 },
2217 {
2218 .name = "mem:0:rw/name=breakpoint/",
2219 .check = test__checkevent_breakpoint_rw,
2220 /* 8 */
2221 },
2222 {
2223 .name = "mem:0:rw/name=breakpoint/kp",
2224 .check = test__checkevent_breakpoint_rw_modifier_name,
2225 /* 9 */
2226 },
2227 {
2228 .name = "mem:0/1/name=breakpoint/",
2229 .check = test__checkevent_breakpoint_len,
2230 /* 0 */
2231 },
2232 {
2233 .name = "mem:0/2:w/name=breakpoint/",
2234 .check = test__checkevent_breakpoint_len_w,
2235 /* 1 */
2236 },
2237 {
2238 .name = "mem:0/4:rw/name=breakpoint/u",
2239 .check = test__checkevent_breakpoint_len_rw_modifier,
2240 /* 2 */
2241 },
2242 {
2243 .name = "mem:0/1/name=breakpoint1/,mem:0/4:rw/name=breakpoint2/",
2244 .check = test__checkevent_breakpoint_2_events,
2245 /* 3 */
2246 },
2247 {
2248 .name = "9p:9p_client_req",
2249 .check = test__checkevent_tracepoint,
2250 /* 4 */
2251 },
2252 };
2253
2254 static const struct evlist_test test__events_pmu[] = {
2255 {
2256 .name = "cpu/config=10,config1=1,config2=3,period=1000/u",
2257 .valid = test__pmu_cpu_valid,
2258 .check = test__checkevent_pmu,
2259 /* 0 */
2260 },
2261 {
2262 .name = "cpu/config=1,name=krava/u,cpu/config=2/u",
2263 .valid = test__pmu_cpu_valid,
2264 .check = test__checkevent_pmu_name,
2265 /* 1 */
2266 },
2267 {
2268 .name = "cpu/config=1,call-graph=fp,time,period=100000/,cpu/config=2,call-graph=no,time=0,period=2000/",
2269 .valid = test__pmu_cpu_valid,
2270 .check = test__checkevent_pmu_partial_time_callgraph,
2271 /* 2 */
2272 },
2273 {
2274 .name = "cpu/name='COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks',period=0x1,event=0x2/ukp",
2275 .valid = test__pmu_cpu_event_valid,
2276 .check = test__checkevent_complex_name,
2277 /* 3 */
2278 },
2279 {
2280 .name = "software/r1a/",
2281 .check = test__checkevent_raw_pmu,
2282 /* 4 */
2283 },
2284 {
2285 .name = "software/r0x1a/",
2286 .check = test__checkevent_raw_pmu,
2287 /* 5 */
2288 },
2289 {
2290 .name = "cpu/L1-dcache-load-miss/",
2291 .valid = test__pmu_cpu_valid,
2292 .check = test__checkevent_genhw,
2293 /* 6 */
2294 },
2295 {
2296 .name = "cpu/L1-dcache-load-miss/kp",
2297 .valid = test__pmu_cpu_valid,
2298 .check = test__checkevent_genhw_modifier,
2299 /* 7 */
2300 },
2301 {
2302 .name = "cpu/L1-dcache-misses,name=cachepmu/",
2303 .valid = test__pmu_cpu_valid,
2304 .check = test__checkevent_config_cache,
2305 /* 8 */
2306 },
2307 {
2308 .name = "cpu/instructions/",
2309 .valid = test__pmu_cpu_valid,
2310 .check = test__checkevent_symbolic_name,
2311 /* 9 */
2312 },
2313 {
2314 .name = "cpu/cycles,period=100000,config2/",
2315 .valid = test__pmu_cpu_valid,
2316 .check = test__checkevent_symbolic_name_config,
2317 /* 0 */
2318 },
2319 {
2320 .name = "cpu/instructions/h",
2321 .valid = test__pmu_cpu_valid,
2322 .check = test__checkevent_symbolic_name_modifier,
2323 /* 1 */
2324 },
2325 {
2326 .name = "cpu/instructions/G",
2327 .valid = test__pmu_cpu_valid,
2328 .check = test__checkevent_exclude_host_modifier,
2329 /* 2 */
2330 },
2331 {
2332 .name = "cpu/instructions/H",
2333 .valid = test__pmu_cpu_valid,
2334 .check = test__checkevent_exclude_guest_modifier,
2335 /* 3 */
2336 },
2337 {
2338 .name = "{cpu/instructions/k,cpu/cycles/upp}",
2339 .valid = test__pmu_cpu_valid,
2340 .check = test__group1,
2341 /* 4 */
2342 },
2343 {
2344 .name = "{cpu/cycles/u,cpu/instructions/kp}:p",
2345 .valid = test__pmu_cpu_valid,
2346 .check = test__group4,
2347 /* 5 */
2348 },
2349 {
2350 .name = "{cpu/cycles/,cpu/cache-misses/G}:H",
2351 .valid = test__pmu_cpu_valid,
2352 .check = test__group_gh1,
2353 /* 6 */
2354 },
2355 {
2356 .name = "{cpu/cycles/,cpu/cache-misses/H}:G",
2357 .valid = test__pmu_cpu_valid,
2358 .check = test__group_gh2,
2359 /* 7 */
2360 },
2361 {
2362 .name = "{cpu/cycles/G,cpu/cache-misses/H}:u",
2363 .valid = test__pmu_cpu_valid,
2364 .check = test__group_gh3,
2365 /* 8 */
2366 },
2367 {
2368 .name = "{cpu/cycles/G,cpu/cache-misses/H}:uG",
2369 .valid = test__pmu_cpu_valid,
2370 .check = test__group_gh4,
2371 /* 9 */
2372 },
2373 {
2374 .name = "{cpu/cycles/,cpu/cache-misses/,cpu/branch-misses/}:S",
2375 .valid = test__pmu_cpu_valid,
2376 .check = test__leader_sample1,
2377 /* 0 */
2378 },
2379 {
2380 .name = "{cpu/instructions/,cpu/branch-misses/}:Su",
2381 .valid = test__pmu_cpu_valid,
2382 .check = test__leader_sample2,
2383 /* 1 */
2384 },
2385 {
2386 .name = "cpu/instructions/uDp",
2387 .valid = test__pmu_cpu_valid,
2388 .check = test__checkevent_pinned_modifier,
2389 /* 2 */
2390 },
2391 {
2392 .name = "{cpu/cycles/,cpu/cache-misses/,cpu/branch-misses/}:D",
2393 .valid = test__pmu_cpu_valid,
2394 .check = test__pinned_group,
2395 /* 3 */
2396 },
2397 {
2398 .name = "cpu/instructions/I",
2399 .valid = test__pmu_cpu_valid,
2400 .check = test__checkevent_exclude_idle_modifier,
2401 /* 4 */
2402 },
2403 {
2404 .name = "cpu/instructions/kIG",
2405 .valid = test__pmu_cpu_valid,
2406 .check = test__checkevent_exclude_idle_modifier_1,
2407 /* 5 */
2408 },
2409 {
2410 .name = "cpu/cycles/u",
2411 .valid = test__pmu_cpu_valid,
2412 .check = test__sym_event_slash,
2413 /* 6 */
2414 },
2415 {
2416 .name = "cpu/cycles/k",
2417 .valid = test__pmu_cpu_valid,
2418 .check = test__sym_event_dc,
2419 /* 7 */
2420 },
2421 {
2422 .name = "cpu/instructions/uep",
2423 .valid = test__pmu_cpu_valid,
2424 .check = test__checkevent_exclusive_modifier,
2425 /* 8 */
2426 },
2427 {
2428 .name = "{cpu/cycles/,cpu/cache-misses/,cpu/branch-misses/}:e",
2429 .valid = test__pmu_cpu_valid,
2430 .check = test__exclusive_group,
2431 /* 9 */
2432 },
2433 {
2434 .name = "cpu/cycles,name=name/",
2435 .valid = test__pmu_cpu_valid,
2436 .check = test__term_equal_term,
2437 /* 0 */
2438 },
2439 {
2440 .name = "cpu/cycles,name=l1d/",
2441 .valid = test__pmu_cpu_valid,
2442 .check = test__term_equal_legacy,
2443 /* 1 */
2444 },
2445 };
2446
2447 struct terms_test {
2448 const char *str;
2449 int (*check)(struct parse_events_terms *terms);
2450 };
2451
2452 static const struct terms_test test__terms[] = {
2453 [0] = {
2454 .str = "config=10,config1,config2=3,config3=4,umask=1,read,r0xead",
2455 .check = test__checkterms_simple,
2456 },
2457 };
2458
test_event(const struct evlist_test * e)2459 static int test_event(const struct evlist_test *e)
2460 {
2461 struct parse_events_error err;
2462 struct evlist *evlist;
2463 int ret;
2464
2465 if (e->valid && !e->valid()) {
2466 pr_debug("... SKIP\n");
2467 return TEST_OK;
2468 }
2469
2470 evlist = evlist__new();
2471 if (evlist == NULL) {
2472 pr_err("Failed allocation");
2473 return TEST_FAIL;
2474 }
2475 parse_events_error__init(&err);
2476 ret = __parse_events(evlist, e->name, /*pmu_filter=*/NULL, &err, /*fake_pmu=*/false,
2477 /*warn_if_reordered=*/true, /*fake_tp=*/true);
2478 if (ret) {
2479 pr_debug("failed to parse event '%s', err %d\n", e->name, ret);
2480 parse_events_error__print(&err, e->name);
2481 ret = TEST_FAIL;
2482 if (parse_events_error__contains(&err, "can't access trace events"))
2483 ret = TEST_SKIP;
2484 } else {
2485 ret = e->check(evlist);
2486 }
2487 parse_events_error__exit(&err);
2488 evlist__delete(evlist);
2489
2490 return ret;
2491 }
2492
test_event_fake_pmu(const char * str)2493 static int test_event_fake_pmu(const char *str)
2494 {
2495 struct parse_events_error err;
2496 struct evlist *evlist;
2497 int ret;
2498
2499 evlist = evlist__new();
2500 if (!evlist)
2501 return -ENOMEM;
2502
2503 parse_events_error__init(&err);
2504 ret = __parse_events(evlist, str, /*pmu_filter=*/NULL, &err,
2505 /*fake_pmu=*/true, /*warn_if_reordered=*/true,
2506 /*fake_tp=*/true);
2507 if (ret) {
2508 pr_debug("failed to parse event '%s', err %d\n",
2509 str, ret);
2510 parse_events_error__print(&err, str);
2511 }
2512
2513 parse_events_error__exit(&err);
2514 evlist__delete(evlist);
2515
2516 return ret;
2517 }
2518
combine_test_results(int existing,int latest)2519 static int combine_test_results(int existing, int latest)
2520 {
2521 if (existing == TEST_FAIL)
2522 return TEST_FAIL;
2523 if (existing == TEST_SKIP)
2524 return latest == TEST_OK ? TEST_SKIP : latest;
2525 return latest;
2526 }
2527
test_events(const struct evlist_test * events,int cnt)2528 static int test_events(const struct evlist_test *events, int cnt)
2529 {
2530 int ret = TEST_OK;
2531
2532 for (int i = 0; i < cnt; i++) {
2533 const struct evlist_test *e = &events[i];
2534 int test_ret;
2535
2536 pr_debug("running test %d '%s'\n", i, e->name);
2537 test_ret = test_event(e);
2538 if (test_ret != TEST_OK) {
2539 pr_debug("Event test failure: test %d '%s'", i, e->name);
2540 ret = combine_test_results(ret, test_ret);
2541 }
2542 }
2543
2544 return ret;
2545 }
2546
test__events2(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2547 static int test__events2(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2548 {
2549 return test_events(test__events, ARRAY_SIZE(test__events));
2550 }
2551
test_term(const struct terms_test * t)2552 static int test_term(const struct terms_test *t)
2553 {
2554 struct parse_events_terms terms;
2555 int ret;
2556
2557
2558 parse_events_terms__init(&terms);
2559 ret = parse_events_terms(&terms, t->str, /*input=*/ NULL);
2560 if (ret) {
2561 pr_debug("failed to parse terms '%s', err %d\n",
2562 t->str , ret);
2563 return ret;
2564 }
2565
2566 ret = t->check(&terms);
2567 parse_events_terms__exit(&terms);
2568
2569 return ret;
2570 }
2571
test_terms(const struct terms_test * terms,int cnt)2572 static int test_terms(const struct terms_test *terms, int cnt)
2573 {
2574 int ret = 0;
2575
2576 for (int i = 0; i < cnt; i++) {
2577 const struct terms_test *t = &terms[i];
2578
2579 pr_debug("running test %d '%s'\n", i, t->str);
2580 ret = test_term(t);
2581 if (ret)
2582 break;
2583 }
2584
2585 return ret;
2586 }
2587
test__terms2(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2588 static int test__terms2(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2589 {
2590 return test_terms(test__terms, ARRAY_SIZE(test__terms));
2591 }
2592
test__pmu_events(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2593 static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2594 {
2595 struct perf_pmu *pmu = NULL;
2596 int ret = TEST_OK;
2597
2598 while ((pmu = perf_pmus__scan(pmu)) != NULL) {
2599 struct stat st;
2600 char path[PATH_MAX];
2601 char pmu_event[PATH_MAX];
2602 char *buf = NULL;
2603 FILE *file;
2604 struct dirent *ent;
2605 size_t len = 0;
2606 DIR *dir;
2607 int err;
2608 int n;
2609
2610 snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/events/",
2611 sysfs__mountpoint(), pmu->name);
2612
2613 err = stat(path, &st);
2614 if (err) {
2615 pr_debug("skipping PMU %s events tests: %s\n", pmu->name, path);
2616 continue;
2617 }
2618
2619 dir = opendir(path);
2620 if (!dir) {
2621 pr_debug("can't open pmu event dir: %s\n", path);
2622 ret = combine_test_results(ret, TEST_SKIP);
2623 continue;
2624 }
2625
2626 while ((ent = readdir(dir))) {
2627 struct evlist_test e = { .name = NULL, };
2628 char name[2 * NAME_MAX + 1 + 12 + 3];
2629 int test_ret;
2630 bool is_event_parameterized = 0;
2631
2632 /* Names containing . are special and cannot be used directly */
2633 if (strchr(ent->d_name, '.'))
2634 continue;
2635
2636 /* exclude parameterized ones (name contains '?') */
2637 n = snprintf(pmu_event, sizeof(pmu_event), "%s%s", path, ent->d_name);
2638 if (n >= PATH_MAX) {
2639 pr_err("pmu event name crossed PATH_MAX(%d) size\n", PATH_MAX);
2640 continue;
2641 }
2642
2643 file = fopen(pmu_event, "r");
2644 if (!file) {
2645 pr_debug("can't open pmu event file for '%s'\n", ent->d_name);
2646 ret = combine_test_results(ret, TEST_FAIL);
2647 continue;
2648 }
2649
2650 if (getline(&buf, &len, file) < 0) {
2651 pr_debug(" pmu event: %s is a null event\n", ent->d_name);
2652 ret = combine_test_results(ret, TEST_FAIL);
2653 fclose(file);
2654 continue;
2655 }
2656
2657 if (strchr(buf, '?'))
2658 is_event_parameterized = 1;
2659
2660 free(buf);
2661 buf = NULL;
2662 fclose(file);
2663
2664 if (is_event_parameterized == 1) {
2665 pr_debug("skipping parameterized PMU event: %s which contains ?\n", pmu_event);
2666 continue;
2667 }
2668
2669 snprintf(name, sizeof(name), "%s/event=%s/u", pmu->name, ent->d_name);
2670
2671 e.name = name;
2672 e.check = test__checkevent_pmu_events;
2673
2674 test_ret = test_event(&e);
2675 if (test_ret != TEST_OK) {
2676 pr_debug("Test PMU event failed for '%s'", name);
2677 ret = combine_test_results(ret, test_ret);
2678 }
2679
2680 if (!is_pmu_core(pmu->name))
2681 continue;
2682
2683 /*
2684 * Names containing '-' are recognized as prefixes and suffixes
2685 * due to '-' being a legacy PMU separator. This fails when the
2686 * prefix or suffix collides with an existing legacy token. For
2687 * example, branch-brs has a prefix (branch) that collides with
2688 * a PE_NAME_CACHE_TYPE token causing a parse error as a suffix
2689 * isn't expected after this. As event names in the config
2690 * slashes are allowed a '-' in the name we check this works
2691 * above.
2692 */
2693 if (strchr(ent->d_name, '-'))
2694 continue;
2695
2696 snprintf(name, sizeof(name), "%s:u,%s/event=%s/u",
2697 ent->d_name, pmu->name, ent->d_name);
2698 e.name = name;
2699 e.check = test__checkevent_pmu_events_mix;
2700 test_ret = test_event(&e);
2701 if (test_ret != TEST_OK) {
2702 pr_debug("Test PMU event failed for '%s'", name);
2703 ret = combine_test_results(ret, test_ret);
2704 }
2705 }
2706
2707 closedir(dir);
2708 }
2709 return ret;
2710 }
2711
test__pmu_events2(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2712 static int test__pmu_events2(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2713 {
2714 return test_events(test__events_pmu, ARRAY_SIZE(test__events_pmu));
2715 }
2716
test_alias(char ** event,char ** alias)2717 static bool test_alias(char **event, char **alias)
2718 {
2719 char path[PATH_MAX];
2720 DIR *dir;
2721 struct dirent *dent;
2722 const char *sysfs = sysfs__mountpoint();
2723 char buf[128];
2724 FILE *file;
2725
2726 if (!sysfs)
2727 return false;
2728
2729 snprintf(path, PATH_MAX, "%s/bus/event_source/devices/", sysfs);
2730 dir = opendir(path);
2731 if (!dir)
2732 return false;
2733
2734 while ((dent = readdir(dir))) {
2735 if (!strcmp(dent->d_name, ".") ||
2736 !strcmp(dent->d_name, ".."))
2737 continue;
2738
2739 snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/alias",
2740 sysfs, dent->d_name);
2741
2742 if (!file_available(path))
2743 continue;
2744
2745 file = fopen(path, "r");
2746 if (!file)
2747 continue;
2748
2749 if (!fgets(buf, sizeof(buf), file)) {
2750 fclose(file);
2751 continue;
2752 }
2753
2754 /* Remove the last '\n' */
2755 buf[strlen(buf) - 1] = 0;
2756
2757 fclose(file);
2758 *event = strdup(dent->d_name);
2759 *alias = strdup(buf);
2760 closedir(dir);
2761
2762 if (*event == NULL || *alias == NULL) {
2763 free(*event);
2764 free(*alias);
2765 return false;
2766 }
2767
2768 return true;
2769 }
2770
2771 closedir(dir);
2772 return false;
2773 }
2774
test__checkevent_pmu_events_alias(struct evlist * evlist)2775 static int test__checkevent_pmu_events_alias(struct evlist *evlist)
2776 {
2777 struct evsel *evsel1 = evlist__first(evlist);
2778 struct evsel *evsel2 = evlist__last(evlist);
2779
2780 TEST_ASSERT_VAL("wrong type", evsel1->core.attr.type == evsel2->core.attr.type);
2781 TEST_ASSERT_VAL("wrong config", evsel1->core.attr.config == evsel2->core.attr.config);
2782 return TEST_OK;
2783 }
2784
test__pmu_events_alias(char * event,char * alias)2785 static int test__pmu_events_alias(char *event, char *alias)
2786 {
2787 struct evlist_test e = { .name = NULL, };
2788 char name[2 * NAME_MAX + 20];
2789
2790 snprintf(name, sizeof(name), "%s/event=1/,%s/event=1/",
2791 event, alias);
2792
2793 e.name = name;
2794 e.check = test__checkevent_pmu_events_alias;
2795 return test_event(&e);
2796 }
2797
test__alias(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2798 static int test__alias(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2799 {
2800 char *event, *alias;
2801 int ret;
2802
2803 if (!test_alias(&event, &alias))
2804 return TEST_SKIP;
2805
2806 ret = test__pmu_events_alias(event, alias);
2807
2808 free(event);
2809 free(alias);
2810 return ret;
2811 }
2812
test__pmu_events_alias2(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2813 static int test__pmu_events_alias2(struct test_suite *test __maybe_unused,
2814 int subtest __maybe_unused)
2815 {
2816 static const char events[][30] = {
2817 "event-hyphen",
2818 "event-two-hyph",
2819 };
2820 int ret = TEST_OK;
2821
2822 for (unsigned int i = 0; i < ARRAY_SIZE(events); i++) {
2823 int test_ret = test_event_fake_pmu(&events[i][0]);
2824
2825 if (test_ret != TEST_OK) {
2826 pr_debug("check_parse_fake %s failed\n", &events[i][0]);
2827 ret = combine_test_results(ret, test_ret);
2828 }
2829 }
2830
2831 return ret;
2832 }
2833
2834 static struct test_case tests__parse_events[] = {
2835 TEST_CASE_REASON("Test event parsing",
2836 events2,
2837 "permissions"),
2838 TEST_CASE_REASON("Parsing of all PMU events from sysfs",
2839 pmu_events,
2840 "permissions"),
2841 TEST_CASE_REASON("Parsing of given PMU events from sysfs",
2842 pmu_events2,
2843 "permissions"),
2844 TEST_CASE_REASON("Parsing of aliased events from sysfs", alias,
2845 "no aliases in sysfs"),
2846 TEST_CASE("Parsing of aliased events", pmu_events_alias2),
2847 TEST_CASE("Parsing of terms (event modifiers)", terms2),
2848 { .name = NULL, }
2849 };
2850
2851 struct test_suite suite__parse_events = {
2852 .desc = "Parse event definition strings",
2853 .test_cases = tests__parse_events,
2854 };
2855