perf record: collect BPF metadata from new programs
authorBlake Jones <blakejones@google.com>
Thu, 12 Jun 2025 19:49:37 +0000 (12:49 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Fri, 20 Jun 2025 21:48:49 +0000 (14:48 -0700)
This collects metadata for any BPF programs that were loaded during a
"perf record" run, and emits it at the end of the run.

Signed-off-by: Blake Jones <blakejones@google.com>
Link: https://lore.kernel.org/r/20250612194939.162730-4-blakejones@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/builtin-record.c
tools/perf/util/bpf-event.c
tools/perf/util/bpf-event.h
tools/perf/util/env.c
tools/perf/util/env.h
tools/perf/util/header.c
tools/perf/util/synthetic-events.h

index 0b566f300569ab021b4ea9d2cac85cd08f42be8f..53971b9de3ba603948936118f60dc54b8aaf5fc3 100644 (file)
@@ -2162,6 +2162,14 @@ out:
        return err;
 }
 
+static void record__synthesize_final_bpf_metadata(struct record *rec __maybe_unused)
+{
+#ifdef HAVE_LIBBPF_SUPPORT
+       perf_event__synthesize_final_bpf_metadata(rec->session,
+                                                 process_synthesized_event);
+#endif
+}
+
 static int record__process_signal_event(union perf_event *event __maybe_unused, void *data)
 {
        struct record *rec = data;
@@ -2807,6 +2815,8 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
        trigger_off(&auxtrace_snapshot_trigger);
        trigger_off(&switch_output_trigger);
 
+       record__synthesize_final_bpf_metadata(rec);
+
        if (opts->auxtrace_snapshot_on_exit)
                record__auxtrace_snapshot_exit(rec);
 
index 1f6e76ee602448fa1c5eb961703647f8d31e7774..dc09a4730c50ce9280e942b925a80cfd59e85e9d 100644 (file)
@@ -472,6 +472,49 @@ void bpf_metadata_free(struct bpf_metadata *metadata __maybe_unused)
 
 #endif /* HAVE_LIBBPF_STRINGS_SUPPORT */
 
+struct bpf_metadata_final_ctx {
+       const struct perf_tool *tool;
+       perf_event__handler_t process;
+       struct machine *machine;
+};
+
+static void synthesize_final_bpf_metadata_cb(struct bpf_prog_info_node *node,
+                                            void *data)
+{
+       struct bpf_metadata_final_ctx *ctx = (struct bpf_metadata_final_ctx *)data;
+       struct bpf_metadata *metadata = node->metadata;
+       int err;
+
+       if (metadata == NULL)
+               return;
+       err = synthesize_perf_record_bpf_metadata(metadata, ctx->tool,
+                                                 ctx->process, ctx->machine);
+       if (err != 0) {
+               const char *prog_name = metadata->prog_names[0];
+
+               if (prog_name != NULL)
+                       pr_warning("Couldn't synthesize final BPF metadata for %s.\n", prog_name);
+               else
+                       pr_warning("Couldn't synthesize final BPF metadata.\n");
+       }
+       bpf_metadata_free(metadata);
+       node->metadata = NULL;
+}
+
+void perf_event__synthesize_final_bpf_metadata(struct perf_session *session,
+                                              perf_event__handler_t process)
+{
+       struct perf_env *env = &session->header.env;
+       struct bpf_metadata_final_ctx ctx = {
+               .tool = session->tool,
+               .process = process,
+               .machine = &session->machines.host,
+       };
+
+       perf_env__iterate_bpf_prog_info(env, synthesize_final_bpf_metadata_cb,
+                                       &ctx);
+}
+
 /*
  * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf
  * program. One PERF_RECORD_BPF_EVENT is generated for the program. And
@@ -612,6 +655,7 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
                }
 
                info_node->info_linear = info_linear;
+               info_node->metadata = NULL;
                if (!perf_env__insert_bpf_prog_info(env, info_node)) {
                        free(info_linear);
                        free(info_node);
@@ -803,6 +847,7 @@ static void perf_env__add_bpf_info(struct perf_env *env, u32 id)
        arrays |= 1UL << PERF_BPIL_JITED_INSNS;
        arrays |= 1UL << PERF_BPIL_LINE_INFO;
        arrays |= 1UL << PERF_BPIL_JITED_LINE_INFO;
+       arrays |= 1UL << PERF_BPIL_MAP_IDS;
 
        info_linear = get_bpf_prog_info_linear(fd, arrays);
        if (IS_ERR_OR_NULL(info_linear)) {
@@ -815,6 +860,7 @@ static void perf_env__add_bpf_info(struct perf_env *env, u32 id)
        info_node = malloc(sizeof(struct bpf_prog_info_node));
        if (info_node) {
                info_node->info_linear = info_linear;
+               info_node->metadata = bpf_metadata_create(&info_linear->info);
                if (!perf_env__insert_bpf_prog_info(env, info_node)) {
                        free(info_linear);
                        free(info_node);
index ef2dd3f1619ecc41c93bb1d88b9f43522310127e..60d2c6637af5d6eb00ca22df8ad6659d082054ec 100644 (file)
@@ -25,6 +25,7 @@ struct bpf_metadata {
 
 struct bpf_prog_info_node {
        struct perf_bpil                *info_linear;
+       struct bpf_metadata             *metadata;
        struct rb_node                  rb_node;
 };
 
index 36411749e0077ef562e17538846b17fa72450721..05a4f2657d7288734cc278335b37b0b446072ff7 100644 (file)
@@ -3,8 +3,10 @@
 #include "debug.h"
 #include "env.h"
 #include "util/header.h"
-#include "linux/compiler.h"
+#include "util/rwsem.h"
+#include <linux/compiler.h>
 #include <linux/ctype.h>
+#include <linux/rbtree.h>
 #include <linux/string.h>
 #include <linux/zalloc.h>
 #include "cgroup.h"
@@ -89,6 +91,20 @@ out:
        return node;
 }
 
+void perf_env__iterate_bpf_prog_info(struct perf_env *env,
+                                    void (*cb)(struct bpf_prog_info_node *node,
+                                               void *data),
+                                    void *data)
+{
+       struct rb_node *first;
+
+       down_read(&env->bpf_progs.lock);
+       first = rb_first(&env->bpf_progs.infos);
+       for (struct rb_node *node = first; node != NULL; node = rb_next(node))
+               (*cb)(rb_entry(node, struct bpf_prog_info_node, rb_node), data);
+       up_read(&env->bpf_progs.lock);
+}
+
 bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node)
 {
        bool ret;
@@ -174,6 +190,7 @@ static void perf_env__purge_bpf(struct perf_env *env)
                next = rb_next(&node->rb_node);
                rb_erase(&node->rb_node, root);
                zfree(&node->info_linear);
+               bpf_metadata_free(node->metadata);
                free(node);
        }
 
index d90e343cf1fa5a661fdae651f24e02345371a2aa..c90c1d717e73dd6bef227f816c7c111f64a9e956 100644 (file)
@@ -174,16 +174,22 @@ const char *perf_env__raw_arch(struct perf_env *env);
 int perf_env__nr_cpus_avail(struct perf_env *env);
 
 void perf_env__init(struct perf_env *env);
+#ifdef HAVE_LIBBPF_SUPPORT
 bool __perf_env__insert_bpf_prog_info(struct perf_env *env,
                                      struct bpf_prog_info_node *info_node);
 bool perf_env__insert_bpf_prog_info(struct perf_env *env,
                                    struct bpf_prog_info_node *info_node);
 struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *env,
                                                        __u32 prog_id);
+void perf_env__iterate_bpf_prog_info(struct perf_env *env,
+                                    void (*cb)(struct bpf_prog_info_node *node,
+                                               void *data),
+                                    void *data);
 bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node);
 bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node);
 struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id);
 struct btf_node *__perf_env__find_btf(struct perf_env *env, __u32 btf_id);
+#endif // HAVE_LIBBPF_SUPPORT
 
 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu);
 char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name,
index d7f6ff6974aa318175b2a974eea9d8008197e4dc..2dea35237e81e6c97d8b4e476d24b0bde903a430 100644 (file)
@@ -3145,6 +3145,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
                /* after reading from file, translate offset to address */
                bpil_offs_to_addr(info_linear);
                info_node->info_linear = info_linear;
+               info_node->metadata = NULL;
                if (!__perf_env__insert_bpf_prog_info(env, info_node)) {
                        free(info_linear);
                        free(info_node);
index b9c936b5cfeb9c377895de3dd389fc6bb2d6a2ac..ee29615d68e57fc38269fcf9383bb6a4ed4743b3 100644 (file)
@@ -92,6 +92,8 @@ int perf_event__synthesize_threads(const struct perf_tool *tool, perf_event__han
 int perf_event__synthesize_tracing_data(const struct perf_tool *tool, int fd, struct evlist *evlist, perf_event__handler_t process);
 int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc, const struct perf_tool *tool, perf_event__handler_t process, struct machine *machine);
 pid_t perf_event__synthesize_comm(const struct perf_tool *tool, union perf_event *event, pid_t pid, perf_event__handler_t process, struct machine *machine);
+void perf_event__synthesize_final_bpf_metadata(struct perf_session *session,
+                                              perf_event__handler_t process);
 
 int perf_tool__process_synth_event(const struct perf_tool *tool, union perf_event *event, struct machine *machine, perf_event__handler_t process);