[PATCH v3 0/2] tracing: add ring-buffer memory usage statistics

From: Xiang Gao

Date: Wed Sep 16 2026 - 02:33:46 EST


Report the memory consumed by the tracing ring buffers, rather than the
usable data capacity exposed by buffer_size_kb. Android low-memory
diagnostics need this to attribute the memory used by tracing when
calculating lost RAM.

The buffers can be spread across the global trace array, dynamically
created instances, and snapshot buffers. Userspace currently has to
discover and sum every instance, and snapshot memory is not exposed by
the per-instance totals.

Patch 1 adds:

/sys/kernel/tracing/trace_stats/memory_usage_kb

reporting:

buffers: ...
snapshot_buffers: ...

covering the global trace array and all instances across all CPUs.

Patch 2 adds the per-CPU view under
trace_stats/per_cpu/cpuN/memory_usage_kb.

The values account for the full pages backing the data sub-buffers and
reader page, plus the cached read page and mmap metadata page when
present. Slab-allocated ring-buffer metadata is not included.

Changes since v2:
- Account for the bootstrapping temp_buffer (Donggeun Yoo).
- Rebase onto latest mainline; adapt to free_page becoming struct
buffer_data_read_page.

Tested on arm64 QEMU with global buffer resize, snapshot alloc/free,
instance create/remove, and per-CPU aggregation, all against the
baseline that includes temp_buffer (3 pages per CPU).

Xiang Gao (2):
tracing: add ring-buffer memory usage statistics in tracefs
tracing: add per-CPU memory usage statistics in tracefs

Documentation/trace/ftrace.rst | 15 +++++
include/linux/ring_buffer.h | 1 +
kernel/trace/ring_buffer.c | 41 ++++++++++++
kernel/trace/trace.c | 118 +++++++++++++++++++++++++++++++++
4 files changed, 175 insertions(+)

--
2.34.1