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