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