libbpf: Add ring__size
authorMartin Kelly <martin.kelly@crowdstrike.com>
Mon, 25 Sep 2023 21:50:40 +0000 (14:50 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 25 Sep 2023 23:22:43 +0000 (16:22 -0700)
Add ring__size to get the total size of a given ringbuffer.

Signed-off-by: Martin Kelly <martin.kelly@crowdstrike.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20230925215045.2375758-10-martin.kelly@crowdstrike.com
tools/lib/bpf/libbpf.h
tools/lib/bpf/libbpf.map
tools/lib/bpf/ringbuf.c

index d60254c5edc6c887633f04afa8ba46ae93cbb0a9..53e2a645c5605d462df959664fd501a0af4b6cfb 100644 (file)
@@ -1293,6 +1293,16 @@ LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
  */
 LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
 
+/**
+ * @brief **ring__size()** returns the total size of the ringbuffer's map data
+ * area (excluding special producer/consumer pages). Effectively this gives the
+ * amount of usable bytes of data inside the ringbuffer.
+ *
+ * @param r A ringbuffer object.
+ * @return The total size of the ringbuffer map data area.
+ */
+LIBBPF_API size_t ring__size(const struct ring *r);
+
 struct user_ring_buffer_opts {
        size_t sz; /* size of this struct, for forward/backward compatibility */
 };
index 5184c94c050269fcb3234a6152fc18fe74c48995..a116d0bb3c5d535e9b1f535c29fb286e9d777d89 100644 (file)
@@ -403,5 +403,6 @@ LIBBPF_1.3.0 {
                ring__avail_data_size;
                ring__consumer_pos;
                ring__producer_pos;
+               ring__size;
                ring_buffer__ring;
 } LIBBPF_1.2.0;
index 07fbc6adcdd9ca2cbf46cb1210c6bf0789683ab8..98d0767dbb50e412534779ef5bd736e770ff1ada 100644 (file)
@@ -361,6 +361,11 @@ size_t ring__avail_data_size(const struct ring *r)
        return prod_pos - cons_pos;
 }
 
+size_t ring__size(const struct ring *r)
+{
+       return r->mask + 1;
+}
+
 static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
 {
        if (rb->consumer_pos) {