xref: /openbmc/linux/tools/perf/tests/pmu-events.c (revision 18afb028)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "math.h"
3 #include "parse-events.h"
4 #include "pmu.h"
5 #include "pmus.h"
6 #include "tests.h"
7 #include <errno.h>
8 #include <stdio.h>
9 #include <linux/kernel.h>
10 #include <linux/zalloc.h>
11 #include "debug.h"
12 #include "../pmu-events/pmu-events.h"
13 #include <perf/evlist.h>
14 #include "util/evlist.h"
15 #include "util/expr.h"
16 #include "util/hashmap.h"
17 #include "util/parse-events.h"
18 #include "metricgroup.h"
19 #include "stat.h"
20 
21 struct perf_pmu_test_event {
22 	/* used for matching against events from generated pmu-events.c */
23 	struct pmu_event event;
24 
25 	/* used for matching against event aliases */
26 	/* extra events for aliases */
27 	const char *alias_str;
28 
29 	/*
30 	 * Note: For when PublicDescription does not exist in the JSON, we
31 	 * will have no long_desc in pmu_event.long_desc, but long_desc may
32 	 * be set in the alias.
33 	 */
34 	const char *alias_long_desc;
35 
36 	/* PMU which we should match against */
37 	const char *matching_pmu;
38 };
39 
40 struct perf_pmu_test_pmu {
41 	struct perf_pmu pmu;
42 	struct perf_pmu_test_event const *aliases[10];
43 };
44 
45 static const struct perf_pmu_test_event bp_l1_btb_correct = {
46 	.event = {
47 		.pmu = "default_core",
48 		.name = "bp_l1_btb_correct",
49 		.event = "event=0x8a",
50 		.desc = "L1 BTB Correction",
51 		.topic = "branch",
52 	},
53 	.alias_str = "event=0x8a",
54 	.alias_long_desc = "L1 BTB Correction",
55 };
56 
57 static const struct perf_pmu_test_event bp_l2_btb_correct = {
58 	.event = {
59 		.pmu = "default_core",
60 		.name = "bp_l2_btb_correct",
61 		.event = "event=0x8b",
62 		.desc = "L2 BTB Correction",
63 		.topic = "branch",
64 	},
65 	.alias_str = "event=0x8b",
66 	.alias_long_desc = "L2 BTB Correction",
67 };
68 
69 static const struct perf_pmu_test_event segment_reg_loads_any = {
70 	.event = {
71 		.pmu = "default_core",
72 		.name = "segment_reg_loads.any",
73 		.event = "event=0x6,period=200000,umask=0x80",
74 		.desc = "Number of segment register loads",
75 		.topic = "other",
76 	},
77 	.alias_str = "event=0x6,period=0x30d40,umask=0x80",
78 	.alias_long_desc = "Number of segment register loads",
79 };
80 
81 static const struct perf_pmu_test_event dispatch_blocked_any = {
82 	.event = {
83 		.pmu = "default_core",
84 		.name = "dispatch_blocked.any",
85 		.event = "event=0x9,period=200000,umask=0x20",
86 		.desc = "Memory cluster signals to block micro-op dispatch for any reason",
87 		.topic = "other",
88 	},
89 	.alias_str = "event=0x9,period=0x30d40,umask=0x20",
90 	.alias_long_desc = "Memory cluster signals to block micro-op dispatch for any reason",
91 };
92 
93 static const struct perf_pmu_test_event eist_trans = {
94 	.event = {
95 		.pmu = "default_core",
96 		.name = "eist_trans",
97 		.event = "event=0x3a,period=200000,umask=0x0",
98 		.desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
99 		.topic = "other",
100 	},
101 	.alias_str = "event=0x3a,period=0x30d40,umask=0",
102 	.alias_long_desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
103 };
104 
105 static const struct perf_pmu_test_event l3_cache_rd = {
106 	.event = {
107 		.pmu = "default_core",
108 		.name = "l3_cache_rd",
109 		.event = "event=0x40",
110 		.desc = "L3 cache access, read",
111 		.long_desc = "Attributable Level 3 cache access, read",
112 		.topic = "cache",
113 	},
114 	.alias_str = "event=0x40",
115 	.alias_long_desc = "Attributable Level 3 cache access, read",
116 };
117 
118 static const struct perf_pmu_test_event *core_events[] = {
119 	&bp_l1_btb_correct,
120 	&bp_l2_btb_correct,
121 	&segment_reg_loads_any,
122 	&dispatch_blocked_any,
123 	&eist_trans,
124 	&l3_cache_rd,
125 	NULL
126 };
127 
128 static const struct perf_pmu_test_event uncore_hisi_ddrc_flux_wcmd = {
129 	.event = {
130 		.name = "uncore_hisi_ddrc.flux_wcmd",
131 		.event = "event=0x2",
132 		.desc = "DDRC write commands",
133 		.topic = "uncore",
134 		.long_desc = "DDRC write commands",
135 		.pmu = "hisi_sccl,ddrc",
136 	},
137 	.alias_str = "event=0x2",
138 	.alias_long_desc = "DDRC write commands",
139 	.matching_pmu = "hisi_sccl1_ddrc2",
140 };
141 
142 static const struct perf_pmu_test_event unc_cbo_xsnp_response_miss_eviction = {
143 	.event = {
144 		.name = "unc_cbo_xsnp_response.miss_eviction",
145 		.event = "event=0x22,umask=0x81",
146 		.desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
147 		.topic = "uncore",
148 		.long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
149 		.pmu = "uncore_cbox",
150 	},
151 	.alias_str = "event=0x22,umask=0x81",
152 	.alias_long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
153 	.matching_pmu = "uncore_cbox_0",
154 };
155 
156 static const struct perf_pmu_test_event uncore_hyphen = {
157 	.event = {
158 		.name = "event-hyphen",
159 		.event = "event=0xe0,umask=0x00",
160 		.desc = "UNC_CBO_HYPHEN",
161 		.topic = "uncore",
162 		.long_desc = "UNC_CBO_HYPHEN",
163 		.pmu = "uncore_cbox",
164 	},
165 	.alias_str = "event=0xe0,umask=0",
166 	.alias_long_desc = "UNC_CBO_HYPHEN",
167 	.matching_pmu = "uncore_cbox_0",
168 };
169 
170 static const struct perf_pmu_test_event uncore_two_hyph = {
171 	.event = {
172 		.name = "event-two-hyph",
173 		.event = "event=0xc0,umask=0x00",
174 		.desc = "UNC_CBO_TWO_HYPH",
175 		.topic = "uncore",
176 		.long_desc = "UNC_CBO_TWO_HYPH",
177 		.pmu = "uncore_cbox",
178 	},
179 	.alias_str = "event=0xc0,umask=0",
180 	.alias_long_desc = "UNC_CBO_TWO_HYPH",
181 	.matching_pmu = "uncore_cbox_0",
182 };
183 
184 static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe = {
185 	.event = {
186 		.name = "uncore_hisi_l3c.rd_hit_cpipe",
187 		.event = "event=0x7",
188 		.desc = "Total read hits",
189 		.topic = "uncore",
190 		.long_desc = "Total read hits",
191 		.pmu = "hisi_sccl,l3c",
192 	},
193 	.alias_str = "event=0x7",
194 	.alias_long_desc = "Total read hits",
195 	.matching_pmu = "hisi_sccl3_l3c7",
196 };
197 
198 static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss = {
199 	.event = {
200 		.name = "uncore_imc_free_running.cache_miss",
201 		.event = "event=0x12",
202 		.desc = "Total cache misses",
203 		.topic = "uncore",
204 		.long_desc = "Total cache misses",
205 		.pmu = "uncore_imc_free_running",
206 	},
207 	.alias_str = "event=0x12",
208 	.alias_long_desc = "Total cache misses",
209 	.matching_pmu = "uncore_imc_free_running_0",
210 };
211 
212 static const struct perf_pmu_test_event uncore_imc_cache_hits = {
213 	.event = {
214 		.name = "uncore_imc.cache_hits",
215 		.event = "event=0x34",
216 		.desc = "Total cache hits",
217 		.topic = "uncore",
218 		.long_desc = "Total cache hits",
219 		.pmu = "uncore_imc",
220 	},
221 	.alias_str = "event=0x34",
222 	.alias_long_desc = "Total cache hits",
223 	.matching_pmu = "uncore_imc_0",
224 };
225 
226 static const struct perf_pmu_test_event *uncore_events[] = {
227 	&uncore_hisi_ddrc_flux_wcmd,
228 	&unc_cbo_xsnp_response_miss_eviction,
229 	&uncore_hyphen,
230 	&uncore_two_hyph,
231 	&uncore_hisi_l3c_rd_hit_cpipe,
232 	&uncore_imc_free_running_cache_miss,
233 	&uncore_imc_cache_hits,
234 	NULL
235 };
236 
237 static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles = {
238 	.event = {
239 		.name = "sys_ddr_pmu.write_cycles",
240 		.event = "event=0x2b",
241 		.desc = "ddr write-cycles event",
242 		.topic = "uncore",
243 		.pmu = "uncore_sys_ddr_pmu",
244 		.compat = "v8",
245 	},
246 	.alias_str = "event=0x2b",
247 	.alias_long_desc = "ddr write-cycles event",
248 	.matching_pmu = "uncore_sys_ddr_pmu",
249 };
250 
251 static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles = {
252 	.event = {
253 		.name = "sys_ccn_pmu.read_cycles",
254 		.event = "config=0x2c",
255 		.desc = "ccn read-cycles event",
256 		.topic = "uncore",
257 		.pmu = "uncore_sys_ccn_pmu",
258 		.compat = "0x01",
259 	},
260 	.alias_str = "config=0x2c",
261 	.alias_long_desc = "ccn read-cycles event",
262 	.matching_pmu = "uncore_sys_ccn_pmu",
263 };
264 
265 static const struct perf_pmu_test_event *sys_events[] = {
266 	&sys_ddr_pmu_write_cycles,
267 	&sys_ccn_pmu_read_cycles,
268 	NULL
269 };
270 
271 static bool is_same(const char *reference, const char *test)
272 {
273 	if (!reference && !test)
274 		return true;
275 
276 	if (reference && !test)
277 		return false;
278 
279 	if (!reference && test)
280 		return false;
281 
282 	return !strcmp(reference, test);
283 }
284 
285 static int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
286 {
287 	if (!is_same(e1->name, e2->name)) {
288 		pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
289 			  e1->name, e1->name, e2->name);
290 		return -1;
291 	}
292 
293 	if (!is_same(e1->compat, e2->compat)) {
294 		pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
295 			  e1->name, e1->compat, e2->compat);
296 		return -1;
297 	}
298 
299 	if (!is_same(e1->event, e2->event)) {
300 		pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
301 			  e1->name, e1->event, e2->event);
302 		return -1;
303 	}
304 
305 	if (!is_same(e1->desc, e2->desc)) {
306 		pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
307 			  e1->name, e1->desc, e2->desc);
308 		return -1;
309 	}
310 
311 	if (!is_same(e1->topic, e2->topic)) {
312 		pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
313 			  e1->name, e1->topic, e2->topic);
314 		return -1;
315 	}
316 
317 	if (!is_same(e1->long_desc, e2->long_desc)) {
318 		pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
319 			  e1->name, e1->long_desc, e2->long_desc);
320 		return -1;
321 	}
322 
323 	if (!is_same(e1->pmu, e2->pmu)) {
324 		pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
325 			  e1->name, e1->pmu, e2->pmu);
326 		return -1;
327 	}
328 
329 	if (!is_same(e1->unit, e2->unit)) {
330 		pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
331 			  e1->name, e1->unit, e2->unit);
332 		return -1;
333 	}
334 
335 	if (e1->perpkg != e2->perpkg) {
336 		pr_debug2("testing event e1 %s: mismatched perpkg, %d vs %d\n",
337 			  e1->name, e1->perpkg, e2->perpkg);
338 		return -1;
339 	}
340 
341 	if (e1->deprecated != e2->deprecated) {
342 		pr_debug2("testing event e1 %s: mismatched deprecated, %d vs %d\n",
343 			  e1->name, e1->deprecated, e2->deprecated);
344 		return -1;
345 	}
346 
347 	return 0;
348 }
349 
350 static int compare_alias_to_test_event(struct pmu_event_info *alias,
351 				struct perf_pmu_test_event const *test_event,
352 				char const *pmu_name)
353 {
354 	struct pmu_event const *event = &test_event->event;
355 
356 	/* An alias was found, ensure everything is in order */
357 	if (!is_same(alias->name, event->name)) {
358 		pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
359 			  pmu_name, alias->name, event->name);
360 		return -1;
361 	}
362 
363 	if (!is_same(alias->desc, event->desc)) {
364 		pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
365 			  pmu_name, alias->desc, event->desc);
366 		return -1;
367 	}
368 
369 	if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
370 		pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
371 			  pmu_name, alias->long_desc,
372 			  test_event->alias_long_desc);
373 		return -1;
374 	}
375 
376 	if (!is_same(alias->topic, event->topic)) {
377 		pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
378 			  pmu_name, alias->topic, event->topic);
379 		return -1;
380 	}
381 
382 	if (!is_same(alias->str, test_event->alias_str)) {
383 		pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
384 			  pmu_name, alias->str, test_event->alias_str);
385 		return -1;
386 	}
387 
388 	if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
389 		pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
390 			  pmu_name, alias->str, test_event->alias_long_desc);
391 		return -1;
392 	}
393 
394 	if (!is_same(alias->pmu_name, test_event->event.pmu) &&
395 	    !is_same(alias->pmu_name, "default_core")) {
396 		pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
397 			  pmu_name, alias->pmu_name, test_event->event.pmu);
398 		return -1;
399 	}
400 
401 	return 0;
402 }
403 
404 static int test__pmu_event_table_core_callback(const struct pmu_event *pe,
405 					       const struct pmu_events_table *table __maybe_unused,
406 					       void *data)
407 {
408 	int *map_events = data;
409 	struct perf_pmu_test_event const **test_event_table;
410 	bool found = false;
411 
412 	if (strcmp(pe->pmu, "default_core"))
413 		test_event_table = &uncore_events[0];
414 	else
415 		test_event_table = &core_events[0];
416 
417 	for (; *test_event_table; test_event_table++) {
418 		struct perf_pmu_test_event const *test_event = *test_event_table;
419 		struct pmu_event const *event = &test_event->event;
420 
421 		if (strcmp(pe->name, event->name))
422 			continue;
423 		found = true;
424 		(*map_events)++;
425 
426 		if (compare_pmu_events(pe, event))
427 			return -1;
428 
429 		pr_debug("testing event table %s: pass\n", pe->name);
430 	}
431 	if (!found) {
432 		pr_err("testing event table: could not find event %s\n", pe->name);
433 		return -1;
434 	}
435 	return 0;
436 }
437 
438 static int test__pmu_event_table_sys_callback(const struct pmu_event *pe,
439 					      const struct pmu_events_table *table __maybe_unused,
440 					      void *data)
441 {
442 	int *map_events = data;
443 	struct perf_pmu_test_event const **test_event_table;
444 	bool found = false;
445 
446 	test_event_table = &sys_events[0];
447 
448 	for (; *test_event_table; test_event_table++) {
449 		struct perf_pmu_test_event const *test_event = *test_event_table;
450 		struct pmu_event const *event = &test_event->event;
451 
452 		if (strcmp(pe->name, event->name))
453 			continue;
454 		found = true;
455 		(*map_events)++;
456 
457 		if (compare_pmu_events(pe, event))
458 			return TEST_FAIL;
459 
460 		pr_debug("testing sys event table %s: pass\n", pe->name);
461 	}
462 	if (!found) {
463 		pr_debug("testing sys event table: could not find event %s\n", pe->name);
464 		return TEST_FAIL;
465 	}
466 	return TEST_OK;
467 }
468 
469 /* Verify generated events from pmu-events.c are as expected */
470 static int test__pmu_event_table(struct test_suite *test __maybe_unused,
471 				 int subtest __maybe_unused)
472 {
473 	const struct pmu_events_table *sys_event_table =
474 		find_sys_events_table("pmu_events__test_soc_sys");
475 	const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
476 	int map_events = 0, expected_events, err;
477 
478 	/* ignore 3x sentinels */
479 	expected_events = ARRAY_SIZE(core_events) +
480 			  ARRAY_SIZE(uncore_events) +
481 			  ARRAY_SIZE(sys_events) - 3;
482 
483 	if (!table || !sys_event_table)
484 		return -1;
485 
486 	err = pmu_events_table__for_each_event(table, /*pmu=*/ NULL,
487 					      test__pmu_event_table_core_callback,
488 					      &map_events);
489 	if (err)
490 		return err;
491 
492 	err = pmu_events_table__for_each_event(sys_event_table, /*pmu=*/ NULL,
493 					      test__pmu_event_table_sys_callback,
494 					      &map_events);
495 	if (err)
496 		return err;
497 
498 	if (map_events != expected_events) {
499 		pr_err("testing event table: found %d, but expected %d\n",
500 		       map_events, expected_events);
501 		return TEST_FAIL;
502 	}
503 
504 	return 0;
505 }
506 
507 struct test_core_pmu_event_aliases_cb_args {
508 	struct perf_pmu_test_event const *test_event;
509 	int *count;
510 };
511 
512 static int test_core_pmu_event_aliases_cb(void *state, struct pmu_event_info *alias)
513 {
514 	struct test_core_pmu_event_aliases_cb_args *args = state;
515 
516 	if (compare_alias_to_test_event(alias, args->test_event, alias->pmu->name))
517 		return -1;
518 	(*args->count)++;
519 	pr_debug2("testing aliases core PMU %s: matched event %s\n",
520 		alias->pmu_name, alias->name);
521 	return 0;
522 }
523 
524 /* Verify aliases are as expected */
525 static int __test_core_pmu_event_aliases(const char *pmu_name, int *count)
526 {
527 	struct perf_pmu_test_event const **test_event_table;
528 	struct perf_pmu *pmu;
529 	int res = 0;
530 	const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
531 
532 	if (!table)
533 		return -1;
534 
535 	test_event_table = &core_events[0];
536 
537 	pmu = zalloc(sizeof(*pmu));
538 	if (!pmu)
539 		return -1;
540 
541 	INIT_LIST_HEAD(&pmu->format);
542 	INIT_LIST_HEAD(&pmu->aliases);
543 	INIT_LIST_HEAD(&pmu->caps);
544 	INIT_LIST_HEAD(&pmu->list);
545 	pmu->name = strdup(pmu_name);
546 	pmu->is_core = true;
547 
548 	pmu->events_table = table;
549 	pmu_add_cpu_aliases_table(pmu, table);
550 	pmu->cpu_aliases_added = true;
551 	pmu->sysfs_aliases_loaded = true;
552 
553 	res = pmu_events_table__find_event(table, pmu, "bp_l1_btb_correct", NULL, NULL);
554 	if (res != 0) {
555 		pr_debug("Missing test event in test architecture");
556 		return res;
557 	}
558 	for (; *test_event_table; test_event_table++) {
559 		struct perf_pmu_test_event test_event = **test_event_table;
560 		struct pmu_event const *event = &test_event.event;
561 		struct test_core_pmu_event_aliases_cb_args args = {
562 			.test_event = &test_event,
563 			.count = count,
564 		};
565 		int err;
566 
567 		test_event.event.pmu = pmu_name;
568 		err = perf_pmu__find_event(pmu, event->name, &args,
569 					   test_core_pmu_event_aliases_cb);
570 		if (err)
571 			res = err;
572 	}
573 	perf_pmu__delete(pmu);
574 
575 	return res;
576 }
577 
578 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
579 {
580 	int alias_count = 0, to_match_count = 0, matched_count = 0;
581 	struct perf_pmu_test_event const **table;
582 	struct perf_pmu *pmu = &test_pmu->pmu;
583 	const char *pmu_name = pmu->name;
584 	const struct pmu_events_table *events_table;
585 	int res = 0;
586 
587 	events_table = find_core_events_table("testarch", "testcpu");
588 	if (!events_table)
589 		return -1;
590 	pmu->events_table = events_table;
591 	pmu_add_cpu_aliases_table(pmu, events_table);
592 	pmu->cpu_aliases_added = true;
593 	pmu->sysfs_aliases_loaded = true;
594 	pmu_add_sys_aliases(pmu);
595 
596 	/* Count how many aliases we generated */
597 	alias_count = perf_pmu__num_events(pmu);
598 
599 	/* Count how many aliases we expect from the known table */
600 	for (table = &test_pmu->aliases[0]; *table; table++)
601 		to_match_count++;
602 
603 	if (alias_count != to_match_count) {
604 		pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
605 			 pmu_name, to_match_count, alias_count);
606 		return -1;
607 	}
608 
609 	for (table = &test_pmu->aliases[0]; *table; table++) {
610 		struct perf_pmu_test_event test_event = **table;
611 		struct pmu_event const *event = &test_event.event;
612 		int err;
613 		struct test_core_pmu_event_aliases_cb_args args = {
614 			.test_event = &test_event,
615 			.count = &matched_count,
616 		};
617 
618 		err = perf_pmu__find_event(pmu, event->name, &args,
619 					   test_core_pmu_event_aliases_cb);
620 		if (err) {
621 			res = err;
622 			pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
623 				 pmu_name, event->name);
624 			return -1;
625 		}
626 	}
627 
628 	if (alias_count != matched_count) {
629 		pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
630 			 pmu_name, matched_count, alias_count);
631 		res = -1;
632 	}
633 	return res;
634 }
635 
636 static struct perf_pmu_test_pmu test_pmus[] = {
637 	{
638 		.pmu = {
639 			.name = "hisi_sccl1_ddrc2",
640 			.is_uncore = 1,
641 		},
642 		.aliases = {
643 			&uncore_hisi_ddrc_flux_wcmd,
644 		},
645 	},
646 	{
647 		.pmu = {
648 			.name = "uncore_cbox_0",
649 			.is_uncore = 1,
650 		},
651 		.aliases = {
652 			&unc_cbo_xsnp_response_miss_eviction,
653 			&uncore_hyphen,
654 			&uncore_two_hyph,
655 		},
656 	},
657 	{
658 		.pmu = {
659 			.name = "hisi_sccl3_l3c7",
660 			.is_uncore = 1,
661 		},
662 		.aliases = {
663 			&uncore_hisi_l3c_rd_hit_cpipe,
664 		},
665 	},
666 	{
667 		.pmu = {
668 			.name = "uncore_imc_free_running_0",
669 			.is_uncore = 1,
670 		},
671 		.aliases = {
672 			&uncore_imc_free_running_cache_miss,
673 		},
674 	},
675 	{
676 		.pmu = {
677 			.name = "uncore_imc_0",
678 			.is_uncore = 1,
679 		},
680 		.aliases = {
681 			&uncore_imc_cache_hits,
682 		},
683 	},
684 	{
685 		.pmu = {
686 			.name = "uncore_sys_ddr_pmu0",
687 			.is_uncore = 1,
688 			.id = "v8",
689 		},
690 		.aliases = {
691 			&sys_ddr_pmu_write_cycles,
692 		},
693 	},
694 	{
695 		.pmu = {
696 			.name = "uncore_sys_ccn_pmu4",
697 			.is_uncore = 1,
698 			.id = "0x01",
699 		},
700 		.aliases = {
701 			&sys_ccn_pmu_read_cycles,
702 		},
703 	},
704 };
705 
706 /* Test that aliases generated are as expected */
707 static int test__aliases(struct test_suite *test __maybe_unused,
708 			int subtest __maybe_unused)
709 {
710 	struct perf_pmu *pmu = NULL;
711 	unsigned long i;
712 
713 	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
714 		int count = 0;
715 
716 		if (list_empty(&pmu->format)) {
717 			pr_debug2("skipping testing core PMU %s\n", pmu->name);
718 			continue;
719 		}
720 
721 		if (__test_core_pmu_event_aliases(pmu->name, &count)) {
722 			pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
723 			return -1;
724 		}
725 
726 		if (count == 0) {
727 			pr_debug("testing core PMU %s aliases: no events to match\n",
728 				  pmu->name);
729 			return -1;
730 		}
731 
732 		pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
733 	}
734 
735 	for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
736 		int res;
737 
738 		INIT_LIST_HEAD(&test_pmus[i].pmu.format);
739 		INIT_LIST_HEAD(&test_pmus[i].pmu.aliases);
740 		INIT_LIST_HEAD(&test_pmus[i].pmu.caps);
741 
742 		res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
743 		if (res)
744 			return res;
745 	}
746 
747 	return 0;
748 }
749 
750 static bool is_number(const char *str)
751 {
752 	char *end_ptr;
753 	double v;
754 
755 	errno = 0;
756 	v = strtod(str, &end_ptr);
757 	(void)v; // We're not interested in this value, only if it is valid
758 	return errno == 0 && end_ptr != str;
759 }
760 
761 static int check_parse_id(const char *id, struct parse_events_error *error,
762 			  struct perf_pmu *fake_pmu)
763 {
764 	struct evlist *evlist;
765 	int ret;
766 	char *dup, *cur;
767 
768 	/* Numbers are always valid. */
769 	if (is_number(id))
770 		return 0;
771 
772 	evlist = evlist__new();
773 	if (!evlist)
774 		return -ENOMEM;
775 
776 	dup = strdup(id);
777 	if (!dup)
778 		return -ENOMEM;
779 
780 	for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
781 		*cur = '/';
782 
783 	ret = __parse_events(evlist, dup, /*pmu_filter=*/NULL, error, fake_pmu,
784 			     /*warn_if_reordered=*/true);
785 	free(dup);
786 
787 	evlist__delete(evlist);
788 	return ret;
789 }
790 
791 static int check_parse_fake(const char *id)
792 {
793 	struct parse_events_error error;
794 	int ret;
795 
796 	parse_events_error__init(&error);
797 	ret = check_parse_id(id, &error, &perf_pmu__fake);
798 	parse_events_error__exit(&error);
799 	return ret;
800 }
801 
802 struct metric {
803 	struct list_head list;
804 	struct metric_ref metric_ref;
805 };
806 
807 static int test__parsing_callback(const struct pmu_metric *pm,
808 				  const struct pmu_metrics_table *table,
809 				  void *data)
810 {
811 	int *failures = data;
812 	int k;
813 	struct evlist *evlist;
814 	struct perf_cpu_map *cpus;
815 	struct evsel *evsel;
816 	struct rblist metric_events = {
817 		.nr_entries = 0,
818 	};
819 	int err = 0;
820 
821 	if (!pm->metric_expr)
822 		return 0;
823 
824 	pr_debug("Found metric '%s'\n", pm->metric_name);
825 	(*failures)++;
826 
827 	/*
828 	 * We need to prepare evlist for stat mode running on CPU 0
829 	 * because that's where all the stats are going to be created.
830 	 */
831 	evlist = evlist__new();
832 	if (!evlist)
833 		return -ENOMEM;
834 
835 	cpus = perf_cpu_map__new("0");
836 	if (!cpus) {
837 		evlist__delete(evlist);
838 		return -ENOMEM;
839 	}
840 
841 	perf_evlist__set_maps(&evlist->core, cpus, NULL);
842 
843 	err = metricgroup__parse_groups_test(evlist, table, pm->metric_name, &metric_events);
844 	if (err) {
845 		if (!strcmp(pm->metric_name, "M1") || !strcmp(pm->metric_name, "M2") ||
846 		    !strcmp(pm->metric_name, "M3")) {
847 			(*failures)--;
848 			pr_debug("Expected broken metric %s skipping\n", pm->metric_name);
849 			err = 0;
850 		}
851 		goto out_err;
852 	}
853 
854 	err = evlist__alloc_stats(/*config=*/NULL, evlist, /*alloc_raw=*/false);
855 	if (err)
856 		goto out_err;
857 	/*
858 	 * Add all ids with a made up value. The value may trigger divide by
859 	 * zero when subtracted and so try to make them unique.
860 	 */
861 	k = 1;
862 	evlist__alloc_aggr_stats(evlist, 1);
863 	evlist__for_each_entry(evlist, evsel) {
864 		evsel->stats->aggr->counts.val = k;
865 		if (evsel__name_is(evsel, "duration_time"))
866 			update_stats(&walltime_nsecs_stats, k);
867 		k++;
868 	}
869 	evlist__for_each_entry(evlist, evsel) {
870 		struct metric_event *me = metricgroup__lookup(&metric_events, evsel, false);
871 
872 		if (me != NULL) {
873 			struct metric_expr *mexp;
874 
875 			list_for_each_entry (mexp, &me->head, nd) {
876 				if (strcmp(mexp->metric_name, pm->metric_name))
877 					continue;
878 				pr_debug("Result %f\n", test_generic_metric(mexp, 0));
879 				err = 0;
880 				(*failures)--;
881 				goto out_err;
882 			}
883 		}
884 	}
885 	pr_debug("Didn't find parsed metric %s", pm->metric_name);
886 	err = 1;
887 out_err:
888 	if (err)
889 		pr_debug("Broken metric %s\n", pm->metric_name);
890 
891 	/* ... cleanup. */
892 	metricgroup__rblist_exit(&metric_events);
893 	evlist__free_stats(evlist);
894 	perf_cpu_map__put(cpus);
895 	evlist__delete(evlist);
896 	return err;
897 }
898 
899 static int test__parsing(struct test_suite *test __maybe_unused,
900 			 int subtest __maybe_unused)
901 {
902 	int failures = 0;
903 
904 	pmu_for_each_core_metric(test__parsing_callback, &failures);
905 	pmu_for_each_sys_metric(test__parsing_callback, &failures);
906 
907 	return failures == 0 ? TEST_OK : TEST_FAIL;
908 }
909 
910 struct test_metric {
911 	const char *str;
912 };
913 
914 static struct test_metric metrics[] = {
915 	{ "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
916 	{ "imx8_ddr0@read\\-cycles@ * 4 * 4", },
917 	{ "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
918 	{ "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
919 	{ "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
920 };
921 
922 static int metric_parse_fake(const char *metric_name, const char *str)
923 {
924 	struct expr_parse_ctx *ctx;
925 	struct hashmap_entry *cur;
926 	double result;
927 	int ret = -1;
928 	size_t bkt;
929 	int i;
930 
931 	pr_debug("parsing '%s': '%s'\n", metric_name, str);
932 
933 	ctx = expr__ctx_new();
934 	if (!ctx) {
935 		pr_debug("expr__ctx_new failed");
936 		return TEST_FAIL;
937 	}
938 	ctx->sctx.is_test = true;
939 	if (expr__find_ids(str, NULL, ctx) < 0) {
940 		pr_err("expr__find_ids failed\n");
941 		return -1;
942 	}
943 
944 	/*
945 	 * Add all ids with a made up value. The value may
946 	 * trigger divide by zero when subtracted and so try to
947 	 * make them unique.
948 	 */
949 	i = 1;
950 	hashmap__for_each_entry(ctx->ids, cur, bkt)
951 		expr__add_id_val(ctx, strdup(cur->pkey), i++);
952 
953 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
954 		if (check_parse_fake(cur->pkey)) {
955 			pr_err("check_parse_fake failed\n");
956 			goto out;
957 		}
958 	}
959 
960 	ret = 0;
961 	if (expr__parse(&result, ctx, str)) {
962 		/*
963 		 * Parsing failed, make numbers go from large to small which can
964 		 * resolve divide by zero issues.
965 		 */
966 		i = 1024;
967 		hashmap__for_each_entry(ctx->ids, cur, bkt)
968 			expr__add_id_val(ctx, strdup(cur->pkey), i--);
969 		if (expr__parse(&result, ctx, str)) {
970 			pr_err("expr__parse failed for %s\n", metric_name);
971 			/* The following have hard to avoid divide by zero. */
972 			if (!strcmp(metric_name, "tma_clears_resteers") ||
973 			    !strcmp(metric_name, "tma_mispredicts_resteers"))
974 				ret = 0;
975 			else
976 				ret = -1;
977 		}
978 	}
979 
980 out:
981 	expr__ctx_free(ctx);
982 	return ret;
983 }
984 
985 static int test__parsing_fake_callback(const struct pmu_metric *pm,
986 				       const struct pmu_metrics_table *table __maybe_unused,
987 				       void *data __maybe_unused)
988 {
989 	return metric_parse_fake(pm->metric_name, pm->metric_expr);
990 }
991 
992 /*
993  * Parse all the metrics for current architecture,
994  * or all defined cpus via the 'fake_pmu'
995  * in parse_events.
996  */
997 static int test__parsing_fake(struct test_suite *test __maybe_unused,
998 			      int subtest __maybe_unused)
999 {
1000 	int err = 0;
1001 
1002 	for (size_t i = 0; i < ARRAY_SIZE(metrics); i++) {
1003 		err = metric_parse_fake("", metrics[i].str);
1004 		if (err)
1005 			return err;
1006 	}
1007 
1008 	err = pmu_for_each_core_metric(test__parsing_fake_callback, NULL);
1009 	if (err)
1010 		return err;
1011 
1012 	return pmu_for_each_sys_metric(test__parsing_fake_callback, NULL);
1013 }
1014 
1015 static int test__parsing_threshold_callback(const struct pmu_metric *pm,
1016 					const struct pmu_metrics_table *table __maybe_unused,
1017 					void *data __maybe_unused)
1018 {
1019 	if (!pm->metric_threshold)
1020 		return 0;
1021 	return metric_parse_fake(pm->metric_name, pm->metric_threshold);
1022 }
1023 
1024 static int test__parsing_threshold(struct test_suite *test __maybe_unused,
1025 			      int subtest __maybe_unused)
1026 {
1027 	int err = 0;
1028 
1029 	err = pmu_for_each_core_metric(test__parsing_threshold_callback, NULL);
1030 	if (err)
1031 		return err;
1032 
1033 	return pmu_for_each_sys_metric(test__parsing_threshold_callback, NULL);
1034 }
1035 
1036 static struct test_case pmu_events_tests[] = {
1037 	TEST_CASE("PMU event table sanity", pmu_event_table),
1038 	TEST_CASE("PMU event map aliases", aliases),
1039 	TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1040 			 "some metrics failed"),
1041 	TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
1042 	TEST_CASE("Parsing of metric thresholds with fake PMUs", parsing_threshold),
1043 	{ .name = NULL, }
1044 };
1045 
1046 struct test_suite suite__pmu_events = {
1047 	.desc = "PMU events",
1048 	.test_cases = pmu_events_tests,
1049 };
1050