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 = "event=0x6,period=200000,umask=0x80",
67 .desc = "Number of segment register loads",
70 .alias_str = "event=0x6,period=0x30d40,umask=0x80",
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 = "event=0x9,period=200000,umask=0x20",
78 .desc = "Memory cluster signals to block micro-op dispatch for any reason",
81 .alias_str = "event=0x9,period=0x30d40,umask=0x20",
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 = "event=0x3a,period=200000,umask=0x0",
89 .desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
92 .alias_str = "event=0x3a,period=0x30d40,umask=0",
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 = "event=0x22,umask=0x81",
136 .desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core. Unit: uncore_cbox ",
138 .long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
139 .pmu = "uncore_cbox",
141 .alias_str = "event=0x22,umask=0x81",
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_hyphen = {
148 .name = "event-hyphen",
149 .event = "event=0xe0,umask=0x00",
150 .desc = "UNC_CBO_HYPHEN. Unit: uncore_cbox ",
152 .long_desc = "UNC_CBO_HYPHEN",
153 .pmu = "uncore_cbox",
155 .alias_str = "event=0xe0,umask=0",
156 .alias_long_desc = "UNC_CBO_HYPHEN",
157 .matching_pmu = "uncore_cbox_0",
160 static const struct perf_pmu_test_event uncore_two_hyph = {
162 .name = "event-two-hyph",
163 .event = "event=0xc0,umask=0x00",
164 .desc = "UNC_CBO_TWO_HYPH. Unit: uncore_cbox ",
166 .long_desc = "UNC_CBO_TWO_HYPH",
167 .pmu = "uncore_cbox",
169 .alias_str = "event=0xc0,umask=0",
170 .alias_long_desc = "UNC_CBO_TWO_HYPH",
171 .matching_pmu = "uncore_cbox_0",
174 static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe = {
176 .name = "uncore_hisi_l3c.rd_hit_cpipe",
177 .event = "event=0x7",
178 .desc = "Total read hits. Unit: hisi_sccl,l3c ",
180 .long_desc = "Total read hits",
181 .pmu = "hisi_sccl,l3c",
183 .alias_str = "event=0x7",
184 .alias_long_desc = "Total read hits",
185 .matching_pmu = "hisi_sccl3_l3c7",
188 static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss = {
190 .name = "uncore_imc_free_running.cache_miss",
191 .event = "event=0x12",
192 .desc = "Total cache misses. Unit: uncore_imc_free_running ",
194 .long_desc = "Total cache misses",
195 .pmu = "uncore_imc_free_running",
197 .alias_str = "event=0x12",
198 .alias_long_desc = "Total cache misses",
199 .matching_pmu = "uncore_imc_free_running_0",
202 static const struct perf_pmu_test_event uncore_imc_cache_hits = {
204 .name = "uncore_imc.cache_hits",
205 .event = "event=0x34",
206 .desc = "Total cache hits. Unit: uncore_imc ",
208 .long_desc = "Total cache hits",
211 .alias_str = "event=0x34",
212 .alias_long_desc = "Total cache hits",
213 .matching_pmu = "uncore_imc_0",
216 static const struct perf_pmu_test_event *uncore_events[] = {
217 &uncore_hisi_ddrc_flux_wcmd,
218 &unc_cbo_xsnp_response_miss_eviction,
221 &uncore_hisi_l3c_rd_hit_cpipe,
222 &uncore_imc_free_running_cache_miss,
223 &uncore_imc_cache_hits,
227 static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles = {
229 .name = "sys_ddr_pmu.write_cycles",
230 .event = "event=0x2b",
231 .desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
233 .pmu = "uncore_sys_ddr_pmu",
236 .alias_str = "event=0x2b",
237 .alias_long_desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
238 .matching_pmu = "uncore_sys_ddr_pmu",
241 static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles = {
243 .name = "sys_ccn_pmu.read_cycles",
244 .event = "config=0x2c",
245 .desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
247 .pmu = "uncore_sys_ccn_pmu",
250 .alias_str = "config=0x2c",
251 .alias_long_desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
252 .matching_pmu = "uncore_sys_ccn_pmu",
255 static const struct perf_pmu_test_event *sys_events[] = {
256 &sys_ddr_pmu_write_cycles,
257 &sys_ccn_pmu_read_cycles,
261 static bool is_same(const char *reference, const char *test)
263 if (!reference && !test)
266 if (reference && !test)
269 if (!reference && test)
272 return !strcmp(reference, test);
275 static const struct pmu_events_map *__test_pmu_get_events_map(void)
277 const struct pmu_events_map *map;
279 for (map = &pmu_events_map[0]; map->cpuid; map++) {
280 if (!strcmp(map->cpuid, "testcpu"))
284 pr_err("could not find test events map\n");
289 static const struct pmu_event *__test_pmu_get_sys_events_table(void)
291 const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
293 for ( ; tables->name; tables++) {
294 if (!strcmp("pme_test_soc_sys", tables->name))
295 return tables->table;
301 static int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
303 if (!is_same(e1->name, e2->name)) {
304 pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
305 e1->name, e1->name, e2->name);
309 if (!is_same(e1->compat, e2->compat)) {
310 pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
311 e1->name, e1->compat, e2->compat);
315 if (!is_same(e1->event, e2->event)) {
316 pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
317 e1->name, e1->event, e2->event);
321 if (!is_same(e1->desc, e2->desc)) {
322 pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
323 e1->name, e1->desc, e2->desc);
327 if (!is_same(e1->topic, e2->topic)) {
328 pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
329 e1->name, e1->topic, e2->topic);
333 if (!is_same(e1->long_desc, e2->long_desc)) {
334 pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
335 e1->name, e1->long_desc, e2->long_desc);
339 if (!is_same(e1->pmu, e2->pmu)) {
340 pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
341 e1->name, e1->pmu, e2->pmu);
345 if (!is_same(e1->unit, e2->unit)) {
346 pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
347 e1->name, e1->unit, e2->unit);
351 if (!is_same(e1->perpkg, e2->perpkg)) {
352 pr_debug2("testing event e1 %s: mismatched perpkg, %s vs %s\n",
353 e1->name, e1->perpkg, e2->perpkg);
357 if (!is_same(e1->aggr_mode, e2->aggr_mode)) {
358 pr_debug2("testing event e1 %s: mismatched aggr_mode, %s vs %s\n",
359 e1->name, e1->aggr_mode, e2->aggr_mode);
363 if (!is_same(e1->metric_expr, e2->metric_expr)) {
364 pr_debug2("testing event e1 %s: mismatched metric_expr, %s vs %s\n",
365 e1->name, e1->metric_expr, e2->metric_expr);
369 if (!is_same(e1->metric_name, e2->metric_name)) {
370 pr_debug2("testing event e1 %s: mismatched metric_name, %s vs %s\n",
371 e1->name, e1->metric_name, e2->metric_name);
375 if (!is_same(e1->metric_group, e2->metric_group)) {
376 pr_debug2("testing event e1 %s: mismatched metric_group, %s vs %s\n",
377 e1->name, e1->metric_group, e2->metric_group);
381 if (!is_same(e1->deprecated, e2->deprecated)) {
382 pr_debug2("testing event e1 %s: mismatched deprecated, %s vs %s\n",
383 e1->name, e1->deprecated, e2->deprecated);
387 if (!is_same(e1->metric_constraint, e2->metric_constraint)) {
388 pr_debug2("testing event e1 %s: mismatched metric_constant, %s vs %s\n",
389 e1->name, e1->metric_constraint, e2->metric_constraint);
396 static int compare_alias_to_test_event(struct perf_pmu_alias *alias,
397 struct perf_pmu_test_event const *test_event,
398 char const *pmu_name)
400 struct pmu_event const *event = &test_event->event;
402 /* An alias was found, ensure everything is in order */
403 if (!is_same(alias->name, event->name)) {
404 pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
405 pmu_name, alias->name, event->name);
409 if (!is_same(alias->desc, event->desc)) {
410 pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
411 pmu_name, alias->desc, event->desc);
415 if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
416 pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
417 pmu_name, alias->long_desc,
418 test_event->alias_long_desc);
422 if (!is_same(alias->topic, event->topic)) {
423 pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
424 pmu_name, alias->topic, event->topic);
428 if (!is_same(alias->str, test_event->alias_str)) {
429 pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
430 pmu_name, alias->str, test_event->alias_str);
434 if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
435 pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
436 pmu_name, alias->str, test_event->alias_long_desc);
441 if (!is_same(alias->pmu_name, test_event->event.pmu)) {
442 pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
443 pmu_name, alias->pmu_name, test_event->event.pmu);
450 /* Verify generated events from pmu-events.c are as expected */
451 static int test__pmu_event_table(struct test_suite *test __maybe_unused,
452 int subtest __maybe_unused)
454 const struct pmu_event *sys_event_tables = __test_pmu_get_sys_events_table();
455 const struct pmu_events_map *map = __test_pmu_get_events_map();
456 const struct pmu_event *table;
457 int map_events = 0, expected_events;
459 /* ignore 3x sentinels */
460 expected_events = ARRAY_SIZE(core_events) +
461 ARRAY_SIZE(uncore_events) +
462 ARRAY_SIZE(sys_events) - 3;
464 if (!map || !sys_event_tables)
467 for (table = map->table; table->name; table++) {
468 struct perf_pmu_test_event const **test_event_table;
472 test_event_table = &uncore_events[0];
474 test_event_table = &core_events[0];
476 for (; *test_event_table; test_event_table++) {
477 struct perf_pmu_test_event const *test_event = *test_event_table;
478 struct pmu_event const *event = &test_event->event;
480 if (strcmp(table->name, event->name))
485 if (compare_pmu_events(table, event))
488 pr_debug("testing event table %s: pass\n", table->name);
492 pr_err("testing event table: could not find event %s\n",
498 for (table = sys_event_tables; table->name; table++) {
499 struct perf_pmu_test_event const **test_event_table;
502 test_event_table = &sys_events[0];
504 for (; *test_event_table; test_event_table++) {
505 struct perf_pmu_test_event const *test_event = *test_event_table;
506 struct pmu_event const *event = &test_event->event;
508 if (strcmp(table->name, event->name))
513 if (compare_pmu_events(table, event))
516 pr_debug("testing sys event table %s: pass\n", table->name);
519 pr_debug("testing event table: could not find event %s\n",
525 if (map_events != expected_events) {
526 pr_err("testing event table: found %d, but expected %d\n",
527 map_events, expected_events);
534 static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
536 struct perf_pmu_alias *alias;
538 list_for_each_entry(alias, aliases, list)
539 if (!strcmp(test_event, alias->name))
545 /* Verify aliases are as expected */
546 static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
548 struct perf_pmu_test_event const **test_event_table;
549 struct perf_pmu *pmu;
552 const struct pmu_events_map *map = __test_pmu_get_events_map();
553 struct perf_pmu_alias *a, *tmp;
558 test_event_table = &core_events[0];
560 pmu = zalloc(sizeof(*pmu));
564 pmu->name = pmu_name;
566 pmu_add_cpu_aliases_map(&aliases, pmu, map);
568 for (; *test_event_table; test_event_table++) {
569 struct perf_pmu_test_event const *test_event = *test_event_table;
570 struct pmu_event const *event = &test_event->event;
571 struct perf_pmu_alias *alias = find_alias(event->name, &aliases);
574 pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
575 pmu_name, event->name);
580 if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
586 pr_debug2("testing aliases core PMU %s: matched event %s\n",
587 pmu_name, alias->name);
590 list_for_each_entry_safe(a, tmp, &aliases, list) {
592 perf_pmu_free_alias(a);
598 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
600 int alias_count = 0, to_match_count = 0, matched_count = 0;
601 struct perf_pmu_test_event const **table;
602 struct perf_pmu *pmu = &test_pmu->pmu;
603 const char *pmu_name = pmu->name;
604 struct perf_pmu_alias *a, *tmp, *alias;
605 const struct pmu_events_map *map;
609 map = __test_pmu_get_events_map();
612 pmu_add_cpu_aliases_map(&aliases, pmu, map);
613 pmu_add_sys_aliases(&aliases, pmu);
615 /* Count how many aliases we generated */
616 list_for_each_entry(alias, &aliases, list)
619 /* Count how many aliases we expect from the known table */
620 for (table = &test_pmu->aliases[0]; *table; table++)
623 if (alias_count != to_match_count) {
624 pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
625 pmu_name, to_match_count, alias_count);
630 list_for_each_entry(alias, &aliases, list) {
631 bool matched = false;
633 for (table = &test_pmu->aliases[0]; *table; table++) {
634 struct perf_pmu_test_event const *test_event = *table;
635 struct pmu_event const *event = &test_event->event;
637 if (!strcmp(event->name, alias->name)) {
638 if (compare_alias_to_test_event(alias,
648 if (matched == false) {
649 pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
650 pmu_name, alias->name);
656 if (alias_count != matched_count) {
657 pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
658 pmu_name, matched_count, alias_count);
663 list_for_each_entry_safe(a, tmp, &aliases, list) {
665 perf_pmu_free_alias(a);
670 static struct perf_pmu_test_pmu test_pmus[] = {
673 .name = (char *)"hisi_sccl1_ddrc2",
677 &uncore_hisi_ddrc_flux_wcmd,
682 .name = (char *)"uncore_cbox_0",
686 &unc_cbo_xsnp_response_miss_eviction,
693 .name = (char *)"hisi_sccl3_l3c7",
697 &uncore_hisi_l3c_rd_hit_cpipe,
702 .name = (char *)"uncore_imc_free_running_0",
706 &uncore_imc_free_running_cache_miss,
711 .name = (char *)"uncore_imc_0",
715 &uncore_imc_cache_hits,
720 .name = (char *)"uncore_sys_ddr_pmu0",
725 &sys_ddr_pmu_write_cycles,
730 .name = (char *)"uncore_sys_ccn_pmu4",
732 .id = (char *)"0x01",
735 &sys_ccn_pmu_read_cycles,
740 /* Test that aliases generated are as expected */
741 static int test__aliases(struct test_suite *test __maybe_unused,
742 int subtest __maybe_unused)
744 struct perf_pmu *pmu = NULL;
747 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
750 if (!is_pmu_core(pmu->name))
753 if (list_empty(&pmu->format)) {
754 pr_debug2("skipping testing core PMU %s\n", pmu->name);
758 if (__test_core_pmu_event_aliases(pmu->name, &count)) {
759 pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
764 pr_debug("testing core PMU %s aliases: no events to match\n",
769 pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
772 for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
773 int res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
782 static bool is_number(const char *str)
788 v = strtod(str, &end_ptr);
789 (void)v; // We're not interested in this value, only if it is valid
790 return errno == 0 && end_ptr != str;
793 static int check_parse_id(const char *id, struct parse_events_error *error,
794 struct perf_pmu *fake_pmu)
796 struct evlist *evlist;
800 /* Numbers are always valid. */
804 evlist = evlist__new();
812 for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
817 * Every call to __parse_events will try to initialize the PMU
818 * state from sysfs and then clean it up at the end. Reset the
819 * PMU events to the test state so that we don't pick up
820 * erroneous prefixes and suffixes.
822 perf_pmu__test_parse_init();
824 ret = __parse_events(evlist, dup, error, fake_pmu);
827 evlist__delete(evlist);
831 static int check_parse_cpu(const char *id, bool same_cpu, const struct pmu_event *pe)
833 struct parse_events_error error;
836 parse_events_error__init(&error);
837 ret = check_parse_id(id, &error, NULL);
838 if (ret && same_cpu) {
839 pr_warning("Parse event failed metric '%s' id '%s' expr '%s'\n",
840 pe->metric_name, id, pe->metric_expr);
841 pr_warning("Error string '%s' help '%s'\n", error.str,
844 pr_debug3("Parse event failed, but for an event that may not be supported by this CPU.\nid '%s' metric '%s' expr '%s'\n",
845 id, pe->metric_name, pe->metric_expr);
848 parse_events_error__exit(&error);
852 static int check_parse_fake(const char *id)
854 struct parse_events_error error;
857 parse_events_error__init(&error);
858 ret = check_parse_id(id, &error, &perf_pmu__fake);
859 parse_events_error__exit(&error);
863 static void expr_failure(const char *msg,
864 const struct pmu_events_map *map,
865 const struct pmu_event *pe)
867 pr_debug("%s for map %s %s %s\n",
868 msg, map->cpuid, map->version, map->type);
869 pr_debug("On metric %s\n", pe->metric_name);
870 pr_debug("On expression %s\n", pe->metric_expr);
874 struct list_head list;
875 struct metric_ref metric_ref;
878 static int resolve_metric_simple(struct expr_parse_ctx *pctx,
879 struct list_head *compound_list,
880 const struct pmu_events_map *map,
881 const char *metric_name)
883 struct hashmap_entry *cur, *cur_tmp;
884 struct metric *metric, *tmp;
891 hashmap__for_each_entry_safe(pctx->ids, cur, cur_tmp, bkt) {
892 struct metric_ref *ref;
893 const struct pmu_event *pe;
895 pe = metricgroup__find_metric(cur->key, map);
899 if (!strcmp(metric_name, (char *)cur->key)) {
900 pr_warning("Recursion detected for metric %s\n", metric_name);
907 /* The metric key itself needs to go out.. */
908 expr__del_id(pctx, cur->key);
910 metric = malloc(sizeof(*metric));
916 ref = &metric->metric_ref;
917 ref->metric_name = pe->metric_name;
918 ref->metric_expr = pe->metric_expr;
919 list_add_tail(&metric->list, compound_list);
921 rc = expr__find_ids(pe->metric_expr, NULL, pctx);
924 break; /* The hashmap has been modified, so restart */
931 list_for_each_entry_safe(metric, tmp, compound_list, list)
938 static int test__parsing(struct test_suite *test __maybe_unused,
939 int subtest __maybe_unused)
941 const struct pmu_events_map *cpus_map = pmu_events_map__find();
942 const struct pmu_events_map *map;
943 const struct pmu_event *pe;
946 struct expr_parse_ctx *ctx;
949 ctx = expr__ctx_new();
951 pr_debug("expr__ctx_new failed");
956 map = &pmu_events_map[i++];
961 struct metric *metric, *tmp;
962 struct hashmap_entry *cur;
963 LIST_HEAD(compound_list);
966 pe = &map->table[j++];
967 if (!pe->name && !pe->metric_group && !pe->metric_name)
969 if (!pe->metric_expr)
971 expr__ctx_clear(ctx);
972 if (expr__find_ids(pe->metric_expr, NULL, ctx) < 0) {
973 expr_failure("Parse find ids failed", map, pe);
978 if (resolve_metric_simple(ctx, &compound_list, map,
980 expr_failure("Could not resolve metrics", map, pe);
982 goto exit; /* Don't tolerate errors due to severity */
986 * Add all ids with a made up value. The value may
987 * trigger divide by zero when subtracted and so try to
991 hashmap__for_each_entry(ctx->ids, cur, bkt)
992 expr__add_id_val(ctx, strdup(cur->key), k++);
994 hashmap__for_each_entry(ctx->ids, cur, bkt) {
995 if (check_parse_cpu(cur->key, map == cpus_map,
1000 list_for_each_entry_safe(metric, tmp, &compound_list, list) {
1001 expr__add_ref(ctx, &metric->metric_ref);
1005 if (expr__parse(&result, ctx, pe->metric_expr)) {
1007 * Parsing failed, make numbers go from large to
1008 * small which can resolve divide by zero
1012 hashmap__for_each_entry(ctx->ids, cur, bkt)
1013 expr__add_id_val(ctx, strdup(cur->key), k--);
1014 if (expr__parse(&result, ctx, pe->metric_expr)) {
1015 expr_failure("Parse failed", map, pe);
1021 expr__ctx_free(ctx);
1022 /* TODO: fail when not ok */
1024 return ret == 0 ? TEST_OK : TEST_SKIP;
1027 struct test_metric {
1031 static struct test_metric metrics[] = {
1032 { "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
1033 { "imx8_ddr0@read\\-cycles@ * 4 * 4", },
1034 { "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
1035 { "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
1036 { "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
1039 static int metric_parse_fake(const char *str)
1041 struct expr_parse_ctx *ctx;
1042 struct hashmap_entry *cur;
1048 pr_debug("parsing '%s'\n", str);
1050 ctx = expr__ctx_new();
1052 pr_debug("expr__ctx_new failed");
1055 if (expr__find_ids(str, NULL, ctx) < 0) {
1056 pr_err("expr__find_ids failed\n");
1061 * Add all ids with a made up value. The value may
1062 * trigger divide by zero when subtracted and so try to
1066 hashmap__for_each_entry(ctx->ids, cur, bkt)
1067 expr__add_id_val(ctx, strdup(cur->key), i++);
1069 hashmap__for_each_entry(ctx->ids, cur, bkt) {
1070 if (check_parse_fake(cur->key)) {
1071 pr_err("check_parse_fake failed\n");
1077 if (expr__parse(&result, ctx, str)) {
1079 * Parsing failed, make numbers go from large to small which can
1080 * resolve divide by zero issues.
1083 hashmap__for_each_entry(ctx->ids, cur, bkt)
1084 expr__add_id_val(ctx, strdup(cur->key), i--);
1085 if (expr__parse(&result, ctx, str)) {
1086 pr_err("expr__parse failed\n");
1092 expr__ctx_free(ctx);
1097 * Parse all the metrics for current architecture,
1098 * or all defined cpus via the 'fake_pmu'
1101 static int test__parsing_fake(struct test_suite *test __maybe_unused,
1102 int subtest __maybe_unused)
1104 const struct pmu_events_map *map;
1105 const struct pmu_event *pe;
1109 for (i = 0; i < ARRAY_SIZE(metrics); i++) {
1110 err = metric_parse_fake(metrics[i].str);
1117 map = &pmu_events_map[i++];
1122 pe = &map->table[j++];
1123 if (!pe->name && !pe->metric_group && !pe->metric_name)
1125 if (!pe->metric_expr)
1127 pr_debug("Found metric '%s' for '%s'\n", pe->metric_name, map->cpuid);
1128 err = metric_parse_fake(pe->metric_expr);
1137 static struct test_case pmu_events_tests[] = {
1138 TEST_CASE("PMU event table sanity", pmu_event_table),
1139 TEST_CASE("PMU event map aliases", aliases),
1140 TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1141 "some metrics failed"),
1142 TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
1146 struct test_suite suite__pmu_events = {
1147 .desc = "PMU events",
1148 .test_cases = pmu_events_tests,