[PATCH bpf-next] libbpf: Add '_wait()' and '_nowait()' macros for 'bpf_ring_buffer__poll()'

From: Pedro Tammela
Date: Sun Mar 28 2021 - 12:17:42 EST


'bpf_ring_buffer__poll()' abstracts the polling method, so abstract the
constants that make the implementation don't wait or wait indefinetly
for data.

Signed-off-by: Pedro Tammela <pctammela@xxxxxxxxxxxx>
---
tools/lib/bpf/libbpf.h | 3 +++
tools/testing/selftests/bpf/benchs/bench_ringbufs.c | 2 +-
tools/testing/selftests/bpf/prog_tests/ringbuf.c | 6 +++---
tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c | 4 ++--
4 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index f500621d28e5..3817d84f91c6 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -540,6 +540,9 @@ LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms);
LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb);
LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb);

+#define ring_buffer__poll_wait(rb) ring_buffer__poll(rb, -1)
+#define ring_buffer__poll_nowait(rb) ring_buffer__poll(rb, 0)
+
/* Perf buffer APIs */
struct perf_buffer;

diff --git a/tools/testing/selftests/bpf/benchs/bench_ringbufs.c b/tools/testing/selftests/bpf/benchs/bench_ringbufs.c
index bde6c9d4cbd4..82db2cc9bab3 100644
--- a/tools/testing/selftests/bpf/benchs/bench_ringbufs.c
+++ b/tools/testing/selftests/bpf/benchs/bench_ringbufs.c
@@ -191,7 +191,7 @@ static void *ringbuf_libbpf_consumer(void *input)
{
struct ringbuf_libbpf_ctx *ctx = &ringbuf_libbpf_ctx;

- while (ring_buffer__poll(ctx->ringbuf, -1) >= 0) {
+ while (ring_buffer__poll_wait(ctx->ringbuf) >= 0) {
if (args.back2back)
bufs_trigger_batch();
}
diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testing/selftests/bpf/prog_tests/ringbuf.c
index fddbc5db5d6a..321c646a0685 100644
--- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c
+++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c
@@ -121,7 +121,7 @@ void test_ringbuf(void)
3L * rec_sz, skel->bss->prod_pos);

/* poll for samples */
- err = ring_buffer__poll(ringbuf, -1);
+ err = ring_buffer__poll_wait(ringbuf);

/* -EDONE is used as an indicator that we are done */
if (CHECK(err != -EDONE, "err_done", "done err: %d\n", err))
@@ -130,7 +130,7 @@ void test_ringbuf(void)
CHECK(cnt != 2, "cnt", "exp %d samples, got %d\n", 2, cnt);

/* we expect extra polling to return nothing */
- err = ring_buffer__poll(ringbuf, 0);
+ err = ring_buffer__poll_nowait(ringbuf);
if (CHECK(err != 0, "extra_samples", "poll result: %d\n", err))
goto cleanup;
cnt = atomic_xchg(&sample_cnt, 0);
@@ -148,7 +148,7 @@ void test_ringbuf(void)
CHECK(skel->bss->cons_pos != 3 * rec_sz,
"err_cons_pos", "exp %ld, got %ld\n",
3L * rec_sz, skel->bss->cons_pos);
- err = ring_buffer__poll(ringbuf, -1);
+ err = ring_buffer__poll_wait(ringbuf);
CHECK(err <= 0, "poll_err", "err %d\n", err);
cnt = atomic_xchg(&sample_cnt, 0);
CHECK(cnt != 2, "cnt", "exp %d samples, got %d\n", 2, cnt);
diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c b/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c
index d37161e59bb2..65ba0a3472f1 100644
--- a/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c
+++ b/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c
@@ -80,12 +80,12 @@ void test_ringbuf_multi(void)
syscall(__NR_getpgid);

/* poll for samples, should get 2 ringbufs back */
- err = ring_buffer__poll(ringbuf, -1);
+ err = ring_buffer__poll_wait(ringbuf);
if (CHECK(err != 2, "poll_res", "expected 2 records, got %d\n", err))
goto cleanup;

/* expect extra polling to return nothing */
- err = ring_buffer__poll(ringbuf, 0);
+ err = ring_buffer__poll_nowait(ringbuf);
if (CHECK(err < 0, "extra_samples", "poll result: %d\n", err))
goto cleanup;

--
2.25.1