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 "util/evlist.h"
13 #include "util/expr.h"
14 #include "util/parse-events.h"
15 #include "metricgroup.h"
17 struct perf_pmu_test_event {
18 /* used for matching against events from generated pmu-events.c */
19 struct pmu_event event;
21 /* used for matching against event aliases */
22 /* extra events for aliases */
23 const char *alias_str;
26 * Note: For when PublicDescription does not exist in the JSON, we
27 * will have no long_desc in pmu_event.long_desc, but long_desc may
28 * be set in the alias.
30 const char *alias_long_desc;
32 /* PMU which we should match against */
33 const char *matching_pmu;
36 struct perf_pmu_test_pmu {
38 struct perf_pmu_test_event const *aliases[10];
41 static const struct perf_pmu_test_event bp_l1_btb_correct = {
43 .name = "bp_l1_btb_correct",
44 .event = "event=0x8a",
45 .desc = "L1 BTB Correction",
48 .alias_str = "event=0x8a",
49 .alias_long_desc = "L1 BTB Correction",
52 static const struct perf_pmu_test_event bp_l2_btb_correct = {
54 .name = "bp_l2_btb_correct",
55 .event = "event=0x8b",
56 .desc = "L2 BTB Correction",
59 .alias_str = "event=0x8b",
60 .alias_long_desc = "L2 BTB Correction",
63 static const struct perf_pmu_test_event segment_reg_loads_any = {
65 .name = "segment_reg_loads.any",
66 .event = "umask=0x80,period=200000,event=0x6",
67 .desc = "Number of segment register loads",
70 .alias_str = "umask=0x80,period=0x30d40,event=0x6",
71 .alias_long_desc = "Number of segment register loads",
74 static const struct perf_pmu_test_event dispatch_blocked_any = {
76 .name = "dispatch_blocked.any",
77 .event = "umask=0x20,period=200000,event=0x9",
78 .desc = "Memory cluster signals to block micro-op dispatch for any reason",
81 .alias_str = "umask=0x20,period=0x30d40,event=0x9",
82 .alias_long_desc = "Memory cluster signals to block micro-op dispatch for any reason",
85 static const struct perf_pmu_test_event eist_trans = {
88 .event = "umask=0x0,period=200000,event=0x3a",
89 .desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
92 .alias_str = "umask=0,period=0x30d40,event=0x3a",
93 .alias_long_desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
96 static const struct perf_pmu_test_event l3_cache_rd = {
98 .name = "l3_cache_rd",
99 .event = "event=0x40",
100 .desc = "L3 cache access, read",
101 .long_desc = "Attributable Level 3 cache access, read",
104 .alias_str = "event=0x40",
105 .alias_long_desc = "Attributable Level 3 cache access, read",
108 static const struct perf_pmu_test_event *core_events[] = {
111 &segment_reg_loads_any,
112 &dispatch_blocked_any,
118 static const struct perf_pmu_test_event uncore_hisi_ddrc_flux_wcmd = {
120 .name = "uncore_hisi_ddrc.flux_wcmd",
121 .event = "event=0x2",
122 .desc = "DDRC write commands. Unit: hisi_sccl,ddrc ",
124 .long_desc = "DDRC write commands",
125 .pmu = "hisi_sccl,ddrc",
127 .alias_str = "event=0x2",
128 .alias_long_desc = "DDRC write commands",
129 .matching_pmu = "hisi_sccl1_ddrc2",
132 static const struct perf_pmu_test_event unc_cbo_xsnp_response_miss_eviction = {
134 .name = "unc_cbo_xsnp_response.miss_eviction",
135 .event = "umask=0x81,event=0x22",
136 .desc = "Unit: uncore_cbox A cross-core snoop resulted from L3 Eviction which misses in some processor core",
138 .long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
139 .pmu = "uncore_cbox",
141 .alias_str = "umask=0x81,event=0x22",
142 .alias_long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
143 .matching_pmu = "uncore_cbox_0",
146 static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe = {
148 .name = "uncore_hisi_l3c.rd_hit_cpipe",
149 .event = "event=0x7",
150 .desc = "Total read hits. Unit: hisi_sccl,l3c ",
152 .long_desc = "Total read hits",
153 .pmu = "hisi_sccl,l3c",
155 .alias_str = "event=0x7",
156 .alias_long_desc = "Total read hits",
157 .matching_pmu = "hisi_sccl3_l3c7",
160 static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss = {
162 .name = "uncore_imc_free_running.cache_miss",
163 .event = "event=0x12",
164 .desc = "Total cache misses. Unit: uncore_imc_free_running ",
166 .long_desc = "Total cache misses",
167 .pmu = "uncore_imc_free_running",
169 .alias_str = "event=0x12",
170 .alias_long_desc = "Total cache misses",
171 .matching_pmu = "uncore_imc_free_running_0",
174 static const struct perf_pmu_test_event uncore_imc_cache_hits = {
176 .name = "uncore_imc.cache_hits",
177 .event = "event=0x34",
178 .desc = "Total cache hits. Unit: uncore_imc ",
180 .long_desc = "Total cache hits",
183 .alias_str = "event=0x34",
184 .alias_long_desc = "Total cache hits",
185 .matching_pmu = "uncore_imc_0",
188 static const struct perf_pmu_test_event *uncore_events[] = {
189 &uncore_hisi_ddrc_flux_wcmd,
190 &unc_cbo_xsnp_response_miss_eviction,
191 &uncore_hisi_l3c_rd_hit_cpipe,
192 &uncore_imc_free_running_cache_miss,
193 &uncore_imc_cache_hits,
197 static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles = {
199 .name = "sys_ddr_pmu.write_cycles",
200 .event = "event=0x2b",
201 .desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
203 .pmu = "uncore_sys_ddr_pmu",
206 .alias_str = "event=0x2b",
207 .alias_long_desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
208 .matching_pmu = "uncore_sys_ddr_pmu",
211 static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles = {
213 .name = "sys_ccn_pmu.read_cycles",
214 .event = "config=0x2c",
215 .desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
217 .pmu = "uncore_sys_ccn_pmu",
220 .alias_str = "config=0x2c",
221 .alias_long_desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
222 .matching_pmu = "uncore_sys_ccn_pmu",
225 static const struct perf_pmu_test_event *sys_events[] = {
226 &sys_ddr_pmu_write_cycles,
227 &sys_ccn_pmu_read_cycles,
231 static bool is_same(const char *reference, const char *test)
233 if (!reference && !test)
236 if (reference && !test)
239 if (!reference && test)
242 return !strcmp(reference, test);
245 static const struct pmu_events_map *__test_pmu_get_events_map(void)
247 const struct pmu_events_map *map;
249 for (map = &pmu_events_map[0]; map->cpuid; map++) {
250 if (!strcmp(map->cpuid, "testcpu"))
254 pr_err("could not find test events map\n");
259 static const struct pmu_event *__test_pmu_get_sys_events_table(void)
261 const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
263 for ( ; tables->name; tables++) {
264 if (!strcmp("pme_test_soc_sys", tables->name))
265 return tables->table;
271 static int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
273 if (!is_same(e1->name, e2->name)) {
274 pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
275 e1->name, e1->name, e2->name);
279 if (!is_same(e1->compat, e2->compat)) {
280 pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
281 e1->name, e1->compat, e2->compat);
285 if (!is_same(e1->event, e2->event)) {
286 pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
287 e1->name, e1->event, e2->event);
291 if (!is_same(e1->desc, e2->desc)) {
292 pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
293 e1->name, e1->desc, e2->desc);
297 if (!is_same(e1->topic, e2->topic)) {
298 pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
299 e1->name, e1->topic, e2->topic);
303 if (!is_same(e1->long_desc, e2->long_desc)) {
304 pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
305 e1->name, e1->long_desc, e2->long_desc);
309 if (!is_same(e1->pmu, e2->pmu)) {
310 pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
311 e1->name, e1->pmu, e2->pmu);
315 if (!is_same(e1->unit, e2->unit)) {
316 pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
317 e1->name, e1->unit, e2->unit);
321 if (!is_same(e1->perpkg, e2->perpkg)) {
322 pr_debug2("testing event e1 %s: mismatched perpkg, %s vs %s\n",
323 e1->name, e1->perpkg, e2->perpkg);
327 if (!is_same(e1->aggr_mode, e2->aggr_mode)) {
328 pr_debug2("testing event e1 %s: mismatched aggr_mode, %s vs %s\n",
329 e1->name, e1->aggr_mode, e2->aggr_mode);
333 if (!is_same(e1->metric_expr, e2->metric_expr)) {
334 pr_debug2("testing event e1 %s: mismatched metric_expr, %s vs %s\n",
335 e1->name, e1->metric_expr, e2->metric_expr);
339 if (!is_same(e1->metric_name, e2->metric_name)) {
340 pr_debug2("testing event e1 %s: mismatched metric_name, %s vs %s\n",
341 e1->name, e1->metric_name, e2->metric_name);
345 if (!is_same(e1->metric_group, e2->metric_group)) {
346 pr_debug2("testing event e1 %s: mismatched metric_group, %s vs %s\n",
347 e1->name, e1->metric_group, e2->metric_group);
351 if (!is_same(e1->deprecated, e2->deprecated)) {
352 pr_debug2("testing event e1 %s: mismatched deprecated, %s vs %s\n",
353 e1->name, e1->deprecated, e2->deprecated);
357 if (!is_same(e1->metric_constraint, e2->metric_constraint)) {
358 pr_debug2("testing event e1 %s: mismatched metric_constant, %s vs %s\n",
359 e1->name, e1->metric_constraint, e2->metric_constraint);
366 static int compare_alias_to_test_event(struct perf_pmu_alias *alias,
367 struct perf_pmu_test_event const *test_event,
368 char const *pmu_name)
370 struct pmu_event const *event = &test_event->event;
372 /* An alias was found, ensure everything is in order */
373 if (!is_same(alias->name, event->name)) {
374 pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
375 pmu_name, alias->name, event->name);
379 if (!is_same(alias->desc, event->desc)) {
380 pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
381 pmu_name, alias->desc, event->desc);
385 if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
386 pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
387 pmu_name, alias->long_desc,
388 test_event->alias_long_desc);
392 if (!is_same(alias->topic, event->topic)) {
393 pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
394 pmu_name, alias->topic, event->topic);
398 if (!is_same(alias->str, test_event->alias_str)) {
399 pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
400 pmu_name, alias->str, test_event->alias_str);
404 if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
405 pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
406 pmu_name, alias->str, test_event->alias_long_desc);
411 if (!is_same(alias->pmu_name, test_event->event.pmu)) {
412 pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
413 pmu_name, alias->pmu_name, test_event->event.pmu);
420 /* Verify generated events from pmu-events.c are as expected */
421 static int test__pmu_event_table(struct test_suite *test __maybe_unused,
422 int subtest __maybe_unused)
424 const struct pmu_event *sys_event_tables = __test_pmu_get_sys_events_table();
425 const struct pmu_events_map *map = __test_pmu_get_events_map();
426 const struct pmu_event *table;
427 int map_events = 0, expected_events;
429 /* ignore 3x sentinels */
430 expected_events = ARRAY_SIZE(core_events) +
431 ARRAY_SIZE(uncore_events) +
432 ARRAY_SIZE(sys_events) - 3;
434 if (!map || !sys_event_tables)
437 for (table = map->table; table->name; table++) {
438 struct perf_pmu_test_event const **test_event_table;
442 test_event_table = &uncore_events[0];
444 test_event_table = &core_events[0];
446 for (; *test_event_table; test_event_table++) {
447 struct perf_pmu_test_event const *test_event = *test_event_table;
448 struct pmu_event const *event = &test_event->event;
450 if (strcmp(table->name, event->name))
455 if (compare_pmu_events(table, event))
458 pr_debug("testing event table %s: pass\n", table->name);
462 pr_err("testing event table: could not find event %s\n",
468 for (table = sys_event_tables; table->name; table++) {
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(table->name, event->name))
483 if (compare_pmu_events(table, event))
486 pr_debug("testing sys event table %s: pass\n", table->name);
489 pr_debug("testing event table: could not find event %s\n",
495 if (map_events != expected_events) {
496 pr_err("testing event table: found %d, but expected %d\n",
497 map_events, expected_events);
504 static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
506 struct perf_pmu_alias *alias;
508 list_for_each_entry(alias, aliases, list)
509 if (!strcmp(test_event, alias->name))
515 /* Verify aliases are as expected */
516 static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
518 struct perf_pmu_test_event const **test_event_table;
519 struct perf_pmu *pmu;
522 const struct pmu_events_map *map = __test_pmu_get_events_map();
523 struct perf_pmu_alias *a, *tmp;
528 test_event_table = &core_events[0];
530 pmu = zalloc(sizeof(*pmu));
534 pmu->name = pmu_name;
536 pmu_add_cpu_aliases_map(&aliases, pmu, map);
538 for (; *test_event_table; test_event_table++) {
539 struct perf_pmu_test_event const *test_event = *test_event_table;
540 struct pmu_event const *event = &test_event->event;
541 struct perf_pmu_alias *alias = find_alias(event->name, &aliases);
544 pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
545 pmu_name, event->name);
550 if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
556 pr_debug2("testing aliases core PMU %s: matched event %s\n",
557 pmu_name, alias->name);
560 list_for_each_entry_safe(a, tmp, &aliases, list) {
562 perf_pmu_free_alias(a);
568 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
570 int alias_count = 0, to_match_count = 0, matched_count = 0;
571 struct perf_pmu_test_event const **table;
572 struct perf_pmu *pmu = &test_pmu->pmu;
573 const char *pmu_name = pmu->name;
574 struct perf_pmu_alias *a, *tmp, *alias;
575 const struct pmu_events_map *map;
579 map = __test_pmu_get_events_map();
582 pmu_add_cpu_aliases_map(&aliases, pmu, map);
583 pmu_add_sys_aliases(&aliases, pmu);
585 /* Count how many aliases we generated */
586 list_for_each_entry(alias, &aliases, list)
589 /* Count how many aliases we expect from the known table */
590 for (table = &test_pmu->aliases[0]; *table; table++)
593 if (alias_count != to_match_count) {
594 pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
595 pmu_name, to_match_count, alias_count);
600 list_for_each_entry(alias, &aliases, list) {
601 bool matched = false;
603 for (table = &test_pmu->aliases[0]; *table; table++) {
604 struct perf_pmu_test_event const *test_event = *table;
605 struct pmu_event const *event = &test_event->event;
607 if (!strcmp(event->name, alias->name)) {
608 if (compare_alias_to_test_event(alias,
618 if (matched == false) {
619 pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
620 pmu_name, alias->name);
626 if (alias_count != matched_count) {
627 pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
628 pmu_name, matched_count, alias_count);
633 list_for_each_entry_safe(a, tmp, &aliases, list) {
635 perf_pmu_free_alias(a);
640 static struct perf_pmu_test_pmu test_pmus[] = {
643 .name = (char *)"hisi_sccl1_ddrc2",
647 &uncore_hisi_ddrc_flux_wcmd,
652 .name = (char *)"uncore_cbox_0",
656 &unc_cbo_xsnp_response_miss_eviction,
661 .name = (char *)"hisi_sccl3_l3c7",
665 &uncore_hisi_l3c_rd_hit_cpipe,
670 .name = (char *)"uncore_imc_free_running_0",
674 &uncore_imc_free_running_cache_miss,
679 .name = (char *)"uncore_imc_0",
683 &uncore_imc_cache_hits,
688 .name = (char *)"uncore_sys_ddr_pmu0",
693 &sys_ddr_pmu_write_cycles,
698 .name = (char *)"uncore_sys_ccn_pmu4",
700 .id = (char *)"0x01",
703 &sys_ccn_pmu_read_cycles,
708 /* Test that aliases generated are as expected */
709 static int test__aliases(struct test_suite *test __maybe_unused,
710 int subtest __maybe_unused)
712 struct perf_pmu *pmu = NULL;
715 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
718 if (!is_pmu_core(pmu->name))
721 if (list_empty(&pmu->format)) {
722 pr_debug2("skipping testing core PMU %s\n", pmu->name);
726 if (__test_core_pmu_event_aliases(pmu->name, &count)) {
727 pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
732 pr_debug("testing core PMU %s aliases: no events to match\n",
737 pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
740 for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
741 int res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
750 static bool is_number(const char *str)
756 v = strtod(str, &end_ptr);
757 (void)v; // We're not interested in this value, only if it is valid
758 return errno == 0 && end_ptr != str;
761 static int check_parse_id(const char *id, struct parse_events_error *error,
762 struct perf_pmu *fake_pmu)
764 struct evlist *evlist;
768 /* Numbers are always valid. */
772 evlist = evlist__new();
780 for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
783 ret = __parse_events(evlist, dup, error, fake_pmu);
786 evlist__delete(evlist);
790 static int check_parse_cpu(const char *id, bool same_cpu, const struct pmu_event *pe)
792 struct parse_events_error error;
795 parse_events_error__init(&error);
796 ret = check_parse_id(id, &error, NULL);
797 if (ret && same_cpu) {
798 pr_warning("Parse event failed metric '%s' id '%s' expr '%s'\n",
799 pe->metric_name, id, pe->metric_expr);
800 pr_warning("Error string '%s' help '%s'\n", error.str,
803 pr_debug3("Parse event failed, but for an event that may not be supported by this CPU.\nid '%s' metric '%s' expr '%s'\n",
804 id, pe->metric_name, pe->metric_expr);
807 parse_events_error__exit(&error);
811 static int check_parse_fake(const char *id)
813 struct parse_events_error error;
816 parse_events_error__init(&error);
817 ret = check_parse_id(id, &error, &perf_pmu__fake);
818 parse_events_error__exit(&error);
822 static void expr_failure(const char *msg,
823 const struct pmu_events_map *map,
824 const struct pmu_event *pe)
826 pr_debug("%s for map %s %s %s\n",
827 msg, map->cpuid, map->version, map->type);
828 pr_debug("On metric %s\n", pe->metric_name);
829 pr_debug("On expression %s\n", pe->metric_expr);
833 struct list_head list;
834 struct metric_ref metric_ref;
837 static int resolve_metric_simple(struct expr_parse_ctx *pctx,
838 struct list_head *compound_list,
839 const struct pmu_events_map *map,
840 const char *metric_name)
842 struct hashmap_entry *cur, *cur_tmp;
843 struct metric *metric, *tmp;
850 hashmap__for_each_entry_safe(pctx->ids, cur, cur_tmp, bkt) {
851 struct metric_ref *ref;
852 const struct pmu_event *pe;
854 pe = metricgroup__find_metric(cur->key, map);
858 if (!strcmp(metric_name, (char *)cur->key)) {
859 pr_warning("Recursion detected for metric %s\n", metric_name);
866 /* The metric key itself needs to go out.. */
867 expr__del_id(pctx, cur->key);
869 metric = malloc(sizeof(*metric));
875 ref = &metric->metric_ref;
876 ref->metric_name = pe->metric_name;
877 ref->metric_expr = pe->metric_expr;
878 list_add_tail(&metric->list, compound_list);
880 rc = expr__find_ids(pe->metric_expr, NULL, pctx);
883 break; /* The hashmap has been modified, so restart */
890 list_for_each_entry_safe(metric, tmp, compound_list, list)
897 static int test__parsing(struct test_suite *test __maybe_unused,
898 int subtest __maybe_unused)
900 const struct pmu_events_map *cpus_map = pmu_events_map__find();
901 const struct pmu_events_map *map;
902 const struct pmu_event *pe;
905 struct expr_parse_ctx *ctx;
908 ctx = expr__ctx_new();
910 pr_debug("expr__ctx_new failed");
915 map = &pmu_events_map[i++];
920 struct metric *metric, *tmp;
921 struct hashmap_entry *cur;
922 LIST_HEAD(compound_list);
925 pe = &map->table[j++];
926 if (!pe->name && !pe->metric_group && !pe->metric_name)
928 if (!pe->metric_expr)
930 expr__ctx_clear(ctx);
931 if (expr__find_ids(pe->metric_expr, NULL, ctx) < 0) {
932 expr_failure("Parse find ids failed", map, pe);
937 if (resolve_metric_simple(ctx, &compound_list, map,
939 expr_failure("Could not resolve metrics", map, pe);
941 goto exit; /* Don't tolerate errors due to severity */
945 * Add all ids with a made up value. The value may
946 * trigger divide by zero when subtracted and so try to
950 hashmap__for_each_entry(ctx->ids, cur, bkt)
951 expr__add_id_val(ctx, strdup(cur->key), k++);
953 hashmap__for_each_entry(ctx->ids, cur, bkt) {
954 if (check_parse_cpu(cur->key, map == cpus_map,
959 list_for_each_entry_safe(metric, tmp, &compound_list, list) {
960 expr__add_ref(ctx, &metric->metric_ref);
964 if (expr__parse(&result, ctx, pe->metric_expr)) {
965 expr_failure("Parse failed", map, pe);
971 /* TODO: fail when not ok */
973 return ret == 0 ? TEST_OK : TEST_SKIP;
980 static struct test_metric metrics[] = {
981 { "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
982 { "imx8_ddr0@read\\-cycles@ * 4 * 4", },
983 { "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
984 { "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
985 { "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
988 static int metric_parse_fake(const char *str)
990 struct expr_parse_ctx *ctx;
991 struct hashmap_entry *cur;
997 pr_debug("parsing '%s'\n", str);
999 ctx = expr__ctx_new();
1001 pr_debug("expr__ctx_new failed");
1004 if (expr__find_ids(str, NULL, ctx) < 0) {
1005 pr_err("expr__find_ids failed\n");
1010 * Add all ids with a made up value. The value may
1011 * trigger divide by zero when subtracted and so try to
1015 hashmap__for_each_entry(ctx->ids, cur, bkt)
1016 expr__add_id_val(ctx, strdup(cur->key), i++);
1018 hashmap__for_each_entry(ctx->ids, cur, bkt) {
1019 if (check_parse_fake(cur->key)) {
1020 pr_err("check_parse_fake failed\n");
1025 if (expr__parse(&result, ctx, str))
1026 pr_err("expr__parse failed\n");
1031 expr__ctx_free(ctx);
1036 * Parse all the metrics for current architecture,
1037 * or all defined cpus via the 'fake_pmu'
1040 static int test__parsing_fake(struct test_suite *test __maybe_unused,
1041 int subtest __maybe_unused)
1043 const struct pmu_events_map *map;
1044 const struct pmu_event *pe;
1048 for (i = 0; i < ARRAY_SIZE(metrics); i++) {
1049 err = metric_parse_fake(metrics[i].str);
1056 map = &pmu_events_map[i++];
1061 pe = &map->table[j++];
1062 if (!pe->name && !pe->metric_group && !pe->metric_name)
1064 if (!pe->metric_expr)
1066 err = metric_parse_fake(pe->metric_expr);
1075 static struct test_case pmu_events_tests[] = {
1076 TEST_CASE("PMU event table sanity", pmu_event_table),
1077 TEST_CASE("PMU event map aliases", aliases),
1078 TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1079 "some metrics failed"),
1080 TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
1084 struct test_suite suite__pmu_events = {
1085 .desc = "PMU events",
1086 .test_cases = pmu_events_tests,