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