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