1 // SPDX-License-Identifier: GPL-2.0
3 #include "parse-events.h"
8 #include <linux/kernel.h>
9 #include <linux/zalloc.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"
20 struct perf_pmu_test_event {
21 /* used for matching against events from generated pmu-events.c */
22 struct pmu_event event;
24 /* used for matching against event aliases */
25 /* extra events for aliases */
26 const char *alias_str;
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.
33 const char *alias_long_desc;
35 /* PMU which we should match against */
36 const char *matching_pmu;
39 struct perf_pmu_test_pmu {
41 struct perf_pmu_test_event const *aliases[10];
44 static const struct perf_pmu_test_event bp_l1_btb_correct = {
46 .name = "bp_l1_btb_correct",
47 .event = "event=0x8a",
48 .desc = "L1 BTB Correction",
51 .alias_str = "event=0x8a",
52 .alias_long_desc = "L1 BTB Correction",
55 static const struct perf_pmu_test_event bp_l2_btb_correct = {
57 .name = "bp_l2_btb_correct",
58 .event = "event=0x8b",
59 .desc = "L2 BTB Correction",
62 .alias_str = "event=0x8b",
63 .alias_long_desc = "L2 BTB Correction",
66 static const struct perf_pmu_test_event segment_reg_loads_any = {
68 .name = "segment_reg_loads.any",
69 .event = "event=0x6,period=200000,umask=0x80",
70 .desc = "Number of segment register loads",
73 .alias_str = "event=0x6,period=0x30d40,umask=0x80",
74 .alias_long_desc = "Number of segment register loads",
77 static const struct perf_pmu_test_event dispatch_blocked_any = {
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",
84 .alias_str = "event=0x9,period=0x30d40,umask=0x20",
85 .alias_long_desc = "Memory cluster signals to block micro-op dispatch for any reason",
88 static const struct perf_pmu_test_event eist_trans = {
91 .event = "event=0x3a,period=200000,umask=0x0",
92 .desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
95 .alias_str = "event=0x3a,period=0x30d40,umask=0",
96 .alias_long_desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
99 static const struct perf_pmu_test_event l3_cache_rd = {
101 .name = "l3_cache_rd",
102 .event = "event=0x40",
103 .desc = "L3 cache access, read",
104 .long_desc = "Attributable Level 3 cache access, read",
107 .alias_str = "event=0x40",
108 .alias_long_desc = "Attributable Level 3 cache access, read",
111 static const struct perf_pmu_test_event *core_events[] = {
114 &segment_reg_loads_any,
115 &dispatch_blocked_any,
121 static const struct perf_pmu_test_event uncore_hisi_ddrc_flux_wcmd = {
123 .name = "uncore_hisi_ddrc.flux_wcmd",
124 .event = "event=0x2",
125 .desc = "DDRC write commands. Unit: hisi_sccl,ddrc ",
127 .long_desc = "DDRC write commands",
128 .pmu = "hisi_sccl,ddrc",
130 .alias_str = "event=0x2",
131 .alias_long_desc = "DDRC write commands",
132 .matching_pmu = "hisi_sccl1_ddrc2",
135 static const struct perf_pmu_test_event unc_cbo_xsnp_response_miss_eviction = {
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 ",
141 .long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
142 .pmu = "uncore_cbox",
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",
149 static const struct perf_pmu_test_event uncore_hyphen = {
151 .name = "event-hyphen",
152 .event = "event=0xe0,umask=0x00",
153 .desc = "UNC_CBO_HYPHEN. Unit: uncore_cbox ",
155 .long_desc = "UNC_CBO_HYPHEN",
156 .pmu = "uncore_cbox",
158 .alias_str = "event=0xe0,umask=0",
159 .alias_long_desc = "UNC_CBO_HYPHEN",
160 .matching_pmu = "uncore_cbox_0",
163 static const struct perf_pmu_test_event uncore_two_hyph = {
165 .name = "event-two-hyph",
166 .event = "event=0xc0,umask=0x00",
167 .desc = "UNC_CBO_TWO_HYPH. Unit: uncore_cbox ",
169 .long_desc = "UNC_CBO_TWO_HYPH",
170 .pmu = "uncore_cbox",
172 .alias_str = "event=0xc0,umask=0",
173 .alias_long_desc = "UNC_CBO_TWO_HYPH",
174 .matching_pmu = "uncore_cbox_0",
177 static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe = {
179 .name = "uncore_hisi_l3c.rd_hit_cpipe",
180 .event = "event=0x7",
181 .desc = "Total read hits. Unit: hisi_sccl,l3c ",
183 .long_desc = "Total read hits",
184 .pmu = "hisi_sccl,l3c",
186 .alias_str = "event=0x7",
187 .alias_long_desc = "Total read hits",
188 .matching_pmu = "hisi_sccl3_l3c7",
191 static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss = {
193 .name = "uncore_imc_free_running.cache_miss",
194 .event = "event=0x12",
195 .desc = "Total cache misses. Unit: uncore_imc_free_running ",
197 .long_desc = "Total cache misses",
198 .pmu = "uncore_imc_free_running",
200 .alias_str = "event=0x12",
201 .alias_long_desc = "Total cache misses",
202 .matching_pmu = "uncore_imc_free_running_0",
205 static const struct perf_pmu_test_event uncore_imc_cache_hits = {
207 .name = "uncore_imc.cache_hits",
208 .event = "event=0x34",
209 .desc = "Total cache hits. Unit: uncore_imc ",
211 .long_desc = "Total cache hits",
214 .alias_str = "event=0x34",
215 .alias_long_desc = "Total cache hits",
216 .matching_pmu = "uncore_imc_0",
219 static const struct perf_pmu_test_event *uncore_events[] = {
220 &uncore_hisi_ddrc_flux_wcmd,
221 &unc_cbo_xsnp_response_miss_eviction,
224 &uncore_hisi_l3c_rd_hit_cpipe,
225 &uncore_imc_free_running_cache_miss,
226 &uncore_imc_cache_hits,
230 static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles = {
232 .name = "sys_ddr_pmu.write_cycles",
233 .event = "event=0x2b",
234 .desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
236 .pmu = "uncore_sys_ddr_pmu",
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",
244 static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles = {
246 .name = "sys_ccn_pmu.read_cycles",
247 .event = "config=0x2c",
248 .desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
250 .pmu = "uncore_sys_ccn_pmu",
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",
258 static const struct perf_pmu_test_event *sys_events[] = {
259 &sys_ddr_pmu_write_cycles,
260 &sys_ccn_pmu_read_cycles,
264 static bool is_same(const char *reference, const char *test)
266 if (!reference && !test)
269 if (reference && !test)
272 if (!reference && test)
275 return !strcmp(reference, test);
278 static int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
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);
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);
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);
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);
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);
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);
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);
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);
328 if (!is_same(e1->perpkg, e2->perpkg)) {
329 pr_debug2("testing event e1 %s: mismatched perpkg, %s vs %s\n",
330 e1->name, e1->perpkg, e2->perpkg);
334 if (!is_same(e1->aggr_mode, e2->aggr_mode)) {
335 pr_debug2("testing event e1 %s: mismatched aggr_mode, %s vs %s\n",
336 e1->name, e1->aggr_mode, e2->aggr_mode);
340 if (!is_same(e1->metric_expr, e2->metric_expr)) {
341 pr_debug2("testing event e1 %s: mismatched metric_expr, %s vs %s\n",
342 e1->name, e1->metric_expr, e2->metric_expr);
346 if (!is_same(e1->metric_name, e2->metric_name)) {
347 pr_debug2("testing event e1 %s: mismatched metric_name, %s vs %s\n",
348 e1->name, e1->metric_name, e2->metric_name);
352 if (!is_same(e1->metric_group, e2->metric_group)) {
353 pr_debug2("testing event e1 %s: mismatched metric_group, %s vs %s\n",
354 e1->name, e1->metric_group, e2->metric_group);
358 if (!is_same(e1->deprecated, e2->deprecated)) {
359 pr_debug2("testing event e1 %s: mismatched deprecated, %s vs %s\n",
360 e1->name, e1->deprecated, e2->deprecated);
364 if (!is_same(e1->metric_constraint, e2->metric_constraint)) {
365 pr_debug2("testing event e1 %s: mismatched metric_constant, %s vs %s\n",
366 e1->name, e1->metric_constraint, e2->metric_constraint);
373 static int compare_alias_to_test_event(struct perf_pmu_alias *alias,
374 struct perf_pmu_test_event const *test_event,
375 char const *pmu_name)
377 struct pmu_event const *event = &test_event->event;
379 /* An alias was found, ensure everything is in order */
380 if (!is_same(alias->name, event->name)) {
381 pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
382 pmu_name, alias->name, event->name);
386 if (!is_same(alias->desc, event->desc)) {
387 pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
388 pmu_name, alias->desc, event->desc);
392 if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
393 pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
394 pmu_name, alias->long_desc,
395 test_event->alias_long_desc);
399 if (!is_same(alias->topic, event->topic)) {
400 pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
401 pmu_name, alias->topic, event->topic);
405 if (!is_same(alias->str, test_event->alias_str)) {
406 pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
407 pmu_name, alias->str, test_event->alias_str);
411 if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
412 pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
413 pmu_name, alias->str, test_event->alias_long_desc);
418 if (!is_same(alias->pmu_name, test_event->event.pmu)) {
419 pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
420 pmu_name, alias->pmu_name, test_event->event.pmu);
427 static int test__pmu_event_table_core_callback(const struct pmu_event *pe,
428 const struct pmu_events_table *table __maybe_unused,
431 int *map_events = data;
432 struct perf_pmu_test_event const **test_event_table;
439 test_event_table = &uncore_events[0];
441 test_event_table = &core_events[0];
443 for (; *test_event_table; test_event_table++) {
444 struct perf_pmu_test_event const *test_event = *test_event_table;
445 struct pmu_event const *event = &test_event->event;
447 if (strcmp(pe->name, event->name))
452 if (compare_pmu_events(pe, event))
455 pr_debug("testing event table %s: pass\n", pe->name);
458 pr_err("testing event table: could not find event %s\n", pe->name);
464 static int test__pmu_event_table_sys_callback(const struct pmu_event *pe,
465 const struct pmu_events_table *table __maybe_unused,
468 int *map_events = data;
469 struct perf_pmu_test_event const **test_event_table;
472 test_event_table = &sys_events[0];
474 for (; *test_event_table; test_event_table++) {
475 struct perf_pmu_test_event const *test_event = *test_event_table;
476 struct pmu_event const *event = &test_event->event;
478 if (strcmp(pe->name, event->name))
483 if (compare_pmu_events(pe, event))
486 pr_debug("testing sys event table %s: pass\n", pe->name);
489 pr_debug("testing sys event table: could not find event %s\n", pe->name);
495 /* Verify generated events from pmu-events.c are as expected */
496 static int test__pmu_event_table(struct test_suite *test __maybe_unused,
497 int subtest __maybe_unused)
499 const struct pmu_events_table *sys_event_table = find_sys_events_table("pme_test_soc_sys");
500 const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
501 int map_events = 0, expected_events, err;
503 /* ignore 3x sentinels */
504 expected_events = ARRAY_SIZE(core_events) +
505 ARRAY_SIZE(uncore_events) +
506 ARRAY_SIZE(sys_events) - 3;
508 if (!table || !sys_event_table)
511 err = pmu_events_table_for_each_event(table, test__pmu_event_table_core_callback,
516 err = pmu_events_table_for_each_event(sys_event_table, test__pmu_event_table_sys_callback,
521 if (map_events != expected_events) {
522 pr_err("testing event table: found %d, but expected %d\n",
523 map_events, expected_events);
530 static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
532 struct perf_pmu_alias *alias;
534 list_for_each_entry(alias, aliases, list)
535 if (!strcmp(test_event, alias->name))
541 /* Verify aliases are as expected */
542 static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
544 struct perf_pmu_test_event const **test_event_table;
545 struct perf_pmu *pmu;
548 const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
549 struct perf_pmu_alias *a, *tmp;
554 test_event_table = &core_events[0];
556 pmu = zalloc(sizeof(*pmu));
560 pmu->name = pmu_name;
562 pmu_add_cpu_aliases_table(&aliases, pmu, table);
564 for (; *test_event_table; test_event_table++) {
565 struct perf_pmu_test_event const *test_event = *test_event_table;
566 struct pmu_event const *event = &test_event->event;
567 struct perf_pmu_alias *alias = find_alias(event->name, &aliases);
570 pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
571 pmu_name, event->name);
576 if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
582 pr_debug2("testing aliases core PMU %s: matched event %s\n",
583 pmu_name, alias->name);
586 list_for_each_entry_safe(a, tmp, &aliases, list) {
588 perf_pmu_free_alias(a);
594 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
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 = &test_pmu->pmu;
599 const char *pmu_name = pmu->name;
600 struct perf_pmu_alias *a, *tmp, *alias;
601 const struct pmu_events_table *events_table;
605 events_table = find_core_events_table("testarch", "testcpu");
608 pmu_add_cpu_aliases_table(&aliases, pmu, events_table);
609 pmu_add_sys_aliases(&aliases, pmu);
611 /* Count how many aliases we generated */
612 list_for_each_entry(alias, &aliases, list)
615 /* Count how many aliases we expect from the known table */
616 for (table = &test_pmu->aliases[0]; *table; table++)
619 if (alias_count != to_match_count) {
620 pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
621 pmu_name, to_match_count, alias_count);
626 list_for_each_entry(alias, &aliases, list) {
627 bool matched = false;
629 for (table = &test_pmu->aliases[0]; *table; table++) {
630 struct perf_pmu_test_event const *test_event = *table;
631 struct pmu_event const *event = &test_event->event;
633 if (!strcmp(event->name, alias->name)) {
634 if (compare_alias_to_test_event(alias,
644 if (matched == false) {
645 pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
646 pmu_name, alias->name);
652 if (alias_count != matched_count) {
653 pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
654 pmu_name, matched_count, alias_count);
659 list_for_each_entry_safe(a, tmp, &aliases, list) {
661 perf_pmu_free_alias(a);
666 static struct perf_pmu_test_pmu test_pmus[] = {
669 .name = (char *)"hisi_sccl1_ddrc2",
673 &uncore_hisi_ddrc_flux_wcmd,
678 .name = (char *)"uncore_cbox_0",
682 &unc_cbo_xsnp_response_miss_eviction,
689 .name = (char *)"hisi_sccl3_l3c7",
693 &uncore_hisi_l3c_rd_hit_cpipe,
698 .name = (char *)"uncore_imc_free_running_0",
702 &uncore_imc_free_running_cache_miss,
707 .name = (char *)"uncore_imc_0",
711 &uncore_imc_cache_hits,
716 .name = (char *)"uncore_sys_ddr_pmu0",
721 &sys_ddr_pmu_write_cycles,
726 .name = (char *)"uncore_sys_ccn_pmu4",
728 .id = (char *)"0x01",
731 &sys_ccn_pmu_read_cycles,
736 /* Test that aliases generated are as expected */
737 static int test__aliases(struct test_suite *test __maybe_unused,
738 int subtest __maybe_unused)
740 struct perf_pmu *pmu = NULL;
743 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
746 if (!is_pmu_core(pmu->name))
749 if (list_empty(&pmu->format)) {
750 pr_debug2("skipping testing core PMU %s\n", pmu->name);
754 if (__test_core_pmu_event_aliases(pmu->name, &count)) {
755 pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
760 pr_debug("testing core PMU %s aliases: no events to match\n",
765 pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
768 for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
769 int res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
778 static bool is_number(const char *str)
784 v = strtod(str, &end_ptr);
785 (void)v; // We're not interested in this value, only if it is valid
786 return errno == 0 && end_ptr != str;
789 static int check_parse_id(const char *id, struct parse_events_error *error,
790 struct perf_pmu *fake_pmu)
792 struct evlist *evlist;
796 /* Numbers are always valid. */
800 evlist = evlist__new();
808 for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
813 * Every call to __parse_events will try to initialize the PMU
814 * state from sysfs and then clean it up at the end. Reset the
815 * PMU events to the test state so that we don't pick up
816 * erroneous prefixes and suffixes.
818 perf_pmu__test_parse_init();
820 ret = __parse_events(evlist, dup, error, fake_pmu);
823 evlist__delete(evlist);
827 static int check_parse_fake(const char *id)
829 struct parse_events_error error;
832 parse_events_error__init(&error);
833 ret = check_parse_id(id, &error, &perf_pmu__fake);
834 parse_events_error__exit(&error);
839 struct list_head list;
840 struct metric_ref metric_ref;
843 static int test__parsing_callback(const struct pmu_event *pe, const struct pmu_events_table *table,
846 int *failures = data;
848 struct evlist *evlist;
849 struct perf_cpu_map *cpus;
850 struct runtime_stat st;
852 struct rblist metric_events = {
857 if (!pe->metric_expr)
860 pr_debug("Found metric '%s'\n", pe->metric_name);
864 * We need to prepare evlist for stat mode running on CPU 0
865 * because that's where all the stats are going to be created.
867 evlist = evlist__new();
871 cpus = perf_cpu_map__new("0");
873 evlist__delete(evlist);
877 perf_evlist__set_maps(&evlist->core, cpus, NULL);
878 runtime_stat__init(&st);
880 err = metricgroup__parse_groups_test(evlist, table, pe->metric_name,
884 if (!strcmp(pe->metric_name, "M1") || !strcmp(pe->metric_name, "M2") ||
885 !strcmp(pe->metric_name, "M3")) {
887 pr_debug("Expected broken metric %s skipping\n", pe->metric_name);
893 err = evlist__alloc_stats(/*config=*/NULL, evlist, /*alloc_raw=*/false);
897 * Add all ids with a made up value. The value may trigger divide by
898 * zero when subtracted and so try to make them unique.
901 perf_stat__reset_shadow_stats();
902 evlist__for_each_entry(evlist, evsel) {
903 perf_stat__update_shadow_stats(evsel, k, 0, &st);
904 if (!strcmp(evsel->name, "duration_time"))
905 update_stats(&walltime_nsecs_stats, k);
908 evlist__for_each_entry(evlist, evsel) {
909 struct metric_event *me = metricgroup__lookup(&metric_events, evsel, false);
912 struct metric_expr *mexp;
914 list_for_each_entry (mexp, &me->head, nd) {
915 if (strcmp(mexp->metric_name, pe->metric_name))
917 pr_debug("Result %f\n", test_generic_metric(mexp, 0, &st));
924 pr_debug("Didn't find parsed metric %s", pe->metric_name);
928 pr_debug("Broken metric %s\n", pe->metric_name);
931 metricgroup__rblist_exit(&metric_events);
932 runtime_stat__exit(&st);
933 evlist__free_stats(evlist);
934 perf_cpu_map__put(cpus);
935 evlist__delete(evlist);
939 static int test__parsing(struct test_suite *test __maybe_unused,
940 int subtest __maybe_unused)
944 pmu_for_each_core_event(test__parsing_callback, &failures);
945 pmu_for_each_sys_event(test__parsing_callback, &failures);
947 return failures == 0 ? TEST_OK : TEST_FAIL;
954 static struct test_metric metrics[] = {
955 { "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
956 { "imx8_ddr0@read\\-cycles@ * 4 * 4", },
957 { "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
958 { "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
959 { "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
962 static int metric_parse_fake(const char *metric_name, const char *str)
964 struct expr_parse_ctx *ctx;
965 struct hashmap_entry *cur;
971 pr_debug("parsing '%s': '%s'\n", metric_name, str);
973 ctx = expr__ctx_new();
975 pr_debug("expr__ctx_new failed");
978 if (expr__find_ids(str, NULL, ctx) < 0) {
979 pr_err("expr__find_ids failed\n");
984 * Add all ids with a made up value. The value may
985 * trigger divide by zero when subtracted and so try to
989 hashmap__for_each_entry(ctx->ids, cur, bkt)
990 expr__add_id_val(ctx, strdup(cur->pkey), i++);
992 hashmap__for_each_entry(ctx->ids, cur, bkt) {
993 if (check_parse_fake(cur->pkey)) {
994 pr_err("check_parse_fake failed\n");
1000 if (expr__parse(&result, ctx, str)) {
1002 * Parsing failed, make numbers go from large to small which can
1003 * resolve divide by zero issues.
1006 hashmap__for_each_entry(ctx->ids, cur, bkt)
1007 expr__add_id_val(ctx, strdup(cur->pkey), i--);
1008 if (expr__parse(&result, ctx, str)) {
1009 pr_err("expr__parse failed for %s\n", metric_name);
1010 /* The following have hard to avoid divide by zero. */
1011 if (!strcmp(metric_name, "tma_clears_resteers") ||
1012 !strcmp(metric_name, "tma_mispredicts_resteers"))
1020 expr__ctx_free(ctx);
1024 static int test__parsing_fake_callback(const struct pmu_event *pe,
1025 const struct pmu_events_table *table __maybe_unused,
1026 void *data __maybe_unused)
1028 if (!pe->metric_expr)
1031 return metric_parse_fake(pe->metric_name, pe->metric_expr);
1035 * Parse all the metrics for current architecture,
1036 * or all defined cpus via the 'fake_pmu'
1039 static int test__parsing_fake(struct test_suite *test __maybe_unused,
1040 int subtest __maybe_unused)
1044 for (size_t i = 0; i < ARRAY_SIZE(metrics); i++) {
1045 err = metric_parse_fake("", metrics[i].str);
1050 err = pmu_for_each_core_event(test__parsing_fake_callback, NULL);
1054 return pmu_for_each_sys_event(test__parsing_fake_callback, NULL);
1057 static struct test_case pmu_events_tests[] = {
1058 TEST_CASE("PMU event table sanity", pmu_event_table),
1059 TEST_CASE("PMU event map aliases", aliases),
1060 TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1061 "some metrics failed"),
1062 TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
1066 struct test_suite suite__pmu_events = {
1067 .desc = "PMU events",
1068 .test_cases = pmu_events_tests,