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