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