From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
To: Vincent Donnefort <vdonnefort@google.com>
Cc: Xiang Gao <gxxa03070307@gmail.com>,
Steven Rostedt <rostedt@goodmis.org>,
Donggeun Yoo <donggeunyoo.kernel@gmail.com>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Lorenzo Stoakes <ljs@kernel.org>, gao xu <gaoxu2@honor.com>,
yinchuang1@xiaomi.com, linux-trace-kernel@vger.kernel.org,
linux-kernel@vger.kernel.org, Xiang Gao <gaoxiang17@xiaomi.com>
Subject: Re: [PATCH v4 1/2] tracing: add ring-buffer memory usage statistics in tracefs
Date: Fri, 25 Sep 2026 10:09:08 +0900 [thread overview]
Message-ID: <20260925100908.66c46353e5a2abeb73b1e15a@kernel.org> (raw)
In-Reply-To: <arTlRPvmR54Fa71o@google.com>
On Thu, 24 Sep 2026 09:54:28 +0100
Vincent Donnefort <vdonnefort@google.com> wrote:
> On Thu, Sep 24, 2026 at 10:27:55AM +0900, Masami Hiramatsu wrote:
> > On Tue, 22 Sep 2026 10:41:30 +0100
> > Vincent Donnefort <vdonnefort@google.com> wrote:
> >
> > > On Mon, Sep 21, 2026 at 07:30:45PM +0800, Xiang Gao wrote:
> > > > 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.
> > > >
> > > > Add a trace_stats directory with memory_usage_kb reporting:
> > > >
> > > > buffers:
> > > > snapshot_buffers:
> > > >
> > > > covering the global trace array, all instances, and the bootstrapping
> > > > temp_buffer across all CPUs.
> > > >
> > > > 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, as it is
> > > > already reported through Slab and would be double-counted when
> > > > subtracting tracing memory from lost RAM. Remote buffers, whose pages
> > > > are externally owned, report zero.
> > >
> > > For the next version, it is good practice to __not__ in-reply-to with previous
> > > version.
> > >
> >
> > Indeed. This is hard to find which is the latest version.
> >
> > > >
> > > > Signed-off-by: Xiang Gao <gaoxiang17@xiaomi.com>
> > > > ---
> > > > Documentation/trace/ftrace.rst | 12 +++++
> > > > include/linux/ring_buffer.h | 1 +
> > > > kernel/trace/ring_buffer.c | 41 +++++++++++++++
> > > > kernel/trace/trace.c | 95 ++++++++++++++++++++++++++++++++++
> > > > 4 files changed, 149 insertions(+)
> > > >
> > > > diff --git a/Documentation/trace/ftrace.rst b/Documentation/trace/ftrace.rst
> > > > index 7261f25f8b4b..99ddfe26b7cd 100644
> > > > --- a/Documentation/trace/ftrace.rst
> > > > +++ b/Documentation/trace/ftrace.rst
> > > > @@ -218,6 +218,18 @@ of ftrace. Here is a list of some of the key files:
> > > >
> > > > This displays the total combined size of all the trace buffers.
> > > >
> > > > + trace_stats/memory_usage_kb:
> > > > +
> > > > + This reports the memory consumed by the ring buffers, as opposed to
> > > > + the usable data capacity shown by buffer_size_kb. The value covers the
> > > > + main and snapshot buffers of the global trace array and all tracing
> > > > + instances. It does not include slab-allocated ring-buffer metadata.
> > > > +
> > > > + Output::
> > > > +
> > > > + buffers: ...
> > > > + snapshot_buffers: ...
> > > > +
> > > > buffer_subbuf_size_kb:
> > > >
> > > > This sets or displays the sub buffer size. The ring buffer is broken up
> > > > diff --git a/include/linux/ring_buffer.h b/include/linux/ring_buffer.h
> > > > index eac3e9080c3c..96b99e6757d4 100644
> > > > --- a/include/linux/ring_buffer.h
> > > > +++ b/include/linux/ring_buffer.h
> > > > @@ -167,6 +167,7 @@ int ring_buffer_iter_empty(struct ring_buffer_iter *iter);
> > > > bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter);
> > > >
> > > > unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu);
> > > > +unsigned long ring_buffer_memory_size(struct trace_buffer *buffer, int cpu);
> > > > unsigned long ring_buffer_max_event_size(struct trace_buffer *buffer);
> > > >
> > > > void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu);
> > > > diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
> > > > index 04bb94c29f58..efb88bf8970c 100644
> > > > --- a/kernel/trace/ring_buffer.c
> > > > +++ b/kernel/trace/ring_buffer.c
> > > > @@ -6559,6 +6559,47 @@ unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
> > > > }
> > > > EXPORT_SYMBOL_GPL(ring_buffer_size);
> > > >
> > > > +/**
> > > > + * ring_buffer_memory_size - return the memory used by the buffer (in bytes)
> > > > + * @buffer: The ring buffer.
> > > > + * @cpu: The CPU to get ring buffer memory from.
> > > > + *
> > > > + * Returns the page-allocator memory consumed by @cpu, including the data
> > > > + * sub-buffers, the reader page, the cached read page, and the mmap
> > > > + * metadata page. Unlike ring_buffer_size(), which reports the usable data
> > > > + * capacity, this accounts for the full pages allocated to the buffer.
> > > > + * Remote buffers do not own page-allocator memory and report zero.
> > > > + */
> > > > +unsigned long ring_buffer_memory_size(struct trace_buffer *buffer, int cpu)
> > > > +{
> > > > + struct ring_buffer_per_cpu *cpu_buffer;
> > > > + unsigned long subbuf_size;
> > > > + unsigned long size;
> > > > +
> > > > + if (!cpumask_test_cpu(cpu, buffer->cpumask))
> > > > + return 0;
> > > > +
> > > > + /* Remote buffers use externally owned memory. */
> > > > + if (buffer->remote)
> > > > + return 0;
> >
> > For the persistent ring buffer, you also need to check `buffer->range_addr_start`.
> > That is a reserved memory, which is outside of page allocator.
> >
> > >
> > > This is a generic interface. If you want to call this function on a remote
> > > buffer, you should be able to.
> >
> > But as the comment said, this function returns the size of page-allocator
> > memory. Is remote ring buffer allocated from host?
>
> It is down to the trace_remote implementer where the memory comes from, but
> right now, all remote ring buffer are allocated from the buddy allocator.
Ah, I got it, its size should be reported via ring_buffer_memory_size().
Hmm, maybe we should add a callback for each trace instance so that it
can return the actual size and the attribute (host memory, guest memory,
reserved memory, or device memory (e.g. GPU memory?))
>
> Although, even coming from a carveout, the low-level function should probably
> return something in any case, as it has all the informations it needs and to
> stay as generic as possible.
>
> Then, the caller (trace_stat) should know if the information is relevant or not,
> or where to account for it. (probably with TRACE_ARRAY_FL_ flags ?). trace_stat
> shouldn't report only what's relevant for Android. It can however split the
> report between persistent ring-buffers and the others.
Yeah, this should return the attribute flag with the memory size.
>
> Overall, we could have
>
> cat trace_mem
>
> main:
> instances:
> snapshots:
> persistents:
> remotes:
>
> total_system:
> total_carveout:
>
> Which I believe would be a more accurate picture: First the memory sorted by
> "type" of buffers and then by "type of memory".
Agreed.
>
>
> >
> > >
> > > Moreover, remote buffer in-production current use is for Android... So not only
> > > ring_buffer_memory_size() should support them, but they should probably be
> > > actively reported somewhere...
> >
> > Maybe we should have different size accounting interface for remote buffer
> > and persistent buffer.
>
> For the remote, it should probably sit in trace_remote.c, which can call
> ring_buffer_memory_size(). trace_stat can then query the memory size from
> trace_remote.
I think the copy of the persistent ring buffer (backup instance) could be
implemented as remote, but the persistent ring buffer itself is not remote
because it is writable. (copy ring buffer is actually like remote, it is
read only, and it should be auto unloaded.)
Thank you,
>
> And actually I have a pending series where I keep the list of trace_remote [1]
> which would be a prerequisite.
>
> [1] https://lore.kernel.org/all/20260817135517.3919534-2-vdonnefort@google.com/
>
> >
> > Thanks,
> >
> > --
> > Masami Hiramatsu (Google) <mhiramat@kernel.org>
>
> --
> Vincent
--
Masami Hiramatsu (Google) <mhiramat@kernel.org>
next prev parent reply other threads:[~2026-09-25 1:09 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-05 11:27 [PATCH 0/2] tracing: add ring-buffer memory usage statistics Xiang Gao
2026-09-05 11:27 ` [PATCH 1/2] tracing: add ring-buffer memory usage statistics in tracefs Xiang Gao
2026-09-07 9:26 ` Vincent Donnefort
2026-09-05 11:27 ` [PATCH 2/2] tracing: add per-CPU " Xiang Gao
2026-09-11 15:50 ` [PATCH v2 0/2] tracing: add ring-buffer memory usage statistics Xiang Gao
2026-09-11 15:50 ` [PATCH v2 1/2] tracing: add ring-buffer memory usage statistics in tracefs Xiang Gao
2026-09-13 16:37 ` Donggeun Yoo
2026-09-11 15:50 ` [PATCH v2 2/2] tracing: add per-CPU " Xiang Gao
2026-09-16 6:33 ` [PATCH v3 0/2] tracing: add ring-buffer memory usage statistics Xiang Gao
2026-09-16 6:33 ` [PATCH v3 1/2] tracing: add ring-buffer memory usage statistics in tracefs Xiang Gao
2026-09-16 6:33 ` [PATCH v3 2/2] tracing: add per-CPU " Xiang Gao
2026-09-21 11:30 ` [PATCH v4 0/2] tracing: add ring-buffer memory usage statistics Xiang Gao
2026-09-21 11:30 ` [PATCH v4 1/2] tracing: add ring-buffer memory usage statistics in tracefs Xiang Gao
2026-09-22 9:41 ` Vincent Donnefort
2026-09-24 1:27 ` Masami Hiramatsu
2026-09-24 8:54 ` Vincent Donnefort
2026-09-25 1:09 ` Masami Hiramatsu [this message]
2026-09-21 11:30 ` [PATCH v4 2/2] tracing: add per-CPU " Xiang Gao
2026-09-22 9:42 ` Vincent Donnefort
2026-09-24 1:36 ` Masami Hiramatsu
2026-09-11 17:22 ` [PATCH 0/2] tracing: add ring-buffer memory usage statistics Vincent Donnefort
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260925100908.66c46353e5a2abeb73b1e15a@kernel.org \
--to=mhiramat@kernel.org \
--cc=donggeunyoo.kernel@gmail.com \
--cc=gaoxiang17@xiaomi.com \
--cc=gaoxu2@honor.com \
--cc=gxxa03070307@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=rostedt@goodmis.org \
--cc=vdonnefort@google.com \
--cc=yinchuang1@xiaomi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®