[PATCH v2 3/3] perf metricgroup: Refine error logs

From: Leo Yan

Date: Thu Apr 02 2026 - 12:11:44 EST


Return -ENOENT when no metric/group matches, and directly use the return
value from expr__find_ids(), so -EINVAL is reserved for parse failures.

Print separate logs to make it clear.

Before:

perf stat -C 5 -vvv
Using CPUID 0x00000000410fd490
metric expr 100 * (STALL_SLOT_BACKEND / (CPU_CYCLES * #slots) - BR_MIS_PRED * 3 / CPU_CYCLES) for backend_bound
parsing metric: 100 * (STALL_SLOT_BACKEND / (CPU_CYCLES * #slots) - BR_MIS_PRED * 3 / CPU_CYCLES)
Failure to read '#slots'
literal: #slots = nan
syntax error
Cannot find metric or group `Default'

After:

perf stat -C 5 -vvv
Using CPUID 0x00000000410fd490
metric expr 100 * (STALL_SLOT_BACKEND / (CPU_CYCLES * #slots) - BR_MIS_PRED * 3 / CPU_CYCLES) for backend_bound
parsing metric: 100 * (STALL_SLOT_BACKEND / (CPU_CYCLES * #slots) - BR_MIS_PRED * 3 / CPU_CYCLES)
Failure to read '#slots'
literal: #slots = nan
syntax error
Fail to parse metric or group `Default'

Signed-off-by: Leo Yan <leo.yan@xxxxxxx>
---
tools/perf/util/metricgroup.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index f7d53b4e46f45cb01181a462cb5bb5ca5a3b1a9c..4db9578efd811cf91d192cc13fb757cf5aab21f7 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -914,10 +914,9 @@ static int __add_metric(struct list_head *metric_list,
expr = metric_no_threshold ? pm->metric_name : pm->metric_threshold;
visited_node.name = "__threshold__";
}
- if (expr__find_ids(expr, NULL, root_metric->pctx) < 0) {
- /* Broken metric. */
- ret = -EINVAL;
- }
+
+ ret = expr__find_ids(expr, NULL, root_metric->pctx);
+
if (!ret) {
/* Resolve referenced metrics. */
struct perf_pmu *pmu;
@@ -1101,7 +1100,7 @@ static int metricgroup__add_metric(const char *pmu, const char *metric_name, con
*/
ret = metricgroup__for_each_metric(table, metricgroup__add_metric_callback, &data);
if (!ret && !data.has_match)
- ret = -EINVAL;
+ ret = -ENOENT;

/*
* add to metric_list so that they can be released
@@ -1152,6 +1151,8 @@ static int metricgroup__add_metric_list(const char *pmu, const char *list,
user_requested_cpu_list,
system_wide, metric_list, table);
if (ret == -EINVAL)
+ pr_err("Fail to parse metric or group `%s'\n", metric_name);
+ else if (ret == -ENOENT)
pr_err("Cannot find metric or group `%s'\n", metric_name);

if (ret)

--
2.34.1