Skip to content

Commit e79abf7

Browse files
Martin Kellyanakryiko
authored andcommitted
libbpf: Add ring__size
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
1 parent f3a01d3 commit e79abf7

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

tools/lib/bpf/libbpf.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,16 @@ LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
12931293
*/
12941294
LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
12951295

1296+
/**
1297+
* @brief **ring__size()** returns the total size of the ringbuffer's map data
1298+
* area (excluding special producer/consumer pages). Effectively this gives the
1299+
* amount of usable bytes of data inside the ringbuffer.
1300+
*
1301+
* @param r A ringbuffer object.
1302+
* @return The total size of the ringbuffer map data area.
1303+
*/
1304+
LIBBPF_API size_t ring__size(const struct ring *r);
1305+
12961306
struct user_ring_buffer_opts {
12971307
size_t sz; /* size of this struct, for forward/backward compatibility */
12981308
};

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,5 +403,6 @@ LIBBPF_1.3.0 {
403403
ring__avail_data_size;
404404
ring__consumer_pos;
405405
ring__producer_pos;
406+
ring__size;
406407
ring_buffer__ring;
407408
} LIBBPF_1.2.0;

tools/lib/bpf/ringbuf.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ size_t ring__avail_data_size(const struct ring *r)
361361
return prod_pos - cons_pos;
362362
}
363363

364+
size_t ring__size(const struct ring *r)
365+
{
366+
return r->mask + 1;
367+
}
368+
364369
static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
365370
{
366371
if (rb->consumer_pos) {

0 commit comments

Comments
 (0)