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(void)
423 const struct pmu_event *sys_event_tables = __test_pmu_get_sys_events_table();
424 const struct pmu_events_map *map = __test_pmu_get_events_map();
425 const struct pmu_event *table;
426 int map_events = 0, expected_events;
428 /* ignore 3x sentinels */
429 expected_events = ARRAY_SIZE(core_events) +
430 ARRAY_SIZE(uncore_events) +
431 ARRAY_SIZE(sys_events) - 3;
433 if (!map || !sys_event_tables)
436 for (table = map->table; table->name; table++) {
437 struct perf_pmu_test_event const **test_event_table;
441 test_event_table = &uncore_events[0];
443 test_event_table = &core_events[0];
445 for (; *test_event_table; test_event_table++) {
446 struct perf_pmu_test_event const *test_event = *test_event_table;
447 struct pmu_event const *event = &test_event->event;
449 if (strcmp(table->name, event->name))
454 if (compare_pmu_events(table, event))
457 pr_debug("testing event table %s: pass\n", table->name);
461 pr_err("testing event table: could not find event %s\n",
467 for (table = sys_event_tables; table->name; table++) {
468 struct perf_pmu_test_event const **test_event_table;
471 test_event_table = &sys_events[0];
473 for (; *test_event_table; test_event_table++) {
474 struct perf_pmu_test_event const *test_event = *test_event_table;
475 struct pmu_event const *event = &test_event->event;
477 if (strcmp(table->name, event->name))
482 if (compare_pmu_events(table, event))
485 pr_debug("testing sys event table %s: pass\n", table->name);
488 pr_debug("testing event table: could not find event %s\n",
494 if (map_events != expected_events) {
495 pr_err("testing event table: found %d, but expected %d\n",
496 map_events, expected_events);
503 static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
505 struct perf_pmu_alias *alias;
507 list_for_each_entry(alias, aliases, list)
508 if (!strcmp(test_event, alias->name))
514 /* Verify aliases are as expected */
515 static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
517 struct perf_pmu_test_event const **test_event_table;
518 struct perf_pmu *pmu;
521 const struct pmu_events_map *map = __test_pmu_get_events_map();
522 struct perf_pmu_alias *a, *tmp;
527 test_event_table = &core_events[0];
529 pmu = zalloc(sizeof(*pmu));
533 pmu->name = pmu_name;
535 pmu_add_cpu_aliases_map(&aliases, pmu, map);
537 for (; *test_event_table; test_event_table++) {
538 struct perf_pmu_test_event const *test_event = *test_event_table;
539 struct pmu_event const *event = &test_event->event;
540 struct perf_pmu_alias *alias = find_alias(event->name, &aliases);
543 pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
544 pmu_name, event->name);
549 if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
555 pr_debug2("testing aliases core PMU %s: matched event %s\n",
556 pmu_name, alias->name);
559 list_for_each_entry_safe(a, tmp, &aliases, list) {
561 perf_pmu_free_alias(a);
567 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
569 int alias_count = 0, to_match_count = 0, matched_count = 0;
570 struct perf_pmu_test_event const **table;
571 struct perf_pmu *pmu = &test_pmu->pmu;
572 const char *pmu_name = pmu->name;
573 struct perf_pmu_alias *a, *tmp, *alias;
574 const struct pmu_events_map *map;
578 map = __test_pmu_get_events_map();
581 pmu_add_cpu_aliases_map(&aliases, pmu, map);
582 pmu_add_sys_aliases(&aliases, pmu);
584 /* Count how many aliases we generated */
585 list_for_each_entry(alias, &aliases, list)
588 /* Count how many aliases we expect from the known table */
589 for (table = &test_pmu->aliases[0]; *table; table++)
592 if (alias_count != to_match_count) {
593 pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
594 pmu_name, to_match_count, alias_count);
599 list_for_each_entry(alias, &aliases, list) {
600 bool matched = false;
602 for (table = &test_pmu->aliases[0]; *table; table++) {
603 struct perf_pmu_test_event const *test_event = *table;
604 struct pmu_event const *event = &test_event->event;
606 if (!strcmp(event->name, alias->name)) {
607 if (compare_alias_to_test_event(alias,
617 if (matched == false) {
618 pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
619 pmu_name, alias->name);
625 if (alias_count != matched_count) {
626 pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
627 pmu_name, matched_count, alias_count);
632 list_for_each_entry_safe(a, tmp, &aliases, list) {
634 perf_pmu_free_alias(a);
639 static struct perf_pmu_test_pmu test_pmus[] = {
642 .name = (char *)"hisi_sccl1_ddrc2",
646 &uncore_hisi_ddrc_flux_wcmd,
651 .name = (char *)"uncore_cbox_0",
655 &unc_cbo_xsnp_response_miss_eviction,
660 .name = (char *)"hisi_sccl3_l3c7",
664 &uncore_hisi_l3c_rd_hit_cpipe,
669 .name = (char *)"uncore_imc_free_running_0",
673 &uncore_imc_free_running_cache_miss,
678 .name = (char *)"uncore_imc_0",
682 &uncore_imc_cache_hits,
687 .name = (char *)"uncore_sys_ddr_pmu0",
692 &sys_ddr_pmu_write_cycles,
697 .name = (char *)"uncore_sys_ccn_pmu4",
699 .id = (char *)"0x01",
702 &sys_ccn_pmu_read_cycles,
707 /* Test that aliases generated are as expected */
708 static int test_aliases(void)
710 struct perf_pmu *pmu = NULL;
713 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
716 if (!is_pmu_core(pmu->name))
719 if (list_empty(&pmu->format)) {
720 pr_debug2("skipping testing core PMU %s\n", pmu->name);
724 if (__test_core_pmu_event_aliases(pmu->name, &count)) {
725 pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
730 pr_debug("testing core PMU %s aliases: no events to match\n",
735 pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
738 for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
739 int res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
748 static bool is_number(const char *str)
754 v = strtod(str, &end_ptr);
755 (void)v; // We're not interested in this value, only if it is valid
756 return errno == 0 && end_ptr != str;
759 static int check_parse_id(const char *id, struct parse_events_error *error,
760 struct perf_pmu *fake_pmu)
762 struct evlist *evlist;
766 /* Numbers are always valid. */
770 evlist = evlist__new();
778 for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
781 ret = __parse_events(evlist, dup, error, fake_pmu);
784 evlist__delete(evlist);
788 static int check_parse_cpu(const char *id, bool same_cpu, const struct pmu_event *pe)
790 struct parse_events_error error;
793 parse_events_error__init(&error);
794 ret = check_parse_id(id, &error, NULL);
795 if (ret && same_cpu) {
796 pr_warning("Parse event failed metric '%s' id '%s' expr '%s'\n",
797 pe->metric_name, id, pe->metric_expr);
798 pr_warning("Error string '%s' help '%s'\n", error.str,
801 pr_debug3("Parse event failed, but for an event that may not be supported by this CPU.\nid '%s' metric '%s' expr '%s'\n",
802 id, pe->metric_name, pe->metric_expr);
805 parse_events_error__exit(&error);
809 static int check_parse_fake(const char *id)
811 struct parse_events_error error;
814 parse_events_error__init(&error);
815 ret = check_parse_id(id, &error, &perf_pmu__fake);
816 parse_events_error__exit(&error);
820 static void expr_failure(const char *msg,
821 const struct pmu_events_map *map,
822 const struct pmu_event *pe)
824 pr_debug("%s for map %s %s %s\n",
825 msg, map->cpuid, map->version, map->type);
826 pr_debug("On metric %s\n", pe->metric_name);
827 pr_debug("On expression %s\n", pe->metric_expr);
831 struct list_head list;
832 struct metric_ref metric_ref;
835 static int resolve_metric_simple(struct expr_parse_ctx *pctx,
836 struct list_head *compound_list,
837 const struct pmu_events_map *map,
838 const char *metric_name)
840 struct hashmap_entry *cur, *cur_tmp;
841 struct metric *metric, *tmp;
848 hashmap__for_each_entry_safe(pctx->ids, cur, cur_tmp, bkt) {
849 struct metric_ref *ref;
850 const struct pmu_event *pe;
852 pe = metricgroup__find_metric(cur->key, map);
856 if (!strcmp(metric_name, (char *)cur->key)) {
857 pr_warning("Recursion detected for metric %s\n", metric_name);
864 /* The metric key itself needs to go out.. */
865 expr__del_id(pctx, cur->key);
867 metric = malloc(sizeof(*metric));
873 ref = &metric->metric_ref;
874 ref->metric_name = pe->metric_name;
875 ref->metric_expr = pe->metric_expr;
876 list_add_tail(&metric->list, compound_list);
878 rc = expr__find_ids(pe->metric_expr, NULL, pctx);
881 break; /* The hashmap has been modified, so restart */
888 list_for_each_entry_safe(metric, tmp, compound_list, list)
895 static int test_parsing(void)
897 const struct pmu_events_map *cpus_map = pmu_events_map__find();
898 const struct pmu_events_map *map;
899 const struct pmu_event *pe;
902 struct expr_parse_ctx *ctx;
905 ctx = expr__ctx_new();
907 pr_debug("expr__ctx_new failed");
912 map = &pmu_events_map[i++];
917 struct metric *metric, *tmp;
918 struct hashmap_entry *cur;
919 LIST_HEAD(compound_list);
922 pe = &map->table[j++];
923 if (!pe->name && !pe->metric_group && !pe->metric_name)
925 if (!pe->metric_expr)
927 expr__ctx_clear(ctx);
928 if (expr__find_ids(pe->metric_expr, NULL, ctx) < 0) {
929 expr_failure("Parse find ids failed", map, pe);
934 if (resolve_metric_simple(ctx, &compound_list, map,
936 expr_failure("Could not resolve metrics", map, pe);
938 goto exit; /* Don't tolerate errors due to severity */
942 * Add all ids with a made up value. The value may
943 * trigger divide by zero when subtracted and so try to
947 hashmap__for_each_entry(ctx->ids, cur, bkt)
948 expr__add_id_val(ctx, strdup(cur->key), k++);
950 hashmap__for_each_entry(ctx->ids, cur, bkt) {
951 if (check_parse_cpu(cur->key, map == cpus_map,
956 list_for_each_entry_safe(metric, tmp, &compound_list, list) {
957 expr__add_ref(ctx, &metric->metric_ref);
961 if (expr__parse(&result, ctx, pe->metric_expr)) {
962 expr_failure("Parse failed", map, pe);
968 /* TODO: fail when not ok */
970 return ret == 0 ? TEST_OK : TEST_SKIP;
977 static struct test_metric metrics[] = {
978 { "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
979 { "imx8_ddr0@read\\-cycles@ * 4 * 4", },
980 { "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
981 { "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
982 { "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
985 static int metric_parse_fake(const char *str)
987 struct expr_parse_ctx *ctx;
988 struct hashmap_entry *cur;
994 pr_debug("parsing '%s'\n", str);
996 ctx = expr__ctx_new();
998 pr_debug("expr__ctx_new failed");
1001 if (expr__find_ids(str, NULL, ctx) < 0) {
1002 pr_err("expr__find_ids failed\n");
1007 * Add all ids with a made up value. The value may
1008 * trigger divide by zero when subtracted and so try to
1012 hashmap__for_each_entry(ctx->ids, cur, bkt)
1013 expr__add_id_val(ctx, strdup(cur->key), i++);
1015 hashmap__for_each_entry(ctx->ids, cur, bkt) {
1016 if (check_parse_fake(cur->key)) {
1017 pr_err("check_parse_fake failed\n");
1022 if (expr__parse(&result, ctx, str))
1023 pr_err("expr__parse failed\n");
1028 expr__ctx_free(ctx);
1033 * Parse all the metrics for current architecture,
1034 * or all defined cpus via the 'fake_pmu'
1037 static int test_parsing_fake(void)
1039 const struct pmu_events_map *map;
1040 const struct pmu_event *pe;
1044 for (i = 0; i < ARRAY_SIZE(metrics); i++) {
1045 err = metric_parse_fake(metrics[i].str);
1052 map = &pmu_events_map[i++];
1057 pe = &map->table[j++];
1058 if (!pe->name && !pe->metric_group && !pe->metric_name)
1060 if (!pe->metric_expr)
1062 err = metric_parse_fake(pe->metric_expr);
1071 static const struct {
1074 } pmu_events_testcase_table[] = {
1076 .func = test_pmu_event_table,
1077 .desc = "PMU event table sanity",
1080 .func = test_aliases,
1081 .desc = "PMU event map aliases",
1084 .func = test_parsing,
1085 .desc = "Parsing of PMU event table metrics",
1088 .func = test_parsing_fake,
1089 .desc = "Parsing of PMU event table metrics with fake PMUs",
1093 static const char *test__pmu_events_subtest_get_desc(int subtest)
1096 subtest >= (int)ARRAY_SIZE(pmu_events_testcase_table))
1098 return pmu_events_testcase_table[subtest].desc;
1101 static const char *test__pmu_events_subtest_skip_reason(int subtest)
1104 subtest >= (int)ARRAY_SIZE(pmu_events_testcase_table))
1106 if (pmu_events_testcase_table[subtest].func != test_parsing)
1108 return "some metrics failed";
1111 static int test__pmu_events_subtest_get_nr(void)
1113 return (int)ARRAY_SIZE(pmu_events_testcase_table);
1116 static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest)
1119 subtest >= (int)ARRAY_SIZE(pmu_events_testcase_table))
1121 return pmu_events_testcase_table[subtest].func();
1124 struct test_suite suite__pmu_events = {
1125 .desc = "PMU events",
1126 .func = test__pmu_events,
1128 .skip_if_fail = false,
1129 .get_nr = test__pmu_events_subtest_get_nr,
1130 .get_desc = test__pmu_events_subtest_get_desc,
1131 .skip_reason = test__pmu_events_subtest_skip_reason,