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