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