]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf sample: Add evsel to struct perf_sample
authorIan Rogers <irogers@google.com>
Sat, 4 Apr 2026 03:43:03 +0000 (20:43 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Mon, 6 Apr 2026 06:12:15 +0000 (23:12 -0700)
Add the evsel from evsel__parse_sample into the struct
perf_sample. Sometimes we want to alter the evsel associated with a
sample, such as with off-cpu bpf-output events. In general the evsel
and perf_sample are passed as a pair, but this makes an altered evsel
something of a chore to keep checking for and setting up. Later
patches will remove passing an evsel with the perf_sample and switch
to just using the perf_sample's value.

Signed-off-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/builtin-inject.c
tools/perf/builtin-script.c
tools/perf/tests/hists_cumulate.c
tools/perf/tests/hists_filter.c
tools/perf/tests/hists_output.c
tools/perf/util/evsel.c
tools/perf/util/sample.c
tools/perf/util/sample.h
tools/perf/util/session.c

index 952e6f6f31687e8744b64fe9ff2aa0e7971e663f..b4add7a70b22f015d672e365e4b2abba3604fb09 100644 (file)
@@ -133,7 +133,7 @@ struct perf_inject {
        struct perf_file_section secs[HEADER_FEAT_BITS];
        struct guest_session    guest_session;
        struct strlist          *known_build_ids;
-       const struct evsel      *mmap_evsel;
+       struct evsel            *mmap_evsel;
        struct ip_callchain     *raw_callchain;
 };
 
@@ -519,7 +519,7 @@ static struct dso *findnew_dso(int pid, int tid, const char *filename,
  * processing mmap events. If not stashed, search the evlist for the first mmap
  * gathering event.
  */
-static const struct evsel *inject__mmap_evsel(struct perf_inject *inject)
+static struct evsel *inject__mmap_evsel(struct perf_inject *inject)
 {
        struct evsel *pos;
 
@@ -1023,7 +1023,6 @@ int perf_event__inject_buildid(const struct perf_tool *tool, union perf_event *e
 
        sample__for_each_callchain_node(thread, evsel, sample, PERF_MAX_STACK_DEPTH,
                                        /*symbols=*/false, mark_dso_hit_callback, &args);
-
        thread__put(thread);
 repipe:
        perf_event__repipe(tool, event, sample, machine);
@@ -1432,6 +1431,7 @@ static int synthesize_build_id(struct perf_inject *inject, struct dso *dso, pid_
 {
        struct machine *machine = perf_session__findnew_machine(inject->session, machine_pid);
        struct perf_sample synth_sample = {
+               .evsel     = inject__mmap_evsel(inject),
                .pid       = -1,
                .tid       = -1,
                .time      = -1,
index 622130d3aed4a4e5103d0da660df80596287ecd9..42d4cc1620397b9f2afa3b529f730cc97e68ff4d 100644 (file)
@@ -2910,8 +2910,12 @@ static int print_event_with_time(const struct perf_tool *tool,
                thread = machine__findnew_thread(machine, pid, tid);
 
        if (evsel) {
+               struct evsel *saved_evsel = sample->evsel;
+
+               sample->evsel = evsel;
                perf_sample__fprintf_start(script, sample, thread, evsel,
                                           event->header.type, stdout);
+               sample->evsel = saved_evsel;
        }
 
        perf_event__fprintf(event, machine, stdout);
index 3eb9ef8d7ec634cbb22227c3670e017fb72a897f..606aa926a8fca117bbaf6e920ee334cc0673aebf 100644 (file)
@@ -81,7 +81,7 @@ static int add_hist_entries(struct hists *hists, struct machine *machine)
 {
        struct addr_location al;
        struct evsel *evsel = hists_to_evsel(hists);
-       struct perf_sample sample = { .period = 1000, };
+       struct perf_sample sample = { .evsel = evsel, .period = 1000, };
        size_t i;
 
        addr_location__init(&al);
index 1cebd20cc91c031fde4f7e5e6c33dadf5279025d..cc6b26e373d13d2d5a18db7231153847222584ea 100644 (file)
@@ -70,6 +70,7 @@ static int add_hist_entries(struct evlist *evlist,
                        };
                        struct hists *hists = evsel__hists(evsel);
 
+                       sample.evsel = evsel;
                        /* make sure it has no filter at first */
                        hists->thread_filter = NULL;
                        hists->dso_filter = NULL;
index ee5ec8bda60e5cd8661d9f4dd72328a502c4ce1b..7818950d786e1e32c58cd0d0ba5cba4287a8eaf4 100644 (file)
@@ -51,7 +51,7 @@ static int add_hist_entries(struct hists *hists, struct machine *machine)
 {
        struct addr_location al;
        struct evsel *evsel = hists_to_evsel(hists);
-       struct perf_sample sample = { .period = 100, };
+       struct perf_sample sample = { .evsel = evsel, .period = 100, };
        size_t i;
 
        addr_location__init(&al);
index df2392713edb39342c7ee592f9b0d2b1f94a09cc..2ee87fd84d3ead49fe8fc2d748d14552e6617351 100644 (file)
@@ -3226,6 +3226,7 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event,
        union u64_swap u;
 
        perf_sample__init(data, /*all=*/true);
+       data->evsel = evsel;
        data->cpu = data->pid = data->tid = -1;
        data->stream_id = data->id = data->time = -1ULL;
        data->period = evsel->core.attr.sample_period;
index 2a30de4573f67c26b320b058cab657fbfca05c67..cf73329326d72042ea100103aadc82a5f81558e7 100644 (file)
@@ -19,6 +19,7 @@ void perf_sample__init(struct perf_sample *sample, bool all)
        if (all) {
                memset(sample, 0, sizeof(*sample));
        } else {
+               sample->evsel = NULL;
                sample->user_regs = NULL;
                sample->intr_regs = NULL;
                sample->merged_callchain = false;
index fea7c77ff80271b08163aec46ec833b1f3e142dc..3d27a0daef8fb2d72ffa8510923857e73130dd40 100644 (file)
@@ -5,6 +5,7 @@
 #include <linux/perf_event.h>
 #include <linux/types.h>
 
+struct evsel;
 struct machine;
 struct thread;
 
@@ -102,6 +103,8 @@ struct simd_flags {
  * and clean up these values.
  */
 struct perf_sample {
+       /** @evsel: Backward reference to the evsel used when constructing the sample. */
+       struct evsel *evsel;
        /** @ip: The sample event PERF_SAMPLE_IP value. */
        u64 ip;
        /** @pid: The sample event PERF_SAMPLE_TID pid value. */
index 53fb2e628b713b7165d054e6ee9ee30c3ede888e..7588cca110d2b08f64e1515f911f8e223eb2820b 100644 (file)
@@ -1264,8 +1264,9 @@ static int deliver_sample_value(struct evlist *evlist,
                                bool per_thread)
 {
        struct perf_sample_id *sid = evlist__id2sid(evlist, v->id);
-       struct evsel *evsel;
+       struct evsel *saved_evsel = sample->evsel;
        u64 *storage = NULL;
+       int ret;
 
        if (sid) {
                storage = perf_sample_id__get_period_storage(sid, sample->tid, per_thread);
@@ -1289,8 +1290,10 @@ static int deliver_sample_value(struct evlist *evlist,
        if (!sample->period)
                return 0;
 
-       evsel = container_of(sid->evsel, struct evsel, core);
-       return tool->sample(tool, event, sample, evsel, machine);
+       sample->evsel = container_of(sid->evsel, struct evsel, core);
+       ret = tool->sample(tool, event, sample, sample->evsel, machine);
+       sample->evsel = saved_evsel;
+       return ret;
 }
 
 static int deliver_sample_group(struct evlist *evlist,
@@ -1362,13 +1365,16 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,
                                              struct machine *machine)
 {
        struct deferred_event *de, *tmp;
-       struct evsel *evsel;
        int ret = 0;
 
        if (!tool->merge_deferred_callchains) {
-               evsel = evlist__id2evsel(evlist, sample->id);
-               return tool->callchain_deferred(tool, event, sample,
-                                               evsel, machine);
+               struct evsel *saved_evsel = sample->evsel;
+
+               sample->evsel = evlist__id2evsel(evlist, sample->id);
+               ret = tool->callchain_deferred(tool, event, sample,
+                                              sample->evsel, machine);
+               sample->evsel = saved_evsel;
+               return ret;
        }
 
        list_for_each_entry_safe(de, tmp, &evlist->deferred_samples, list) {
@@ -1392,9 +1398,9 @@ static int evlist__deliver_deferred_callchain(struct evlist *evlist,
                else
                        orig_sample.deferred_callchain = false;
 
-               evsel = evlist__id2evsel(evlist, orig_sample.id);
+               orig_sample.evsel = evlist__id2evsel(evlist, orig_sample.id);
                ret = evlist__deliver_sample(evlist, tool, de->event,
-                                            &orig_sample, evsel, machine);
+                                            &orig_sample, orig_sample.evsel, machine);
 
                perf_sample__exit(&orig_sample);
                list_del(&de->list);
@@ -1417,7 +1423,6 @@ static int session__flush_deferred_samples(struct perf_session *session,
        struct evlist *evlist = session->evlist;
        struct machine *machine = &session->machines.host;
        struct deferred_event *de, *tmp;
-       struct evsel *evsel;
        int ret = 0;
 
        list_for_each_entry_safe(de, tmp, &evlist->deferred_samples, list) {
@@ -1431,9 +1436,9 @@ static int session__flush_deferred_samples(struct perf_session *session,
                        break;
                }
 
-               evsel = evlist__id2evsel(evlist, sample.id);
+               sample.evsel = evlist__id2evsel(evlist, sample.id);
                ret = evlist__deliver_sample(evlist, tool, de->event,
-                                            &sample, evsel, machine);
+                                            &sample, sample.evsel, machine);
 
                perf_sample__exit(&sample);
                list_del(&de->list);
@@ -1458,8 +1463,12 @@ static int machines__deliver_event(struct machines *machines,
 
        dump_event(evlist, event, file_offset, sample, file_path);
 
-       evsel = evlist__id2evsel(evlist, sample->id);
+       if (!sample->evsel)
+               sample->evsel = evlist__id2evsel(evlist, sample->id);
+       else
+               assert(sample->evsel == evlist__id2evsel(evlist, sample->id));
 
+       evsel = sample->evsel;
        machine = machines__find_for_cpumode(machines, event, sample);
 
        switch (event->header.type) {