Merge branch 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 "util.h"
10 #include <dirent.h>
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <linux/kernel.h>
16 #include <linux/hw_breakpoint.h>
17 #include <api/fs/tracing_path.h>
18
19 #define PERF_TP_SAMPLE_TYPE (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | \
20                              PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD)
21
22 #if defined(__s390x__)
23 /* Return true if kvm module is available and loaded. Test this
24  * and retun success when trace point kvm_s390_create_vm
25  * exists. Otherwise this test always fails.
26  */
27 static bool kvm_s390_create_vm_valid(void)
28 {
29         char *eventfile;
30         bool rc = false;
31
32         eventfile = get_events_file("kvm-s390");
33
34         if (eventfile) {
35                 DIR *mydir = opendir(eventfile);
36
37                 if (mydir) {
38                         rc = true;
39                         closedir(mydir);
40                 }
41                 put_events_file(eventfile);
42         }
43
44         return rc;
45 }
46 #endif
47
48 static int test__checkevent_tracepoint(struct evlist *evlist)
49 {
50         struct evsel *evsel = perf_evlist__first(evlist);
51
52         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
53         TEST_ASSERT_VAL("wrong number of groups", 0 == evlist->nr_groups);
54         TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
55         TEST_ASSERT_VAL("wrong sample_type",
56                 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
57         TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period);
58         return 0;
59 }
60
61 static int test__checkevent_tracepoint_multi(struct evlist *evlist)
62 {
63         struct evsel *evsel;
64
65         TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries > 1);
66         TEST_ASSERT_VAL("wrong number of groups", 0 == evlist->nr_groups);
67
68         evlist__for_each_entry(evlist, evsel) {
69                 TEST_ASSERT_VAL("wrong type",
70                         PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
71                 TEST_ASSERT_VAL("wrong sample_type",
72                         PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
73                 TEST_ASSERT_VAL("wrong sample_period",
74                         1 == evsel->core.attr.sample_period);
75         }
76         return 0;
77 }
78
79 static int test__checkevent_raw(struct evlist *evlist)
80 {
81         struct evsel *evsel = perf_evlist__first(evlist);
82
83         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
84         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
85         TEST_ASSERT_VAL("wrong config", 0x1a == evsel->core.attr.config);
86         return 0;
87 }
88
89 static int test__checkevent_numeric(struct evlist *evlist)
90 {
91         struct evsel *evsel = perf_evlist__first(evlist);
92
93         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
94         TEST_ASSERT_VAL("wrong type", 1 == evsel->core.attr.type);
95         TEST_ASSERT_VAL("wrong config", 1 == evsel->core.attr.config);
96         return 0;
97 }
98
99 static int test__checkevent_symbolic_name(struct evlist *evlist)
100 {
101         struct evsel *evsel = perf_evlist__first(evlist);
102
103         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
104         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
105         TEST_ASSERT_VAL("wrong config",
106                         PERF_COUNT_HW_INSTRUCTIONS == evsel->core.attr.config);
107         return 0;
108 }
109
110 static int test__checkevent_symbolic_name_config(struct evlist *evlist)
111 {
112         struct evsel *evsel = perf_evlist__first(evlist);
113
114         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
115         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
116         TEST_ASSERT_VAL("wrong config",
117                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
118         /*
119          * The period value gets configured within perf_evlist__config,
120          * while this test executes only parse events method.
121          */
122         TEST_ASSERT_VAL("wrong period",
123                         0 == evsel->core.attr.sample_period);
124         TEST_ASSERT_VAL("wrong config1",
125                         0 == evsel->core.attr.config1);
126         TEST_ASSERT_VAL("wrong config2",
127                         1 == evsel->core.attr.config2);
128         return 0;
129 }
130
131 static int test__checkevent_symbolic_alias(struct evlist *evlist)
132 {
133         struct evsel *evsel = perf_evlist__first(evlist);
134
135         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
136         TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type);
137         TEST_ASSERT_VAL("wrong config",
138                         PERF_COUNT_SW_PAGE_FAULTS == evsel->core.attr.config);
139         return 0;
140 }
141
142 static int test__checkevent_genhw(struct evlist *evlist)
143 {
144         struct evsel *evsel = perf_evlist__first(evlist);
145
146         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
147         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->core.attr.type);
148         TEST_ASSERT_VAL("wrong config", (1 << 16) == evsel->core.attr.config);
149         return 0;
150 }
151
152 static int test__checkevent_breakpoint(struct evlist *evlist)
153 {
154         struct evsel *evsel = perf_evlist__first(evlist);
155
156         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
157         TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
158         TEST_ASSERT_VAL("wrong config", 0 == evsel->core.attr.config);
159         TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
160                                          evsel->core.attr.bp_type);
161         TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 ==
162                                         evsel->core.attr.bp_len);
163         return 0;
164 }
165
166 static int test__checkevent_breakpoint_x(struct evlist *evlist)
167 {
168         struct evsel *evsel = perf_evlist__first(evlist);
169
170         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
171         TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
172         TEST_ASSERT_VAL("wrong config", 0 == evsel->core.attr.config);
173         TEST_ASSERT_VAL("wrong bp_type",
174                         HW_BREAKPOINT_X == evsel->core.attr.bp_type);
175         TEST_ASSERT_VAL("wrong bp_len", sizeof(long) == evsel->core.attr.bp_len);
176         return 0;
177 }
178
179 static int test__checkevent_breakpoint_r(struct evlist *evlist)
180 {
181         struct evsel *evsel = perf_evlist__first(evlist);
182
183         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
184         TEST_ASSERT_VAL("wrong type",
185                         PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
186         TEST_ASSERT_VAL("wrong config", 0 == evsel->core.attr.config);
187         TEST_ASSERT_VAL("wrong bp_type",
188                         HW_BREAKPOINT_R == evsel->core.attr.bp_type);
189         TEST_ASSERT_VAL("wrong bp_len",
190                         HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len);
191         return 0;
192 }
193
194 static int test__checkevent_breakpoint_w(struct evlist *evlist)
195 {
196         struct evsel *evsel = perf_evlist__first(evlist);
197
198         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
199         TEST_ASSERT_VAL("wrong type",
200                         PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
201         TEST_ASSERT_VAL("wrong config", 0 == evsel->core.attr.config);
202         TEST_ASSERT_VAL("wrong bp_type",
203                         HW_BREAKPOINT_W == evsel->core.attr.bp_type);
204         TEST_ASSERT_VAL("wrong bp_len",
205                         HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len);
206         return 0;
207 }
208
209 static int test__checkevent_breakpoint_rw(struct evlist *evlist)
210 {
211         struct evsel *evsel = perf_evlist__first(evlist);
212
213         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
214         TEST_ASSERT_VAL("wrong type",
215                         PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
216         TEST_ASSERT_VAL("wrong config", 0 == evsel->core.attr.config);
217         TEST_ASSERT_VAL("wrong bp_type",
218                 (HW_BREAKPOINT_R|HW_BREAKPOINT_W) == evsel->core.attr.bp_type);
219         TEST_ASSERT_VAL("wrong bp_len",
220                         HW_BREAKPOINT_LEN_4 == evsel->core.attr.bp_len);
221         return 0;
222 }
223
224 static int test__checkevent_tracepoint_modifier(struct evlist *evlist)
225 {
226         struct evsel *evsel = perf_evlist__first(evlist);
227
228         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
229         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
230         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
231         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
232
233         return test__checkevent_tracepoint(evlist);
234 }
235
236 static int
237 test__checkevent_tracepoint_multi_modifier(struct evlist *evlist)
238 {
239         struct evsel *evsel;
240
241         TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries > 1);
242
243         evlist__for_each_entry(evlist, evsel) {
244                 TEST_ASSERT_VAL("wrong exclude_user",
245                                 !evsel->core.attr.exclude_user);
246                 TEST_ASSERT_VAL("wrong exclude_kernel",
247                                 evsel->core.attr.exclude_kernel);
248                 TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
249                 TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
250         }
251
252         return test__checkevent_tracepoint_multi(evlist);
253 }
254
255 static int test__checkevent_raw_modifier(struct evlist *evlist)
256 {
257         struct evsel *evsel = perf_evlist__first(evlist);
258
259         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
260         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
261         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
262         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
263
264         return test__checkevent_raw(evlist);
265 }
266
267 static int test__checkevent_numeric_modifier(struct evlist *evlist)
268 {
269         struct evsel *evsel = perf_evlist__first(evlist);
270
271         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
272         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
273         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
274         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
275
276         return test__checkevent_numeric(evlist);
277 }
278
279 static int test__checkevent_symbolic_name_modifier(struct evlist *evlist)
280 {
281         struct evsel *evsel = perf_evlist__first(evlist);
282
283         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
284         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
285         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
286         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
287
288         return test__checkevent_symbolic_name(evlist);
289 }
290
291 static int test__checkevent_exclude_host_modifier(struct evlist *evlist)
292 {
293         struct evsel *evsel = perf_evlist__first(evlist);
294
295         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
296         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
297
298         return test__checkevent_symbolic_name(evlist);
299 }
300
301 static int test__checkevent_exclude_guest_modifier(struct evlist *evlist)
302 {
303         struct evsel *evsel = perf_evlist__first(evlist);
304
305         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
306         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
307
308         return test__checkevent_symbolic_name(evlist);
309 }
310
311 static int test__checkevent_symbolic_alias_modifier(struct evlist *evlist)
312 {
313         struct evsel *evsel = perf_evlist__first(evlist);
314
315         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
316         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
317         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
318         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
319
320         return test__checkevent_symbolic_alias(evlist);
321 }
322
323 static int test__checkevent_genhw_modifier(struct evlist *evlist)
324 {
325         struct evsel *evsel = perf_evlist__first(evlist);
326
327         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
328         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
329         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
330         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
331
332         return test__checkevent_genhw(evlist);
333 }
334
335 static int test__checkevent_exclude_idle_modifier(struct evlist *evlist)
336 {
337         struct evsel *evsel = perf_evlist__first(evlist);
338
339         TEST_ASSERT_VAL("wrong exclude idle", evsel->core.attr.exclude_idle);
340         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
341         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
342         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
343         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
344         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
345         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
346
347         return test__checkevent_symbolic_name(evlist);
348 }
349
350 static int test__checkevent_exclude_idle_modifier_1(struct evlist *evlist)
351 {
352         struct evsel *evsel = perf_evlist__first(evlist);
353
354         TEST_ASSERT_VAL("wrong exclude idle", evsel->core.attr.exclude_idle);
355         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
356         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
357         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
358         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
359         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
360         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
361
362         return test__checkevent_symbolic_name(evlist);
363 }
364
365 static int test__checkevent_breakpoint_modifier(struct evlist *evlist)
366 {
367         struct evsel *evsel = perf_evlist__first(evlist);
368
369
370         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
371         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
372         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
373         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
374         TEST_ASSERT_VAL("wrong name",
375                         !strcmp(perf_evsel__name(evsel), "mem:0:u"));
376
377         return test__checkevent_breakpoint(evlist);
378 }
379
380 static int test__checkevent_breakpoint_x_modifier(struct evlist *evlist)
381 {
382         struct evsel *evsel = perf_evlist__first(evlist);
383
384         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
385         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
386         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
387         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
388         TEST_ASSERT_VAL("wrong name",
389                         !strcmp(perf_evsel__name(evsel), "mem:0:x:k"));
390
391         return test__checkevent_breakpoint_x(evlist);
392 }
393
394 static int test__checkevent_breakpoint_r_modifier(struct evlist *evlist)
395 {
396         struct evsel *evsel = perf_evlist__first(evlist);
397
398         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
399         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
400         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
401         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
402         TEST_ASSERT_VAL("wrong name",
403                         !strcmp(perf_evsel__name(evsel), "mem:0:r:hp"));
404
405         return test__checkevent_breakpoint_r(evlist);
406 }
407
408 static int test__checkevent_breakpoint_w_modifier(struct evlist *evlist)
409 {
410         struct evsel *evsel = perf_evlist__first(evlist);
411
412         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
413         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
414         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
415         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
416         TEST_ASSERT_VAL("wrong name",
417                         !strcmp(perf_evsel__name(evsel), "mem:0:w:up"));
418
419         return test__checkevent_breakpoint_w(evlist);
420 }
421
422 static int test__checkevent_breakpoint_rw_modifier(struct evlist *evlist)
423 {
424         struct evsel *evsel = perf_evlist__first(evlist);
425
426         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
427         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
428         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
429         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
430         TEST_ASSERT_VAL("wrong name",
431                         !strcmp(perf_evsel__name(evsel), "mem:0:rw:kp"));
432
433         return test__checkevent_breakpoint_rw(evlist);
434 }
435
436 static int test__checkevent_pmu(struct evlist *evlist)
437 {
438
439         struct evsel *evsel = perf_evlist__first(evlist);
440
441         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
442         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
443         TEST_ASSERT_VAL("wrong config",    10 == evsel->core.attr.config);
444         TEST_ASSERT_VAL("wrong config1",    1 == evsel->core.attr.config1);
445         TEST_ASSERT_VAL("wrong config2",    3 == evsel->core.attr.config2);
446         /*
447          * The period value gets configured within perf_evlist__config,
448          * while this test executes only parse events method.
449          */
450         TEST_ASSERT_VAL("wrong period",     0 == evsel->core.attr.sample_period);
451
452         return 0;
453 }
454
455 static int test__checkevent_list(struct evlist *evlist)
456 {
457         struct evsel *evsel = perf_evlist__first(evlist);
458
459         TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->core.nr_entries);
460
461         /* r1 */
462         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
463         TEST_ASSERT_VAL("wrong config", 1 == evsel->core.attr.config);
464         TEST_ASSERT_VAL("wrong config1", 0 == evsel->core.attr.config1);
465         TEST_ASSERT_VAL("wrong config2", 0 == evsel->core.attr.config2);
466         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
467         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
468         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
469         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
470
471         /* syscalls:sys_enter_openat:k */
472         evsel = perf_evsel__next(evsel);
473         TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
474         TEST_ASSERT_VAL("wrong sample_type",
475                 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
476         TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period);
477         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
478         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
479         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
480         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
481
482         /* 1:1:hp */
483         evsel = perf_evsel__next(evsel);
484         TEST_ASSERT_VAL("wrong type", 1 == evsel->core.attr.type);
485         TEST_ASSERT_VAL("wrong config", 1 == evsel->core.attr.config);
486         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
487         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
488         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
489         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
490
491         return 0;
492 }
493
494 static int test__checkevent_pmu_name(struct evlist *evlist)
495 {
496         struct evsel *evsel = perf_evlist__first(evlist);
497
498         /* cpu/config=1,name=krava/u */
499         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
500         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
501         TEST_ASSERT_VAL("wrong config",  1 == evsel->core.attr.config);
502         TEST_ASSERT_VAL("wrong name", !strcmp(perf_evsel__name(evsel), "krava"));
503
504         /* cpu/config=2/u" */
505         evsel = perf_evsel__next(evsel);
506         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
507         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
508         TEST_ASSERT_VAL("wrong config",  2 == evsel->core.attr.config);
509         TEST_ASSERT_VAL("wrong name",
510                         !strcmp(perf_evsel__name(evsel), "cpu/config=2/u"));
511
512         return 0;
513 }
514
515 static int test__checkevent_pmu_partial_time_callgraph(struct evlist *evlist)
516 {
517         struct evsel *evsel = perf_evlist__first(evlist);
518
519         /* cpu/config=1,call-graph=fp,time,period=100000/ */
520         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
521         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
522         TEST_ASSERT_VAL("wrong config",  1 == evsel->core.attr.config);
523         /*
524          * The period, time and callgraph value gets configured
525          * within perf_evlist__config,
526          * while this test executes only parse events method.
527          */
528         TEST_ASSERT_VAL("wrong period",     0 == evsel->core.attr.sample_period);
529         TEST_ASSERT_VAL("wrong callgraph",  !evsel__has_callchain(evsel));
530         TEST_ASSERT_VAL("wrong time",  !(PERF_SAMPLE_TIME & evsel->core.attr.sample_type));
531
532         /* cpu/config=2,call-graph=no,time=0,period=2000/ */
533         evsel = perf_evsel__next(evsel);
534         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
535         TEST_ASSERT_VAL("wrong config",  2 == evsel->core.attr.config);
536         /*
537          * The period, time and callgraph value gets configured
538          * within perf_evlist__config,
539          * while this test executes only parse events method.
540          */
541         TEST_ASSERT_VAL("wrong period",     0 == evsel->core.attr.sample_period);
542         TEST_ASSERT_VAL("wrong callgraph",  !evsel__has_callchain(evsel));
543         TEST_ASSERT_VAL("wrong time",  !(PERF_SAMPLE_TIME & evsel->core.attr.sample_type));
544
545         return 0;
546 }
547
548 static int test__checkevent_pmu_events(struct evlist *evlist)
549 {
550         struct evsel *evsel = perf_evlist__first(evlist);
551
552         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
553         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
554         TEST_ASSERT_VAL("wrong exclude_user",
555                         !evsel->core.attr.exclude_user);
556         TEST_ASSERT_VAL("wrong exclude_kernel",
557                         evsel->core.attr.exclude_kernel);
558         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
559         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
560         TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
561
562         return 0;
563 }
564
565
566 static int test__checkevent_pmu_events_mix(struct evlist *evlist)
567 {
568         struct evsel *evsel = perf_evlist__first(evlist);
569
570         /* pmu-event:u */
571         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
572         TEST_ASSERT_VAL("wrong exclude_user",
573                         !evsel->core.attr.exclude_user);
574         TEST_ASSERT_VAL("wrong exclude_kernel",
575                         evsel->core.attr.exclude_kernel);
576         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
577         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
578         TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
579
580         /* cpu/pmu-event/u*/
581         evsel = perf_evsel__next(evsel);
582         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
583         TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
584         TEST_ASSERT_VAL("wrong exclude_user",
585                         !evsel->core.attr.exclude_user);
586         TEST_ASSERT_VAL("wrong exclude_kernel",
587                         evsel->core.attr.exclude_kernel);
588         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
589         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
590         TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
591
592         return 0;
593 }
594
595 static int test__checkterms_simple(struct list_head *terms)
596 {
597         struct parse_events_term *term;
598
599         /* config=10 */
600         term = list_entry(terms->next, struct parse_events_term, list);
601         TEST_ASSERT_VAL("wrong type term",
602                         term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG);
603         TEST_ASSERT_VAL("wrong type val",
604                         term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
605         TEST_ASSERT_VAL("wrong val", term->val.num == 10);
606         TEST_ASSERT_VAL("wrong config", !term->config);
607
608         /* config1 */
609         term = list_entry(term->list.next, struct parse_events_term, list);
610         TEST_ASSERT_VAL("wrong type term",
611                         term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG1);
612         TEST_ASSERT_VAL("wrong type val",
613                         term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
614         TEST_ASSERT_VAL("wrong val", term->val.num == 1);
615         TEST_ASSERT_VAL("wrong config", !term->config);
616
617         /* config2=3 */
618         term = list_entry(term->list.next, struct parse_events_term, list);
619         TEST_ASSERT_VAL("wrong type term",
620                         term->type_term == PARSE_EVENTS__TERM_TYPE_CONFIG2);
621         TEST_ASSERT_VAL("wrong type val",
622                         term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
623         TEST_ASSERT_VAL("wrong val", term->val.num == 3);
624         TEST_ASSERT_VAL("wrong config", !term->config);
625
626         /* umask=1*/
627         term = list_entry(term->list.next, struct parse_events_term, list);
628         TEST_ASSERT_VAL("wrong type term",
629                         term->type_term == PARSE_EVENTS__TERM_TYPE_USER);
630         TEST_ASSERT_VAL("wrong type val",
631                         term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
632         TEST_ASSERT_VAL("wrong val", term->val.num == 1);
633         TEST_ASSERT_VAL("wrong config", !strcmp(term->config, "umask"));
634
635         return 0;
636 }
637
638 static int test__group1(struct evlist *evlist)
639 {
640         struct evsel *evsel, *leader;
641
642         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
643         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->nr_groups);
644
645         /* instructions:k */
646         evsel = leader = perf_evlist__first(evlist);
647         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
648         TEST_ASSERT_VAL("wrong config",
649                         PERF_COUNT_HW_INSTRUCTIONS == evsel->core.attr.config);
650         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
651         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
652         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
653         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
654         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
655         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
656         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
657         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
658         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 0);
659         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
660
661         /* cycles:upp */
662         evsel = perf_evsel__next(evsel);
663         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
664         TEST_ASSERT_VAL("wrong config",
665                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
666         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
667         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
668         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
669         /* use of precise requires exclude_guest */
670         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
671         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
672         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 2);
673         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
674         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
675         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
676
677         return 0;
678 }
679
680 static int test__group2(struct evlist *evlist)
681 {
682         struct evsel *evsel, *leader;
683
684         TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->core.nr_entries);
685         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->nr_groups);
686
687         /* faults + :ku modifier */
688         evsel = leader = perf_evlist__first(evlist);
689         TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type);
690         TEST_ASSERT_VAL("wrong config",
691                         PERF_COUNT_SW_PAGE_FAULTS == evsel->core.attr.config);
692         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
693         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
694         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
695         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
696         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
697         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
698         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
699         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
700         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 0);
701         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
702
703         /* cache-references + :u modifier */
704         evsel = perf_evsel__next(evsel);
705         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
706         TEST_ASSERT_VAL("wrong config",
707                         PERF_COUNT_HW_CACHE_REFERENCES == evsel->core.attr.config);
708         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
709         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
710         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
711         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
712         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
713         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
714         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
715         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
716         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
717
718         /* cycles:k */
719         evsel = perf_evsel__next(evsel);
720         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
721         TEST_ASSERT_VAL("wrong config",
722                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
723         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
724         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
725         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
726         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
727         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
728         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
729         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
730         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
731
732         return 0;
733 }
734
735 static int test__group3(struct evlist *evlist __maybe_unused)
736 {
737         struct evsel *evsel, *leader;
738
739         TEST_ASSERT_VAL("wrong number of entries", 5 == evlist->core.nr_entries);
740         TEST_ASSERT_VAL("wrong number of groups", 2 == evlist->nr_groups);
741
742         /* group1 syscalls:sys_enter_openat:H */
743         evsel = leader = perf_evlist__first(evlist);
744         TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
745         TEST_ASSERT_VAL("wrong sample_type",
746                 PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
747         TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->core.attr.sample_period);
748         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
749         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
750         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
751         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
752         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
753         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
754         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
755         TEST_ASSERT_VAL("wrong group name",
756                 !strcmp(leader->group_name, "group1"));
757         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
758         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 0);
759         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
760
761         /* group1 cycles:kppp */
762         evsel = perf_evsel__next(evsel);
763         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
764         TEST_ASSERT_VAL("wrong config",
765                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
766         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
767         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
768         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
769         /* use of precise requires exclude_guest */
770         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
771         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
772         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 3);
773         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
774         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
775         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
776         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
777
778         /* group2 cycles + G modifier */
779         evsel = leader = perf_evsel__next(evsel);
780         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
781         TEST_ASSERT_VAL("wrong config",
782                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
783         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
784         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
785         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
786         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
787         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
788         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
789         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
790         TEST_ASSERT_VAL("wrong group name",
791                 !strcmp(leader->group_name, "group2"));
792         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
793         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 0);
794         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
795
796         /* group2 1:3 + G modifier */
797         evsel = perf_evsel__next(evsel);
798         TEST_ASSERT_VAL("wrong type", 1 == evsel->core.attr.type);
799         TEST_ASSERT_VAL("wrong config", 3 == evsel->core.attr.config);
800         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
801         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
802         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
803         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
804         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
805         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
806         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
807         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
808         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
809
810         /* instructions:u */
811         evsel = perf_evsel__next(evsel);
812         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
813         TEST_ASSERT_VAL("wrong config",
814                         PERF_COUNT_HW_INSTRUCTIONS == evsel->core.attr.config);
815         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
816         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
817         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
818         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
819         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
820         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
821         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
822         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
823
824         return 0;
825 }
826
827 static int test__group4(struct evlist *evlist __maybe_unused)
828 {
829         struct evsel *evsel, *leader;
830
831         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
832         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->nr_groups);
833
834         /* cycles:u + p */
835         evsel = leader = perf_evlist__first(evlist);
836         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
837         TEST_ASSERT_VAL("wrong config",
838                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
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         /* use of precise requires exclude_guest */
843         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
844         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
845         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 1);
846         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
847         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
848         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
849         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 0);
850         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
851
852         /* instructions:kp + p */
853         evsel = perf_evsel__next(evsel);
854         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
855         TEST_ASSERT_VAL("wrong config",
856                         PERF_COUNT_HW_INSTRUCTIONS == evsel->core.attr.config);
857         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
858         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
859         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
860         /* use of precise requires exclude_guest */
861         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
862         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
863         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip == 2);
864         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
865         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
866         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
867
868         return 0;
869 }
870
871 static int test__group5(struct evlist *evlist __maybe_unused)
872 {
873         struct evsel *evsel, *leader;
874
875         TEST_ASSERT_VAL("wrong number of entries", 5 == evlist->core.nr_entries);
876         TEST_ASSERT_VAL("wrong number of groups", 2 == evlist->nr_groups);
877
878         /* cycles + G */
879         evsel = leader = perf_evlist__first(evlist);
880         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
881         TEST_ASSERT_VAL("wrong config",
882                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
883         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
884         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
885         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
886         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
887         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
888         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
889         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
890         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
891         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
892         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 0);
893         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
894
895         /* instructions + G */
896         evsel = perf_evsel__next(evsel);
897         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
898         TEST_ASSERT_VAL("wrong config",
899                         PERF_COUNT_HW_INSTRUCTIONS == evsel->core.attr.config);
900         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
901         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
902         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
903         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
904         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
905         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
906         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
907         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
908         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
909
910         /* cycles:G */
911         evsel = leader = perf_evsel__next(evsel);
912         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
913         TEST_ASSERT_VAL("wrong config",
914                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
915         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
916         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
917         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
918         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
919         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
920         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
921         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
922         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
923         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
924         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 0);
925         TEST_ASSERT_VAL("wrong sample_read", !evsel->sample_read);
926
927         /* instructions:G */
928         evsel = perf_evsel__next(evsel);
929         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
930         TEST_ASSERT_VAL("wrong config",
931                         PERF_COUNT_HW_INSTRUCTIONS == evsel->core.attr.config);
932         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
933         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
934         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
935         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
936         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
937         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
938         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
939         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
940
941         /* cycles */
942         evsel = perf_evsel__next(evsel);
943         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
944         TEST_ASSERT_VAL("wrong config",
945                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
946         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
947         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
948         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
949         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
950         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
951         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
952         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
953
954         return 0;
955 }
956
957 static int test__group_gh1(struct evlist *evlist)
958 {
959         struct evsel *evsel, *leader;
960
961         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
962         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->nr_groups);
963
964         /* cycles + :H group modifier */
965         evsel = leader = perf_evlist__first(evlist);
966         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
967         TEST_ASSERT_VAL("wrong config",
968                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
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", perf_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", perf_evsel__group_idx(evsel) == 0);
979
980         /* cache-misses:G + :H group modifier */
981         evsel = perf_evsel__next(evsel);
982         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
983         TEST_ASSERT_VAL("wrong config",
984                         PERF_COUNT_HW_CACHE_MISSES == evsel->core.attr.config);
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->leader == leader);
992         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
993
994         return 0;
995 }
996
997 static int test__group_gh2(struct evlist *evlist)
998 {
999         struct evsel *evsel, *leader;
1000
1001         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
1002         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->nr_groups);
1003
1004         /* cycles + :G group modifier */
1005         evsel = leader = perf_evlist__first(evlist);
1006         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1007         TEST_ASSERT_VAL("wrong config",
1008                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
1009         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1010         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1011         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1012         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1013         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1014         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1015         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1016         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
1017         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1018         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 0);
1019
1020         /* cache-misses:H + :G group modifier */
1021         evsel = perf_evsel__next(evsel);
1022         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1023         TEST_ASSERT_VAL("wrong config",
1024                         PERF_COUNT_HW_CACHE_MISSES == evsel->core.attr.config);
1025         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1026         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1027         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1028         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1029         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1030         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1031         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
1032         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
1033
1034         return 0;
1035 }
1036
1037 static int test__group_gh3(struct evlist *evlist)
1038 {
1039         struct evsel *evsel, *leader;
1040
1041         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
1042         TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->nr_groups);
1043
1044         /* cycles:G + :u group modifier */
1045         evsel = leader = perf_evlist__first(evlist);
1046         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1047         TEST_ASSERT_VAL("wrong config",
1048                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
1049         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1050         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1051         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1052         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1053         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1054         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1055         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1056         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
1057         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1058         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 0);
1059
1060         /* cache-misses:H + :u group modifier */
1061         evsel = perf_evsel__next(evsel);
1062         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1063         TEST_ASSERT_VAL("wrong config",
1064                         PERF_COUNT_HW_CACHE_MISSES == evsel->core.attr.config);
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->leader == leader);
1072         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
1073
1074         return 0;
1075 }
1076
1077 static int test__group_gh4(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);
1083
1084         /* cycles:G + :uG group modifier */
1085         evsel = leader = perf_evlist__first(evlist);
1086         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1087         TEST_ASSERT_VAL("wrong config",
1088                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
1089         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1090         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1091         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1092         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1093         TEST_ASSERT_VAL("wrong exclude host", evsel->core.attr.exclude_host);
1094         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1095         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1096         TEST_ASSERT_VAL("wrong leader", perf_evsel__is_group_leader(evsel));
1097         TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1098         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 0);
1099
1100         /* cache-misses:H + :uG group modifier */
1101         evsel = perf_evsel__next(evsel);
1102         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1103         TEST_ASSERT_VAL("wrong config",
1104                         PERF_COUNT_HW_CACHE_MISSES == evsel->core.attr.config);
1105         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1106         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1107         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1108         TEST_ASSERT_VAL("wrong exclude guest", !evsel->core.attr.exclude_guest);
1109         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1110         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1111         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
1112         TEST_ASSERT_VAL("wrong group_idx", perf_evsel__group_idx(evsel) == 1);
1113
1114         return 0;
1115 }
1116
1117 static int test__leader_sample1(struct evlist *evlist)
1118 {
1119         struct evsel *evsel, *leader;
1120
1121         TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->core.nr_entries);
1122
1123         /* cycles - sampling group leader */
1124         evsel = leader = perf_evlist__first(evlist);
1125         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1126         TEST_ASSERT_VAL("wrong config",
1127                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
1128         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1129         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1130         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1131         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1132         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1133         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1134         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1135         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
1136         TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1137
1138         /* cache-misses - not sampling */
1139         evsel = perf_evsel__next(evsel);
1140         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1141         TEST_ASSERT_VAL("wrong config",
1142                         PERF_COUNT_HW_CACHE_MISSES == evsel->core.attr.config);
1143         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1144         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1145         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1146         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1147         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1148         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1149         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
1150         TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1151
1152         /* branch-misses - not sampling */
1153         evsel = perf_evsel__next(evsel);
1154         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1155         TEST_ASSERT_VAL("wrong config",
1156                         PERF_COUNT_HW_BRANCH_MISSES == evsel->core.attr.config);
1157         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1158         TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->core.attr.exclude_kernel);
1159         TEST_ASSERT_VAL("wrong exclude_hv", !evsel->core.attr.exclude_hv);
1160         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1161         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1162         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1163         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1164         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
1165         TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1166
1167         return 0;
1168 }
1169
1170 static int test__leader_sample2(struct evlist *evlist __maybe_unused)
1171 {
1172         struct evsel *evsel, *leader;
1173
1174         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
1175
1176         /* instructions - sampling group leader */
1177         evsel = leader = perf_evlist__first(evlist);
1178         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1179         TEST_ASSERT_VAL("wrong config",
1180                         PERF_COUNT_HW_INSTRUCTIONS == evsel->core.attr.config);
1181         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1182         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1183         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1184         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1185         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1186         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1187         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1188         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
1189         TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1190
1191         /* branch-misses - not sampling */
1192         evsel = perf_evsel__next(evsel);
1193         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1194         TEST_ASSERT_VAL("wrong config",
1195                         PERF_COUNT_HW_BRANCH_MISSES == evsel->core.attr.config);
1196         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1197         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1198         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1199         TEST_ASSERT_VAL("wrong exclude guest", evsel->core.attr.exclude_guest);
1200         TEST_ASSERT_VAL("wrong exclude host", !evsel->core.attr.exclude_host);
1201         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1202         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1203         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
1204         TEST_ASSERT_VAL("wrong sample_read", evsel->sample_read);
1205
1206         return 0;
1207 }
1208
1209 static int test__checkevent_pinned_modifier(struct evlist *evlist)
1210 {
1211         struct evsel *evsel = perf_evlist__first(evlist);
1212
1213         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1214         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1215         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1216         TEST_ASSERT_VAL("wrong precise_ip", evsel->core.attr.precise_ip);
1217         TEST_ASSERT_VAL("wrong pinned", evsel->core.attr.pinned);
1218
1219         return test__checkevent_symbolic_name(evlist);
1220 }
1221
1222 static int test__pinned_group(struct evlist *evlist)
1223 {
1224         struct evsel *evsel, *leader;
1225
1226         TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->core.nr_entries);
1227
1228         /* cycles - group leader */
1229         evsel = leader = perf_evlist__first(evlist);
1230         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1231         TEST_ASSERT_VAL("wrong config",
1232                         PERF_COUNT_HW_CPU_CYCLES == evsel->core.attr.config);
1233         TEST_ASSERT_VAL("wrong group name", !evsel->group_name);
1234         TEST_ASSERT_VAL("wrong leader", evsel->leader == leader);
1235         TEST_ASSERT_VAL("wrong pinned", evsel->core.attr.pinned);
1236
1237         /* cache-misses - can not be pinned, but will go on with the leader */
1238         evsel = perf_evsel__next(evsel);
1239         TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->core.attr.type);
1240         TEST_ASSERT_VAL("wrong config",
1241                         PERF_COUNT_HW_CACHE_MISSES == evsel->core.attr.config);
1242         TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
1243
1244         /* branch-misses - ditto */
1245         evsel = perf_evsel__next(evsel);
1246         TEST_ASSERT_VAL("wrong config",
1247                         PERF_COUNT_HW_BRANCH_MISSES == evsel->core.attr.config);
1248         TEST_ASSERT_VAL("wrong pinned", !evsel->core.attr.pinned);
1249
1250         return 0;
1251 }
1252
1253 static int test__checkevent_breakpoint_len(struct evlist *evlist)
1254 {
1255         struct evsel *evsel = perf_evlist__first(evlist);
1256
1257         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
1258         TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
1259         TEST_ASSERT_VAL("wrong config", 0 == evsel->core.attr.config);
1260         TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
1261                                          evsel->core.attr.bp_type);
1262         TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_1 ==
1263                                         evsel->core.attr.bp_len);
1264
1265         return 0;
1266 }
1267
1268 static int test__checkevent_breakpoint_len_w(struct evlist *evlist)
1269 {
1270         struct evsel *evsel = perf_evlist__first(evlist);
1271
1272         TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
1273         TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->core.attr.type);
1274         TEST_ASSERT_VAL("wrong config", 0 == evsel->core.attr.config);
1275         TEST_ASSERT_VAL("wrong bp_type", HW_BREAKPOINT_W ==
1276                                          evsel->core.attr.bp_type);
1277         TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_2 ==
1278                                         evsel->core.attr.bp_len);
1279
1280         return 0;
1281 }
1282
1283 static int
1284 test__checkevent_breakpoint_len_rw_modifier(struct evlist *evlist)
1285 {
1286         struct evsel *evsel = perf_evlist__first(evlist);
1287
1288         TEST_ASSERT_VAL("wrong exclude_user", !evsel->core.attr.exclude_user);
1289         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1290         TEST_ASSERT_VAL("wrong exclude_hv", evsel->core.attr.exclude_hv);
1291         TEST_ASSERT_VAL("wrong precise_ip", !evsel->core.attr.precise_ip);
1292
1293         return test__checkevent_breakpoint_rw(evlist);
1294 }
1295
1296 static int test__checkevent_precise_max_modifier(struct evlist *evlist)
1297 {
1298         struct evsel *evsel = perf_evlist__first(evlist);
1299
1300         TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
1301         TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type);
1302         TEST_ASSERT_VAL("wrong config",
1303                         PERF_COUNT_SW_TASK_CLOCK == evsel->core.attr.config);
1304         return 0;
1305 }
1306
1307 static int test__checkevent_config_symbol(struct evlist *evlist)
1308 {
1309         struct evsel *evsel = perf_evlist__first(evlist);
1310
1311         TEST_ASSERT_VAL("wrong name setting", strcmp(evsel->name, "insn") == 0);
1312         return 0;
1313 }
1314
1315 static int test__checkevent_config_raw(struct evlist *evlist)
1316 {
1317         struct evsel *evsel = perf_evlist__first(evlist);
1318
1319         TEST_ASSERT_VAL("wrong name setting", strcmp(evsel->name, "rawpmu") == 0);
1320         return 0;
1321 }
1322
1323 static int test__checkevent_config_num(struct evlist *evlist)
1324 {
1325         struct evsel *evsel = perf_evlist__first(evlist);
1326
1327         TEST_ASSERT_VAL("wrong name setting", strcmp(evsel->name, "numpmu") == 0);
1328         return 0;
1329 }
1330
1331 static int test__checkevent_config_cache(struct evlist *evlist)
1332 {
1333         struct evsel *evsel = perf_evlist__first(evlist);
1334
1335         TEST_ASSERT_VAL("wrong name setting", strcmp(evsel->name, "cachepmu") == 0);
1336         return 0;
1337 }
1338
1339 static bool test__intel_pt_valid(void)
1340 {
1341         return !!perf_pmu__find("intel_pt");
1342 }
1343
1344 static int test__intel_pt(struct evlist *evlist)
1345 {
1346         struct evsel *evsel = perf_evlist__first(evlist);
1347
1348         TEST_ASSERT_VAL("wrong name setting", strcmp(evsel->name, "intel_pt//u") == 0);
1349         return 0;
1350 }
1351
1352 static int test__checkevent_complex_name(struct evlist *evlist)
1353 {
1354         struct evsel *evsel = perf_evlist__first(evlist);
1355
1356         TEST_ASSERT_VAL("wrong complex name parsing", strcmp(evsel->name, "COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks") == 0);
1357         return 0;
1358 }
1359
1360 static int test__sym_event_slash(struct evlist *evlist)
1361 {
1362         struct evsel *evsel = perf_evlist__first(evlist);
1363
1364         TEST_ASSERT_VAL("wrong type", evsel->core.attr.type == PERF_TYPE_HARDWARE);
1365         TEST_ASSERT_VAL("wrong config", evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES);
1366         TEST_ASSERT_VAL("wrong exclude_kernel", evsel->core.attr.exclude_kernel);
1367         return 0;
1368 }
1369
1370 static int test__sym_event_dc(struct evlist *evlist)
1371 {
1372         struct evsel *evsel = perf_evlist__first(evlist);
1373
1374         TEST_ASSERT_VAL("wrong type", evsel->core.attr.type == PERF_TYPE_HARDWARE);
1375         TEST_ASSERT_VAL("wrong config", evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES);
1376         TEST_ASSERT_VAL("wrong exclude_user", evsel->core.attr.exclude_user);
1377         return 0;
1378 }
1379
1380 static int count_tracepoints(void)
1381 {
1382         struct dirent *events_ent;
1383         DIR *events_dir;
1384         int cnt = 0;
1385
1386         events_dir = tracing_events__opendir();
1387
1388         TEST_ASSERT_VAL("Can't open events dir", events_dir);
1389
1390         while ((events_ent = readdir(events_dir))) {
1391                 char *sys_path;
1392                 struct dirent *sys_ent;
1393                 DIR *sys_dir;
1394
1395                 if (!strcmp(events_ent->d_name, ".")
1396                     || !strcmp(events_ent->d_name, "..")
1397                     || !strcmp(events_ent->d_name, "enable")
1398                     || !strcmp(events_ent->d_name, "header_event")
1399                     || !strcmp(events_ent->d_name, "header_page"))
1400                         continue;
1401
1402                 sys_path = get_events_file(events_ent->d_name);
1403                 TEST_ASSERT_VAL("Can't get sys path", sys_path);
1404
1405                 sys_dir = opendir(sys_path);
1406                 TEST_ASSERT_VAL("Can't open sys dir", sys_dir);
1407
1408                 while ((sys_ent = readdir(sys_dir))) {
1409                         if (!strcmp(sys_ent->d_name, ".")
1410                             || !strcmp(sys_ent->d_name, "..")
1411                             || !strcmp(sys_ent->d_name, "enable")
1412                             || !strcmp(sys_ent->d_name, "filter"))
1413                                 continue;
1414
1415                         cnt++;
1416                 }
1417
1418                 closedir(sys_dir);
1419                 put_events_file(sys_path);
1420         }
1421
1422         closedir(events_dir);
1423         return cnt;
1424 }
1425
1426 static int test__all_tracepoints(struct evlist *evlist)
1427 {
1428         TEST_ASSERT_VAL("wrong events count",
1429                         count_tracepoints() == evlist->core.nr_entries);
1430
1431         return test__checkevent_tracepoint_multi(evlist);
1432 }
1433
1434 struct evlist_test {
1435         const char *name;
1436         __u32 type;
1437         const int id;
1438         bool (*valid)(void);
1439         int (*check)(struct evlist *evlist);
1440 };
1441
1442 static struct evlist_test test__events[] = {
1443         {
1444                 .name  = "syscalls:sys_enter_openat",
1445                 .check = test__checkevent_tracepoint,
1446                 .id    = 0,
1447         },
1448         {
1449                 .name  = "syscalls:*",
1450                 .check = test__checkevent_tracepoint_multi,
1451                 .id    = 1,
1452         },
1453         {
1454                 .name  = "r1a",
1455                 .check = test__checkevent_raw,
1456                 .id    = 2,
1457         },
1458         {
1459                 .name  = "1:1",
1460                 .check = test__checkevent_numeric,
1461                 .id    = 3,
1462         },
1463         {
1464                 .name  = "instructions",
1465                 .check = test__checkevent_symbolic_name,
1466                 .id    = 4,
1467         },
1468         {
1469                 .name  = "cycles/period=100000,config2/",
1470                 .check = test__checkevent_symbolic_name_config,
1471                 .id    = 5,
1472         },
1473         {
1474                 .name  = "faults",
1475                 .check = test__checkevent_symbolic_alias,
1476                 .id    = 6,
1477         },
1478         {
1479                 .name  = "L1-dcache-load-miss",
1480                 .check = test__checkevent_genhw,
1481                 .id    = 7,
1482         },
1483         {
1484                 .name  = "mem:0",
1485                 .check = test__checkevent_breakpoint,
1486                 .id    = 8,
1487         },
1488         {
1489                 .name  = "mem:0:x",
1490                 .check = test__checkevent_breakpoint_x,
1491                 .id    = 9,
1492         },
1493         {
1494                 .name  = "mem:0:r",
1495                 .check = test__checkevent_breakpoint_r,
1496                 .id    = 10,
1497         },
1498         {
1499                 .name  = "mem:0:w",
1500                 .check = test__checkevent_breakpoint_w,
1501                 .id    = 11,
1502         },
1503         {
1504                 .name  = "syscalls:sys_enter_openat:k",
1505                 .check = test__checkevent_tracepoint_modifier,
1506                 .id    = 12,
1507         },
1508         {
1509                 .name  = "syscalls:*:u",
1510                 .check = test__checkevent_tracepoint_multi_modifier,
1511                 .id    = 13,
1512         },
1513         {
1514                 .name  = "r1a:kp",
1515                 .check = test__checkevent_raw_modifier,
1516                 .id    = 14,
1517         },
1518         {
1519                 .name  = "1:1:hp",
1520                 .check = test__checkevent_numeric_modifier,
1521                 .id    = 15,
1522         },
1523         {
1524                 .name  = "instructions:h",
1525                 .check = test__checkevent_symbolic_name_modifier,
1526                 .id    = 16,
1527         },
1528         {
1529                 .name  = "faults:u",
1530                 .check = test__checkevent_symbolic_alias_modifier,
1531                 .id    = 17,
1532         },
1533         {
1534                 .name  = "L1-dcache-load-miss:kp",
1535                 .check = test__checkevent_genhw_modifier,
1536                 .id    = 18,
1537         },
1538         {
1539                 .name  = "mem:0:u",
1540                 .check = test__checkevent_breakpoint_modifier,
1541                 .id    = 19,
1542         },
1543         {
1544                 .name  = "mem:0:x:k",
1545                 .check = test__checkevent_breakpoint_x_modifier,
1546                 .id    = 20,
1547         },
1548         {
1549                 .name  = "mem:0:r:hp",
1550                 .check = test__checkevent_breakpoint_r_modifier,
1551                 .id    = 21,
1552         },
1553         {
1554                 .name  = "mem:0:w:up",
1555                 .check = test__checkevent_breakpoint_w_modifier,
1556                 .id    = 22,
1557         },
1558         {
1559                 .name  = "r1,syscalls:sys_enter_openat:k,1:1:hp",
1560                 .check = test__checkevent_list,
1561                 .id    = 23,
1562         },
1563         {
1564                 .name  = "instructions:G",
1565                 .check = test__checkevent_exclude_host_modifier,
1566                 .id    = 24,
1567         },
1568         {
1569                 .name  = "instructions:H",
1570                 .check = test__checkevent_exclude_guest_modifier,
1571                 .id    = 25,
1572         },
1573         {
1574                 .name  = "mem:0:rw",
1575                 .check = test__checkevent_breakpoint_rw,
1576                 .id    = 26,
1577         },
1578         {
1579                 .name  = "mem:0:rw:kp",
1580                 .check = test__checkevent_breakpoint_rw_modifier,
1581                 .id    = 27,
1582         },
1583         {
1584                 .name  = "{instructions:k,cycles:upp}",
1585                 .check = test__group1,
1586                 .id    = 28,
1587         },
1588         {
1589                 .name  = "{faults:k,cache-references}:u,cycles:k",
1590                 .check = test__group2,
1591                 .id    = 29,
1592         },
1593         {
1594                 .name  = "group1{syscalls:sys_enter_openat:H,cycles:kppp},group2{cycles,1:3}:G,instructions:u",
1595                 .check = test__group3,
1596                 .id    = 30,
1597         },
1598         {
1599                 .name  = "{cycles:u,instructions:kp}:p",
1600                 .check = test__group4,
1601                 .id    = 31,
1602         },
1603         {
1604                 .name  = "{cycles,instructions}:G,{cycles:G,instructions:G},cycles",
1605                 .check = test__group5,
1606                 .id    = 32,
1607         },
1608         {
1609                 .name  = "*:*",
1610                 .check = test__all_tracepoints,
1611                 .id    = 33,
1612         },
1613         {
1614                 .name  = "{cycles,cache-misses:G}:H",
1615                 .check = test__group_gh1,
1616                 .id    = 34,
1617         },
1618         {
1619                 .name  = "{cycles,cache-misses:H}:G",
1620                 .check = test__group_gh2,
1621                 .id    = 35,
1622         },
1623         {
1624                 .name  = "{cycles:G,cache-misses:H}:u",
1625                 .check = test__group_gh3,
1626                 .id    = 36,
1627         },
1628         {
1629                 .name  = "{cycles:G,cache-misses:H}:uG",
1630                 .check = test__group_gh4,
1631                 .id    = 37,
1632         },
1633         {
1634                 .name  = "{cycles,cache-misses,branch-misses}:S",
1635                 .check = test__leader_sample1,
1636                 .id    = 38,
1637         },
1638         {
1639                 .name  = "{instructions,branch-misses}:Su",
1640                 .check = test__leader_sample2,
1641                 .id    = 39,
1642         },
1643         {
1644                 .name  = "instructions:uDp",
1645                 .check = test__checkevent_pinned_modifier,
1646                 .id    = 40,
1647         },
1648         {
1649                 .name  = "{cycles,cache-misses,branch-misses}:D",
1650                 .check = test__pinned_group,
1651                 .id    = 41,
1652         },
1653         {
1654                 .name  = "mem:0/1",
1655                 .check = test__checkevent_breakpoint_len,
1656                 .id    = 42,
1657         },
1658         {
1659                 .name  = "mem:0/2:w",
1660                 .check = test__checkevent_breakpoint_len_w,
1661                 .id    = 43,
1662         },
1663         {
1664                 .name  = "mem:0/4:rw:u",
1665                 .check = test__checkevent_breakpoint_len_rw_modifier,
1666                 .id    = 44
1667         },
1668 #if defined(__s390x__)
1669         {
1670                 .name  = "kvm-s390:kvm_s390_create_vm",
1671                 .check = test__checkevent_tracepoint,
1672                 .valid = kvm_s390_create_vm_valid,
1673                 .id    = 100,
1674         },
1675 #endif
1676         {
1677                 .name  = "instructions:I",
1678                 .check = test__checkevent_exclude_idle_modifier,
1679                 .id    = 45,
1680         },
1681         {
1682                 .name  = "instructions:kIG",
1683                 .check = test__checkevent_exclude_idle_modifier_1,
1684                 .id    = 46,
1685         },
1686         {
1687                 .name  = "task-clock:P,cycles",
1688                 .check = test__checkevent_precise_max_modifier,
1689                 .id    = 47,
1690         },
1691         {
1692                 .name  = "instructions/name=insn/",
1693                 .check = test__checkevent_config_symbol,
1694                 .id    = 48,
1695         },
1696         {
1697                 .name  = "r1234/name=rawpmu/",
1698                 .check = test__checkevent_config_raw,
1699                 .id    = 49,
1700         },
1701         {
1702                 .name  = "4:0x6530160/name=numpmu/",
1703                 .check = test__checkevent_config_num,
1704                 .id    = 50,
1705         },
1706         {
1707                 .name  = "L1-dcache-misses/name=cachepmu/",
1708                 .check = test__checkevent_config_cache,
1709                 .id    = 51,
1710         },
1711         {
1712                 .name  = "intel_pt//u",
1713                 .valid = test__intel_pt_valid,
1714                 .check = test__intel_pt,
1715                 .id    = 52,
1716         },
1717         {
1718                 .name  = "cycles/name='COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks'/Duk",
1719                 .check = test__checkevent_complex_name,
1720                 .id    = 53
1721         },
1722         {
1723                 .name  = "cycles//u",
1724                 .check = test__sym_event_slash,
1725                 .id    = 54,
1726         },
1727         {
1728                 .name  = "cycles:k",
1729                 .check = test__sym_event_dc,
1730                 .id    = 55,
1731         }
1732 };
1733
1734 static struct evlist_test test__events_pmu[] = {
1735         {
1736                 .name  = "cpu/config=10,config1,config2=3,period=1000/u",
1737                 .check = test__checkevent_pmu,
1738                 .id    = 0,
1739         },
1740         {
1741                 .name  = "cpu/config=1,name=krava/u,cpu/config=2/u",
1742                 .check = test__checkevent_pmu_name,
1743                 .id    = 1,
1744         },
1745         {
1746                 .name  = "cpu/config=1,call-graph=fp,time,period=100000/,cpu/config=2,call-graph=no,time=0,period=2000/",
1747                 .check = test__checkevent_pmu_partial_time_callgraph,
1748                 .id    = 2,
1749         },
1750         {
1751                 .name  = "cpu/name='COMPLEX_CYCLES_NAME:orig=cycles,desc=chip-clock-ticks',period=0x1,event=0x2/ukp",
1752                 .check = test__checkevent_complex_name,
1753                 .id    = 3,
1754         }
1755 };
1756
1757 struct terms_test {
1758         const char *str;
1759         __u32 type;
1760         int (*check)(struct list_head *terms);
1761 };
1762
1763 static struct terms_test test__terms[] = {
1764         [0] = {
1765                 .str   = "config=10,config1,config2=3,umask=1",
1766                 .check = test__checkterms_simple,
1767         },
1768 };
1769
1770 static int test_event(struct evlist_test *e)
1771 {
1772         struct parse_events_error err = { .idx = 0, };
1773         struct evlist *evlist;
1774         int ret;
1775
1776         if (e->valid && !e->valid()) {
1777                 pr_debug("... SKIP");
1778                 return 0;
1779         }
1780
1781         evlist = evlist__new();
1782         if (evlist == NULL)
1783                 return -ENOMEM;
1784
1785         ret = parse_events(evlist, e->name, &err);
1786         if (ret) {
1787                 pr_debug("failed to parse event '%s', err %d, str '%s'\n",
1788                          e->name, ret, err.str);
1789                 parse_events_print_error(&err, e->name);
1790         } else {
1791                 ret = e->check(evlist);
1792         }
1793
1794         evlist__delete(evlist);
1795
1796         return ret;
1797 }
1798
1799 static int test_events(struct evlist_test *events, unsigned cnt)
1800 {
1801         int ret1, ret2 = 0;
1802         unsigned i;
1803
1804         for (i = 0; i < cnt; i++) {
1805                 struct evlist_test *e = &events[i];
1806
1807                 pr_debug("running test %d '%s'", e->id, e->name);
1808                 ret1 = test_event(e);
1809                 if (ret1)
1810                         ret2 = ret1;
1811                 pr_debug("\n");
1812         }
1813
1814         return ret2;
1815 }
1816
1817 static int test_term(struct terms_test *t)
1818 {
1819         struct list_head terms;
1820         int ret;
1821
1822         INIT_LIST_HEAD(&terms);
1823
1824         ret = parse_events_terms(&terms, t->str);
1825         if (ret) {
1826                 pr_debug("failed to parse terms '%s', err %d\n",
1827                          t->str , ret);
1828                 return ret;
1829         }
1830
1831         ret = t->check(&terms);
1832         parse_events_terms__purge(&terms);
1833
1834         return ret;
1835 }
1836
1837 static int test_terms(struct terms_test *terms, unsigned cnt)
1838 {
1839         int ret = 0;
1840         unsigned i;
1841
1842         for (i = 0; i < cnt; i++) {
1843                 struct terms_test *t = &terms[i];
1844
1845                 pr_debug("running test %d '%s'\n", i, t->str);
1846                 ret = test_term(t);
1847                 if (ret)
1848                         break;
1849         }
1850
1851         return ret;
1852 }
1853
1854 static int test_pmu(void)
1855 {
1856         struct stat st;
1857         char path[PATH_MAX];
1858         int ret;
1859
1860         snprintf(path, PATH_MAX, "%s/bus/event_source/devices/cpu/format/",
1861                  sysfs__mountpoint());
1862
1863         ret = stat(path, &st);
1864         if (ret)
1865                 pr_debug("omitting PMU cpu tests\n");
1866         return !ret;
1867 }
1868
1869 static int test_pmu_events(void)
1870 {
1871         struct stat st;
1872         char path[PATH_MAX];
1873         struct dirent *ent;
1874         DIR *dir;
1875         int ret;
1876
1877         snprintf(path, PATH_MAX, "%s/bus/event_source/devices/cpu/events/",
1878                  sysfs__mountpoint());
1879
1880         ret = stat(path, &st);
1881         if (ret) {
1882                 pr_debug("omitting PMU cpu events tests\n");
1883                 return 0;
1884         }
1885
1886         dir = opendir(path);
1887         if (!dir) {
1888                 pr_debug("can't open pmu event dir");
1889                 return -1;
1890         }
1891
1892         while (!ret && (ent = readdir(dir))) {
1893                 struct evlist_test e = { .id = 0, };
1894                 char name[2 * NAME_MAX + 1 + 12 + 3];
1895
1896                 /* Names containing . are special and cannot be used directly */
1897                 if (strchr(ent->d_name, '.'))
1898                         continue;
1899
1900                 snprintf(name, sizeof(name), "cpu/event=%s/u", ent->d_name);
1901
1902                 e.name  = name;
1903                 e.check = test__checkevent_pmu_events;
1904
1905                 ret = test_event(&e);
1906                 if (ret)
1907                         break;
1908                 snprintf(name, sizeof(name), "%s:u,cpu/event=%s/u", ent->d_name, ent->d_name);
1909                 e.name  = name;
1910                 e.check = test__checkevent_pmu_events_mix;
1911                 ret = test_event(&e);
1912         }
1913
1914         closedir(dir);
1915         return ret;
1916 }
1917
1918 int test__parse_events(struct test *test __maybe_unused, int subtest __maybe_unused)
1919 {
1920         int ret1, ret2 = 0;
1921
1922 #define TEST_EVENTS(tests)                              \
1923 do {                                                    \
1924         ret1 = test_events(tests, ARRAY_SIZE(tests));   \
1925         if (!ret2)                                      \
1926                 ret2 = ret1;                            \
1927 } while (0)
1928
1929         TEST_EVENTS(test__events);
1930
1931         if (test_pmu())
1932                 TEST_EVENTS(test__events_pmu);
1933
1934         if (test_pmu()) {
1935                 int ret = test_pmu_events();
1936                 if (ret)
1937                         return ret;
1938         }
1939
1940         ret1 = test_terms(test__terms, ARRAY_SIZE(test__terms));
1941         if (!ret2)
1942                 ret2 = ret1;
1943
1944         return ret2;
1945 }