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 int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
277 if (!is_same(e1->name, e2->name)) {
278 pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
279 e1->name, e1->name, e2->name);
283 if (!is_same(e1->compat, e2->compat)) {
284 pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
285 e1->name, e1->compat, e2->compat);
289 if (!is_same(e1->event, e2->event)) {
290 pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
291 e1->name, e1->event, e2->event);
295 if (!is_same(e1->desc, e2->desc)) {
296 pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
297 e1->name, e1->desc, e2->desc);
301 if (!is_same(e1->topic, e2->topic)) {
302 pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
303 e1->name, e1->topic, e2->topic);
307 if (!is_same(e1->long_desc, e2->long_desc)) {
308 pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
309 e1->name, e1->long_desc, e2->long_desc);
313 if (!is_same(e1->pmu, e2->pmu)) {
314 pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
315 e1->name, e1->pmu, e2->pmu);
319 if (!is_same(e1->unit, e2->unit)) {
320 pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
321 e1->name, e1->unit, e2->unit);
325 if (!is_same(e1->perpkg, e2->perpkg)) {
326 pr_debug2("testing event e1 %s: mismatched perpkg, %s vs %s\n",
327 e1->name, e1->perpkg, e2->perpkg);
331 if (!is_same(e1->aggr_mode, e2->aggr_mode)) {
332 pr_debug2("testing event e1 %s: mismatched aggr_mode, %s vs %s\n",
333 e1->name, e1->aggr_mode, e2->aggr_mode);
337 if (!is_same(e1->metric_expr, e2->metric_expr)) {
338 pr_debug2("testing event e1 %s: mismatched metric_expr, %s vs %s\n",
339 e1->name, e1->metric_expr, e2->metric_expr);
343 if (!is_same(e1->metric_name, e2->metric_name)) {
344 pr_debug2("testing event e1 %s: mismatched metric_name, %s vs %s\n",
345 e1->name, e1->metric_name, e2->metric_name);
349 if (!is_same(e1->metric_group, e2->metric_group)) {
350 pr_debug2("testing event e1 %s: mismatched metric_group, %s vs %s\n",
351 e1->name, e1->metric_group, e2->metric_group);
355 if (!is_same(e1->deprecated, e2->deprecated)) {
356 pr_debug2("testing event e1 %s: mismatched deprecated, %s vs %s\n",
357 e1->name, e1->deprecated, e2->deprecated);
361 if (!is_same(e1->metric_constraint, e2->metric_constraint)) {
362 pr_debug2("testing event e1 %s: mismatched metric_constant, %s vs %s\n",
363 e1->name, e1->metric_constraint, e2->metric_constraint);
370 static int compare_alias_to_test_event(struct perf_pmu_alias *alias,
371 struct perf_pmu_test_event const *test_event,
372 char const *pmu_name)
374 struct pmu_event const *event = &test_event->event;
376 /* An alias was found, ensure everything is in order */
377 if (!is_same(alias->name, event->name)) {
378 pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
379 pmu_name, alias->name, event->name);
383 if (!is_same(alias->desc, event->desc)) {
384 pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
385 pmu_name, alias->desc, event->desc);
389 if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
390 pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
391 pmu_name, alias->long_desc,
392 test_event->alias_long_desc);
396 if (!is_same(alias->topic, event->topic)) {
397 pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
398 pmu_name, alias->topic, event->topic);
402 if (!is_same(alias->str, test_event->alias_str)) {
403 pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
404 pmu_name, alias->str, test_event->alias_str);
408 if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
409 pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
410 pmu_name, alias->str, test_event->alias_long_desc);
415 if (!is_same(alias->pmu_name, test_event->event.pmu)) {
416 pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
417 pmu_name, alias->pmu_name, test_event->event.pmu);
424 /* Verify generated events from pmu-events.c are as expected */
425 static int test__pmu_event_table(struct test_suite *test __maybe_unused,
426 int subtest __maybe_unused)
428 const struct pmu_event *sys_event_tables = find_sys_events_table("pme_test_soc_sys");
429 const struct pmu_event *table = find_core_events_table("testarch", "testcpu");
430 int map_events = 0, expected_events;
432 /* ignore 3x sentinels */
433 expected_events = ARRAY_SIZE(core_events) +
434 ARRAY_SIZE(uncore_events) +
435 ARRAY_SIZE(sys_events) - 3;
437 if (!table || !sys_event_tables)
440 for (; table->name; table++) {
441 struct perf_pmu_test_event const **test_event_table;
445 test_event_table = &uncore_events[0];
447 test_event_table = &core_events[0];
449 for (; *test_event_table; test_event_table++) {
450 struct perf_pmu_test_event const *test_event = *test_event_table;
451 struct pmu_event const *event = &test_event->event;
453 if (strcmp(table->name, event->name))
458 if (compare_pmu_events(table, event))
461 pr_debug("testing event table %s: pass\n", table->name);
465 pr_err("testing event table: could not find event %s\n",
471 for (table = sys_event_tables; table->name; table++) {
472 struct perf_pmu_test_event const **test_event_table;
475 test_event_table = &sys_events[0];
477 for (; *test_event_table; test_event_table++) {
478 struct perf_pmu_test_event const *test_event = *test_event_table;
479 struct pmu_event const *event = &test_event->event;
481 if (strcmp(table->name, event->name))
486 if (compare_pmu_events(table, event))
489 pr_debug("testing sys event table %s: pass\n", table->name);
492 pr_debug("testing event table: could not find event %s\n",
498 if (map_events != expected_events) {
499 pr_err("testing event table: found %d, but expected %d\n",
500 map_events, expected_events);
507 static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
509 struct perf_pmu_alias *alias;
511 list_for_each_entry(alias, aliases, list)
512 if (!strcmp(test_event, alias->name))
518 /* Verify aliases are as expected */
519 static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
521 struct perf_pmu_test_event const **test_event_table;
522 struct perf_pmu *pmu;
525 const struct pmu_event *table = find_core_events_table("testarch", "testcpu");
526 struct perf_pmu_alias *a, *tmp;
531 test_event_table = &core_events[0];
533 pmu = zalloc(sizeof(*pmu));
537 pmu->name = pmu_name;
539 pmu_add_cpu_aliases_table(&aliases, pmu, table);
541 for (; *test_event_table; test_event_table++) {
542 struct perf_pmu_test_event const *test_event = *test_event_table;
543 struct pmu_event const *event = &test_event->event;
544 struct perf_pmu_alias *alias = find_alias(event->name, &aliases);
547 pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
548 pmu_name, event->name);
553 if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
559 pr_debug2("testing aliases core PMU %s: matched event %s\n",
560 pmu_name, alias->name);
563 list_for_each_entry_safe(a, tmp, &aliases, list) {
565 perf_pmu_free_alias(a);
571 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
573 int alias_count = 0, to_match_count = 0, matched_count = 0;
574 struct perf_pmu_test_event const **table;
575 struct perf_pmu *pmu = &test_pmu->pmu;
576 const char *pmu_name = pmu->name;
577 struct perf_pmu_alias *a, *tmp, *alias;
578 const struct pmu_event *events_table;
582 events_table = find_core_events_table("testarch", "testcpu");
585 pmu_add_cpu_aliases_table(&aliases, pmu, events_table);
586 pmu_add_sys_aliases(&aliases, pmu);
588 /* Count how many aliases we generated */
589 list_for_each_entry(alias, &aliases, list)
592 /* Count how many aliases we expect from the known table */
593 for (table = &test_pmu->aliases[0]; *table; table++)
596 if (alias_count != to_match_count) {
597 pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
598 pmu_name, to_match_count, alias_count);
603 list_for_each_entry(alias, &aliases, list) {
604 bool matched = false;
606 for (table = &test_pmu->aliases[0]; *table; table++) {
607 struct perf_pmu_test_event const *test_event = *table;
608 struct pmu_event const *event = &test_event->event;
610 if (!strcmp(event->name, alias->name)) {
611 if (compare_alias_to_test_event(alias,
621 if (matched == false) {
622 pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
623 pmu_name, alias->name);
629 if (alias_count != matched_count) {
630 pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
631 pmu_name, matched_count, alias_count);
636 list_for_each_entry_safe(a, tmp, &aliases, list) {
638 perf_pmu_free_alias(a);
643 static struct perf_pmu_test_pmu test_pmus[] = {
646 .name = (char *)"hisi_sccl1_ddrc2",
650 &uncore_hisi_ddrc_flux_wcmd,
655 .name = (char *)"uncore_cbox_0",
659 &unc_cbo_xsnp_response_miss_eviction,
666 .name = (char *)"hisi_sccl3_l3c7",
670 &uncore_hisi_l3c_rd_hit_cpipe,
675 .name = (char *)"uncore_imc_free_running_0",
679 &uncore_imc_free_running_cache_miss,
684 .name = (char *)"uncore_imc_0",
688 &uncore_imc_cache_hits,
693 .name = (char *)"uncore_sys_ddr_pmu0",
698 &sys_ddr_pmu_write_cycles,
703 .name = (char *)"uncore_sys_ccn_pmu4",
705 .id = (char *)"0x01",
708 &sys_ccn_pmu_read_cycles,
713 /* Test that aliases generated are as expected */
714 static int test__aliases(struct test_suite *test __maybe_unused,
715 int subtest __maybe_unused)
717 struct perf_pmu *pmu = NULL;
720 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
723 if (!is_pmu_core(pmu->name))
726 if (list_empty(&pmu->format)) {
727 pr_debug2("skipping testing core PMU %s\n", pmu->name);
731 if (__test_core_pmu_event_aliases(pmu->name, &count)) {
732 pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
737 pr_debug("testing core PMU %s aliases: no events to match\n",
742 pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
745 for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
746 int res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
755 static bool is_number(const char *str)
761 v = strtod(str, &end_ptr);
762 (void)v; // We're not interested in this value, only if it is valid
763 return errno == 0 && end_ptr != str;
766 static int check_parse_id(const char *id, struct parse_events_error *error,
767 struct perf_pmu *fake_pmu)
769 struct evlist *evlist;
773 /* Numbers are always valid. */
777 evlist = evlist__new();
785 for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
790 * Every call to __parse_events will try to initialize the PMU
791 * state from sysfs and then clean it up at the end. Reset the
792 * PMU events to the test state so that we don't pick up
793 * erroneous prefixes and suffixes.
795 perf_pmu__test_parse_init();
797 ret = __parse_events(evlist, dup, error, fake_pmu);
800 evlist__delete(evlist);
804 static int check_parse_cpu(const char *id, bool same_cpu, const struct pmu_event *pe)
806 struct parse_events_error error;
809 parse_events_error__init(&error);
810 ret = check_parse_id(id, &error, NULL);
811 if (ret && same_cpu) {
812 pr_warning("Parse event failed metric '%s' id '%s' expr '%s'\n",
813 pe->metric_name, id, pe->metric_expr);
814 pr_warning("Error string '%s' help '%s'\n", error.str,
817 pr_debug3("Parse event failed, but for an event that may not be supported by this CPU.\nid '%s' metric '%s' expr '%s'\n",
818 id, pe->metric_name, pe->metric_expr);
821 parse_events_error__exit(&error);
825 static int check_parse_fake(const char *id)
827 struct parse_events_error error;
830 parse_events_error__init(&error);
831 ret = check_parse_id(id, &error, &perf_pmu__fake);
832 parse_events_error__exit(&error);
837 struct list_head list;
838 struct metric_ref metric_ref;
841 static int resolve_metric_simple(struct expr_parse_ctx *pctx,
842 struct list_head *compound_list,
843 const struct pmu_event *map,
844 const char *metric_name)
846 struct hashmap_entry *cur, *cur_tmp;
847 struct metric *metric, *tmp;
854 hashmap__for_each_entry_safe(pctx->ids, cur, cur_tmp, bkt) {
855 struct metric_ref *ref;
856 const struct pmu_event *pe;
858 pe = metricgroup__find_metric(cur->key, map);
862 if (!strcmp(metric_name, (char *)cur->key)) {
863 pr_warning("Recursion detected for metric %s\n", metric_name);
870 /* The metric key itself needs to go out.. */
871 expr__del_id(pctx, cur->key);
873 metric = malloc(sizeof(*metric));
879 ref = &metric->metric_ref;
880 ref->metric_name = pe->metric_name;
881 ref->metric_expr = pe->metric_expr;
882 list_add_tail(&metric->list, compound_list);
884 rc = expr__find_ids(pe->metric_expr, NULL, pctx);
887 break; /* The hashmap has been modified, so restart */
894 list_for_each_entry_safe(metric, tmp, compound_list, list)
901 static void expr_failure(const char *msg, const struct pmu_event *pe)
903 pr_debug("%s\nOn metric %s\nOn expression %s\n", msg, pe->metric_name, pe->metric_expr);
907 struct test__parsing_data {
908 const struct pmu_event *cpus_table;
909 struct expr_parse_ctx *ctx;
913 static int test__parsing_callback(const struct pmu_event *pe, const struct pmu_event *table,
916 struct test__parsing_data *data = vdata;
917 struct metric *metric, *tmp;
918 struct hashmap_entry *cur;
919 LIST_HEAD(compound_list);
924 if (!pe->metric_expr)
927 pr_debug("Found metric '%s'\n", pe->metric_name);
929 expr__ctx_clear(data->ctx);
930 if (expr__find_ids(pe->metric_expr, NULL, data->ctx) < 0) {
931 expr_failure("Parse find ids failed", pe);
936 if (resolve_metric_simple(data->ctx, &compound_list, table,
938 expr_failure("Could not resolve metrics", pe);
940 return TEST_FAIL; /* Don't tolerate errors due to severity */
944 * Add all ids with a made up value. The value may trigger divide by
945 * zero when subtracted and so try to make them unique.
948 hashmap__for_each_entry(data->ctx->ids, cur, bkt)
949 expr__add_id_val(data->ctx, strdup(cur->key), k++);
951 hashmap__for_each_entry(data->ctx->ids, cur, bkt) {
952 if (check_parse_cpu(cur->key, table == data->cpus_table, pe))
956 list_for_each_entry_safe(metric, tmp, &compound_list, list) {
957 expr__add_ref(data->ctx, &metric->metric_ref);
961 if (expr__parse(&result, data->ctx, pe->metric_expr)) {
963 * Parsing failed, make numbers go from large to small which can
964 * resolve divide by zero issues.
967 hashmap__for_each_entry(data->ctx->ids, cur, bkt)
968 expr__add_id_val(data->ctx, strdup(cur->key), k--);
969 if (expr__parse(&result, data->ctx, pe->metric_expr)) {
970 expr_failure("Parse failed", pe);
977 static int test__parsing(struct test_suite *test __maybe_unused,
978 int subtest __maybe_unused)
980 struct test__parsing_data data = {
981 .cpus_table = pmu_events_table__find(),
982 .ctx = expr__ctx_new(),
987 pr_debug("expr__ctx_new failed");
990 pmu_for_each_core_event(test__parsing_callback, &data);
991 pmu_for_each_sys_event(test__parsing_callback, &data);
993 expr__ctx_free(data.ctx);
994 return data.failures == 0 ? TEST_OK : TEST_FAIL;
1001 static struct test_metric metrics[] = {
1002 { "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
1003 { "imx8_ddr0@read\\-cycles@ * 4 * 4", },
1004 { "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
1005 { "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
1006 { "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
1009 static int metric_parse_fake(const char *str)
1011 struct expr_parse_ctx *ctx;
1012 struct hashmap_entry *cur;
1018 pr_debug("parsing '%s'\n", str);
1020 ctx = expr__ctx_new();
1022 pr_debug("expr__ctx_new failed");
1025 if (expr__find_ids(str, NULL, ctx) < 0) {
1026 pr_err("expr__find_ids failed\n");
1031 * Add all ids with a made up value. The value may
1032 * trigger divide by zero when subtracted and so try to
1036 hashmap__for_each_entry(ctx->ids, cur, bkt)
1037 expr__add_id_val(ctx, strdup(cur->key), i++);
1039 hashmap__for_each_entry(ctx->ids, cur, bkt) {
1040 if (check_parse_fake(cur->key)) {
1041 pr_err("check_parse_fake failed\n");
1047 if (expr__parse(&result, ctx, str)) {
1049 * Parsing failed, make numbers go from large to small which can
1050 * resolve divide by zero issues.
1053 hashmap__for_each_entry(ctx->ids, cur, bkt)
1054 expr__add_id_val(ctx, strdup(cur->key), i--);
1055 if (expr__parse(&result, ctx, str)) {
1056 pr_err("expr__parse failed\n");
1062 expr__ctx_free(ctx);
1066 static int test__parsing_fake_callback(const struct pmu_event *pe,
1067 const struct pmu_event *table __maybe_unused,
1068 void *data __maybe_unused)
1070 if (!pe->metric_expr)
1073 return metric_parse_fake(pe->metric_expr);
1077 * Parse all the metrics for current architecture,
1078 * or all defined cpus via the 'fake_pmu'
1081 static int test__parsing_fake(struct test_suite *test __maybe_unused,
1082 int subtest __maybe_unused)
1086 for (size_t i = 0; i < ARRAY_SIZE(metrics); i++) {
1087 err = metric_parse_fake(metrics[i].str);
1092 err = pmu_for_each_core_event(test__parsing_fake_callback, NULL);
1096 return pmu_for_each_sys_event(test__parsing_fake_callback, NULL);
1099 static struct test_case pmu_events_tests[] = {
1100 TEST_CASE("PMU event table sanity", pmu_event_table),
1101 TEST_CASE("PMU event map aliases", aliases),
1102 TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1103 "some metrics failed"),
1104 TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
1108 struct test_suite suite__pmu_events = {
1109 .desc = "PMU events",
1110 .test_cases = pmu_events_tests,