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