linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrea Righi <andrea.righi@canonical.com>
To: Jiri Olsa <olsajiri@gmail.com>
Cc: Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Mykola Lysenko <mykolal@fb.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
	Shuah Khan <shuah@kernel.org>,
	linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: Re: [PATCH] selftests/bpf: Add ring_buffer__consume_n test.
Date: Thu, 25 Apr 2024 08:19:04 +0200	[thread overview]
Message-ID: <Zin12J-emVljvVrJ@gpd> (raw)
In-Reply-To: <ZiVy9bYrX-w24huD@krava>

On Sun, Apr 21, 2024 at 10:11:33PM +0200, Jiri Olsa wrote:
...
> >  static struct test_ringbuf_map_key_lskel *skel_map_key;
> > +static struct test_ringbuf_n_lskel *skel_n;
> 
> seems like there's no need for this to be static variable

Can you elaborate more? I think we want these pointers to be static to
limit the scope to this file, no?

> 
> >  static struct test_ringbuf_lskel *skel;
> >  static struct ring_buffer *ringbuf;
> >  
> > @@ -326,6 +328,67 @@ static void ringbuf_subtest(void)
> >  	test_ringbuf_lskel__destroy(skel);
> >  }
> >  
> > +/*
> > + * Test ring_buffer__consume_n() by producing N_TOT_SAMPLES samples in the ring
> > + * buffer, via getpid(), and consuming them in chunks of N_SAMPLES.
> > + */
> > +#define N_TOT_SAMPLES	32
> > +#define N_SAMPLES	4
> > +
> > +/* Sample value to verify the callback validity */
> > +#define SAMPLE_VALUE	42L
> > +
> > +static int process_n_sample(void *ctx, void *data, size_t len)
> > +{
> > +	struct sample *s = data;
> > +
> > +	CHECK(s->value != SAMPLE_VALUE,
> > +	      "sample_value", "exp %ld, got %ld\n", SAMPLE_VALUE, s->value);
> 
> I think we should use ASSERT macros instead in the new code

Good catch, I'll change this to an ASSERT_EQ().

> 
> > +
> > +	return 0;
> > +}
> > +
> > +static void ringbuf_n_subtest(void)
> > +{
> > +	int err, i;
> > +
> > +	skel_n = test_ringbuf_n_lskel__open();
> > +	if (!ASSERT_OK_PTR(skel_n, "test_ringbuf_n_lskel__open"))
> > +		return;
> > +
> > +	skel_n->maps.ringbuf.max_entries = getpagesize();
> > +	skel_n->bss->pid = getpid();
> > +
> > +	err = test_ringbuf_n_lskel__load(skel_n);
> > +	if (!ASSERT_OK(err, "test_ringbuf_n_lskel__load"))
> > +		goto cleanup;
> > +
> > +	ringbuf = ring_buffer__new(skel_n->maps.ringbuf.map_fd,
> > +				   process_n_sample, NULL, NULL);
> > +	if (!ASSERT_OK_PTR(ringbuf, "ring_buffer__new"))
> > +		goto cleanup;
> > +
> > +	err = test_ringbuf_n_lskel__attach(skel_n);
> > +	if (!ASSERT_OK(err, "test_ringbuf_n_lskel__attach"))
> > +		goto cleanup_ringbuf;
> > +
> > +	/* Produce N_TOT_SAMPLES samples in the ring buffer by calling getpid() */
> > +	skel->bss->value = SAMPLE_VALUE;
> 
> skel_n ?

Absolutely... I'm suprised that it works actually, I guess pure luck
(unluck) to reuse the old pointer and have value mapped to the same
location. Anyway, I'll fix this.

> 
> > +	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");
> > +	}
> > +
> 
> SNIP
> 
> > diff --git a/tools/testing/selftests/bpf/progs/test_ringbuf_n.c b/tools/testing/selftests/bpf/progs/test_ringbuf_n.c
> > new file mode 100644
> > index 000000000000..b98b5bb20699
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/progs/test_ringbuf_n.c
> > @@ -0,0 +1,52 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +// Copyright (c) 2024 Andrea Righi <andrea.righi@canonical.com>
> > +
> > +#include <linux/bpf.h>
> > +#include <sched.h>
> > +#include <unistd.h>
> > +#include <bpf/bpf_helpers.h>
> > +#include "bpf_misc.h"
> > +
> > +char _license[] SEC("license") = "GPL";
> > +
> > +#define TASK_COMM_LEN 16
> > +
> > +struct sample {
> > +	int pid;
> > +	int seq;
> 
> seq does not seem to be checked, is it needed?

seq is not used at all, I can definitely drop it.

Thanks for the review! I'll send a v2.

-Andrea

  reply	other threads:[~2024-04-25  6:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-20 15:59 [PATCH] selftests/bpf: Add ring_buffer__consume_n test Andrea Righi
2024-04-21 20:11 ` Jiri Olsa
2024-04-25  6:19   ` Andrea Righi [this message]
2024-04-25 12:23     ` Jiri Olsa
2024-04-25 14:01       ` Andrea Righi

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=Zin12J-emVljvVrJ@gpd \
    --to=andrea.righi@canonical.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mykolal@fb.com \
    --cc=olsajiri@gmail.com \
    --cc=sdf@google.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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;
as well as URLs for NNTP newsgroup(s).