[PATCH v2 22/25] perf evlist: Try to avoid computing evsel from sample

Ian Rogers
2026-03-19T23:24:52Z

If the sample has an evsel, don't recompute using the sample.id.

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-inject.c | 5 +++--
tools/perf/builtin-top.c | 4 +++-
tools/perf/tests/mmap-basic.c | 4 +++-
tools/perf/tests/switch-tracking.c | 5 ++++-
tools/perf/util/intel-pt.c | 10 +++++++---
5 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 6e265b66a551..9ea082bccbe6 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -1693,11 +1693,12 @@ static int guest_session__fetch(struct guest_session *gs)
static int evlist__append_id_sample(struct evlist *evlist, union perf_event *ev,
const struct perf_sample *sample)
{
- struct evsel *evsel;
+ struct evsel *evsel = sample->evsel;
void *array;
int ret;

- evsel = evlist__id2evsel(evlist, sample->id);
+ if (!evsel)
+ evsel = evlist__id2evsel(evlist, sample->id);
array = ev;

if (!evsel) {
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index b89ca48e5d02..faa422e35166 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1163,7 +1163,9 @@ static int deliver_event(struct ordered_events *qe,
goto next_event;
}

- evsel = evlist__id2evsel(session->evlist, sample.id);
+ evsel = sample.evsel;
+ if (!evsel)
+ evsel = evlist__id2evsel(session->evlist, sample.id);
assert(evsel != NULL);

if (event->header.type == PERF_RECORD_SAMPLE) {
diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c
index 3313c236104e..a18d84d858aa 100644
--- a/tools/perf/tests/mmap-basic.c
+++ b/tools/perf/tests/mmap-basic.c
@@ -142,7 +142,9 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest
}

err = -1;
- evsel = evlist__id2evsel(evlist, sample.id);
+ evsel = sample.evsel;
+ if (!evsel)
+ evsel = evlist__id2evsel(evlist, sample.id);
perf_sample__exit(&sample);
if (evsel == NULL) {
pr_debug("event with id %" PRIu64
diff --git a/tools/perf/tests/switch-tracking.c b/tools/perf/tests/switch-tracking.c
index a7ea4b7874be..ed17c3be71c4 100644
--- a/tools/perf/tests/switch-tracking.c
+++ b/tools/perf/tests/switch-tracking.c
@@ -138,7 +138,10 @@ static int process_sample_event(struct evlist *evlist,
goto out;
}

- evsel = evlist__id2evsel(evlist, sample.id);
+ evsel = sample.evsel;
+ if (!evsel)
+ evsel = evlist__id2evsel(evlist, sample.id);
+
if (evsel == switch_tracking->switch_evsel) {
next_tid = perf_sample__intval(&sample, "next_pid");
prev_tid = perf_sample__intval(&sample, "prev_pid");
diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index dab23a96b1d8..3d759546a7c0 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -3421,7 +3421,10 @@ static int intel_pt_process_switch(struct intel_pt *pt,
{
pid_t tid;
int cpu, ret;
- struct evsel *evsel = evlist__id2evsel(pt->session->evlist, sample->id);
+ struct evsel *evsel = sample->evsel;
+
+ if (!evsel)
+ evsel = evlist__id2evsel(pt->session->evlist, sample->id);

if (evsel != pt->switch_evsel)
return 0;
@@ -3611,10 +3614,11 @@ static int intel_pt_process_aux_output_hw_id(struct intel_pt *pt,
u64 hw_id = event->aux_output_hw_id.hw_id;
struct auxtrace_queue *queue;
struct intel_pt_queue *ptq;
- struct evsel *evsel;
+ struct evsel *evsel = sample->evsel;

queue = auxtrace_queues__sample_queue(&pt->queues, sample, pt->session);
- evsel = evlist__id2evsel_strict(pt->session->evlist, sample->id);
+ if (!evsel)
+ evsel = evlist__id2evsel_strict(pt->session->evlist, sample->id);
if (!queue || !queue->priv || !evsel || hw_id > INTEL_PT_MAX_PEBS) {
pr_err("Bad AUX output hardware ID\n");
return -EINVAL;
--
2.53.0.959.g497ff81fa9-goog