ring_buffer__poll() waits for data and then consumes it through the sample callbacks, so it fails when a ring has no callback. Rings that are consumed with ring__next() don't have one, so the only way to wait for them is epoll_wait() on ring_buffer__epoll_fd(). Add ring_buffer__wait(), which only does the epoll_wait() and returns the number of rings that have data. It works for rings with and without callbacks: the caller consumes the data afterwards, with ring__consume() or ring__next() on each ring. rb = ring_buffer__new(map_fd, NULL, NULL, NULL); r = ring_buffer__ring(rb, 0); while (ring_buffer__wait(rb, -1) >= 0) { do { while ((rec = ring__next(r))) handle(rec->data, rec->len); } while (ring__release(r) > 0); } Suggested-by: Andrii Nakryiko Signed-off-by: Kumar Kartikeya Dwivedi --- tools/lib/bpf/libbpf.h | 15 +++++++++++++++ tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/ringbuf.c | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index efd796603fa3..91d25bed5a04 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1560,6 +1560,21 @@ LIBBPF_API void ring_buffer__free(struct ring_buffer *rb); LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd, ring_buffer_sample_fn sample_cb, void *ctx); LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms); + +/** + * @brief **ring_buffer__wait()** waits until at least one of the rings has + * data, without consuming it. + * + * Unlike **ring_buffer__poll()** it doesn't call sample callbacks, so it also + * works for rings that are consumed with **ring__next()**. + * + * @param rb A ringbuffer manager object. + * @param timeout_ms Maximum time to wait in milliseconds, 0 to not wait at + * all, -1 to wait indefinitely. + * @return The number of rings that have data, 0 if the timeout expired, or a + * negative error code on failure. + */ +LIBBPF_API int ring_buffer__wait(struct ring_buffer *rb, int timeout_ms); LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb); LIBBPF_API int ring_buffer__consume_n(struct ring_buffer *rb, size_t n); LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index e373257d32fe..943f6f3f0639 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -482,4 +482,5 @@ LIBBPF_1.9.0 { ring__next; ring__release; ring__rewind; + ring_buffer__wait; } LIBBPF_1.8.0; diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index 822557b7149b..12d586afed1d 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -412,6 +412,18 @@ int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms) return res; } +/* Wait for data in the ring(s) without consuming it */ +int ring_buffer__wait(struct ring_buffer *rb, int timeout_ms) +{ + int cnt; + + cnt = epoll_wait(rb->epoll_fd, rb->events, rb->ring_cnt, timeout_ms); + if (cnt < 0) + return libbpf_err(-errno); + + return cnt; +} + /* Get an fd that can be used to sleep until data is available in the ring(s) */ int ring_buffer__epoll_fd(const struct ring_buffer *rb) { -- 2.53.1-Meta