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