71c77d9d2744a990ff4ac01bea4fb8a4e97a82d0
[sfrench/cifs-2.6.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 bool test_config(const struct evsel *evsel, __u64 expected_config)
24 {
25         __u32 type = evsel->core.attr.type;
26         __u64 config = evsel->core.attr.config;
27
28         if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) {
29                 /*
30                  * HARDWARE and HW_CACHE events encode the PMU's extended type
31                  * in the top 32-bits. Mask in order to ignore.
32                  */
33                 config &= PERF_HW_EVENT_MASK;
34         }
35         return config == expected_config;
36 }
37
38 static bool test_perf_config(const struct perf_evsel *evsel, __u64 expected_config)
39 {
40         return (evsel->attr.config & PERF_HW_EVENT_MASK) == expected_config;
41 }
42
43 #ifdef HAVE_LIBTRACEEVENT
44
45 #if defined(__s390x__)
46 /* Return true if kvm module is available and loaded. Test this
47  * and return success when trace point kvm_s390_create_vm
48  * exists. Otherwise this test always fails.
49  */
50 static bool kvm_s390_create_vm_valid(void)
51 {
52         char *eventfile;
53         bool rc = false;
54
55         eventfile = get_events_file("kvm-s390");
56
57         if (eventfile) {
58                 DIR *mydir = opendir(eventfile);
59
60                 if (mydir) {
61                         rc = true;
62                         closedir(mydir);
63                 }
64                 put_events_file(eventfile);
65         }
66
67         return rc;
68 }
69 #endif
70
71 static int test__checkevent_tracepoint(struct evlist *evlist)
72 {
73         struct evsel *evsel = evlist__first(evlist);
74
75         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
76         TEST_ASSERT_VAL("wrong number of groups", 0 == evlist__nr_groups(evlist));
77         TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
78         TEST_ASSERT_VAL("wrong sample_type",
79                 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
80         TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period);
81         return TEST_OK;
82 }
83
84 static int test__checkevent_tracepoint_multi(struct evlist *evlist)
85 {
86         struct evsel *evsel;
87
88         TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries > 1);
89         TEST_ASSERT_VAL("wrong number of groups", 0 == evlist__nr_groups(evlist));
90
91         evlist__for_each_entry(evlist, evsel) {
92                 TEST_ASSERT_VAL("wrong type",
93                         PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
94                 TEST_ASSERT_VAL("wrong sample_type",
95                         PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
96                 TEST_ASSERT_VAL("wrong sample_period",
97                         1 == evsel->core.attr.sample_period);
98         }
99         return TEST_OK;
100 }
101 #endif /* HAVE_LIBTRACEEVENT */
102
103 static int test__checkevent_raw(struct evlist *evlist)
104 {
105         struct perf_evsel *evsel;
106         bool raw_type_match = false;
107
108         TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries);
109
110         perf_evlist__for_each_evsel(&evlist->core, evsel) {
111                 struct perf_pmu *pmu;
112                 bool type_matched = false;
113
114                 TEST_ASSERT_VAL("wrong config", test_perf_config(evsel, 0x1a));
115                 perf_pmus__for_each_pmu(pmu) {
116                         if (pmu->type == evsel->attr.type) {
117                                 TEST_ASSERT_VAL("PMU type expected once", !type_matched);
118                                 type_matched = true;
119                                 if (pmu->type == PERF_TYPE_RAW)
120                                         raw_type_match = true;
121                         }
122                 }
123                 TEST_ASSERT_VAL("No PMU found for type", type_matched);
124         }
125         TEST_ASSERT_VAL("Raw PMU not matched", raw_type_match);
126         return TEST_OK;
127 }
128
129 static int test__checkevent_numeric(struct evlist *evlist)
130 {
131         struct evsel *evsel = evlist__first(evlist);
132
133         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
134         TEST_ASSERT_VAL("wrong type", 1 == evsel->core.attr.type);
135         TEST_ASSERT_VAL("wrong config", test_config(evsel, 1));
136         return TEST_OK;
137 }
138
139 static int test__checkevent_symbolic_name(struct evlist *evlist)
140 {
141         struct perf_evsel *evsel;
142
143         TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries);
144
145         perf_evlist__for_each_evsel(&evlist->core, evsel) {
146                 TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
147                 TEST_ASSERT_VAL("wrong config",
148                                 test_perf_config(evsel, PERF_COUNT_HW_INSTRUCTIONS));
149         }
150         return TEST_OK;
151 }
152
153 static int test__checkevent_symbolic_name_config(struct evlist *evlist)
154 {
155         struct perf_evsel *evsel;
156
157         TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries);
158
159         perf_evlist__for_each_evsel(&evlist->core, evsel) {
160                 TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
161                 TEST_ASSERT_VAL("wrong config", test_perf_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
162                 /*
163                  * The period value gets configured within evlist__config,
164                  * while this test executes only parse events method.
165                  */
166                 TEST_ASSERT_VAL("wrong period", 0 == evsel->attr.sample_period);
167                 TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
168                 TEST_ASSERT_VAL("wrong config2", 1 == evsel->attr.config2);
169         }
170         return TEST_OK;
171 }
172
173 static int test__checkevent_symbolic_alias(struct evlist *evlist)
174 {
175         struct evsel *evsel = evlist__first(evlist);
176
177         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
178         TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type);
179         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_SW_PAGE_FAULTS));
180         return TEST_OK;
181 }
182
183 static int test__checkevent_genhw(struct evlist *evlist)
184 {
185         struct perf_evsel *evsel;
186
187         TEST_ASSERT_VAL("wrong number of entries", 0 != evlist->core.nr_entries);
188
189         perf_evlist__for_each_entry(&evlist->core, evsel) {
190                 TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->attr.type);
191                 TEST_ASSERT_VAL("wrong config", test_perf_config(evsel, 1 << 16));
192         }
193         return TEST_OK;
194 }
195
196 static int test__checkevent_breakpoint(struct evlist *evlist)
197 {
198         struct evsel *evsel = evlist__first(evlist);
199
200         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
201         TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
202         TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
203         TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
204                                          evsel->core.attr.bp_type);
205         TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 ==
206                                         evsel->core.attr.bp_len);
207         return TEST_OK;
208 }
209
210 static int test__checkevent_breakpoint_x(struct evlist *evlist)
211 {
212         struct evsel *evsel = evlist__first(evlist);
213
214         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
215         TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
216         TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
217         TEST_ASSERT_VAL("wrong bp_type",
218                         HW_BREAKPOINT_X == evsel->core.attr.bp_type);
219         TEST_ASSERT_VAL("wrong bp_len", sizeof(long) == evsel->core.attr.bp_len);
220         return TEST_OK;
221 }
222
223 static int test__checkevent_breakpoint_r(struct evlist *evlist)
224 {
225         struct evsel *evsel = evlist__first(evlist);
226
227         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
228         TEST_ASSERT_VAL("wrong type",
229                         PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
230         TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
231         TEST_ASSERT_VAL("wrong bp_type",
232                         HW_BREAKPOINT_R == evsel->core.attr.bp_type);
233         TEST_ASSERT_VAL("wrong bp_len",
234                         HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len);
235         return TEST_OK;
236 }
237
238 static int test__checkevent_breakpoint_w(struct evlist *evlist)
239 {
240         struct evsel *evsel = evlist__first(evlist);
241
242         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
243         TEST_ASSERT_VAL("wrong type",
244                         PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
245         TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
246         TEST_ASSERT_VAL("wrong bp_type",
247                         HW_BREAKPOINT_W == evsel->core.attr.bp_type);
248         TEST_ASSERT_VAL("wrong bp_len",
249                         HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len);
250         return TEST_OK;
251 }
252
253 static int test__checkevent_breakpoint_rw(struct evlist *evlist)
254 {
255         struct evsel *evsel = evlist__first(evlist);
256
257         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
258         TEST_ASSERT_VAL("wrong type",
259                         PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
260         TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
261         TEST_ASSERT_VAL("wrong bp_type",
262                 (HW_BREAKPOINT_R|HW_BREAKPOINT_W) == evsel->core.attr.bp_type);
263         TEST_ASSERT_VAL("wrong bp_len",
264                         HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len);
265         return TEST_OK;
266 }
267
268 #ifdef HAVE_LIBTRACEEVENT
269 static int test__checkevent_tracepoint_modifier(struct evlist *evlist)
270 {
271         struct evsel *evsel = evlist__first(evlist);
272
273         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
274         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
275         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
276         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
277
278         return test__checkevent_tracepoint(evlist);
279 }
280
281 static int
282 test__checkevent_tracepoint_multi_modifier(struct evlist *evlist)
283 {
284         struct perf_evsel *evsel;
285
286         TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries > 1);
287
288         perf_evlist__for_each_entry(&evlist->core, evsel) {
289                 TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
290                 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
291                 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
292                 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
293         }
294
295         return test__checkevent_tracepoint_multi(evlist);
296 }
297 #endif /* HAVE_LIBTRACEEVENT */
298
299 static int test__checkevent_raw_modifier(struct evlist *evlist)
300 {
301         struct perf_evsel *evsel;
302
303         perf_evlist__for_each_entry(&evlist->core, evsel) {
304                 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
305                 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
306                 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
307                 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
308         }
309         return test__checkevent_raw(evlist);
310 }
311
312 static int test__checkevent_numeric_modifier(struct evlist *evlist)
313 {
314         struct perf_evsel *evsel;
315
316         perf_evlist__for_each_entry(&evlist->core, evsel) {
317                 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
318                 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
319                 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
320                 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
321         }
322         return test__checkevent_numeric(evlist);
323 }
324
325 static int test__checkevent_symbolic_name_modifier(struct evlist *evlist)
326 {
327         struct evsel *evsel = evlist__first(evlist);
328
329         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
330         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
331         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
332         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
333
334         return test__checkevent_symbolic_name(evlist);
335 }
336
337 static int test__checkevent_exclude_host_modifier(struct evlist *evlist)
338 {
339         struct perf_evsel *evsel;
340
341         perf_evlist__for_each_entry(&evlist->core, evsel) {
342                 TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
343                 TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
344         }
345         return test__checkevent_symbolic_name(evlist);
346 }
347
348 static int test__checkevent_exclude_guest_modifier(struct evlist *evlist)
349 {
350         struct perf_evsel *evsel;
351
352         perf_evlist__for_each_entry(&evlist->core, evsel) {
353                 TEST_ASSERT_VAL("wrong exclude guest", evsel->attr.exclude_guest);
354                 TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
355         }
356         return test__checkevent_symbolic_name(evlist);
357 }
358
359 static int test__checkevent_symbolic_alias_modifier(struct evlist *evlist)
360 {
361         struct evsel *evsel = evlist__first(evlist);
362
363         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
364         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
365         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
366         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
367
368         return test__checkevent_symbolic_alias(evlist);
369 }
370
371 static int test__checkevent_genhw_modifier(struct evlist *evlist)
372 {
373         struct perf_evsel *evsel;
374
375         perf_evlist__for_each_entry(&evlist->core, evsel) {
376                 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
377                 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
378                 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
379                 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
380         }
381         return test__checkevent_genhw(evlist);
382 }
383
384 static int test__checkevent_exclude_idle_modifier(struct evlist *evlist)
385 {
386         struct evsel *evsel = evlist__first(evlist);
387
388         TEST_ASSERT_VAL("wrong exclude idle", evsel->core.attr.exclude_idle);
389         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
390         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
391         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
392         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
393         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
394         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
395
396         return test__checkevent_symbolic_name(evlist);
397 }
398
399 static int test__checkevent_exclude_idle_modifier_1(struct evlist *evlist)
400 {
401         struct evsel *evsel = evlist__first(evlist);
402
403         TEST_ASSERT_VAL("wrong exclude idle", evsel->core.attr.exclude_idle);
404         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
405         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
406         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
407         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
408         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
409         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
410
411         return test__checkevent_symbolic_name(evlist);
412 }
413
414 static int test__checkevent_breakpoint_modifier(struct evlist *evlist)
415 {
416         struct evsel *evsel = evlist__first(evlist);
417
418
419         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
420         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
421         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
422         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
423         TEST_ASSERT_VAL("wrong name",
424                         !strcmp(evsel__name(evsel), "mem:0:u"));
425
426         return test__checkevent_breakpoint(evlist);
427 }
428
429 static int test__checkevent_breakpoint_x_modifier(struct evlist *evlist)
430 {
431         struct evsel *evsel = evlist__first(evlist);
432
433         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
434         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
435         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
436         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
437         TEST_ASSERT_VAL("wrong name",
438                         !strcmp(evsel__name(evsel), "mem:0:x:k"));
439
440         return test__checkevent_breakpoint_x(evlist);
441 }
442
443 static int test__checkevent_breakpoint_r_modifier(struct evlist *evlist)
444 {
445         struct evsel *evsel = evlist__first(evlist);
446
447         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
448         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
449         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
450         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
451         TEST_ASSERT_VAL("wrong name",
452                         !strcmp(evsel__name(evsel), "mem:0:r:hp"));
453
454         return test__checkevent_breakpoint_r(evlist);
455 }
456
457 static int test__checkevent_breakpoint_w_modifier(struct evlist *evlist)
458 {
459         struct evsel *evsel = evlist__first(evlist);
460
461         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
462         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
463         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
464         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
465         TEST_ASSERT_VAL("wrong name",
466                         !strcmp(evsel__name(evsel), "mem:0:w:up"));
467
468         return test__checkevent_breakpoint_w(evlist);
469 }
470
471 static int test__checkevent_breakpoint_rw_modifier(struct evlist *evlist)
472 {
473         struct evsel *evsel = evlist__first(evlist);
474
475         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
476         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
477         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
478         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
479         TEST_ASSERT_VAL("wrong name",
480                         !strcmp(evsel__name(evsel), "mem:0:rw:kp"));
481
482         return test__checkevent_breakpoint_rw(evlist);
483 }
484
485 static int test__checkevent_pmu(struct evlist *evlist)
486 {
487
488         struct evsel *evsel = evlist__first(evlist);
489
490         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
491         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
492         TEST_ASSERT_VAL("wrong config",    test_config(evsel, 10));
493         TEST_ASSERT_VAL("wrong config1",    1 == evsel->core.attr.config1);
494         TEST_ASSERT_VAL("wrong config2",    3 == evsel->core.attr.config2);
495         TEST_ASSERT_VAL("wrong config3",    0 == evsel->core.attr.config3);
496         /*
497          * The period value gets configured within evlist__config,
498          * while this test executes only parse events method.
499          */
500         TEST_ASSERT_VAL("wrong period",     0 == evsel->core.attr.sample_period);
501
502         return TEST_OK;
503 }
504
505 #ifdef HAVE_LIBTRACEEVENT
506 static int test__checkevent_list(struct evlist *evlist)
507 {
508         struct evsel *evsel = evlist__first(evlist);
509
510         TEST_ASSERT_VAL("wrong number of entries", 3 <= evlist->core.nr_entries);
511
512         /* r1 */
513         TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT != evsel->core.attr.type);
514         while (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
515                 TEST_ASSERT_VAL("wrong config", test_config(evsel, 1));
516                 TEST_ASSERT_VAL("wrong config1", 0 == evsel->core.attr.config1);
517                 TEST_ASSERT_VAL("wrong config2", 0 == evsel->core.attr.config2);
518                 TEST_ASSERT_VAL("wrong config3", 0 == evsel->core.attr.config3);
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                 evsel = evsel__next(evsel);
524         }
525
526         /* syscalls:sys_enter_openat:k */
527         TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
528         TEST_ASSERT_VAL("wrong sample_type",
529                 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
530         TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period);
531         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
532         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
533         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
534         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
535
536         /* 1:1:hp */
537         evsel = evsel__next(evsel);
538         TEST_ASSERT_VAL("wrong type", 1 == evsel->core.attr.type);
539         TEST_ASSERT_VAL("wrong config", test_config(evsel, 1));
540         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
541         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
542         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
543         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
544
545         return TEST_OK;
546 }
547 #endif
548
549 static int test__checkevent_pmu_name(struct evlist *evlist)
550 {
551         struct evsel *evsel = evlist__first(evlist);
552
553         /* cpu/config=1,name=krava/u */
554         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
555         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
556         TEST_ASSERT_VAL("wrong config", test_config(evsel, 1));
557         TEST_ASSERT_VAL("wrong name", !strcmp(evsel__name(evsel), "krava"));
558
559         /* cpu/config=2/u" */
560         evsel = evsel__next(evsel);
561         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
562         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
563         TEST_ASSERT_VAL("wrong config", test_config(evsel, 2));
564         TEST_ASSERT_VAL("wrong name",
565                         !strcmp(evsel__name(evsel), "cpu/config=2/u"));
566
567         return TEST_OK;
568 }
569
570 static int test__checkevent_pmu_partial_time_callgraph(struct evlist *evlist)
571 {
572         struct evsel *evsel = evlist__first(evlist);
573
574         /* cpu/config=1,call-graph=fp,time,period=100000/ */
575         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
576         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
577         TEST_ASSERT_VAL("wrong config", test_config(evsel, 1));
578         /*
579          * The period, time and callgraph value gets configured within evlist__config,
580          * while this test executes only parse events method.
581          */
582         TEST_ASSERT_VAL("wrong period",     0 == evsel->core.attr.sample_period);
583         TEST_ASSERT_VAL("wrong callgraph",  !evsel__has_callchain(evsel));
584         TEST_ASSERT_VAL("wrong time",  !(PERF_SAMPLE_TIME & evsel->core.attr.sample_type));
585
586         /* cpu/config=2,call-graph=no,time=0,period=2000/ */
587         evsel = evsel__next(evsel);
588         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
589         TEST_ASSERT_VAL("wrong config", test_config(evsel, 2));
590         /*
591          * The period, time and callgraph value gets configured within evlist__config,
592          * while this test executes only parse events method.
593          */
594         TEST_ASSERT_VAL("wrong period",     0 == evsel->core.attr.sample_period);
595         TEST_ASSERT_VAL("wrong callgraph",  !evsel__has_callchain(evsel));
596         TEST_ASSERT_VAL("wrong time",  !(PERF_SAMPLE_TIME & evsel->core.attr.sample_type));
597
598         return TEST_OK;
599 }
600
601 static int test__checkevent_pmu_events(struct evlist *evlist)
602 {
603         struct evsel *evsel = evlist__first(evlist);
604
605         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
606         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type ||
607                                       strcmp(evsel->pmu_name, "cpu"));
608         TEST_ASSERT_VAL("wrong exclude_user",
609                         !evsel->core.attr.exclude_user);
610         TEST_ASSERT_VAL("wrong exclude_kernel",
611                         evsel->core.attr.exclude_kernel);
612         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
613         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
614         TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
615         TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive);
616
617         return TEST_OK;
618 }
619
620
621 static int test__checkevent_pmu_events_mix(struct evlist *evlist)
622 {
623         struct evsel *evsel = evlist__first(evlist);
624
625         /* pmu-event:u */
626         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
627         TEST_ASSERT_VAL("wrong exclude_user",
628                         !evsel->core.attr.exclude_user);
629         TEST_ASSERT_VAL("wrong exclude_kernel",
630                         evsel->core.attr.exclude_kernel);
631         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
632         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
633         TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
634         TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive);
635
636         /* cpu/pmu-event/u*/
637         evsel = evsel__next(evsel);
638         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
639         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type ||
640                                       strcmp(evsel->pmu_name, "cpu"));
641         TEST_ASSERT_VAL("wrong exclude_user",
642                         !evsel->core.attr.exclude_user);
643         TEST_ASSERT_VAL("wrong exclude_kernel",
644                         evsel->core.attr.exclude_kernel);
645         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
646         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
647         TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
648         TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.pinned);
649
650         return TEST_OK;
651 }
652
653 static int test__checkterms_simple(struct list_head *terms)
654 {
655         struct parse_events_term *term;
656
657         /* config=10 */
658         term = list_entry(terms->next, struct parse_events_term, list);
659         TEST_ASSERT_VAL("wrong type term",
660                         term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG);
661         TEST_ASSERT_VAL("wrong type val",
662                         term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
663         TEST_ASSERT_VAL("wrong val", term->val.num == 10);
664         TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config"));
665
666         /* config1 */
667         term = list_entry(term->list.next, struct parse_events_term, list);
668         TEST_ASSERT_VAL("wrong type term",
669                         term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG1);
670         TEST_ASSERT_VAL("wrong type val",
671                         term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
672         TEST_ASSERT_VAL("wrong val", term->val.num == 1);
673         TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config1"));
674
675         /* config2=3 */
676         term = list_entry(term->list.next, struct parse_events_term, list);
677         TEST_ASSERT_VAL("wrong type term",
678                         term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG2);
679         TEST_ASSERT_VAL("wrong type val",
680                         term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
681         TEST_ASSERT_VAL("wrong val", term->val.num == 3);
682         TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config2"));
683
684         /* config3=4 */
685         term = list_entry(term->list.next, struct parse_events_term, list);
686         TEST_ASSERT_VAL("wrong type term",
687                         term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG3);
688         TEST_ASSERT_VAL("wrong type val",
689                         term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
690         TEST_ASSERT_VAL("wrong val", term->val.num == 4);
691         TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "config3"));
692
693         /* umask=1*/
694         term = list_entry(term->list.next, struct parse_events_term, list);
695         TEST_ASSERT_VAL("wrong type term",
696                         term->type_term == PARSE_EVENTS__TERM_TYPE_USER);
697         TEST_ASSERT_VAL("wrong type val",
698                         term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
699         TEST_ASSERT_VAL("wrong val", term->val.num == 1);
700         TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "umask"));
701
702         /*
703          * read
704          *
705          * The perf_pmu__test_parse_init injects 'read' term into
706          * perf_pmu_events_list, so 'read' is evaluated as read term
707          * and not as raw event with 'ead' hex value.
708          */
709         term = list_entry(term->list.next, struct parse_events_term, list);
710         TEST_ASSERT_VAL("wrong type term",
711                         term->type_term == PARSE_EVENTS__TERM_TYPE_RAW);
712         TEST_ASSERT_VAL("wrong type val",
713                         term->type_val == PARSE_EVENTS__TERM_TYPE_STR);
714         TEST_ASSERT_VAL("wrong val", !strcmp(term->val.str, "read"));
715         TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "raw"));
716
717         /*
718          * r0xead
719          *
720          * To be still able to pass 'ead' value with 'r' syntax,
721          * we added support to parse 'r0xHEX' event.
722          */
723         term = list_entry(term->list.next, struct parse_events_term, list);
724         TEST_ASSERT_VAL("wrong type term",
725                         term->type_term == PARSE_EVENTS__TERM_TYPE_RAW);
726         TEST_ASSERT_VAL("wrong type val",
727                         term->type_val == PARSE_EVENTS__TERM_TYPE_STR);
728         TEST_ASSERT_VAL("wrong val", !strcmp(term->val.str, "r0xead"));
729         TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "raw"));
730         return TEST_OK;
731 }
732
733 static int test__group1(struct evlist *evlist)
734 {
735         struct evsel *evsel, *leader;
736
737         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
738         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
739
740         /* instructions:k */
741         evsel = leader = evlist__first(evlist);
742         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
743         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_INSTRUCTIONS));
744         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
745         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
746         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
747         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
748         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
749         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
750         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
751         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
752         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
753         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
754
755         /* cycles:upp */
756         evsel = evsel__next(evsel);
757         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
758         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
759         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
760         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
761         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
762         /* use of precise requires exclude_guest */
763         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
764         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
765         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 2);
766         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
767         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
768         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
769
770         return TEST_OK;
771 }
772
773 static int test__group2(struct evlist *evlist)
774 {
775         struct evsel *evsel, *leader;
776
777         TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->core.nr_entries);
778         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
779
780         /* faults + :ku modifier */
781         evsel = leader = evlist__first(evlist);
782         TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type);
783         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_SW_PAGE_FAULTS));
784         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
785         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
786         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
787         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
788         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
789         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
790         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
791         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
792         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
793         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
794
795         /* cache-references + :u modifier */
796         evsel = evsel__next(evsel);
797         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
798         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CACHE_REFERENCES));
799         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
800         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
801         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
802         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
803         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
804         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
805         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
806         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
807         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
808
809         /* cycles:k */
810         evsel = evsel__next(evsel);
811         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
812         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
813         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
814         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
815         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
816         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
817         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
818         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
819         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
820         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
821
822         return TEST_OK;
823 }
824
825 #ifdef HAVE_LIBTRACEEVENT
826 static int test__group3(struct evlist *evlist __maybe_unused)
827 {
828         struct evsel *evsel, *leader;
829
830         TEST_ASSERT_VAL("wrong number of entries", 5 == evlist->core.nr_entries);
831         TEST_ASSERT_VAL("wrong number of groups", 2 == evlist__nr_groups(evlist));
832
833         /* group1 syscalls:sys_enter_openat:H */
834         evsel = leader = evlist__first(evlist);
835         TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
836         TEST_ASSERT_VAL("wrong sample_type",
837                 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
838         TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period);
839         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
840         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
841         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
842         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
843         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
844         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
845         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
846         TEST_ASSERT_VAL("wrong group name",
847                 !strcmp(leader->group_name, "group1"));
848         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
849         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
850         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
851
852         /* group1 cycles:kppp */
853         evsel = evsel__next(evsel);
854         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
855         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
856         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
857         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
858         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
859         /* use of precise requires exclude_guest */
860         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
861         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
862         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 3);
863         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
864         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
865         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
866         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
867
868         /* group2 cycles + G modifier */
869         evsel = leader = evsel__next(evsel);
870         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
871         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
872         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
873         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
874         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
875         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
876         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
877         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
878         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
879         TEST_ASSERT_VAL("wrong group name",
880                 !strcmp(leader->group_name, "group2"));
881         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
882         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
883         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
884
885         /* group2 1:3 + G modifier */
886         evsel = evsel__next(evsel);
887         TEST_ASSERT_VAL("wrong type", 1 == evsel->core.attr.type);
888         TEST_ASSERT_VAL("wrong config", test_config(evsel, 3));
889         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
890         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
891         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
892         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
893         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
894         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
895         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
896         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
897         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
898
899         /* instructions:u */
900         evsel = evsel__next(evsel);
901         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
902         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_INSTRUCTIONS));
903         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
904         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
905         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
906         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
907         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
908         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
909         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
910         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
911
912         return TEST_OK;
913 }
914 #endif
915
916 static int test__group4(struct evlist *evlist __maybe_unused)
917 {
918         struct evsel *evsel, *leader;
919
920         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
921         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
922
923         /* cycles:u + p */
924         evsel = leader = evlist__first(evlist);
925         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
926         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
927         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
928         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
929         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
930         /* use of precise requires exclude_guest */
931         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
932         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
933         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 1);
934         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
935         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
936         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
937         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
938         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
939
940         /* instructions:kp + p */
941         evsel = evsel__next(evsel);
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_INSTRUCTIONS));
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         /* use of precise requires exclude_guest */
948         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
949         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
950         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 2);
951         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
952         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
953         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
954
955         return TEST_OK;
956 }
957
958 static int test__group5(struct evlist *evlist __maybe_unused)
959 {
960         struct evsel *evsel, *leader;
961
962         TEST_ASSERT_VAL("wrong number of entries", 5 == evlist->core.nr_entries);
963         TEST_ASSERT_VAL("wrong number of groups", 2 == evlist__nr_groups(evlist));
964
965         /* cycles + G */
966         evsel = leader = evlist__first(evlist);
967         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
968         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
969         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
970         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
971         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
972         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
973         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
974         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
975         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
976         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
977         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
978         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
979         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
980
981         /* instructions + G */
982         evsel = evsel__next(evsel);
983         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
984         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_INSTRUCTIONS));
985         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
986         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
987         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
988         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
989         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
990         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
991         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
992         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
993         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
994
995         /* cycles:G */
996         evsel = leader = evsel__next(evsel);
997         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
998         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
999         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1000         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1001         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1002         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1003         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1004         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1005         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1006         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1007         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1008         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1009         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
1010
1011         /* instructions:G */
1012         evsel = evsel__next(evsel);
1013         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1014         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_INSTRUCTIONS));
1015         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1016         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1017         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1018         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1019         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1020         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1021         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1022         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1023
1024         /* cycles */
1025         evsel = evsel__next(evsel);
1026         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1027         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
1028         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1029         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1030         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1031         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1032         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1033         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1034         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1035
1036         return TEST_OK;
1037 }
1038
1039 static int test__group_gh1(struct evlist *evlist)
1040 {
1041         struct evsel *evsel, *leader;
1042
1043         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
1044         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
1045
1046         /* cycles + :H group modifier */
1047         evsel = leader = evlist__first(evlist);
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_CPU_CYCLES));
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 group name", !evsel->group_name);
1057         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1058         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1059         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1060
1061         /* cache-misses:G + :H group modifier */
1062         evsel = evsel__next(evsel);
1063         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1064         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CACHE_MISSES));
1065         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1066         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1067         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1068         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1069         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1070         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1071         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1072         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1073
1074         return TEST_OK;
1075 }
1076
1077 static int test__group_gh2(struct evlist *evlist)
1078 {
1079         struct evsel *evsel, *leader;
1080
1081         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
1082         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
1083
1084         /* cycles + :G group modifier */
1085         evsel = leader = evlist__first(evlist);
1086         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1087         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
1088         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1089         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1090         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1091         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1092         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1093         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1094         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1095         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1096         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1097         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1098
1099         /* cache-misses:H + :G group modifier */
1100         evsel = evsel__next(evsel);
1101         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1102         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CACHE_MISSES));
1103         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1104         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1105         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1106         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1107         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1108         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1109         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1110         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1111
1112         return TEST_OK;
1113 }
1114
1115 static int test__group_gh3(struct evlist *evlist)
1116 {
1117         struct evsel *evsel, *leader;
1118
1119         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
1120         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
1121
1122         /* cycles:G + :u group modifier */
1123         evsel = leader = evlist__first(evlist);
1124         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1125         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
1126         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1127         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1128         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1129         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1130         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1131         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1132         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1133         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1134         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1135         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1136
1137         /* cache-misses:H + :u group modifier */
1138         evsel = evsel__next(evsel);
1139         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1140         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CACHE_MISSES));
1141         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1142         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1143         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1144         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1145         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1146         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1147         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1148         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1149
1150         return TEST_OK;
1151 }
1152
1153 static int test__group_gh4(struct evlist *evlist)
1154 {
1155         struct evsel *evsel, *leader;
1156
1157         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
1158         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
1159
1160         /* cycles:G + :uG group modifier */
1161         evsel = leader = evlist__first(evlist);
1162         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1163         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
1164         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1165         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1166         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1167         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1168         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1169         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1170         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1171         TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1172         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1173         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1174
1175         /* cache-misses:H + :uG group modifier */
1176         evsel = evsel__next(evsel);
1177         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1178         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CACHE_MISSES));
1179         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1180         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1181         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1182         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1183         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1184         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1185         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1186         TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1187
1188         return TEST_OK;
1189 }
1190
1191 static int test__leader_sample1(struct evlist *evlist)
1192 {
1193         struct evsel *evsel, *leader;
1194
1195         TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->core.nr_entries);
1196
1197         /* cycles - sampling group leader */
1198         evsel = leader = evlist__first(evlist);
1199         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1200         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
1201         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1202         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1203         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1204         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1205         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1206         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1207         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1208         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1209         TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1210
1211         /* cache-misses - not sampling */
1212         evsel = evsel__next(evsel);
1213         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1214         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CACHE_MISSES));
1215         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1216         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1217         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1218         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1219         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1220         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1221         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1222         TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1223
1224         /* branch-misses - not sampling */
1225         evsel = evsel__next(evsel);
1226         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1227         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_BRANCH_MISSES));
1228         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1229         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1230         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1231         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1232         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1233         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1234         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1235         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1236         TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1237
1238         return TEST_OK;
1239 }
1240
1241 static int test__leader_sample2(struct evlist *evlist __maybe_unused)
1242 {
1243         struct evsel *evsel, *leader;
1244
1245         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
1246
1247         /* instructions - sampling group leader */
1248         evsel = leader = evlist__first(evlist);
1249         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1250         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_INSTRUCTIONS));
1251         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1252         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1253         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1254         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1255         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1256         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1257         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1258         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1259         TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1260
1261         /* branch-misses - not sampling */
1262         evsel = evsel__next(evsel);
1263         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1264         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_BRANCH_MISSES));
1265         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1266         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1267         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1268         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1269         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1270         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1271         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1272         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1273         TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1274
1275         return TEST_OK;
1276 }
1277
1278 static int test__checkevent_pinned_modifier(struct evlist *evlist)
1279 {
1280         struct evsel *evsel = evlist__first(evlist);
1281
1282         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1283         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1284         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1285         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
1286         TEST_ASSERT_VAL("wrong pinned", evsel->core.attr.pinned);
1287
1288         return test__checkevent_symbolic_name(evlist);
1289 }
1290
1291 static int test__pinned_group(struct evlist *evlist)
1292 {
1293         struct evsel *evsel, *leader;
1294
1295         TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->core.nr_entries);
1296
1297         /* cycles - group leader */
1298         evsel = leader = evlist__first(evlist);
1299         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1300         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
1301         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1302         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1303         TEST_ASSERT_VAL("wrong pinned", evsel->core.attr.pinned);
1304
1305         /* cache-misses - can not be pinned, but will go on with the leader */
1306         evsel = evsel__next(evsel);
1307         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1308         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CACHE_MISSES));
1309         TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
1310
1311         /* branch-misses - ditto */
1312         evsel = evsel__next(evsel);
1313         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_BRANCH_MISSES));
1314         TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
1315
1316         return TEST_OK;
1317 }
1318
1319 static int test__checkevent_exclusive_modifier(struct evlist *evlist)
1320 {
1321         struct evsel *evsel = evlist__first(evlist);
1322
1323         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1324         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1325         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1326         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
1327         TEST_ASSERT_VAL("wrong exclusive", evsel->core.attr.exclusive);
1328
1329         return test__checkevent_symbolic_name(evlist);
1330 }
1331
1332 static int test__exclusive_group(struct evlist *evlist)
1333 {
1334         struct evsel *evsel, *leader;
1335
1336         TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->core.nr_entries);
1337
1338         /* cycles - group leader */
1339         evsel = leader = evlist__first(evlist);
1340         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1341         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
1342         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1343         TEST_ASSERT_VAL("wrong leader", evsel__has_leader(evsel, leader));
1344         TEST_ASSERT_VAL("wrong exclusive", evsel->core.attr.exclusive);
1345
1346         /* cache-misses - can not be pinned, but will go on with the leader */
1347         evsel = evsel__next(evsel);
1348         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1349         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CACHE_MISSES));
1350         TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive);
1351
1352         /* branch-misses - ditto */
1353         evsel = evsel__next(evsel);
1354         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_BRANCH_MISSES));
1355         TEST_ASSERT_VAL("wrong exclusive", !evsel->core.attr.exclusive);
1356
1357         return TEST_OK;
1358 }
1359 static int test__checkevent_breakpoint_len(struct evlist *evlist)
1360 {
1361         struct evsel *evsel = evlist__first(evlist);
1362
1363         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
1364         TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
1365         TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
1366         TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
1367                                          evsel->core.attr.bp_type);
1368         TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_1 ==
1369                                         evsel->core.attr.bp_len);
1370
1371         return TEST_OK;
1372 }
1373
1374 static int test__checkevent_breakpoint_len_w(struct evlist *evlist)
1375 {
1376         struct evsel *evsel = evlist__first(evlist);
1377
1378         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
1379         TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
1380         TEST_ASSERT_VAL("wrong config", test_config(evsel, 0));
1381         TEST_ASSERT_VAL("wrong bp_type", HW_BREAKPOINT_W ==
1382                                          evsel->core.attr.bp_type);
1383         TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_2 ==
1384                                         evsel->core.attr.bp_len);
1385
1386         return TEST_OK;
1387 }
1388
1389 static int
1390 test__checkevent_breakpoint_len_rw_modifier(struct evlist *evlist)
1391 {
1392         struct evsel *evsel = evlist__first(evlist);
1393
1394         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1395         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1396         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1397         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1398
1399         return test__checkevent_breakpoint_rw(evlist);
1400 }
1401
1402 static int test__checkevent_precise_max_modifier(struct evlist *evlist)
1403 {
1404         struct evsel *evsel = evlist__first(evlist);
1405
1406         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
1407         TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type);
1408         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_SW_TASK_CLOCK));
1409         return TEST_OK;
1410 }
1411
1412 static int test__checkevent_config_symbol(struct evlist *evlist)
1413 {
1414         struct evsel *evsel = evlist__first(evlist);
1415
1416         TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "insn"));
1417         return TEST_OK;
1418 }
1419
1420 static int test__checkevent_config_raw(struct evlist *evlist)
1421 {
1422         struct evsel *evsel = evlist__first(evlist);
1423
1424         TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "rawpmu"));
1425         return TEST_OK;
1426 }
1427
1428 static int test__checkevent_config_num(struct evlist *evlist)
1429 {
1430         struct evsel *evsel = evlist__first(evlist);
1431
1432         TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "numpmu"));
1433         return TEST_OK;
1434 }
1435
1436 static int test__checkevent_config_cache(struct evlist *evlist)
1437 {
1438         struct evsel *evsel = evlist__first(evlist);
1439
1440         TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "cachepmu"));
1441         return test__checkevent_genhw(evlist);
1442 }
1443
1444 static bool test__pmu_cpu_valid(void)
1445 {
1446         return !!perf_pmu__find("cpu");
1447 }
1448
1449 static bool test__intel_pt_valid(void)
1450 {
1451         return !!perf_pmu__find("intel_pt");
1452 }
1453
1454 static int test__intel_pt(struct evlist *evlist)
1455 {
1456         struct evsel *evsel = evlist__first(evlist);
1457
1458         TEST_ASSERT_VAL("wrong name setting", evsel__name_is(evsel, "intel_pt//u"));
1459         return TEST_OK;
1460 }
1461
1462 static int test__checkevent_complex_name(struct evlist *evlist)
1463 {
1464         struct evsel *evsel = evlist__first(evlist);
1465
1466         TEST_ASSERT_VAL("wrong complex name parsing",
1467                         evsel__name_is(evsel,
1468                                        "COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks"));
1469         return TEST_OK;
1470 }
1471
1472 static int test__checkevent_raw_pmu(struct evlist *evlist)
1473 {
1474         struct evsel *evsel = evlist__first(evlist);
1475
1476         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
1477         TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type);
1478         TEST_ASSERT_VAL("wrong config", test_config(evsel, 0x1a));
1479         return TEST_OK;
1480 }
1481
1482 static int test__sym_event_slash(struct evlist *evlist)
1483 {
1484         struct evsel *evsel = evlist__first(evlist);
1485
1486         TEST_ASSERT_VAL("wrong type", evsel->core.attr.type == PERF_TYPE_HARDWARE);
1487         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
1488         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1489         return TEST_OK;
1490 }
1491
1492 static int test__sym_event_dc(struct evlist *evlist)
1493 {
1494         struct evsel *evsel = evlist__first(evlist);
1495
1496         TEST_ASSERT_VAL("wrong type", evsel->core.attr.type == PERF_TYPE_HARDWARE);
1497         TEST_ASSERT_VAL("wrong config", test_config(evsel, PERF_COUNT_HW_CPU_CYCLES));
1498         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
1499         return TEST_OK;
1500 }
1501
1502 #ifdef HAVE_LIBTRACEEVENT
1503 static int count_tracepoints(void)
1504 {
1505         struct dirent *events_ent;
1506         DIR *events_dir;
1507         int cnt = 0;
1508
1509         events_dir = tracing_events__opendir();
1510
1511         TEST_ASSERT_VAL("Can't open events dir", events_dir);
1512
1513         while ((events_ent = readdir(events_dir))) {
1514                 char *sys_path;
1515                 struct dirent *sys_ent;
1516                 DIR *sys_dir;
1517
1518                 if (!strcmp(events_ent->d_name, ".")
1519                     || !strcmp(events_ent->d_name, "..")
1520                     || !strcmp(events_ent->d_name, "enable")
1521                     || !strcmp(events_ent->d_name, "header_event")
1522                     || !strcmp(events_ent->d_name, "header_page"))
1523                         continue;
1524
1525                 sys_path = get_events_file(events_ent->d_name);
1526                 TEST_ASSERT_VAL("Can't get sys path", sys_path);
1527
1528                 sys_dir = opendir(sys_path);
1529                 TEST_ASSERT_VAL("Can't open sys dir", sys_dir);
1530
1531                 while ((sys_ent = readdir(sys_dir))) {
1532                         if (!strcmp(sys_ent->d_name, ".")
1533                             || !strcmp(sys_ent->d_name, "..")
1534                             || !strcmp(sys_ent->d_name, "enable")
1535                             || !strcmp(sys_ent->d_name, "filter"))
1536                                 continue;
1537
1538                         cnt++;
1539                 }
1540
1541                 closedir(sys_dir);
1542                 put_events_file(sys_path);
1543         }
1544
1545         closedir(events_dir);
1546         return cnt;
1547 }
1548
1549 static int test__all_tracepoints(struct evlist *evlist)
1550 {
1551         TEST_ASSERT_VAL("wrong events count",
1552                         count_tracepoints() == evlist->core.nr_entries);
1553
1554         return test__checkevent_tracepoint_multi(evlist);
1555 }
1556 #endif /* HAVE_LIBTRACEVENT */
1557
1558 struct evlist_test {
1559         const char *name;
1560         bool (*valid)(void);
1561         int (*check)(struct evlist *evlist);
1562 };
1563
1564 static const struct evlist_test test__events[] = {
1565 #ifdef HAVE_LIBTRACEEVENT
1566         {
1567                 .name  = "syscalls:sys_enter_openat",
1568                 .check = test__checkevent_tracepoint,
1569                 /* 0 */
1570         },
1571         {
1572                 .name  = "syscalls:*",
1573                 .check = test__checkevent_tracepoint_multi,
1574                 /* 1 */
1575         },
1576 #endif
1577         {
1578                 .name  = "r1a",
1579                 .check = test__checkevent_raw,
1580                 /* 2 */
1581         },
1582         {
1583                 .name  = "1:1",
1584                 .check = test__checkevent_numeric,
1585                 /* 3 */
1586         },
1587         {
1588                 .name  = "instructions",
1589                 .check = test__checkevent_symbolic_name,
1590                 /* 4 */
1591         },
1592         {
1593                 .name  = "cycles/period=100000,config2/",
1594                 .check = test__checkevent_symbolic_name_config,
1595                 /* 5 */
1596         },
1597         {
1598                 .name  = "faults",
1599                 .check = test__checkevent_symbolic_alias,
1600                 /* 6 */
1601         },
1602         {
1603                 .name  = "L1-dcache-load-miss",
1604                 .check = test__checkevent_genhw,
1605                 /* 7 */
1606         },
1607         {
1608                 .name  = "mem:0",
1609                 .check = test__checkevent_breakpoint,
1610                 /* 8 */
1611         },
1612         {
1613                 .name  = "mem:0:x",
1614                 .check = test__checkevent_breakpoint_x,
1615                 /* 9 */
1616         },
1617         {
1618                 .name  = "mem:0:r",
1619                 .check = test__checkevent_breakpoint_r,
1620                 /* 0 */
1621         },
1622         {
1623                 .name  = "mem:0:w",
1624                 .check = test__checkevent_breakpoint_w,
1625                 /* 1 */
1626         },
1627 #ifdef HAVE_LIBTRACEEVENT
1628         {
1629                 .name  = "syscalls:sys_enter_openat:k",
1630                 .check = test__checkevent_tracepoint_modifier,
1631                 /* 2 */
1632         },
1633         {
1634                 .name  = "syscalls:*:u",
1635                 .check = test__checkevent_tracepoint_multi_modifier,
1636                 /* 3 */
1637         },
1638 #endif
1639         {
1640                 .name  = "r1a:kp",
1641                 .check = test__checkevent_raw_modifier,
1642                 /* 4 */
1643         },
1644         {
1645                 .name  = "1:1:hp",
1646                 .check = test__checkevent_numeric_modifier,
1647                 /* 5 */
1648         },
1649         {
1650                 .name  = "instructions:h",
1651                 .check = test__checkevent_symbolic_name_modifier,
1652                 /* 6 */
1653         },
1654         {
1655                 .name  = "faults:u",
1656                 .check = test__checkevent_symbolic_alias_modifier,
1657                 /* 7 */
1658         },
1659         {
1660                 .name  = "L1-dcache-load-miss:kp",
1661                 .check = test__checkevent_genhw_modifier,
1662                 /* 8 */
1663         },
1664         {
1665                 .name  = "mem:0:u",
1666                 .check = test__checkevent_breakpoint_modifier,
1667                 /* 9 */
1668         },
1669         {
1670                 .name  = "mem:0:x:k",
1671                 .check = test__checkevent_breakpoint_x_modifier,
1672                 /* 0 */
1673         },
1674         {
1675                 .name  = "mem:0:r:hp",
1676                 .check = test__checkevent_breakpoint_r_modifier,
1677                 /* 1 */
1678         },
1679         {
1680                 .name  = "mem:0:w:up",
1681                 .check = test__checkevent_breakpoint_w_modifier,
1682                 /* 2 */
1683         },
1684 #ifdef HAVE_LIBTRACEEVENT
1685         {
1686                 .name  = "r1,syscalls:sys_enter_openat:k,1:1:hp",
1687                 .check = test__checkevent_list,
1688                 /* 3 */
1689         },
1690 #endif
1691         {
1692                 .name  = "instructions:G",
1693                 .check = test__checkevent_exclude_host_modifier,
1694                 /* 4 */
1695         },
1696         {
1697                 .name  = "instructions:H",
1698                 .check = test__checkevent_exclude_guest_modifier,
1699                 /* 5 */
1700         },
1701         {
1702                 .name  = "mem:0:rw",
1703                 .check = test__checkevent_breakpoint_rw,
1704                 /* 6 */
1705         },
1706         {
1707                 .name  = "mem:0:rw:kp",
1708                 .check = test__checkevent_breakpoint_rw_modifier,
1709                 /* 7 */
1710         },
1711         {
1712                 .name  = "{instructions:k,cycles:upp}",
1713                 .check = test__group1,
1714                 /* 8 */
1715         },
1716         {
1717                 .name  = "{faults:k,cache-references}:u,cycles:k",
1718                 .check = test__group2,
1719                 /* 9 */
1720         },
1721 #ifdef HAVE_LIBTRACEEVENT
1722         {
1723                 .name  = "group1{syscalls:sys_enter_openat:H,cycles:kppp},group2{cycles,1:3}:G,instructions:u",
1724                 .check = test__group3,
1725                 /* 0 */
1726         },
1727 #endif
1728         {
1729                 .name  = "{cycles:u,instructions:kp}:p",
1730                 .check = test__group4,
1731                 /* 1 */
1732         },
1733         {
1734                 .name  = "{cycles,instructions}:G,{cycles:G,instructions:G},cycles",
1735                 .check = test__group5,
1736                 /* 2 */
1737         },
1738 #ifdef HAVE_LIBTRACEEVENT
1739         {
1740                 .name  = "*:*",
1741                 .check = test__all_tracepoints,
1742                 /* 3 */
1743         },
1744 #endif
1745         {
1746                 .name  = "{cycles,cache-misses:G}:H",
1747                 .check = test__group_gh1,
1748                 /* 4 */
1749         },
1750         {
1751                 .name  = "{cycles,cache-misses:H}:G",
1752                 .check = test__group_gh2,
1753                 /* 5 */
1754         },
1755         {
1756                 .name  = "{cycles:G,cache-misses:H}:u",
1757                 .check = test__group_gh3,
1758                 /* 6 */
1759         },
1760         {
1761                 .name  = "{cycles:G,cache-misses:H}:uG",
1762                 .check = test__group_gh4,
1763                 /* 7 */
1764         },
1765         {
1766                 .name  = "{cycles,cache-misses,branch-misses}:S",
1767                 .check = test__leader_sample1,
1768                 /* 8 */
1769         },
1770         {
1771                 .name  = "{instructions,branch-misses}:Su",
1772                 .check = test__leader_sample2,
1773                 /* 9 */
1774         },
1775         {
1776                 .name  = "instructions:uDp",
1777                 .check = test__checkevent_pinned_modifier,
1778                 /* 0 */
1779         },
1780         {
1781                 .name  = "{cycles,cache-misses,branch-misses}:D",
1782                 .check = test__pinned_group,
1783                 /* 1 */
1784         },
1785         {
1786                 .name  = "mem:0/1",
1787                 .check = test__checkevent_breakpoint_len,
1788                 /* 2 */
1789         },
1790         {
1791                 .name  = "mem:0/2:w",
1792                 .check = test__checkevent_breakpoint_len_w,
1793                 /* 3 */
1794         },
1795         {
1796                 .name  = "mem:0/4:rw:u",
1797                 .check = test__checkevent_breakpoint_len_rw_modifier,
1798                 /* 4 */
1799         },
1800 #if defined(__s390x__) && defined(HAVE_LIBTRACEEVENT)
1801         {
1802                 .name  = "kvm-s390:kvm_s390_create_vm",
1803                 .check = test__checkevent_tracepoint,
1804                 .valid = kvm_s390_create_vm_valid,
1805                 /* 0 */
1806         },
1807 #endif
1808         {
1809                 .name  = "instructions:I",
1810                 .check = test__checkevent_exclude_idle_modifier,
1811                 /* 5 */
1812         },
1813         {
1814                 .name  = "instructions:kIG",
1815                 .check = test__checkevent_exclude_idle_modifier_1,
1816                 /* 6 */
1817         },
1818         {
1819                 .name  = "task-clock:P,cycles",
1820                 .check = test__checkevent_precise_max_modifier,
1821                 /* 7 */
1822         },
1823         {
1824                 .name  = "instructions/name=insn/",
1825                 .check = test__checkevent_config_symbol,
1826                 /* 8 */
1827         },
1828         {
1829                 .name  = "r1234/name=rawpmu/",
1830                 .check = test__checkevent_config_raw,
1831                 /* 9 */
1832         },
1833         {
1834                 .name  = "4:0x6530160/name=numpmu/",
1835                 .check = test__checkevent_config_num,
1836                 /* 0 */
1837         },
1838         {
1839                 .name  = "L1-dcache-misses/name=cachepmu/",
1840                 .check = test__checkevent_config_cache,
1841                 /* 1 */
1842         },
1843         {
1844                 .name  = "intel_pt//u",
1845                 .valid = test__intel_pt_valid,
1846                 .check = test__intel_pt,
1847                 /* 2 */
1848         },
1849         {
1850                 .name  = "cycles/name='COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks'/Duk",
1851                 .check = test__checkevent_complex_name,
1852                 /* 3 */
1853         },
1854         {
1855                 .name  = "cycles//u",
1856                 .check = test__sym_event_slash,
1857                 /* 4 */
1858         },
1859         {
1860                 .name  = "cycles:k",
1861                 .check = test__sym_event_dc,
1862                 /* 5 */
1863         },
1864         {
1865                 .name  = "instructions:uep",
1866                 .check = test__checkevent_exclusive_modifier,
1867                 /* 6 */
1868         },
1869         {
1870                 .name  = "{cycles,cache-misses,branch-misses}:e",
1871                 .check = test__exclusive_group,
1872                 /* 7 */
1873         },
1874 };
1875
1876 static const struct evlist_test test__events_pmu[] = {
1877         {
1878                 .name  = "cpu/config=10,config1,config2=3,period=1000/u",
1879                 .valid = test__pmu_cpu_valid,
1880                 .check = test__checkevent_pmu,
1881                 /* 0 */
1882         },
1883         {
1884                 .name  = "cpu/config=1,name=krava/u,cpu/config=2/u",
1885                 .valid = test__pmu_cpu_valid,
1886                 .check = test__checkevent_pmu_name,
1887                 /* 1 */
1888         },
1889         {
1890                 .name  = "cpu/config=1,call-graph=fp,time,period=100000/,cpu/config=2,call-graph=no,time=0,period=2000/",
1891                 .valid = test__pmu_cpu_valid,
1892                 .check = test__checkevent_pmu_partial_time_callgraph,
1893                 /* 2 */
1894         },
1895         {
1896                 .name  = "cpu/name='COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks',period=0x1,event=0x2/ukp",
1897                 .valid = test__pmu_cpu_valid,
1898                 .check = test__checkevent_complex_name,
1899                 /* 3 */
1900         },
1901         {
1902                 .name  = "software/r1a/",
1903                 .check = test__checkevent_raw_pmu,
1904                 /* 4 */
1905         },
1906         {
1907                 .name  = "software/r0x1a/",
1908                 .check = test__checkevent_raw_pmu,
1909                 /* 5 */
1910         },
1911         {
1912                 .name  = "cpu/L1-dcache-load-miss/",
1913                 .valid = test__pmu_cpu_valid,
1914                 .check = test__checkevent_genhw,
1915                 /* 6 */
1916         },
1917         {
1918                 .name  = "cpu/L1-dcache-load-miss/kp",
1919                 .valid = test__pmu_cpu_valid,
1920                 .check = test__checkevent_genhw_modifier,
1921                 /* 7 */
1922         },
1923         {
1924                 .name  = "cpu/L1-dcache-misses,name=cachepmu/",
1925                 .valid = test__pmu_cpu_valid,
1926                 .check = test__checkevent_config_cache,
1927                 /* 8 */
1928         },
1929 };
1930
1931 struct terms_test {
1932         const char *str;
1933         int (*check)(struct list_head *terms);
1934 };
1935
1936 static const struct terms_test test__terms[] = {
1937         [0] = {
1938                 .str   = "config=10,config1,config2=3,config3=4,umask=1,read,r0xead",
1939                 .check = test__checkterms_simple,
1940         },
1941 };
1942
1943 static int test_event(const struct evlist_test *e)
1944 {
1945         struct parse_events_error err;
1946         struct evlist *evlist;
1947         int ret;
1948
1949         if (e->valid && !e->valid()) {
1950                 pr_debug("... SKIP\n");
1951                 return TEST_OK;
1952         }
1953
1954         evlist = evlist__new();
1955         if (evlist == NULL) {
1956                 pr_err("Failed allocation");
1957                 return TEST_FAIL;
1958         }
1959         parse_events_error__init(&err);
1960         ret = parse_events(evlist, e->name, &err);
1961         if (ret) {
1962                 pr_debug("failed to parse event '%s', err %d, str '%s'\n",
1963                          e->name, ret, err.str);
1964                 parse_events_error__print(&err, e->name);
1965                 ret = TEST_FAIL;
1966                 if (err.str && strstr(err.str, "can't access trace events"))
1967                         ret = TEST_SKIP;
1968         } else {
1969                 ret = e->check(evlist);
1970         }
1971         parse_events_error__exit(&err);
1972         evlist__delete(evlist);
1973
1974         return ret;
1975 }
1976
1977 static int test_event_fake_pmu(const char *str)
1978 {
1979         struct parse_events_error err;
1980         struct evlist *evlist;
1981         int ret;
1982
1983         evlist = evlist__new();
1984         if (!evlist)
1985                 return -ENOMEM;
1986
1987         parse_events_error__init(&err);
1988         ret = __parse_events(evlist, str, /*pmu_filter=*/NULL, &err,
1989                              &perf_pmu__fake, /*warn_if_reordered=*/true);
1990         if (ret) {
1991                 pr_debug("failed to parse event '%s', err %d, str '%s'\n",
1992                          str, ret, err.str);
1993                 parse_events_error__print(&err, str);
1994         }
1995
1996         parse_events_error__exit(&err);
1997         evlist__delete(evlist);
1998
1999         return ret;
2000 }
2001
2002 static int combine_test_results(int existing, int latest)
2003 {
2004         if (existing == TEST_FAIL)
2005                 return TEST_FAIL;
2006         if (existing == TEST_SKIP)
2007                 return latest == TEST_OK ? TEST_SKIP : latest;
2008         return latest;
2009 }
2010
2011 static int test_events(const struct evlist_test *events, int cnt)
2012 {
2013         int ret = TEST_OK;
2014
2015         for (int i = 0; i < cnt; i++) {
2016                 const struct evlist_test *e = &events[i];
2017                 int test_ret;
2018
2019                 pr_debug("running test %d '%s'\n", i, e->name);
2020                 test_ret = test_event(e);
2021                 if (test_ret != TEST_OK) {
2022                         pr_debug("Event test failure: test %d '%s'", i, e->name);
2023                         ret = combine_test_results(ret, test_ret);
2024                 }
2025         }
2026
2027         return ret;
2028 }
2029
2030 static int test__events2(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2031 {
2032         return test_events(test__events, ARRAY_SIZE(test__events));
2033 }
2034
2035 static int test_term(const struct terms_test *t)
2036 {
2037         struct list_head terms;
2038         int ret;
2039
2040         INIT_LIST_HEAD(&terms);
2041
2042         ret = parse_events_terms(&terms, t->str);
2043         if (ret) {
2044                 pr_debug("failed to parse terms '%s', err %d\n",
2045                          t->str , ret);
2046                 return ret;
2047         }
2048
2049         ret = t->check(&terms);
2050         parse_events_terms__purge(&terms);
2051
2052         return ret;
2053 }
2054
2055 static int test_terms(const struct terms_test *terms, int cnt)
2056 {
2057         int ret = 0;
2058
2059         for (int i = 0; i < cnt; i++) {
2060                 const struct terms_test *t = &terms[i];
2061
2062                 pr_debug("running test %d '%s'\n", i, t->str);
2063                 ret = test_term(t);
2064                 if (ret)
2065                         break;
2066         }
2067
2068         return ret;
2069 }
2070
2071 static int test__terms2(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2072 {
2073         return test_terms(test__terms, ARRAY_SIZE(test__terms));
2074 }
2075
2076 static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2077 {
2078         struct perf_pmu *pmu;
2079         int ret = TEST_OK;
2080
2081         if (list_empty(&pmus))
2082                 perf_pmu__scan(NULL);
2083
2084         perf_pmus__for_each_pmu(pmu) {
2085                 struct stat st;
2086                 char path[PATH_MAX];
2087                 struct dirent *ent;
2088                 DIR *dir;
2089                 int err;
2090
2091                 snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/events/",
2092                         sysfs__mountpoint(), pmu->name);
2093
2094                 err = stat(path, &st);
2095                 if (err) {
2096                         pr_debug("skipping PMU %s events tests: %s\n", pmu->name, path);
2097                         continue;
2098                 }
2099
2100                 dir = opendir(path);
2101                 if (!dir) {
2102                         pr_debug("can't open pmu event dir: %s\n", path);
2103                         ret = combine_test_results(ret, TEST_SKIP);
2104                         continue;
2105                 }
2106
2107                 while ((ent = readdir(dir))) {
2108                         struct evlist_test e = { .name = NULL, };
2109                         char name[2 * NAME_MAX + 1 + 12 + 3];
2110                         int test_ret;
2111
2112                         /* Names containing . are special and cannot be used directly */
2113                         if (strchr(ent->d_name, '.'))
2114                                 continue;
2115
2116                         snprintf(name, sizeof(name), "%s/event=%s/u", pmu->name, ent->d_name);
2117
2118                         e.name  = name;
2119                         e.check = test__checkevent_pmu_events;
2120
2121                         test_ret = test_event(&e);
2122                         if (test_ret != TEST_OK) {
2123                                 pr_debug("Test PMU event failed for '%s'", name);
2124                                 ret = combine_test_results(ret, test_ret);
2125                         }
2126
2127                         if (!is_pmu_core(pmu->name))
2128                                 continue;
2129
2130                         /*
2131                          * Names containing '-' are recognized as prefixes and suffixes
2132                          * due to '-' being a legacy PMU separator. This fails when the
2133                          * prefix or suffix collides with an existing legacy token. For
2134                          * example, branch-brs has a prefix (branch) that collides with
2135                          * a PE_NAME_CACHE_TYPE token causing a parse error as a suffix
2136                          * isn't expected after this. As event names in the config
2137                          * slashes are allowed a '-' in the name we check this works
2138                          * above.
2139                          */
2140                         if (strchr(ent->d_name, '-'))
2141                                 continue;
2142
2143                         snprintf(name, sizeof(name), "%s:u,%s/event=%s/u",
2144                                  ent->d_name, pmu->name, ent->d_name);
2145                         e.name  = name;
2146                         e.check = test__checkevent_pmu_events_mix;
2147                         test_ret = test_event(&e);
2148                         if (test_ret != TEST_OK) {
2149                                 pr_debug("Test PMU event failed for '%s'", name);
2150                                 ret = combine_test_results(ret, test_ret);
2151                         }
2152                 }
2153
2154                 closedir(dir);
2155         }
2156         return ret;
2157 }
2158
2159 static int test__pmu_events2(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2160 {
2161         return test_events(test__events_pmu, ARRAY_SIZE(test__events_pmu));
2162 }
2163
2164 static bool test_alias(char **event, char **alias)
2165 {
2166         char path[PATH_MAX];
2167         DIR *dir;
2168         struct dirent *dent;
2169         const char *sysfs = sysfs__mountpoint();
2170         char buf[128];
2171         FILE *file;
2172
2173         if (!sysfs)
2174                 return false;
2175
2176         snprintf(path, PATH_MAX, "%s/bus/event_source/devices/", sysfs);
2177         dir = opendir(path);
2178         if (!dir)
2179                 return false;
2180
2181         while ((dent = readdir(dir))) {
2182                 if (!strcmp(dent->d_name, ".") ||
2183                     !strcmp(dent->d_name, ".."))
2184                         continue;
2185
2186                 snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/alias",
2187                          sysfs, dent->d_name);
2188
2189                 if (!file_available(path))
2190                         continue;
2191
2192                 file = fopen(path, "r");
2193                 if (!file)
2194                         continue;
2195
2196                 if (!fgets(buf, sizeof(buf), file)) {
2197                         fclose(file);
2198                         continue;
2199                 }
2200
2201                 /* Remove the last '\n' */
2202                 buf[strlen(buf) - 1] = 0;
2203
2204                 fclose(file);
2205                 *event = strdup(dent->d_name);
2206                 *alias = strdup(buf);
2207                 closedir(dir);
2208
2209                 if (*event == NULL || *alias == NULL) {
2210                         free(*event);
2211                         free(*alias);
2212                         return false;
2213                 }
2214
2215                 return true;
2216         }
2217
2218         closedir(dir);
2219         return false;
2220 }
2221
2222 static int test__checkevent_pmu_events_alias(struct evlist *evlist)
2223 {
2224         struct evsel *evsel1 = evlist__first(evlist);
2225         struct evsel *evsel2 = evlist__last(evlist);
2226
2227         TEST_ASSERT_VAL("wrong type", evsel1->core.attr.type == evsel2->core.attr.type);
2228         TEST_ASSERT_VAL("wrong config", evsel1->core.attr.config == evsel2->core.attr.config);
2229         return TEST_OK;
2230 }
2231
2232 static int test__pmu_events_alias(char *event, char *alias)
2233 {
2234         struct evlist_test e = { .name = NULL, };
2235         char name[2 * NAME_MAX + 20];
2236
2237         snprintf(name, sizeof(name), "%s/event=1/,%s/event=1/",
2238                  event, alias);
2239
2240         e.name  = name;
2241         e.check = test__checkevent_pmu_events_alias;
2242         return test_event(&e);
2243 }
2244
2245 static int test__alias(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2246 {
2247         char *event, *alias;
2248         int ret;
2249
2250         if (!test_alias(&event, &alias))
2251                 return TEST_SKIP;
2252
2253         ret = test__pmu_events_alias(event, alias);
2254
2255         free(event);
2256         free(alias);
2257         return ret;
2258 }
2259
2260 static int test__pmu_events_alias2(struct test_suite *test __maybe_unused,
2261                                    int subtest __maybe_unused)
2262 {
2263         static const char events[][30] = {
2264                         "event-hyphen",
2265                         "event-two-hyph",
2266         };
2267         int ret = TEST_OK;
2268
2269         for (unsigned int i = 0; i < ARRAY_SIZE(events); i++) {
2270                 int test_ret = test_event_fake_pmu(&events[i][0]);
2271
2272                 if (test_ret != TEST_OK) {
2273                         pr_debug("check_parse_fake %s failed\n", &events[i][0]);
2274                         ret = combine_test_results(ret, test_ret);
2275                 }
2276         }
2277
2278         return ret;
2279 }
2280
2281 static struct test_case tests__parse_events[] = {
2282         TEST_CASE_REASON("Test event parsing",
2283                          events2,
2284                          "permissions"),
2285         TEST_CASE_REASON("Parsing of all PMU events from sysfs",
2286                          pmu_events,
2287                          "permissions"),
2288         TEST_CASE_REASON("Parsing of given PMU events from sysfs",
2289                          pmu_events2,
2290                          "permissions"),
2291         TEST_CASE_REASON("Parsing of aliased events from sysfs", alias,
2292                          "no aliases in sysfs"),
2293         TEST_CASE("Parsing of aliased events", pmu_events_alias2),
2294         TEST_CASE("Parsing of terms (event modifiers)", terms2),
2295         {       .name = NULL, }
2296 };
2297
2298 struct test_suite suite__parse_events = {
2299         .desc = "Parse event definition strings",
2300         .test_cases = tests__parse_events,
2301 };