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