Linux Perf Users
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: linux-perf-users@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
	mingo@redhat.com,  acme@kernel.org, namhyung@kernel.org,
	jolsa@kernel.org,  adrian.hunter@intel.com,
	james.clark@linaro.org, dsahern@gmail.com,  andrii@kernel.org,
	Ian Rogers <irogers@google.com>
Subject: [PATCH v3 3/7] perf bench synthesize: Initialize symbols so build IDs can be read
Date: Thu,  3 Dec 2026 22:23:24 -0800	[thread overview]
Message-ID: <20261204062328.2508164-4-irogers@google.com> (raw)
In-Reply-To: <20261204062328.2508164-1-irogers@google.com>

Since build IDs became the default in synthesized MMAP2 events, the
synthesize benchmark reads the build ID of every mapped file. The
benchmark never called symbol__init(), so elf_version() was never
called, elf_begin() fails and every build ID read fails. As no build ID
is found the DSO build ID cache is never populated, and so every mapping
re-opens its file and enters the mount namespace again on every
iteration. This made the benchmark measure a failure path that doesn't
occur in 'perf record', for example on an Intel Core i7-14700:

Before:
  Average synthesis took: 3876.387 usec (+- 13.053 usec)
  Average data synthesis took: 24886.623 usec (+- 1238.682 usec)

After (median of 10 runs):
  Average synthesis took: 258.8 usec
  Average data synthesis took: 1621.8 usec

Create a single host perf_env in bench_synthesize(), pass it to
symbol__init() so libelf is initialized and vmlinux paths match the
host, and share it with the sessions the single and multi-threaded
benchmarks create rather than each creating their own.

Fixes: 53b00ff358dc ("perf record: Make --buildid-mmap the default")
Signed-off-by: Ian Rogers <irogers@google.com>
---

Notes:
    This change was developed with the help of an AI coding assistant
    (Gemini in Jetski), which wrote the initial code, tests and
    changelog; it was reviewed, built and tested by the author.

 tools/perf/bench/synthesize.c | 52 +++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 23 deletions(-)

diff --git a/tools/perf/bench/synthesize.c b/tools/perf/bench/synthesize.c
index 265d49a913d9..fdc5465db3f0 100644
--- a/tools/perf/bench/synthesize.c
+++ b/tools/perf/bench/synthesize.c
@@ -12,6 +12,7 @@
 #include "../util/debug.h"
 #include "../util/session.h"
 #include "../util/stat.h"
+#include "../util/symbol.h"
 #include "../util/synthetic-events.h"
 #include "../util/target.h"
 #include "../util/thread_map.h"
@@ -108,23 +109,20 @@ static int do_run_single_threaded(struct perf_session *session,
 	return 0;
 }
 
-static int run_single_threaded(void)
+static int run_single_threaded(struct perf_env *host_env)
 {
 	struct perf_session *session;
 	struct target target = {
 		.pid = "self",
 	};
 	struct perf_thread_map *threads;
-	struct perf_env host_env;
 	int err;
 
 	perf_set_singlethreaded();
-	perf_env__init(&host_env);
 	session = __perf_session__new(/*data=*/NULL, /*tool=*/NULL,
-				      /*trace_event_repipe=*/false, &host_env);
+				      /*trace_event_repipe=*/false, host_env);
 	if (IS_ERR(session)) {
 		pr_err("Session creation failed.\n");
-		perf_env__exit(&host_env);
 		return PTR_ERR(session);
 	}
 	threads = thread_map__new_by_pid(getpid());
@@ -149,12 +147,12 @@ static int run_single_threaded(void)
 		perf_thread_map__put(threads);
 
 	perf_session__delete(session);
-	perf_env__exit(&host_env);
 	return err;
 }
 
