Skip to content

Commit 3b34d29

Browse files
Martin Kellyanakryiko
authored andcommitted
libbpf: Add ring__avail_data_size
Add ring__avail_data_size for querying the currently available data in the ringbuffer, similar to the BPF_RB_AVAIL_DATA flag in bpf_ringbuf_query. This is racy during ongoing operations but is still useful for overall information on how a ringbuffer is behaving. 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-8-martin.kelly@crowdstrike.com
1 parent b18db87 commit 3b34d29

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

tools/lib/bpf/libbpf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,17 @@ LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r);
12821282
*/
12831283
LIBBPF_API unsigned long ring__producer_pos(const struct ring *r);
12841284

1285+
/**
1286+
* @brief **ring__avail_data_size()** returns the number of bytes in the
1287+
* ringbuffer not yet consumed. This has no locking associated with it, so it
1288+
* can be inaccurate if operations are ongoing while this is called. However, it
1289+
* should still show the correct trend over the long-term.
1290+
*
1291+
* @param r A ringbuffer object.
1292+
* @return The number of bytes not yet consumed.
1293+
*/
1294+
LIBBPF_API size_t ring__avail_data_size(const struct ring *r);
1295+
12851296
struct user_ring_buffer_opts {
12861297
size_t sz; /* size of this struct, for forward/backward compatibility */
12871298
};

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ LIBBPF_1.3.0 {
400400
bpf_program__attach_netfilter;
401401
bpf_program__attach_tcx;
402402
bpf_program__attach_uprobe_multi;
403+
ring__avail_data_size;
403404
ring__consumer_pos;
404405
ring__producer_pos;
405406
ring_buffer__ring;

tools/lib/bpf/ringbuf.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,15 @@ unsigned long ring__producer_pos(const struct ring *r)
352352
return smp_load_acquire(r->producer_pos);
353353
}
354354

355+
size_t ring__avail_data_size(const struct ring *r)
356+
{
357+
unsigned long cons_pos, prod_pos;
358+
359+
cons_pos = ring__consumer_pos(r);
360+
prod_pos = ring__producer_pos(r);
361+
return prod_pos - cons_pos;
362+
}
363+
355364
static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb)
356365
{
357366
if (rb->consumer_pos) {

0 commit comments

Comments
 (0)