xref: /linux/tools/perf/tests/parse-events.c (revision 9e906a9dead17d81d6c2687f65e159231d0e3286)
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 
check_evlist(const char * test,int line,bool cond,struct evlist * evlist)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 
check_evsel(const char * test,int line,bool cond,struct evsel * evsel)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 
num_core_entries(struct evlist * evlist)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 
test_hw_config(const struct evsel * evsel,__u64 expected_config)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  */
kvm_s390_create_vm_valid(void)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 
test__checkevent_tracepoint(struct evlist * evlist)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 
test__checkevent_tracepoint_multi(struct evlist * evlist)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 
test__checkevent_raw(struct evlist * evlist)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 
test__checkevent_numeric(struct evlist * evlist)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 
test__checkevent_symbolic_name(struct evlist * evlist)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 
test__checkevent_symbolic_name_config(struct evlist * evlist)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 
test__checkevent_symbolic_alias(struct evlist * evlist)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 
test__checkevent_genhw(struct evlist * evlist)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 
test__checkevent_breakpoint(struct evlist * evlist)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 
test__checkevent_breakpoint_x(struct evlist * evlist)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 
test__checkevent_breakpoint_r(struct evlist * evlist)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 
test__checkevent_breakpoint_w(struct evlist * evlist)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 
test__checkevent_breakpoint_rw(struct evlist * evlist)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 
test__checkevent_tracepoint_modifier(struct evlist * evlist)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
test__checkevent_tracepoint_multi_modifier(struct evlist * evlist)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 
test__checkevent_raw_modifier(struct evlist * evlist)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 
test__checkevent_numeric_modifier(struct evlist * evlist)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 
test__checkevent_symbolic_name_modifier(struct evlist * evlist)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 
test__checkevent_exclude_host_modifier(struct evlist * evlist)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 
test__checkevent_exclude_guest_modifier(struct evlist * evlist)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 
test__checkevent_symbolic_alias_modifier(struct evlist * evlist)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 
test__checkevent_genhw_modifier(struct evlist * evlist)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 
test__checkevent_exclude_idle_modifier(struct evlist * evlist)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 
test__checkevent_exclude_idle_modifier_1(struct evlist * evlist)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 
test__checkevent_breakpoint_modifier(struct evlist * evlist)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 
test__checkevent_breakpoint_x_modifier(struct evlist * evlist)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 
test__checkevent_breakpoint_r_modifier(struct evlist * evlist)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 
test__checkevent_breakpoint_w_modifier(struct evlist * evlist)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 
test__checkevent_breakpoint_rw_modifier(struct evlist * evlist)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 
test__checkevent_breakpoint_modifier_name(struct evlist * evlist)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 
test__checkevent_breakpoint_x_modifier_name(struct evlist * evlist)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 
test__checkevent_breakpoint_r_modifier_name(struct evlist * evlist)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 
test__checkevent_breakpoint_w_modifier_name(struct evlist * evlist)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 
test__checkevent_breakpoint_rw_modifier_name(struct evlist * evlist)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 
test__checkevent_breakpoint_2_events(struct evlist * evlist)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 
test__checkevent_pmu(struct evlist * evlist)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 
test__checkevent_list(struct evlist * evlist)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 
test__checkevent_pmu_name(struct evlist * evlist)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 
test__checkevent_pmu_partial_time_callgraph(struct evlist * evlist)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 
test__checkevent_pmu_events(struct evlist * evlist)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 
test__checkevent_pmu_events_mix(struct evlist * evlist)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 
test__checkterms_simple(struct parse_events_terms * terms)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 
test__group1(struct evlist * evlist)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 
test__group2(struct evlist * evlist)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 
test__group3(struct evlist * evlist __maybe_unused)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 
test__group4(struct evlist * evlist __maybe_unused)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 
test__group5(struct evlist * evlist __maybe_unused)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 
test__group_gh1(struct evlist * evlist)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 
test__group_gh2(struct evlist * evlist)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 
test__group_gh3(struct evlist * evlist)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 
test__group_gh4(struct evlist * evlist)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 
test__leader_sample1(struct evlist * evlist)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 
test__leader_sample2(struct evlist * evlist __maybe_unused)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 
test__checkevent_pinned_modifier(struct evlist * evlist)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 
test__pinned_group(struct evlist * evlist)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 
test__checkevent_exclusive_modifier(struct evlist * evlist)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 
test__exclusive_group(struct evlist * evlist)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 }
test__checkevent_breakpoint_len(struct evlist * evlist)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 
test__checkevent_breakpoint_len_w(struct evlist * evlist)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
test__checkevent_breakpoint_len_rw_modifier(struct evlist * evlist)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 
test__checkevent_precise_max_modifier(struct evlist * evlist)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 
test__checkevent_config_symbol(struct evlist * evlist)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 
test__checkevent_config_raw(struct evlist * evlist)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 
test__checkevent_config_num(struct evlist * evlist)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 
test__checkevent_config_cache(struct evlist * evlist)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 
test__pmu_default_core_event_valid(void)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 
test__intel_pt_valid(void)1771 static bool test__intel_pt_valid(void)
1772 {
1773 	return !!perf_pmus__find("intel_pt");
1774 }
1775 
test__intel_pt(struct evlist * evlist)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 
test__acr_valid(void)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 
test__ratio_to_prev(struct evlist * evlist)1797 static int test__ratio_to_prev(struct evlist *evlist)
1798 {
1799 	struct evsel *evsel;
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 (!perf_pmu__has_format(evsel->pmu, "acr_mask"))
1805 			return TEST_OK;
1806 
1807 		if (evsel == evlist__first(evlist)) {
1808 			TEST_ASSERT_VAL("wrong config2", 0 == evsel->core.attr.config2);
1809 			TEST_ASSERT_VAL("wrong leader", evsel__is_group_leader(evsel));
1810 			TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 2);
1811 			TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 0);
1812 			TEST_ASSERT_EVSEL("unexpected event",
1813 					evsel__match(evsel, HARDWARE, HW_CPU_CYCLES),
1814 					evsel);
1815 		} else {
1816 			TEST_ASSERT_VAL("wrong config2", 0 == evsel->core.attr.config2);
1817 			TEST_ASSERT_VAL("wrong leader", !evsel__is_group_leader(evsel));
1818 			TEST_ASSERT_VAL("wrong core.nr_members", evsel->core.nr_members == 0);
1819 			TEST_ASSERT_VAL("wrong group_idx", evsel__group_idx(evsel) == 1);
1820 			TEST_ASSERT_EVSEL("unexpected event",
1821 					evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS),
1822 					evsel);
1823 		}
1824 		/*
1825 		 * The period value gets configured within evlist__config,
1826 		 * while this test executes only parse events method.
1827 		 */
1828 		TEST_ASSERT_VAL("wrong period", 0 == evsel->core.attr.sample_period);
1829 	}
1830 	return TEST_OK;
1831 }
1832 
test__checkevent_complex_name(struct evlist * evlist)1833 static int test__checkevent_complex_name(struct evlist *evlist)
1834 {
1835 	struct evsel *evsel = evlist__first(evlist);
1836 
1837 	TEST_ASSERT_EVLIST("wrong number of entries",
1838 			   evlist->core.nr_entries == num_core_entries(evlist),
1839 			   evlist);
1840 	TEST_ASSERT_EVSEL("wrong complex name parsing",
1841 			  evsel__name_is(evsel,
1842 				  "COMPLEX_CYCLES_NAME:orig=cpu-cycles,desc=chip-clock-ticks"),
1843 			  evsel);
1844 	return TEST_OK;
1845 }
1846 
test__checkevent_raw_pmu(struct evlist * evlist)1847 static int test__checkevent_raw_pmu(struct evlist *evlist)
1848 {
1849 	struct evsel *evsel = evlist__first(evlist);
1850 
1851 	TEST_ASSERT_EVLIST("wrong number of entries", 1 == evlist->core.nr_entries, evlist);
1852 	TEST_ASSERT_EVSEL("wrong type", PERF_TYPE_SOFTWARE == evsel->core.attr.type, evsel);
1853 	TEST_ASSERT_EVSEL("wrong config", 0x1a == evsel->core.attr.config, evsel);
1854 	return TEST_OK;
1855 }
1856 
test__sym_event_slash(struct evlist * evlist)1857 static int test__sym_event_slash(struct evlist *evlist)
1858 {
1859 	struct evsel *evsel = evlist__first(evlist);
1860 
1861 	TEST_ASSERT_EVLIST("wrong number of entries",
1862 			   evlist->core.nr_entries == num_core_entries(evlist),
1863 			   evlist);
1864 	TEST_ASSERT_EVSEL("unexpected event", evsel__match(evsel, HARDWARE, HW_CPU_CYCLES), evsel);
1865 	TEST_ASSERT_EVSEL("wrong exclude_kernel", evsel->core.attr.exclude_kernel, evsel);
1866 	return TEST_OK;
1867 }
1868 
test__sym_event_dc(struct evlist * evlist)1869 static int test__sym_event_dc(struct evlist *evlist)
1870 {
1871 	struct evsel *evsel = evlist__first(evlist);
1872 
1873 	TEST_ASSERT_EVLIST("wrong number of entries",
1874 			   evlist->core.nr_entries == num_core_entries(evlist),
1875 			   evlist);
1876 	TEST_ASSERT_EVSEL("unexpected event", evsel__match(evsel, HARDWARE, HW_CPU_CYCLES), evsel);
1877 	TEST_ASSERT_EVSEL("wrong exclude_user", evsel->core.attr.exclude_user, evsel);
1878 	return TEST_OK;
1879 }
1880 
test__term_equal_term(struct evlist * evlist)1881 static int test__term_equal_term(struct evlist *evlist)
1882 {
1883 	struct evsel *evsel = evlist__first(evlist);
1884 
1885 	TEST_ASSERT_EVLIST("wrong number of entries",
1886 			   evlist->core.nr_entries == num_core_entries(evlist),
1887 			   evlist);
1888 	TEST_ASSERT_EVSEL("unexpected event", evsel__match(evsel, HARDWARE, HW_CPU_CYCLES), evsel);
1889 	TEST_ASSERT_EVSEL("wrong name setting", strcmp(evsel->name, "name") == 0, evsel);
1890 	return TEST_OK;
1891 }
1892 
test__term_equal_legacy(struct evlist * evlist)1893 static int test__term_equal_legacy(struct evlist *evlist)
1894 {
1895 	struct evsel *evsel = evlist__first(evlist);
1896 
1897 	TEST_ASSERT_EVLIST("wrong number of entries",
1898 			   evlist->core.nr_entries == num_core_entries(evlist),
1899 			   evlist);
1900 	TEST_ASSERT_EVSEL("unexpected event", evsel__match(evsel, HARDWARE, HW_CPU_CYCLES), evsel);
1901 	TEST_ASSERT_EVSEL("wrong name setting", strcmp(evsel->name, "l1d") == 0, evsel);
1902 	return TEST_OK;
1903 }
1904 
count_tracepoints(void)1905 static int count_tracepoints(void)
1906 {
1907 	struct dirent *events_ent;
1908 	DIR *events_dir;
1909 	int cnt = 0;
1910 
1911 	events_dir = tracing_events__opendir();
1912 
1913 	TEST_ASSERT_VAL("Can't open events dir", events_dir);
1914 
1915 	while ((events_ent = readdir(events_dir))) {
1916 		char *sys_path;
1917 		struct dirent *sys_ent;
1918 		DIR *sys_dir;
1919 
1920 		if (!strcmp(events_ent->d_name, ".")
1921 		    || !strcmp(events_ent->d_name, "..")
1922 		    || !strcmp(events_ent->d_name, "enable")
1923 		    || !strcmp(events_ent->d_name, "header_event")
1924 		    || !strcmp(events_ent->d_name, "header_page"))
1925 			continue;
1926 
1927 		sys_path = get_events_file(events_ent->d_name);
1928 		TEST_ASSERT_VAL("Can't get sys path", sys_path);
1929 
1930 		sys_dir = opendir(sys_path);
1931 		TEST_ASSERT_VAL("Can't open sys dir", sys_dir);
1932 
1933 		while ((sys_ent = readdir(sys_dir))) {
1934 			if (!strcmp(sys_ent->d_name, ".")
1935 			    || !strcmp(sys_ent->d_name, "..")
1936 			    || !strcmp(sys_ent->d_name, "enable")
1937 			    || !strcmp(sys_ent->d_name, "filter"))
1938 				continue;
1939 
1940 			cnt++;
1941 		}
1942 
1943 		closedir(sys_dir);
1944 		put_events_file(sys_path);
1945 	}
1946 
1947 	closedir(events_dir);
1948 	return cnt;
1949 }
1950 
test__all_tracepoints(struct evlist * evlist)1951 static int test__all_tracepoints(struct evlist *evlist)
1952 {
1953 	TEST_ASSERT_VAL("wrong events count",
1954 			count_tracepoints() == evlist->core.nr_entries);
1955 
1956 	return test__checkevent_tracepoint_multi(evlist);
1957 }
1958 
1959 struct evlist_test {
1960 	const char *name;
1961 	bool (*valid)(void);
1962 	int (*check)(struct evlist *evlist);
1963 };
1964 
1965 static const struct evlist_test test__events[] = {
1966 	{
1967 		.name  = "syscalls:sys_enter_openat",
1968 		.check = test__checkevent_tracepoint,
1969 		/* 0 */
1970 	},
1971 	{
1972 		.name  = "syscalls:*",
1973 		.check = test__checkevent_tracepoint_multi,
1974 		/* 1 */
1975 	},
1976 	{
1977 		.name  = "r1a",
1978 		.check = test__checkevent_raw,
1979 		/* 2 */
1980 	},
1981 	{
1982 		.name  = "1:1",
1983 		.check = test__checkevent_numeric,
1984 		/* 3 */
1985 	},
1986 	{
1987 		.name  = "instructions",
1988 		.check = test__checkevent_symbolic_name,
1989 		/* 4 */
1990 	},
1991 	{
1992 		.name  = "cpu-cycles/period=100000,config2/",
1993 		.check = test__checkevent_symbolic_name_config,
1994 		/* 5 */
1995 	},
1996 	{
1997 		.name  = "faults",
1998 		.check = test__checkevent_symbolic_alias,
1999 		/* 6 */
2000 	},
2001 	{
2002 		.name  = "L1-dcache-load-miss",
2003 		.check = test__checkevent_genhw,
2004 		/* 7 */
2005 	},
2006 	{
2007 		.name  = "mem:0",
2008 		.check = test__checkevent_breakpoint,
2009 		/* 8 */
2010 	},
2011 	{
2012 		.name  = "mem:0:x",
2013 		.check = test__checkevent_breakpoint_x,
2014 		/* 9 */
2015 	},
2016 	{
2017 		.name  = "mem:0:r",
2018 		.check = test__checkevent_breakpoint_r,
2019 		/* 0 */
2020 	},
2021 	{
2022 		.name  = "mem:0:w",
2023 		.check = test__checkevent_breakpoint_w,
2024 		/* 1 */
2025 	},
2026 	{
2027 		.name  = "syscalls:sys_enter_openat:k",
2028 		.check = test__checkevent_tracepoint_modifier,
2029 		/* 2 */
2030 	},
2031 	{
2032 		.name  = "syscalls:*:u",
2033 		.check = test__checkevent_tracepoint_multi_modifier,
2034 		/* 3 */
2035 	},
2036 	{
2037 		.name  = "r1a:kp",
2038 		.check = test__checkevent_raw_modifier,
2039 		/* 4 */
2040 	},
2041 	{
2042 		.name  = "1:1:hp",
2043 		.check = test__checkevent_numeric_modifier,
2044 		/* 5 */
2045 	},
2046 	{
2047 		.name  = "instructions:h",
2048 		.check = test__checkevent_symbolic_name_modifier,
2049 		/* 6 */
2050 	},
2051 	{
2052 		.name  = "faults:u",
2053 		.check = test__checkevent_symbolic_alias_modifier,
2054 		/* 7 */
2055 	},
2056 	{
2057 		.name  = "L1-dcache-load-miss:kp",
2058 		.check = test__checkevent_genhw_modifier,
2059 		/* 8 */
2060 	},
2061 	{
2062 		.name  = "mem:0:u",
2063 		.check = test__checkevent_breakpoint_modifier,
2064 		/* 9 */
2065 	},
2066 	{
2067 		.name  = "mem:0:x:k",
2068 		.check = test__checkevent_breakpoint_x_modifier,
2069 		/* 0 */
2070 	},
2071 	{
2072 		.name  = "mem:0:r:hp",
2073 		.check = test__checkevent_breakpoint_r_modifier,
2074 		/* 1 */
2075 	},
2076 	{
2077 		.name  = "mem:0:w:up",
2078 		.check = test__checkevent_breakpoint_w_modifier,
2079 		/* 2 */
2080 	},
2081 	{
2082 		.name  = "r1,syscalls:sys_enter_openat:k,1:1:hp",
2083 		.check = test__checkevent_list,
2084 		/* 3 */
2085 	},
2086 	{
2087 		.name  = "instructions:G",
2088 		.check = test__checkevent_exclude_host_modifier,
2089 		/* 4 */
2090 	},
2091 	{
2092 		.name  = "instructions:H",
2093 		.check = test__checkevent_exclude_guest_modifier,
2094 		/* 5 */
2095 	},
2096 	{
2097 		.name  = "mem:0:rw",
2098 		.check = test__checkevent_breakpoint_rw,
2099 		/* 6 */
2100 	},
2101 	{
2102 		.name  = "mem:0:rw:kp",
2103 		.check = test__checkevent_breakpoint_rw_modifier,
2104 		/* 7 */
2105 	},
2106 	{
2107 		.name  = "{instructions:k,cpu-cycles:upp}",
2108 		.check = test__group1,
2109 		/* 8 */
2110 	},
2111 	{
2112 		.name  = "{faults:k,branches}:u,cpu-cycles:k",
2113 		.check = test__group2,
2114 		/* 9 */
2115 	},
2116 	{
2117 		.name  = "group1{syscalls:sys_enter_openat:H,cpu-cycles:kppp},group2{cpu-cycles,1:3}:G,instructions:u",
2118 		.check = test__group3,
2119 		/* 0 */
2120 	},
2121 	{
2122 		.name  = "{cpu-cycles:u,instructions:kp}:p",
2123 		.check = test__group4,
2124 		/* 1 */
2125 	},
2126 	{
2127 		.name  = "{cpu-cycles,instructions}:G,{cpu-cycles:G,instructions:G},cpu-cycles",
2128 		.check = test__group5,
2129 		/* 2 */
2130 	},
2131 	{
2132 		.name  = "*:*",
2133 		.check = test__all_tracepoints,
2134 		/* 3 */
2135 	},
2136 	{
2137 		.name  = "{cpu-cycles,cache-misses:G}:H",
2138 		.check = test__group_gh1,
2139 		/* 4 */
2140 	},
2141 	{
2142 		.name  = "{cpu-cycles,cache-misses:H}:G",
2143 		.check = test__group_gh2,
2144 		/* 5 */
2145 	},
2146 	{
2147 		.name  = "{cpu-cycles:G,cache-misses:H}:u",
2148 		.check = test__group_gh3,
2149 		/* 6 */
2150 	},
2151 	{
2152 		.name  = "{cpu-cycles:G,cache-misses:H}:uG",
2153 		.check = test__group_gh4,
2154 		/* 7 */
2155 	},
2156 	{
2157 		.name  = "{cpu-cycles,cache-misses,branch-misses}:S",
2158 		.check = test__leader_sample1,
2159 		/* 8 */
2160 	},
2161 	{
2162 		.name  = "{instructions,branch-misses}:Su",
2163 		.check = test__leader_sample2,
2164 		/* 9 */
2165 	},
2166 	{
2167 		.name  = "instructions:uDp",
2168 		.check = test__checkevent_pinned_modifier,
2169 		/* 0 */
2170 	},
2171 	{
2172 		.name  = "{cpu-cycles,cache-misses,branch-misses}:D",
2173 		.check = test__pinned_group,
2174 		/* 1 */
2175 	},
2176 	{
2177 		.name  = "mem:0/1",
2178 		.check = test__checkevent_breakpoint_len,
2179 		/* 2 */
2180 	},
2181 	{
2182 		.name  = "mem:0/2:w",
2183 		.check = test__checkevent_breakpoint_len_w,
2184 		/* 3 */
2185 	},
2186 	{
2187 		.name  = "mem:0/4:rw:u",
2188 		.check = test__checkevent_breakpoint_len_rw_modifier,
2189 		/* 4 */
2190 	},
2191 #if defined(__s390x__)
2192 	{
2193 		.name  = "kvm-s390:kvm_s390_create_vm",
2194 		.check = test__checkevent_tracepoint,
2195 		.valid = kvm_s390_create_vm_valid,
2196 		/* 0 */
2197 	},
2198 #endif
2199 	{
2200 		.name  = "instructions:I",
2201 		.check = test__checkevent_exclude_idle_modifier,
2202 		/* 5 */
2203 	},
2204 	{
2205 		.name  = "instructions:kIG",
2206 		.check = test__checkevent_exclude_idle_modifier_1,
2207 		/* 6 */
2208 	},
2209 	{
2210 		.name  = "task-clock:P,cpu-cycles",
2211 		.check = test__checkevent_precise_max_modifier,
2212 		/* 7 */
2213 	},
2214 	{
2215 		.name  = "instructions/name=insn/",
2216 		.check = test__checkevent_config_symbol,
2217 		/* 8 */
2218 	},
2219 	{
2220 		.name  = "r1234/name=rawpmu/",
2221 		.check = test__checkevent_config_raw,
2222 		/* 9 */
2223 	},
2224 	{
2225 		.name  = "4:0x6530160/name=numpmu/",
2226 		.check = test__checkevent_config_num,
2227 		/* 0 */
2228 	},
2229 	{
2230 		.name  = "L1-dcache-misses/name=cachepmu/",
2231 		.check = test__checkevent_config_cache,
2232 		/* 1 */
2233 	},
2234 	{
2235 		.name  = "intel_pt//u",
2236 		.valid = test__intel_pt_valid,
2237 		.check = test__intel_pt,
2238 		/* 2 */
2239 	},
2240 	{
2241 		.name  = "cpu-cycles/name='COMPLEX_CYCLES_NAME:orig=cpu-cycles,desc=chip-clock-ticks'/Duk",
2242 		.check = test__checkevent_complex_name,
2243 		/* 3 */
2244 	},
2245 	{
2246 		.name  = "cpu-cycles//u",
2247 		.check = test__sym_event_slash,
2248 		/* 4 */
2249 	},
2250 	{
2251 		.name  = "cpu-cycles:k",
2252 		.check = test__sym_event_dc,
2253 		/* 5 */
2254 	},
2255 	{
2256 		.name  = "instructions:uep",
2257 		.check = test__checkevent_exclusive_modifier,
2258 		/* 6 */
2259 	},
2260 	{
2261 		.name  = "{cpu-cycles,cache-misses,branch-misses}:e",
2262 		.check = test__exclusive_group,
2263 		/* 7 */
2264 	},
2265 	{
2266 		.name  = "cpu-cycles/name=name/",
2267 		.check = test__term_equal_term,
2268 		/* 8 */
2269 	},
2270 	{
2271 		.name  = "cpu-cycles/name=l1d/",
2272 		.check = test__term_equal_legacy,
2273 		/* 9 */
2274 	},
2275 	{
2276 		.name  = "mem:0/name=breakpoint/",
2277 		.check = test__checkevent_breakpoint,
2278 		/* 0 */
2279 	},
2280 	{
2281 		.name  = "mem:0:x/name=breakpoint/",
2282 		.check = test__checkevent_breakpoint_x,
2283 		/* 1 */
2284 	},
2285 	{
2286 		.name  = "mem:0:r/name=breakpoint/",
2287 		.check = test__checkevent_breakpoint_r,
2288 		/* 2 */
2289 	},
2290 	{
2291 		.name  = "mem:0:w/name=breakpoint/",
2292 		.check = test__checkevent_breakpoint_w,
2293 		/* 3 */
2294 	},
2295 	{
2296 		.name  = "mem:0/name=breakpoint/u",
2297 		.check = test__checkevent_breakpoint_modifier_name,
2298 		/* 4 */
2299 	},
2300 	{
2301 		.name  = "mem:0:x/name=breakpoint/k",
2302 		.check = test__checkevent_breakpoint_x_modifier_name,
2303 		/* 5 */
2304 	},
2305 	{
2306 		.name  = "mem:0:r/name=breakpoint/hp",
2307 		.check = test__checkevent_breakpoint_r_modifier_name,
2308 		/* 6 */
2309 	},
2310 	{
2311 		.name  = "mem:0:w/name=breakpoint/up",
2312 		.check = test__checkevent_breakpoint_w_modifier_name,
2313 		/* 7 */
2314 	},
2315 	{
2316 		.name  = "mem:0:rw/name=breakpoint/",
2317 		.check = test__checkevent_breakpoint_rw,
2318 		/* 8 */
2319 	},
2320 	{
2321 		.name  = "mem:0:rw/name=breakpoint/kp",
2322 		.check = test__checkevent_breakpoint_rw_modifier_name,
2323 		/* 9 */
2324 	},
2325 	{
2326 		.name  = "mem:0/1/name=breakpoint/",
2327 		.check = test__checkevent_breakpoint_len,
2328 		/* 0 */
2329 	},
2330 	{
2331 		.name  = "mem:0/2:w/name=breakpoint/",
2332 		.check = test__checkevent_breakpoint_len_w,
2333 		/* 1 */
2334 	},
2335 	{
2336 		.name  = "mem:0/4:rw/name=breakpoint/u",
2337 		.check = test__checkevent_breakpoint_len_rw_modifier,
2338 		/* 2 */
2339 	},
2340 	{
2341 		.name  = "mem:0/1/name=breakpoint1/,mem:0/4:rw/name=breakpoint2/",
2342 		.check = test__checkevent_breakpoint_2_events,
2343 		/* 3 */
2344 	},
2345 	{
2346 		.name = "9p:9p_client_req",
2347 		.check = test__checkevent_tracepoint,
2348 		/* 4 */
2349 	},
2350 	{
2351 		.name  = "{cycles,instructions/period=200000,ratio-to-prev=2.0/}",
2352 		.valid = test__acr_valid,
2353 		.check = test__ratio_to_prev,
2354 		/* 5 */
2355 	},
2356 
2357 };
2358 
2359 static const struct evlist_test test__events_pmu[] = {
2360 	{
2361 		.name  = "default_core/config=10,config1=1,config2=3,period=1000/u",
2362 		.check = test__checkevent_pmu,
2363 		/* 0 */
2364 	},
2365 	{
2366 		.name  = "default_core/config=1,name=krava/u,default_core/config=2/u",
2367 		.check = test__checkevent_pmu_name,
2368 		/* 1 */
2369 	},
2370 	{
2371 		.name  = "default_core/config=1,call-graph=fp,time,period=100000/,default_core/config=2,call-graph=no,time=0,period=2000/",
2372 		.check = test__checkevent_pmu_partial_time_callgraph,
2373 		/* 2 */
2374 	},
2375 	{
2376 		.name  = "default_core/name='COMPLEX_CYCLES_NAME:orig=cpu-cycles,desc=chip-clock-ticks',period=0x1,event=0x2/ukp",
2377 		.valid = test__pmu_default_core_event_valid,
2378 		.check = test__checkevent_complex_name,
2379 		/* 3 */
2380 	},
2381 	{
2382 		.name  = "software/r1a/",
2383 		.check = test__checkevent_raw_pmu,
2384 		/* 4 */
2385 	},
2386 	{
2387 		.name  = "software/r0x1a/",
2388 		.check = test__checkevent_raw_pmu,
2389 		/* 5 */
2390 	},
2391 	{
2392 		.name  = "default_core/L1-dcache-load-miss/",
2393 		.check = test__checkevent_genhw,
2394 		/* 6 */
2395 	},
2396 	{
2397 		.name  = "default_core/L1-dcache-load-miss/kp",
2398 		.check = test__checkevent_genhw_modifier,
2399 		/* 7 */
2400 	},
2401 	{
2402 		.name  = "default_core/L1-dcache-misses,name=cachepmu/",
2403 		.check = test__checkevent_config_cache,
2404 		/* 8 */
2405 	},
2406 	{
2407 		.name  = "default_core/instructions/",
2408 		.check = test__checkevent_symbolic_name,
2409 		/* 9 */
2410 	},
2411 	{
2412 		.name  = "default_core/cycles,period=100000,config2/",
2413 		.check = test__checkevent_symbolic_name_config,
2414 		/* 0 */
2415 	},
2416 	{
2417 		.name  = "default_core/instructions/h",
2418 		.check = test__checkevent_symbolic_name_modifier,
2419 		/* 1 */
2420 	},
2421 	{
2422 		.name  = "default_core/instructions/G",
2423 		.check = test__checkevent_exclude_host_modifier,
2424 		/* 2 */
2425 	},
2426 	{
2427 		.name  = "default_core/instructions/H",
2428 		.check = test__checkevent_exclude_guest_modifier,
2429 		/* 3 */
2430 	},
2431 	{
2432 		.name  = "{default_core/instructions/k,default_core/cycles/upp}",
2433 		.check = test__group1,
2434 		/* 4 */
2435 	},
2436 	{
2437 		.name  = "{default_core/cycles/u,default_core/instructions/kp}:p",
2438 		.check = test__group4,
2439 		/* 5 */
2440 	},
2441 	{
2442 		.name  = "{default_core/cycles/,default_core/cache-misses/G}:H",
2443 		.check = test__group_gh1,
2444 		/* 6 */
2445 	},
2446 	{
2447 		.name  = "{default_core/cycles/,default_core/cache-misses/H}:G",
2448 		.check = test__group_gh2,
2449 		/* 7 */
2450 	},
2451 	{
2452 		.name  = "{default_core/cycles/G,default_core/cache-misses/H}:u",
2453 		.check = test__group_gh3,
2454 		/* 8 */
2455 	},
2456 	{
2457 		.name  = "{default_core/cycles/G,default_core/cache-misses/H}:uG",
2458 		.check = test__group_gh4,
2459 		/* 9 */
2460 	},
2461 	{
2462 		.name  = "{default_core/cycles/,default_core/cache-misses/,default_core/branch-misses/}:S",
2463 		.check = test__leader_sample1,
2464 		/* 0 */
2465 	},
2466 	{
2467 		.name  = "{default_core/instructions/,default_core/branch-misses/}:Su",
2468 		.check = test__leader_sample2,
2469 		/* 1 */
2470 	},
2471 	{
2472 		.name  = "default_core/instructions/uDp",
2473 		.check = test__checkevent_pinned_modifier,
2474 		/* 2 */
2475 	},
2476 	{
2477 		.name  = "{default_core/cycles/,default_core/cache-misses/,default_core/branch-misses/}:D",
2478 		.check = test__pinned_group,
2479 		/* 3 */
2480 	},
2481 	{
2482 		.name  = "default_core/instructions/I",
2483 		.check = test__checkevent_exclude_idle_modifier,
2484 		/* 4 */
2485 	},
2486 	{
2487 		.name  = "default_core/instructions/kIG",
2488 		.check = test__checkevent_exclude_idle_modifier_1,
2489 		/* 5 */
2490 	},
2491 	{
2492 		.name  = "default_core/cycles/u",
2493 		.check = test__sym_event_slash,
2494 		/* 6 */
2495 	},
2496 	{
2497 		.name  = "default_core/cycles/k",
2498 		.check = test__sym_event_dc,
2499 		/* 7 */
2500 	},
2501 	{
2502 		.name  = "default_core/instructions/uep",
2503 		.check = test__checkevent_exclusive_modifier,
2504 		/* 8 */
2505 	},
2506 	{
2507 		.name  = "{default_core/cycles/,default_core/cache-misses/,default_core/branch-misses/}:e",
2508 		.check = test__exclusive_group,
2509 		/* 9 */
2510 	},
2511 	{
2512 		.name  = "default_core/cycles,name=name/",
2513 		.check = test__term_equal_term,
2514 		/* 0 */
2515 	},
2516 	{
2517 		.name  = "default_core/cycles,name=l1d/",
2518 		.check = test__term_equal_legacy,
2519 		/* 1 */
2520 	},
2521 };
2522 
2523 struct terms_test {
2524 	const char *str;
2525 	int (*check)(struct parse_events_terms *terms);
2526 };
2527 
2528 static const struct terms_test test__terms[] = {
2529 	[0] = {
2530 		.str   = "config=10,config1,config2=3,config3=4,config4=5,umask=1,read,r0xead",
2531 		.check = test__checkterms_simple,
2532 	},
2533 };
2534 
test_event(const struct evlist_test * e)2535 static int test_event(const struct evlist_test *e)
2536 {
2537 	struct parse_events_error err;
2538 	struct evlist *evlist;
2539 	int ret;
2540 
2541 	if (e->valid && !e->valid()) {
2542 		pr_debug("... SKIP\n");
2543 		return TEST_OK;
2544 	}
2545 
2546 	evlist = evlist__new();
2547 	if (evlist == NULL) {
2548 		pr_err("Failed allocation");
2549 		return TEST_FAIL;
2550 	}
2551 	parse_events_error__init(&err);
2552 	ret = __parse_events(evlist, e->name, /*pmu_filter=*/NULL, &err, /*fake_pmu=*/false,
2553 			     /*warn_if_reordered=*/true, /*fake_tp=*/true);
2554 	if (ret) {
2555 		pr_debug("failed to parse event '%s', err %d\n", e->name, ret);
2556 		parse_events_error__print(&err, e->name);
2557 		ret = TEST_FAIL;
2558 		if (parse_events_error__contains(&err, "can't access trace events"))
2559 			ret = TEST_SKIP;
2560 	} else {
2561 		ret = e->check(evlist);
2562 	}
2563 	parse_events_error__exit(&err);
2564 	evlist__delete(evlist);
2565 
2566 	return ret;
2567 }
2568 
test_event_fake_pmu(const char * str)2569 static int test_event_fake_pmu(const char *str)
2570 {
2571 	struct parse_events_error err;
2572 	struct evlist *evlist;
2573 	int ret;
2574 
2575 	evlist = evlist__new();
2576 	if (!evlist)
2577 		return -ENOMEM;
2578 
2579 	parse_events_error__init(&err);
2580 	ret = __parse_events(evlist, str, /*pmu_filter=*/NULL, &err,
2581 			     /*fake_pmu=*/true, /*warn_if_reordered=*/true,
2582 			     /*fake_tp=*/true);
2583 	if (ret) {
2584 		pr_debug("failed to parse event '%s', err %d\n",
2585 			 str, ret);
2586 		parse_events_error__print(&err, str);
2587 	}
2588 
2589 	parse_events_error__exit(&err);
2590 	evlist__delete(evlist);
2591 
2592 	return ret;
2593 }
2594 
combine_test_results(int existing,int latest)2595 static int combine_test_results(int existing, int latest)
2596 {
2597 	if (existing == TEST_FAIL)
2598 		return TEST_FAIL;
2599 	if (existing == TEST_SKIP)
2600 		return latest == TEST_OK ? TEST_SKIP : latest;
2601 	return latest;
2602 }
2603 
test_events(const struct evlist_test * events,int cnt)2604 static int test_events(const struct evlist_test *events, int cnt)
2605 {
2606 	int ret = TEST_OK;
2607 	struct perf_pmu *core_pmu = perf_pmus__find_core_pmu();
2608 
2609 	for (int i = 0; i < cnt; i++) {
2610 		struct evlist_test e = events[i];
2611 		int test_ret;
2612 		const char *pos = e.name;
2613 		char buf[1024], *buf_pos = buf, *end;
2614 
2615 		while ((end = strstr(pos, "default_core"))) {
2616 			size_t len = end - pos;
2617 
2618 			strncpy(buf_pos, pos, len);
2619 			pos = end + 12;
2620 			buf_pos += len;
2621 			strcpy(buf_pos, core_pmu->name);
2622 			buf_pos += strlen(core_pmu->name);
2623 		}
2624 		strcpy(buf_pos, pos);
2625 
2626 		e.name = buf;
2627 		pr_debug("running test %d '%s'\n", i, e.name);
2628 		test_ret = test_event(&e);
2629 		if (test_ret != TEST_OK) {
2630 			pr_debug("Event test failure: test %d '%s'", i, e.name);
2631 			ret = combine_test_results(ret, test_ret);
2632 		}
2633 	}
2634 
2635 	return ret;
2636 }
2637 
test__events2(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2638 static int test__events2(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2639 {
2640 	return test_events(test__events, ARRAY_SIZE(test__events));
2641 }
2642 
test_term(const struct terms_test * t)2643 static int test_term(const struct terms_test *t)
2644 {
2645 	struct parse_events_terms terms;
2646 	int ret;
2647 
2648 
2649 	parse_events_terms__init(&terms);
2650 	ret = parse_events_terms(&terms, t->str);
2651 	if (ret) {
2652 		pr_debug("failed to parse terms '%s', err %d\n",
2653 			 t->str , ret);
2654 		return ret;
2655 	}
2656 
2657 	ret = t->check(&terms);
2658 	parse_events_terms__exit(&terms);
2659 
2660 	return ret;
2661 }
2662 
test_terms(const struct terms_test * terms,int cnt)2663 static int test_terms(const struct terms_test *terms, int cnt)
2664 {
2665 	int ret = 0;
2666 
2667 	for (int i = 0; i < cnt; i++) {
2668 		const struct terms_test *t = &terms[i];
2669 
2670 		pr_debug("running test %d '%s'\n", i, t->str);
2671 		ret = test_term(t);
2672 		if (ret)
2673 			break;
2674 	}
2675 
2676 	return ret;
2677 }
2678 
test__terms2(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2679 static int test__terms2(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2680 {
2681 	return test_terms(test__terms, ARRAY_SIZE(test__terms));
2682 }
2683 
test__pmu_events(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2684 static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2685 {
2686 	struct perf_pmu *pmu = NULL;
2687 	int ret = TEST_OK;
2688 
2689 	while ((pmu = perf_pmus__scan(pmu)) != NULL) {
2690 		struct stat st;
2691 		char path[PATH_MAX];
2692 		char pmu_event[PATH_MAX];
2693 		char *buf = NULL;
2694 		FILE *file;
2695 		struct dirent *ent;
2696 		size_t len = 0;
2697 		DIR *dir;
2698 		int err;
2699 		int n;
2700 
2701 		snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/events/",
2702 			sysfs__mountpoint(), pmu->name);
2703 
2704 		err = stat(path, &st);
2705 		if (err) {
2706 			pr_debug("skipping PMU %s events tests: %s\n", pmu->name, path);
2707 			continue;
2708 		}
2709 
2710 		dir = opendir(path);
2711 		if (!dir) {
2712 			pr_debug("can't open pmu event dir: %s\n", path);
2713 			ret = combine_test_results(ret, TEST_SKIP);
2714 			continue;
2715 		}
2716 
2717 		while ((ent = readdir(dir))) {
2718 			struct evlist_test e = { .name = NULL, };
2719 			char name[2 * NAME_MAX + 1 + 12 + 3];
2720 			int test_ret;
2721 			bool is_event_parameterized = 0;
2722 
2723 			/* Names containing . are special and cannot be used directly */
2724 			if (strchr(ent->d_name, '.'))
2725 				continue;
2726 
2727 			/* exclude parameterized ones (name contains '?') */
2728 			n = snprintf(pmu_event, sizeof(pmu_event), "%s%s", path, ent->d_name);
2729 			if (n >= PATH_MAX) {
2730 				pr_err("pmu event name crossed PATH_MAX(%d) size\n", PATH_MAX);
2731 				continue;
2732 			}
2733 
2734 			file = fopen(pmu_event, "r");
2735 			if (!file) {
2736 				pr_debug("can't open pmu event file for '%s'\n", ent->d_name);
2737 				ret = combine_test_results(ret, TEST_FAIL);
2738 				continue;
2739 			}
2740 
2741 			if (getline(&buf, &len, file) < 0) {
2742 				pr_debug(" pmu event: %s is a null event\n", ent->d_name);
2743 				ret = combine_test_results(ret, TEST_FAIL);
2744 				fclose(file);
2745 				continue;
2746 			}
2747 
2748 			if (strchr(buf, '?'))
2749 				is_event_parameterized = 1;
2750 
2751 			free(buf);
2752 			buf = NULL;
2753 			fclose(file);
2754 
2755 			if (is_event_parameterized == 1) {
2756 				pr_debug("skipping parameterized PMU event: %s which contains ?\n", pmu_event);
2757 				continue;
2758 			}
2759 
2760 			snprintf(name, sizeof(name), "%s/event=%s/u", pmu->name, ent->d_name);
2761 
2762 			e.name  = name;
2763 			e.check = test__checkevent_pmu_events;
2764 
2765 			test_ret = test_event(&e);
2766 			if (test_ret != TEST_OK) {
2767 				pr_debug("Test PMU event failed for '%s'", name);
2768 				ret = combine_test_results(ret, test_ret);
2769 			}
2770 
2771 			if (!is_pmu_core(pmu->name))
2772 				continue;
2773 
2774 			/*
2775 			 * Names containing '-' are recognized as prefixes and suffixes
2776 			 * due to '-' being a legacy PMU separator. This fails when the
2777 			 * prefix or suffix collides with an existing legacy token. For
2778 			 * example, branch-brs has a prefix (branch) that collides with
2779 			 * a PE_NAME_CACHE_TYPE token causing a parse error as a suffix
2780 			 * isn't expected after this. As event names in the config
2781 			 * slashes are allowed a '-' in the name we check this works
2782 			 * above.
2783 			 */
2784 			if (strchr(ent->d_name, '-'))
2785 				continue;
2786 
2787 			snprintf(name, sizeof(name), "%s:u,%s/event=%s/u",
2788 				 ent->d_name, pmu->name, ent->d_name);
2789 			e.name  = name;
2790 			e.check = test__checkevent_pmu_events_mix;
2791 			test_ret = test_event(&e);
2792 			if (test_ret != TEST_OK) {
2793 				pr_debug("Test PMU event failed for '%s'", name);
2794 				ret = combine_test_results(ret, test_ret);
2795 			}
2796 		}
2797 
2798 		closedir(dir);
2799 	}
2800 	return ret;
2801 }
2802 
test__pmu_events2(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2803 static int test__pmu_events2(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2804 {
2805 	return test_events(test__events_pmu, ARRAY_SIZE(test__events_pmu));
2806 }
2807 
test_alias(char ** event,char ** alias)2808 static bool test_alias(char **event, char **alias)
2809 {
2810 	char path[PATH_MAX];
2811 	DIR *dir;
2812 	struct dirent *dent;
2813 	const char *sysfs = sysfs__mountpoint();
2814 	char buf[128];
2815 	FILE *file;
2816 
2817 	if (!sysfs)
2818 		return false;
2819 
2820 	snprintf(path, PATH_MAX, "%s/bus/event_source/devices/", sysfs);
2821 	dir = opendir(path);
2822 	if (!dir)
2823 		return false;
2824 
2825 	while ((dent = readdir(dir))) {
2826 		if (!strcmp(dent->d_name, ".") ||
2827 		    !strcmp(dent->d_name, ".."))
2828 			continue;
2829 
2830 		snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/alias",
2831 			 sysfs, dent->d_name);
2832 
2833 		if (!file_available(path))
2834 			continue;
2835 
2836 		file = fopen(path, "r");
2837 		if (!file)
2838 			continue;
2839 
2840 		if (!fgets(buf, sizeof(buf), file)) {
2841 			fclose(file);
2842 			continue;
2843 		}
2844 
2845 		/* Remove the last '\n' */
2846 		buf[strlen(buf) - 1] = 0;
2847 
2848 		fclose(file);
2849 		*event = strdup(dent->d_name);
2850 		*alias = strdup(buf);
2851 		closedir(dir);
2852 
2853 		if (*event == NULL || *alias == NULL) {
2854 			free(*event);
2855 			free(*alias);
2856 			return false;
2857 		}
2858 
2859 		return true;
2860 	}
2861 
2862 	closedir(dir);
2863 	return false;
2864 }
2865 
test__checkevent_pmu_events_alias(struct evlist * evlist)2866 static int test__checkevent_pmu_events_alias(struct evlist *evlist)
2867 {
2868 	struct evsel *evsel1 = evlist__first(evlist);
2869 	struct evsel *evsel2 = evlist__last(evlist);
2870 
2871 	TEST_ASSERT_EVSEL("wrong type", evsel1->core.attr.type == evsel2->core.attr.type, evsel1);
2872 	TEST_ASSERT_EVSEL("wrong config", evsel1->core.attr.config == evsel2->core.attr.config,
2873 			  evsel1);
2874 	return TEST_OK;
2875 }
2876 
test__pmu_events_alias(char * event,char * alias)2877 static int test__pmu_events_alias(char *event, char *alias)
2878 {
2879 	struct evlist_test e = { .name = NULL, };
2880 	char name[2 * NAME_MAX + 20];
2881 
2882 	snprintf(name, sizeof(name), "%s/event=1/,%s/event=1/",
2883 		 event, alias);
2884 
2885 	e.name  = name;
2886 	e.check = test__checkevent_pmu_events_alias;
2887 	return test_event(&e);
2888 }
2889 
test__alias(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2890 static int test__alias(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
2891 {
2892 	char *event, *alias;
2893 	int ret;
2894 
2895 	if (!test_alias(&event, &alias))
2896 		return TEST_SKIP;
2897 
2898 	ret = test__pmu_events_alias(event, alias);
2899 
2900 	free(event);
2901 	free(alias);
2902 	return ret;
2903 }
2904 
test__pmu_events_alias2(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2905 static int test__pmu_events_alias2(struct test_suite *test __maybe_unused,
2906 				   int subtest __maybe_unused)
2907 {
2908 	static const char events[][30] = {
2909 			"event-hyphen",
2910 			"event-two-hyph",
2911 	};
2912 	int ret = TEST_OK;
2913 
2914 	for (unsigned int i = 0; i < ARRAY_SIZE(events); i++) {
2915 		int test_ret = test_event_fake_pmu(&events[i][0]);
2916 
2917 		if (test_ret != TEST_OK) {
2918 			pr_debug("check_parse_fake %s failed\n", &events[i][0]);
2919 			ret = combine_test_results(ret, test_ret);
2920 		}
2921 	}
2922 
2923 	return ret;
2924 }
2925 
2926 static struct test_case tests__parse_events[] = {
2927 	TEST_CASE_REASON("Test event parsing",
2928 			 events2,
2929 			 "permissions"),
2930 	TEST_CASE_REASON("Parsing of all PMU events from sysfs",
2931 			 pmu_events,
2932 			 "permissions"),
2933 	TEST_CASE_REASON("Parsing of given PMU events from sysfs",
2934 			 pmu_events2,
2935 			 "permissions"),
2936 	TEST_CASE_REASON("Parsing of aliased events from sysfs", alias,
2937 			 "no aliases in sysfs"),
2938 	TEST_CASE("Parsing of aliased events", pmu_events_alias2),
2939 	TEST_CASE("Parsing of terms (event modifiers)", terms2),
2940 	{	.name = NULL, }
2941 };
2942 
2943 struct test_suite suite__parse_events = {
2944 	.desc = "Parse event definition strings",
2945 	.test_cases = tests__parse_events,
2946 };
2947