xref: /openbmc/linux/tools/perf/tests/pmu-events.c (revision c3245d2093c16c60bddc2ec5e945354ffe16bb95)
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 pmu_event_info *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 struct test_core_pmu_event_aliases_cb_args {
500 	struct perf_pmu_test_event const *test_event;
501 	int *count;
502 };
503 
504 static int test_core_pmu_event_aliases_cb(void *state, struct pmu_event_info *alias)
505 {
506 	struct test_core_pmu_event_aliases_cb_args *args = state;
507 
508 	if (compare_alias_to_test_event(alias, args->test_event, alias->pmu->name))
509 		return -1;
510 	(*args->count)++;
511 	pr_debug2("testing aliases core PMU %s: matched event %s\n",
512 		alias->pmu_name, alias->name);
513 	return 0;
514 }
515 
516 /* Verify aliases are as expected */
517 static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
518 {
519 	struct perf_pmu_test_event const **test_event_table;
520 	struct perf_pmu *pmu;
521 	int res = 0;
522 	const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
523 
524 	if (!table)
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 	INIT_LIST_HEAD(&pmu->format);
534 	INIT_LIST_HEAD(&pmu->aliases);
535 	INIT_LIST_HEAD(&pmu->caps);
536 	INIT_LIST_HEAD(&pmu->list);
537 	pmu->name = strdup(pmu_name);
538 
539 	pmu_add_cpu_aliases_table(pmu, table);
540 
541 	for (; *test_event_table; test_event_table++) {
542 		struct perf_pmu_test_event test_event = **test_event_table;
543 		struct pmu_event const *event = &test_event.event;
544 		struct test_core_pmu_event_aliases_cb_args args = {
545 			.test_event = &test_event,
546 			.count = count,
547 		};
548 		int err;
549 
550 		test_event.event.pmu = pmu_name;
551 		err = perf_pmu__find_event(pmu, event->name, &args,
552 					   test_core_pmu_event_aliases_cb);
553 		if (err)
554 			res = err;
555 	}
556 	perf_pmu__delete(pmu);
557 
558 	return res;
559 }
560 
561 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
562 {
563 	int alias_count = 0, to_match_count = 0, matched_count = 0;
564 	struct perf_pmu_test_event const **table;
565 	struct perf_pmu *pmu = &test_pmu->pmu;
566 	const char *pmu_name = pmu->name;
567 	const struct pmu_events_table *events_table;
568 	int res = 0;
569 
570 	events_table = find_core_events_table("testarch", "testcpu");
571 	if (!events_table)
572 		return -1;
573 	pmu_add_cpu_aliases_table(pmu, events_table);
574 	pmu_add_sys_aliases(pmu);
575 
576 	/* Count how many aliases we generated */
577 	alias_count = perf_pmu__num_events(pmu);
578 
579 	/* Count how many aliases we expect from the known table */
580 	for (table = &test_pmu->aliases[0]; *table; table++)
581 		to_match_count++;
582 
583 	if (alias_count != to_match_count) {
584 		pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
585 			 pmu_name, to_match_count, alias_count);
586 		return -1;
587 	}
588 
589 	for (table = &test_pmu->aliases[0]; *table; table++) {
590 		struct perf_pmu_test_event test_event = **table;
591 		struct pmu_event const *event = &test_event.event;
592 		int err;
593 		struct test_core_pmu_event_aliases_cb_args args = {
594 			.test_event = &test_event,
595 			.count = &matched_count,
596 		};
597 
598 		err = perf_pmu__find_event(pmu, event->name, &args,
599 					   test_core_pmu_event_aliases_cb);
600 		if (err) {
601 			res = err;
602 			pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
603 				 pmu_name, event->name);
604 			return -1;
605 		}
606 	}
607 
608 	if (alias_count != matched_count) {
609 		pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
610 			 pmu_name, matched_count, alias_count);
611 		res = -1;
612 	}
613 	return res;
614 }
615 
616 static struct perf_pmu_test_pmu test_pmus[] = {
617 	{
618 		.pmu = {
619 			.name = (char *)"hisi_sccl1_ddrc2",
620 			.is_uncore = 1,
621 		},
622 		.aliases = {
623 			&uncore_hisi_ddrc_flux_wcmd,
624 		},
625 	},
626 	{
627 		.pmu = {
628 			.name = (char *)"uncore_cbox_0",
629 			.is_uncore = 1,
630 		},
631 		.aliases = {
632 			&unc_cbo_xsnp_response_miss_eviction,
633 			&uncore_hyphen,
634 			&uncore_two_hyph,
635 		},
636 	},
637 	{
638 		.pmu = {
639 			.name = (char *)"hisi_sccl3_l3c7",
640 			.is_uncore = 1,
641 		},
642 		.aliases = {
643 			&uncore_hisi_l3c_rd_hit_cpipe,
644 		},
645 	},
646 	{
647 		.pmu = {
648 			.name = (char *)"uncore_imc_free_running_0",
649 			.is_uncore = 1,
650 		},
651 		.aliases = {
652 			&uncore_imc_free_running_cache_miss,
653 		},
654 	},
655 	{
656 		.pmu = {
657 			.name = (char *)"uncore_imc_0",
658 			.is_uncore = 1,
659 		},
660 		.aliases = {
661 			&uncore_imc_cache_hits,
662 		},
663 	},
664 	{
665 		.pmu = {
666 			.name = (char *)"uncore_sys_ddr_pmu0",
667 			.is_uncore = 1,
668 			.id = (char *)"v8",
669 		},
670 		.aliases = {
671 			&sys_ddr_pmu_write_cycles,
672 		},
673 	},
674 	{
675 		.pmu = {
676 			.name = (char *)"uncore_sys_ccn_pmu4",
677 			.is_uncore = 1,
678 			.id = (char *)"0x01",
679 		},
680 		.aliases = {
681 			&sys_ccn_pmu_read_cycles,
682 		},
683 	},
684 };
685 
686 /* Test that aliases generated are as expected */
687 static int test__aliases(struct test_suite *test __maybe_unused,
688 			int subtest __maybe_unused)
689 {
690 	struct perf_pmu *pmu = NULL;
691 	unsigned long i;
692 
693 	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
694 		int count = 0;
695 
696 		if (list_empty(&pmu->format)) {
697 			pr_debug2("skipping testing core PMU %s\n", pmu->name);
698 			continue;
699 		}
700 
701 		if (__test_core_pmu_event_aliases(pmu->name, &count)) {
702 			pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
703 			return -1;
704 		}
705 
706 		if (count == 0) {
707 			pr_debug("testing core PMU %s aliases: no events to match\n",
708 				  pmu->name);
709 			return -1;
710 		}
711 
712 		pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
713 	}
714 
715 	for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
716 		int res;
717 
718 		INIT_LIST_HEAD(&test_pmus[i].pmu.format);
719 		INIT_LIST_HEAD(&test_pmus[i].pmu.aliases);
720 		INIT_LIST_HEAD(&test_pmus[i].pmu.caps);
721 
722 		res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
723 		if (res)
724 			return res;
725 	}
726 
727 	return 0;
728 }
729 
730 static bool is_number(const char *str)
731 {
732 	char *end_ptr;
733 	double v;
734 
735 	errno = 0;
736 	v = strtod(str, &end_ptr);
737 	(void)v; // We're not interested in this value, only if it is valid
738 	return errno == 0 && end_ptr != str;
739 }
740 
741 static int check_parse_id(const char *id, struct parse_events_error *error,
742 			  struct perf_pmu *fake_pmu)
743 {
744 	struct evlist *evlist;
745 	int ret;
746 	char *dup, *cur;
747 
748 	/* Numbers are always valid. */
749 	if (is_number(id))
750 		return 0;
751 
752 	evlist = evlist__new();
753 	if (!evlist)
754 		return -ENOMEM;
755 
756 	dup = strdup(id);
757 	if (!dup)
758 		return -ENOMEM;
759 
760 	for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
761 		*cur = '/';
762 
763 	ret = __parse_events(evlist, dup, /*pmu_filter=*/NULL, error, fake_pmu,
764 			     /*warn_if_reordered=*/true);
765 	free(dup);
766 
767 	evlist__delete(evlist);
768 	return ret;
769 }
770 
771 static int check_parse_fake(const char *id)
772 {
773 	struct parse_events_error error;
774 	int ret;
775 
776 	parse_events_error__init(&error);
777 	ret = check_parse_id(id, &error, &perf_pmu__fake);
778 	parse_events_error__exit(&error);
779 	return ret;
780 }
781 
782 struct metric {
783 	struct list_head list;
784 	struct metric_ref metric_ref;
785 };
786 
787 static int test__parsing_callback(const struct pmu_metric *pm,
788 				  const struct pmu_metrics_table *table,
789 				  void *data)
790 {
791 	int *failures = data;
792 	int k;
793 	struct evlist *evlist;
794 	struct perf_cpu_map *cpus;
795 	struct evsel *evsel;
796 	struct rblist metric_events = {
797 		.nr_entries = 0,
798 	};
799 	int err = 0;
800 
801 	if (!pm->metric_expr)
802 		return 0;
803 
804 	pr_debug("Found metric '%s'\n", pm->metric_name);
805 	(*failures)++;
806 
807 	/*
808 	 * We need to prepare evlist for stat mode running on CPU 0
809 	 * because that's where all the stats are going to be created.
810 	 */
811 	evlist = evlist__new();
812 	if (!evlist)
813 		return -ENOMEM;
814 
815 	cpus = perf_cpu_map__new("0");
816 	if (!cpus) {
817 		evlist__delete(evlist);
818 		return -ENOMEM;
819 	}
820 
821 	perf_evlist__set_maps(&evlist->core, cpus, NULL);
822 
823 	err = metricgroup__parse_groups_test(evlist, table, pm->metric_name, &metric_events);
824 	if (err) {
825 		if (!strcmp(pm->metric_name, "M1") || !strcmp(pm->metric_name, "M2") ||
826 		    !strcmp(pm->metric_name, "M3")) {
827 			(*failures)--;
828 			pr_debug("Expected broken metric %s skipping\n", pm->metric_name);
829 			err = 0;
830 		}
831 		goto out_err;
832 	}
833 
834 	err = evlist__alloc_stats(/*config=*/NULL, evlist, /*alloc_raw=*/false);
835 	if (err)
836 		goto out_err;
837 	/*
838 	 * Add all ids with a made up value. The value may trigger divide by
839 	 * zero when subtracted and so try to make them unique.
840 	 */
841 	k = 1;
842 	evlist__alloc_aggr_stats(evlist, 1);
843 	evlist__for_each_entry(evlist, evsel) {
844 		evsel->stats->aggr->counts.val = k;
845 		if (evsel__name_is(evsel, "duration_time"))
846 			update_stats(&walltime_nsecs_stats, k);
847 		k++;
848 	}
849 	evlist__for_each_entry(evlist, evsel) {
850 		struct metric_event *me = metricgroup__lookup(&metric_events, evsel, false);
851 
852 		if (me != NULL) {
853 			struct metric_expr *mexp;
854 
855 			list_for_each_entry (mexp, &me->head, nd) {
856 				if (strcmp(mexp->metric_name, pm->metric_name))
857 					continue;
858 				pr_debug("Result %f\n", test_generic_metric(mexp, 0));
859 				err = 0;
860 				(*failures)--;
861 				goto out_err;
862 			}
863 		}
864 	}
865 	pr_debug("Didn't find parsed metric %s", pm->metric_name);
866 	err = 1;
867 out_err:
868 	if (err)
869 		pr_debug("Broken metric %s\n", pm->metric_name);
870 
871 	/* ... cleanup. */
872 	metricgroup__rblist_exit(&metric_events);
873 	evlist__free_stats(evlist);
874 	perf_cpu_map__put(cpus);
875 	evlist__delete(evlist);
876 	return err;
877 }
878 
879 static int test__parsing(struct test_suite *test __maybe_unused,
880 			 int subtest __maybe_unused)
881 {
882 	int failures = 0;
883 
884 	pmu_for_each_core_metric(test__parsing_callback, &failures);
885 	pmu_for_each_sys_metric(test__parsing_callback, &failures);
886 
887 	return failures == 0 ? TEST_OK : TEST_FAIL;
888 }
889 
890 struct test_metric {
891 	const char *str;
892 };
893 
894 static struct test_metric metrics[] = {
895 	{ "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
896 	{ "imx8_ddr0@read\\-cycles@ * 4 * 4", },
897 	{ "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
898 	{ "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
899 	{ "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
900 };
901 
902 static int metric_parse_fake(const char *metric_name, const char *str)
903 {
904 	struct expr_parse_ctx *ctx;
905 	struct hashmap_entry *cur;
906 	double result;
907 	int ret = -1;
908 	size_t bkt;
909 	int i;
910 
911 	pr_debug("parsing '%s': '%s'\n", metric_name, str);
912 
913 	ctx = expr__ctx_new();
914 	if (!ctx) {
915 		pr_debug("expr__ctx_new failed");
916 		return TEST_FAIL;
917 	}
918 	ctx->sctx.is_test = true;
919 	if (expr__find_ids(str, NULL, ctx) < 0) {
920 		pr_err("expr__find_ids failed\n");
921 		return -1;
922 	}
923 
924 	/*
925 	 * Add all ids with a made up value. The value may
926 	 * trigger divide by zero when subtracted and so try to
927 	 * make them unique.
928 	 */
929 	i = 1;
930 	hashmap__for_each_entry(ctx->ids, cur, bkt)
931 		expr__add_id_val(ctx, strdup(cur->pkey), i++);
932 
933 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
934 		if (check_parse_fake(cur->pkey)) {
935 			pr_err("check_parse_fake failed\n");
936 			goto out;
937 		}
938 	}
939 
940 	ret = 0;
941 	if (expr__parse(&result, ctx, str)) {
942 		/*
943 		 * Parsing failed, make numbers go from large to small which can
944 		 * resolve divide by zero issues.
945 		 */
946 		i = 1024;
947 		hashmap__for_each_entry(ctx->ids, cur, bkt)
948 			expr__add_id_val(ctx, strdup(cur->pkey), i--);
949 		if (expr__parse(&result, ctx, str)) {
950 			pr_err("expr__parse failed for %s\n", metric_name);
951 			/* The following have hard to avoid divide by zero. */
952 			if (!strcmp(metric_name, "tma_clears_resteers") ||
953 			    !strcmp(metric_name, "tma_mispredicts_resteers"))
954 				ret = 0;
955 			else
956 				ret = -1;
957 		}
958 	}
959 
960 out:
961 	expr__ctx_free(ctx);
962 	return ret;
963 }
964 
965 static int test__parsing_fake_callback(const struct pmu_metric *pm,
966 				       const struct pmu_metrics_table *table __maybe_unused,
967 				       void *data __maybe_unused)
968 {
969 	return metric_parse_fake(pm->metric_name, pm->metric_expr);
970 }
971 
972 /*
973  * Parse all the metrics for current architecture,
974  * or all defined cpus via the 'fake_pmu'
975  * in parse_events.
976  */
977 static int test__parsing_fake(struct test_suite *test __maybe_unused,
978 			      int subtest __maybe_unused)
979 {
980 	int err = 0;
981 
982 	for (size_t i = 0; i < ARRAY_SIZE(metrics); i++) {
983 		err = metric_parse_fake("", metrics[i].str);
984 		if (err)
985 			return err;
986 	}
987 
988 	err = pmu_for_each_core_metric(test__parsing_fake_callback, NULL);
989 	if (err)
990 		return err;
991 
992 	return pmu_for_each_sys_metric(test__parsing_fake_callback, NULL);
993 }
994 
995 static int test__parsing_threshold_callback(const struct pmu_metric *pm,
996 					const struct pmu_metrics_table *table __maybe_unused,
997 					void *data __maybe_unused)
998 {
999 	if (!pm->metric_threshold)
1000 		return 0;
1001 	return metric_parse_fake(pm->metric_name, pm->metric_threshold);
1002 }
1003 
1004 static int test__parsing_threshold(struct test_suite *test __maybe_unused,
1005 			      int subtest __maybe_unused)
1006 {
1007 	int err = 0;
1008 
1009 	err = pmu_for_each_core_metric(test__parsing_threshold_callback, NULL);
1010 	if (err)
1011 		return err;
1012 
1013 	return pmu_for_each_sys_metric(test__parsing_threshold_callback, NULL);
1014 }
1015 
1016 static struct test_case pmu_events_tests[] = {
1017 	TEST_CASE("PMU event table sanity", pmu_event_table),
1018 	TEST_CASE("PMU event map aliases", aliases),
1019 	TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1020 			 "some metrics failed"),
1021 	TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
1022 	TEST_CASE("Parsing of metric thresholds with fake PMUs", parsing_threshold),
1023 	{ .name = NULL, }
1024 };
1025 
1026 struct test_suite suite__pmu_events = {
1027 	.desc = "PMU events",
1028 	.test_cases = pmu_events_tests,
1029 };
1030