On Thu, Sep 14, 2023 at 4:12 PM Martin Kelly <martin.kelly@xxxxxxxxxxxxxxx> wrote: > > Add APIs to get the producer and consumer position for a given > ringbuffer. > > Signed-off-by: Martin Kelly <martin.kelly@xxxxxxxxxxxxxxx> > --- > tools/lib/bpf/libbpf.h | 16 ++++++++++++++++ > tools/lib/bpf/libbpf.map | 2 ++ > tools/lib/bpf/ringbuf.c | 14 ++++++++++++++ > 3 files changed, 32 insertions(+) > > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h > index 2d6c39e20863..935162dbb3bf 100644 > --- a/tools/lib/bpf/libbpf.h > +++ b/tools/lib/bpf/libbpf.h > @@ -1263,6 +1263,22 @@ LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb); > LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb, > unsigned int idx); > > +/** > + * @brief **ring__consumer_pos()** returns the current consumer position. > + * > + * @param r A ring object. > + * @return The current consumer position. > + */ > +LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r); > + > +/** > + * @brief **ring__producer_pos()** returns the current producer position. > + * > + * @param r A ring object. > + * @return The current producer position. > + */ > +LIBBPF_API unsigned long ring__producer_pos(const struct ring *r); > + > struct user_ring_buffer_opts { > size_t sz; /* size of this struct, for forward/backward compatibility */ > }; > diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map > index 7a7370c2bc25..1c532fe7a445 100644 > --- a/tools/lib/bpf/libbpf.map > +++ b/tools/lib/bpf/libbpf.map > @@ -401,4 +401,6 @@ LIBBPF_1.3.0 { > bpf_program__attach_tcx; > bpf_program__attach_uprobe_multi; > ring_buffer__ring; > + ring__consumer_pos; > + ring__producer_pos; we keep this list ordered, so ring__ goes before ring_b > } LIBBPF_1.2.0; > diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c > index 2857df0f2d03..54c596db57a4 100644 > --- a/tools/lib/bpf/ringbuf.c > +++ b/tools/lib/bpf/ringbuf.c > @@ -336,6 +336,20 @@ struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx) > return rb->rings[idx]; > } > > +unsigned long ring__consumer_pos(const struct ring *r) > +{ > + /* Synchronizes with smp_store_release() in ringbuf_process_ring(). */ > + return smp_load_acquire(r->consumer_pos); > +} > + > +unsigned long ring__producer_pos(const struct ring *r) > +{ > + /* Synchronizes with smp_store_release() in __bpf_ringbuf_reserve() in > + * the kernel. > + */ > + return smp_load_acquire(r->producer_pos); > +} > + > static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) > { > if (rb->consumer_pos) { > -- > 2.34.1 >