-static int do_run_multi_threaded(struct target *target,
-				unsigned int nr_threads_synthesize)
+static int do_run_multi_threaded(struct perf_env *host_env,
+				 struct target *target,
+				 unsigned int nr_threads_synthesize)
 {
 	struct timeval start, end, diff;
 	u64 runtime_us;
@@ -163,18 +161,14 @@ static int do_run_multi_threaded(struct target *target,
 	int err = 0;
 	struct stats time_stats, event_stats;
 	struct perf_session *session;
-	struct perf_env host_env;
 
-	perf_env__init(&host_env);
 	init_stats(&time_stats);
 	init_stats(&event_stats);
 	for (i = 0; i < multi_iterations; i++) {
 		session = __perf_session__new(/*data=*/NULL, /*tool=*/NULL,
-					      /*trace_event_repipe=*/false, &host_env);
-		if (IS_ERR(session)) {
-			err = PTR_ERR(session);
-			goto err_out;
-		}
+					      /*trace_event_repipe=*/false, host_env);
+		if (IS_ERR(session))
+			return PTR_ERR(session);
 		atomic_set(&event_count, 0);
 		gettimeofday(&start, NULL);
 		err = __machine__synthesize_threads(&session->machines.host,
@@ -185,7 +179,7 @@ static int do_run_multi_threaded(struct target *target,
 						nr_threads_synthesize);
 		if (err) {
 			perf_session__delete(session);
-			goto err_out;
+			return err;
 		}
 
 		gettimeofday(&end, NULL);
@@ -208,12 +202,10 @@ static int do_run_multi_threaded(struct target *target,
 
 	printf("    Average time per event %.3f usec\n",
 		time_average / event_average);
-err_out:
-	perf_env__exit(&host_env);
-	return err;
+	return 0;
 }
 
-static int run_multi_threaded(void)
+static int run_multi_threaded(struct perf_env *host_env)
 {
 	struct target target = {
 		.cpu_list = "0"
@@ -239,7 +231,7 @@ static int run_multi_threaded(void)
 		printf("  Number of synthesis threads: %u\n",
 			nr_threads_synthesize);
 
-		err = do_run_multi_threaded(&target, nr_threads_synthesize);
+		err = do_run_multi_threaded(host_env, &target, nr_threads_synthesize);
 		if (err)
 			return err;
 	}
@@ -249,6 +241,7 @@ static int run_multi_threaded(void)
 
 int bench_synthesize(int argc, const char **argv)
 {
+	struct perf_env host_env;
 	int err = 0;
 
 	argc = parse_options(argc, argv, options, bench_usage, 0);
@@ -264,11 +257,24 @@ int bench_synthesize(int argc, const char **argv)
 	if (!run_st && !run_mt)
 		run_st = true;
 
+	/*
+	 * Initialize libelf, otherwise reading build IDs fails and the DSO
+	 * build ID cache is never populated.
+	 */
+	perf_env__init(&host_env);
+	err = symbol__init(&host_env);
+	if (err < 0) {
+		perf_env__exit(&host_env);
+		return err;
+	}
+
 	if (run_st)
-		err = run_single_threaded();
+		err = run_single_threaded(&host_env);
 
 	if (!err && run_mt)
-		err = run_multi_threaded();
+		err = run_multi_threaded(&host_env);
 
+	symbol__exit();
+	perf_env__exit(&host_env);
 	return err;
 }
-- 
2.56.1.915.g69d5ec7298-goog


  parent reply	other threads:[~2026-12-04  6:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  6:23 [PATCH v3 0/7] perf synthetic-events: Use PROCMAP_QUERY for mmap synthesis Ian Rogers
2026-12-04  6:23 ` [PATCH v3 1/7] perf machine: Fix leak when creating host kernel maps fails Ian Rogers
2026-12-04  6:23 ` [PATCH v3 2/7] perf synthetic-events: Fix race on DSO build IDs when synthesizing Ian Rogers
2026-12-04  6:23 ` Ian Rogers [this message]
2026-12-04  6:23 ` [PATCH v3 4/7] tools headers UAPI: Synchronize linux/fs.h with the kernel sources Ian Rogers
2026-12-04  6:23 ` [PATCH v3 5/7] perf synthetic-events: Use PROCMAP_QUERY to synthesize mmap events Ian Rogers
2026-12-04  6:23 ` [PATCH v3 6/7] perf bench synthesize: Add --procmap-query option Ian Rogers
2026-12-04  6:23 ` [PATCH v3 7/7] perf test: Add test comparing PROCMAP_QUERY and /proc/pid/maps synthesis Ian Rogers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204062328.2508164-4-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=andrii@kernel.org \
    --cc=dsahern@gmail.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox