Re: [PATCH v6] Documentation: userspace-api: Document perf ring buffer mechanism

From: Leo Yan
Date: Sat Aug 26 2023 - 03:30:57 EST


Hi Namhyung,

On Tue, Aug 22, 2023 at 09:26:05PM -0700, Namhyung Kim wrote:

[...]

> > > > +Why ring buffers are mapped with above two different modes? Here the
> > > > +write direction matters. The forward writing starts to save data from
> > > > +the beginning of the ring buffer and wrap around when overflow, which is
> > > > +used with the read-write mode in the normal ring buffer. When the
> > > > +consumer doesn't keep up with the producer, it would lose some data, the
> > > > +kernel keeps how many records it lost and generates the
> > > > +``PERF_RECORD_LOST`` records in the next time when it finds a space in the
> > > > +ring buffer.
> > >
> > > Thanks for the update. It's unclear to me if all 4 combination of
> > > (rw, ro) x (fwd, bwd) are possible (yes!). The rw mode and back-
> > > ward is also possible but just not used for perf tool.
> >
> > I can add a matrix for the combinations:
> >
> > The combination is supported in perf tool:
> >
> > ---+------------+-----------
> > | Forward | Backward
> > ---+------------+-----------
> > rw | Yes | No
> > ---+------------+-----------
> > ro | X | Yes
> > ---+------------+-----------
> >
> > Yes: is supported
> > No: is not supported
> > X: is not feasible
>
> I think they are all supported. You can use rw mode with backward
> direction but it's just not intuitive. Also ro mode with forward direction
> is working but there's a chance to miss the start position of the
> previous event.

I am a bit confused for the all four modes are supported.

>From the code [1], we can see there have only two combinations:

- overwrite + read-only mode (PROT_READ);
- no-overwrite + read-write mode (PROT_READ | PROT_WRITE);

And I think perf tool must use the backward writing for "overwrite",
and use the forward writing for "non-overwrite" mode. I think the
code [2] can help us to conclude this.

In the end, the perf tool doesn't support the combinations:

- overwrite + read-write mode
- no-overwrite + read-only mode

I have sent a v6 patch, please let me know if you have any concerns or
if I misunderstand anything. Thanks!

Leo

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/lib/perf/evlist.c#n474
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/lib/perf/mmap.c#n141