Re: [PATCH v3] selftests/bpf: Add ring_buffer__consume_n test.

From: Andrea Righi
Date: Thu Apr 25 2024 - 16:12:48 EST


On Thu, Apr 25, 2024 at 11:47:07AM -0700, Andrii Nakryiko wrote:
> On Thu, Apr 25, 2024 at 7:06 AM Andrea Righi <andrea.righi@xxxxxxxxxxxxx> wrote:
> >
> > Add a testcase for the ring_buffer__consume_n() API.
> >
> > The test produces multiple samples in a ring buffer, using a
> > sys_getpid() fentry prog, and consumes them from user-space in batches,
> > rather than consuming all of them greedily, like ring_buffer__consume()
> > does.
> >
> > Link: https://lore.kernel.org/lkml/CAEf4BzaR4zqUpDmj44KNLdpJ=Tpa97GrvzuzVNO5nM6b7oWd1w@xxxxxxxxxxxxxx
> > Signed-off-by: Andrea Righi <andrea.righi@xxxxxxxxxxxxx>
> > ---
> > tools/testing/selftests/bpf/Makefile | 2 +-
> > .../selftests/bpf/prog_tests/ringbuf.c | 64 +++++++++++++++++++
> > .../selftests/bpf/progs/test_ringbuf_n.c | 47 ++++++++++++++
> > 3 files changed, 112 insertions(+), 1 deletion(-)
> > create mode 100644 tools/testing/selftests/bpf/progs/test_ringbuf_n.c
> >
> > ChangeLog v2 -> v3:
> > - move skel_n inside ringbuf_n_subtest()
> >
> > ChangeLog v1 -> v2:
> > - replace CHECK() with ASSERT_EQ()
> > - fix skel -> skel_n
> > - drop unused "seq" field from struct sample
> >
>
> [...]
>
> > + /* Produce N_TOT_SAMPLES samples in the ring buffer by calling getpid() */
> > + skel_n->bss->value = SAMPLE_VALUE;
> > + for (i = 0; i < N_TOT_SAMPLES; i++)
> > + syscall(__NR_getpgid);
> > +
> > + /* Consume all samples from the ring buffer in batches of N_SAMPLES */
> > + for (i = 0; i < N_TOT_SAMPLES; i += err) {
> > + err = ring_buffer__consume_n(ringbuf, N_SAMPLES);
> > + ASSERT_EQ(err, N_SAMPLES, "rb_consume");
>
> if something goes wrong and err is < 0, we might end up with a very
> long loop. I changed this to:
>
> if (!ASSERT_EQ(...))
> goto cleanup_ringbuf;
>
> to avoid this problem

Looks good, tested, just in case, and it works a expected.

Thanks!
-Andrea

>
> > + }
> > +
> > +cleanup_ringbuf:
> > + ring_buffer__free(ringbuf);
> > +cleanup:
> > + test_ringbuf_n_lskel__destroy(skel_n);
> > +}
> > +
>
> [...]