From 2fb45894e80d246ac6a9f0343343d581921cc3f2 Mon Sep 17 00:00:00 2001 From: Ian Rogers Date: Sun, 25 Oct 2026 23:50:44 -0700 Subject: [PATCH] perf debug: Avoid calling eprintf() in pr_debugN() when verbose < n pr_debugN() unconditionally evaluated its arguments and called eprintf() -> veprintf() before checking if verbose >= n. In hot paths such as dso__load_sym_internal(), two pr_debug4() calls per symbol accounted for ~15% of total CPU time in 'perf top' even at the default verbose == 0. Guard eprintf() in pr_debugN() with unlikely(verbose >= (n)) and define pr_debug() in terms of pr_debugN(1, ...). In test__parsing_callback() (tools/perf/tests/pmu-events.c), move the test_generic_metric(mexp, 0) call out of the pr_debug() argument list so the metric evaluation continues to run during 'perf test' when verbose == 0. Signed-off-by: Ian Rogers Cc: Adrian Hunter Cc: Alessio Podda Cc: Alireza Haghdoost Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Michael Liang Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Shimin Guo Cc: Tanushree Shah Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/pmu-events.c | 5 ++++- tools/perf/util/debug.h | 5 ++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c index f507ce39439a..e6e4f4cf1fa2 100644 --- a/tools/perf/tests/pmu-events.c +++ b/tools/perf/tests/pmu-events.c @@ -906,9 +906,12 @@ static int test__parsing_callback(const struct pmu_metric *pm, struct metric_expr *mexp; list_for_each_entry (mexp, &me->head, nd) { + double result; + if (strcmp(mexp->metric_name, pm->metric_name)) continue; - pr_debug("Result %f\n", test_generic_metric(mexp, 0)); + result = test_generic_metric(mexp, 0); + pr_debug("Result %f\n", result); err = 0; (*failures)--; goto out_err; diff --git a/tools/perf/util/debug.h b/tools/perf/util/debug.h index 6b737e195ce1..7552c9171d90 100644 --- a/tools/perf/util/debug.h +++ b/tools/perf/util/debug.h @@ -33,10 +33,9 @@ extern int debug_type_profile; }) #define pr_info(fmt, ...) \ eprintf(0, verbose, pr_fmt(fmt), ##__VA_ARGS__) -#define pr_debug(fmt, ...) \ - eprintf(1, verbose, pr_fmt(fmt), ##__VA_ARGS__) #define pr_debugN(n, fmt, ...) \ - eprintf(n, verbose, pr_fmt(fmt), ##__VA_ARGS__) + (unlikely(verbose >= (n)) ? eprintf(n, verbose, pr_fmt(fmt), ##__VA_ARGS__) : 0) +#define pr_debug(fmt, ...) pr_debugN(1, pr_fmt(fmt), ##__VA_ARGS__) #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__) #define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__) #define pr_debug4(fmt, ...) pr_debugN(4, pr_fmt(fmt), ##__VA_ARGS__)