[PATCH v1 01/11] perf tools: Add HEADER_BOOT_ID feature
From: Namhyung Kim
Date: Fri Oct 23 2026 - 03:20:55 EST
Save /proc/sys/kernel/random/boot_id in the perf.data header and
struct perf_env so post-processing tools can identify if it's the
same kernel boot on which the data was recorded.
Assisted-by: LLM
Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
---
.../Documentation/perf.data-file-format.txt | 5 +++
tools/perf/builtin-inject.c | 1 +
tools/perf/util/env.c | 44 +++++++++++++++++++
tools/perf/util/env.h | 2 +
tools/perf/util/header.c | 28 ++++++++++++
tools/perf/util/header.h | 1 +
6 files changed, 81 insertions(+)
diff --git a/tools/perf/Documentation/perf.data-file-format.txt b/tools/perf/Documentation/perf.data-file-format.txt
index 1f8e7d8e9ae21d73..359d6ef0c80aac26 100644
--- a/tools/perf/Documentation/perf.data-file-format.txt
+++ b/tools/perf/Documentation/perf.data-file-format.txt
@@ -491,6 +491,11 @@ struct memory_range {
u8 remote_region_id;
}; [nr_memory_ranges]
+ HEADER_BOOT_ID = 36,
+
+ A perf_header_string with the kernel boot ID read from
+ /proc/sys/kernel/random/boot_id.
+
other bits are reserved and should be ignored for now
HEADER_FEAT_BITS = 256,
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index ce7562fc788c246e..ddb21d888e00d143 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -2307,6 +2307,7 @@ static bool keep_feat(struct perf_inject *inject, int feat)
case HEADER_CPU_DOMAIN_INFO:
case HEADER_CLN_SIZE:
case HEADER_MEMORY_RANGES:
+ case HEADER_BOOT_ID:
return true;
/* Information that can be updated */
case HEADER_BUILD_ID:
diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c
index 50993b0dedc577c7..8ca0b677f3172b5f 100644
--- a/tools/perf/util/env.c
+++ b/tools/perf/util/env.c
@@ -12,6 +12,7 @@
#include <linux/string.h>
#include <linux/zalloc.h>
#include "cgroup.h"
+#include <api/fs/fs.h>
#include <errno.h>
#include <sys/utsname.h>
#include <stdlib.h>
@@ -258,6 +259,7 @@ void perf_env__exit(struct perf_env *env)
zfree(&env->os_release);
zfree(&env->version);
zfree(&env->arch);
+ zfree(&env->boot_id);
zfree(&env->cpu_desc);
zfree(&env->cpuid);
zfree(&env->cmdline);
@@ -400,6 +402,48 @@ const char *perf_env__os_release(struct perf_env *env)
return release;
}
+static char *read_host_boot_id(void)
+{
+ char *buf = NULL;
+ size_t sz;
+
+ if (procfs__read_str("sys/kernel/random/boot_id", &buf, &sz) < 0)
+ return NULL;
+
+ strim(buf);
+ return buf;
+}
+
+const char *perf_env__boot_id(struct perf_env *env)
+{
+ const char *boot_id;
+
+ if (!env)
+ return NULL;
+
+ mutex_lock(&env->lock);
+ if (env->boot_id) {
+ boot_id = env->boot_id;
+ goto out;
+ }
+
+ /*
+ * If env->arch or env->hostname is set, this is an offline target
+ * environment. If the boot_id is not populated in the file, do not
+ * fall back to the host's boot_id.
+ */
+ if (env->arch || env->hostname) {
+ boot_id = NULL;
+ goto out;
+ }
+
+ env->boot_id = read_host_boot_id();
+ boot_id = env->boot_id;
+out:
+ mutex_unlock(&env->lock);
+ return boot_id;
+}
+
int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
{
int i;
diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h
index 23d35425efe38b24..772d11eed1bc2a7c 100644
--- a/tools/perf/util/env.h
+++ b/tools/perf/util/env.h
@@ -81,6 +81,7 @@ struct perf_env {
char *os_release;
char *version;
char *arch;
+ char *boot_id;
/* e_machine expanded from 16 to 32-bits for alignment. */
u32 e_machine;
u32 e_flags;
@@ -187,6 +188,7 @@ void perf_env__exit(struct perf_env *env);
int perf_env__kernel_is_64_bit(struct perf_env *env);
bool perf_arch_is_big_endian(const char *arch);
const char *perf_env__os_release(struct perf_env *env);
+const char *perf_env__boot_id(struct perf_env *env);
int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]);
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 9eec6deee190ff21..3e0b6fce55973dd1 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -494,6 +494,27 @@ static int write_version(struct feat_fd *ff,
return do_write_string(ff, perf_version_string);
}
+static int write_boot_id(struct feat_fd *ff, struct evlist *evlist)
+{
+ const char *boot_id = NULL;
+ char *buf = NULL;
+ size_t sz;
+ int ret;
+
+ if (evlist__session(evlist))
+ boot_id = perf_env__boot_id(perf_session__env(evlist__session(evlist)));
+
+ if (!boot_id) {
+ if (procfs__read_str("sys/kernel/random/boot_id", &buf, &sz) < 0)
+ return -1;
+ boot_id = strim(buf);
+ }
+
+ ret = do_write_string(ff, boot_id);
+ free(buf);
+ return ret;
+}
+
static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
{
FILE *file;
@@ -2079,6 +2100,11 @@ static void print_version(struct feat_fd *ff, FILE *fp)
fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
}
+static void print_boot_id(struct feat_fd *ff, FILE *fp)
+{
+ fprintf(fp, "# boot id : %s\n", ff->ph->env.boot_id);
+}
+
static void print_cmdline(struct feat_fd *ff, FILE *fp)
{
int nr, i;
@@ -3022,6 +3048,7 @@ FEAT_PROCESS_STR_FUN(osrelease, os_release);
FEAT_PROCESS_STR_FUN(version, version);
FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
FEAT_PROCESS_STR_FUN(cpuid, cpuid);
+FEAT_PROCESS_STR_FUN(boot_id, boot_id);
static int process_arch(struct feat_fd *ff, void *data __maybe_unused)
{
@@ -4496,6 +4523,7 @@ const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
FEAT_OPR(E_MACHINE, e_machine, false),
FEAT_OPR(CLN_SIZE, cln_size, false),
FEAT_OPR(MEMORY_RANGES, memory_ranges, false),
+ FEAT_OPR(BOOT_ID, boot_id, false),
};
struct header_print_data {
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index 765037762758ca11..5bbf16342b4d26d8 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -57,6 +57,7 @@ enum {
HEADER_E_MACHINE,
HEADER_CLN_SIZE,
HEADER_MEMORY_RANGES,
+ HEADER_BOOT_ID,
HEADER_LAST_FEATURE,
HEADER_FEAT_BITS = 256,
};
--
2.56.0.257.gf7497c7d27-goog