diff options
author | Martin Kelly <martin.kelly@crowdstrike.com> | 2023-09-25 14:50:38 -0700 |
---|---|---|
committer | Andrii Nakryiko <andrii.nakryiko@gmail.com> | 2023-10-02 11:17:48 -0700 |
commit | 3e675ed6aba10468577783eb792e9a981d6b639a (patch) | |
tree | 5d1c1b906f65be76a241f25c93ad6c1163ab6e78 | |
parent | 2ad16b970a6e80cc82abe78386fa9257169d238e (diff) | |
download | libbpf-3e675ed6aba10468577783eb792e9a981d6b639a.tar.gz |
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
-rw-r--r-- | src/libbpf.h | 11 | ||||
-rw-r--r-- | src/libbpf.map | 1 | ||||
-rw-r--r-- | src/ringbuf.c | 9 |
3 files changed, 21 insertions, 0 deletions
diff --git a/src/libbpf.h b/src/libbpf.h index ab47017..d60254c 100644 --- a/src/libbpf.h +++ b/src/libbpf.h @@ -1282,6 +1282,17 @@ LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r); */ LIBBPF_API unsigned long ring__producer_pos(const struct ring *r); +/** + * @brief **ring__avail_data_size()** returns the number of bytes in the + * ringbuffer not yet consumed. This has no locking associated with it, so it + * can be inaccurate if operations are ongoing while this is called. However, it + * should still show the correct trend over the long-term. + * + * @param r A ringbuffer object. + * @return The number of bytes not yet consumed. + */ +LIBBPF_API size_t ring__avail_data_size(const struct ring *r); + struct user_ring_buffer_opts { size_t sz; /* size of this struct, for forward/backward compatibility */ }; diff --git a/src/libbpf.map b/src/libbpf.map index 3bec002..5184c94 100644 --- a/src/libbpf.map +++ b/src/libbpf.map @@ -400,6 +400,7 @@ LIBBPF_1.3.0 { bpf_program__attach_netfilter; bpf_program__attach_tcx; bpf_program__attach_uprobe_multi; + ring__avail_data_size; ring__consumer_pos; ring__producer_pos; ring_buffer__ring; diff --git a/src/ringbuf.c b/src/ringbuf.c index d14a607..07fbc6a 100644 --- a/src/ringbuf.c +++ b/src/ringbuf.c @@ -352,6 +352,15 @@ unsigned long ring__producer_pos(const struct ring *r) return smp_load_acquire(r->producer_pos); } +size_t ring__avail_data_size(const struct ring *r) +{ + unsigned long cons_pos, prod_pos; + + cons_pos = ring__consumer_pos(r); + prod_pos = ring__producer_pos(r); + return prod_pos - cons_pos; +} + static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) { if (rb->consumer_pos) { |