[PATCH v5 3/4] tracing: add per-CPU buffer memory usage statistics

From: Xiang Gao

Date: Thu Nov 05 2026 - 09:03:53 EST


Provide a per-CPU view of the tracing ring-buffer memory usage under
trace_stats/per_cpu/cpuN/memory_usage_kb, using the same categorized
format as the aggregate file. Each file reports only the buffers
belonging to that CPU.

Signed-off-by: Xiang Gao <gaoxiang17@xxxxxxxxxx>
---
kernel/trace/trace.c | 75 +++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 74 insertions(+), 1 deletion(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 6036e368fe10..e7e23ff38105 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -5898,6 +5898,60 @@ static const struct file_operations trace_mem_fops = {
.release = single_release,
};

+static struct trace_mem_stats trace_buffers_memory_cpu(int cpu)
+{
+ struct trace_mem_stats stats = {};
+ struct trace_array *tr;
+
+ guard(mutex)(&trace_types_lock);
+
+ list_for_each_entry(tr, &ftrace_trace_arrays, list)
+ trace_array_buffer_memory(tr, cpu, &stats);
+
+ /* Account for the bootstrapping temp_buffer as well. */
+ if (temp_buffer)
+ stats.global += ring_buffer_memory_size(temp_buffer, cpu);
+
+ stats.total_system = stats.global + stats.instances +
+ stats.snapshots + stats.remotes;
+ stats.total_carveout = stats.persistents;
+
+ return stats;
+}
+
+static int trace_mem_per_cpu_show(struct seq_file *m, void *v)
+{
+ struct trace_mem_stats stats = trace_buffers_memory_cpu((long)m->private);
+
+ seq_printf(m, "global: %lu\n", stats.global >> 10);
+ seq_printf(m, "instances: %lu\n", stats.instances >> 10);
+ seq_printf(m, "snapshots: %lu\n", stats.snapshots >> 10);
+ seq_printf(m, "persistents: %lu\n", stats.persistents >> 10);
+ seq_printf(m, "remotes: %lu\n", stats.remotes >> 10);
+ seq_printf(m, "total_system: %lu\n", stats.total_system >> 10);
+ seq_printf(m, "total_carveout: %lu\n", stats.total_carveout >> 10);
+
+ return 0;
+}
+
+static int trace_mem_per_cpu_open(struct inode *inode, struct file *file)
+{
+ int ret;
+
+ ret = tracing_check_open_get_tr(NULL);
+ if (ret)
+ return ret;
+
+ return single_open(file, trace_mem_per_cpu_show, inode->i_private);
+}
+
+static const struct file_operations trace_mem_per_cpu_fops = {
+ .open = trace_mem_per_cpu_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
#define LAST_BOOT_HEADER ((void *)1)

static void *l_next(struct seq_file *m, void *v, loff_t *pos)
@@ -9416,7 +9470,9 @@ static struct notifier_block trace_module_nb = {

static __init void init_trace_stats_tracefs(void)
{
- struct dentry *stats_dir;
+ struct dentry *stats_dir, *per_cpu_dir, *cpu_dir;
+ char cpu_dir_name[30];
+ int cpu;

stats_dir = tracefs_create_dir("trace_stats", NULL);
if (!stats_dir)
@@ -9424,6 +9480,23 @@ static __init void init_trace_stats_tracefs(void)

trace_create_file("memory_usage_kb", TRACE_MODE_READ, stats_dir,
NULL, &trace_mem_fops);
+
+ per_cpu_dir = tracefs_create_dir("per_cpu", stats_dir);
+ if (!per_cpu_dir)
+ return;
+
+ for_each_tracing_cpu(cpu) {
+ snprintf(cpu_dir_name, 30, "cpu%d", cpu);
+ cpu_dir = tracefs_create_dir(cpu_dir_name, per_cpu_dir);
+ if (!cpu_dir) {
+ pr_warn("Could not create tracefs '%s' entry\n",
+ cpu_dir_name);
+ continue;
+ }
+
+ trace_create_file("memory_usage_kb", TRACE_MODE_READ, cpu_dir,
+ (void *)(long)cpu, &trace_mem_per_cpu_fops);
+ }
}

static __init void tracer_init_tracefs_work_func(struct work_struct *work)
--
2.34.1