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