Skip to contentSkip to conversation

libbpf: perfbuf custom event reader

PATCH Superseded Jon Doron 2 participants 4 messages Jul 7, 2022 – Jul 8, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jon Doron

author
#0 lore raw

cover libbpf: perfbuf custom event reader

From: Jon Doron <[email protected]>

Add support for writing a custom event reader, by exposing the ring buffer state, and allowing to set it's tail.

Few simple examples where this type of needed:
1. perf_event_read_simple is allocating using malloc, perhaps you want to handle the wrap-around in some other way.
2. Since perf buf is per-cpu then the order of the events is not guarnteed, for example:
Given 3 events where each event has a timestamp t0 < t1 < t2, and the events are spread on more than 1 CPU, then we can end up with the following state in the ring buf:
CPU[0] => [t0, t2]
CPU[1] => [t1]
When you consume the events from CPU[0], you could know there is a t1 missing, (assuming there are no drops, and your event data contains a sequential index).
So now one can simply do the following, for CPU[0], you can store the address of t0 and t2 in an array (without moving the tail, so there data is not perished) then move on the CPU[1] and set the address of t1 in the same array.
So you end up with something like:
void **arr[] = [&t0, &t1, &t2], now you can consume it orderely and move the tails as you process in order.

Jon Doron (1):
libbpf: perfbuf: allow raw access to buffers

 tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h |  6 ++++++
 2 files changed, 46 insertions(+)
Signature · 1 line
2.36.1
author
#1 lore raw

PATCH 1/1 libbpf: perfbuf: allow raw access to buffers

From: Jon Doron <[email protected]>

Add API for perfbuf to support writing a custom event reader.

---
 tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h |  6 ++++++
 2 files changed, 46 insertions(+)
tools/lib/bpf/libbpf.c +40 -0
index e89cc9c885b3..37299aa05185 100644
@@ -12433,6 +12433,46 @@ static int perf_buffer__process_records(struct perf_buffer *pb,
 	return 0;
 }
 
+int perf_buffer__raw_ring_buf(const struct perf_buffer *pb, size_t buf_idx,
+			      void **base, size_t *buf_size, __u64 *head,
+			      __u64 *tail)
+{
+	struct perf_cpu_buf *cpu_buf;
+	struct perf_event_mmap_page *header;
+
+	if (buf_idx >= pb->cpu_cnt)
+		return libbpf_err(-EINVAL);
+
+	cpu_buf = pb->cpu_bufs[buf_idx];
+	if (!cpu_buf)
+		return libbpf_err(-ENOENT);
+
+	header = cpu_buf->base;
+	*head = ring_buffer_read_head(header);
+	*tail = header->data_tail;
+	*base = ((__u8 *)header) + pb->page_size;
+	*buf_size = pb->mmap_size;
+	return 0;
+}
+
+int perf_buffer__set_ring_buf_tail(const struct perf_buffer *pb, size_t buf_idx,
+				   __u64 tail)
+{
+	struct perf_cpu_buf *cpu_buf;
+	struct perf_event_mmap_page *header;
+
+	if (buf_idx >= pb->cpu_cnt)
+		return libbpf_err(-EINVAL);
+
+	cpu_buf = pb->cpu_bufs[buf_idx];
+	if (!cpu_buf)
+		return libbpf_err(-ENOENT);
+
+	header = cpu_buf->base;
+	ring_buffer_write_tail(header, tail);
+	return 0;
+}
+
 int perf_buffer__epoll_fd(const struct perf_buffer *pb)
 {
 	return pb->epoll_fd;
tools/lib/bpf/libbpf.h +6 -0
index 9e9a3fd3edd8..b6f6b6a12d70 100644
@@ -1381,6 +1381,12 @@ LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
+LIBBPF_API int perf_buffer__raw_ring_buf(const struct perf_buffer *pb,
+					 size_t buf_idx, void **base,
+					 size_t *buf_size, __u64 *head,
+					 __u64 *tail);
+LIBBPF_API int perf_buffer__set_ring_buf_tail(const struct perf_buffer *pb,
+					      size_t buf_idx, __u64 tail);
 
 typedef enum bpf_perf_event_ret
 	(*bpf_perf_event_print_t)(struct perf_event_header *hdr,
Signature · 1 line
2.36.1
reviewer
#2 lore raw

Re: [PATCH v1 1/1] libbpf: perfbuf: allow raw access to buffers

On Thu, Jul 7, 2022 at 12:14 AM Jon Doron <[email protected]> wrote:

Please prefix the patch with the target tree (bpf or bpf-next). For example, this patch should go via bpf-next.

Quoted 4 lines

From: Jon Doron <[email protected]>

Add API for perfbuf to support writing a custom event reader.

This is too brief for such change. It is ok to duplicate text from the cover letter.

Please also update libbpf.map.

Also, we should add a selftest to use these new APIs.

Quoted 12 lines
---
 tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h |  6 ++++++
 2 files changed, 46 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index e89cc9c885b3..37299aa05185 100644

tools/lib/bpf/libbpf.c static int perf_buffer__process_records(struct perf_buffer *pb, · line 12438

Quoted 6 lines
tools/lib/bpf/libbpf.c +3 -0
@@ -12433,6 +12433,46 @@ static int perf_buffer__process_records(struct perf_buffer *pb,
        return 0;
 }
 
+int perf_buffer__raw_ring_buf(const struct perf_buffer *pb, size_t buf_idx,
+                             void **base, size_t *buf_size, __u64 *head,
+                             __u64 *tail)

Please add comments to each API function.

Quoted 60 lines
tools/lib/bpf/libbpf.c +37 -0
+{
+       struct perf_cpu_buf *cpu_buf;
+       struct perf_event_mmap_page *header;
+
+       if (buf_idx >= pb->cpu_cnt)
+               return libbpf_err(-EINVAL);
+
+       cpu_buf = pb->cpu_bufs[buf_idx];
+       if (!cpu_buf)
+               return libbpf_err(-ENOENT);
+
+       header = cpu_buf->base;
+       *head = ring_buffer_read_head(header);
+       *tail = header->data_tail;
+       *base = ((__u8 *)header) + pb->page_size;
+       *buf_size = pb->mmap_size;
+       return 0;
+}
+
+int perf_buffer__set_ring_buf_tail(const struct perf_buffer *pb, size_t buf_idx,
+                                  __u64 tail)
+{
+       struct perf_cpu_buf *cpu_buf;
+       struct perf_event_mmap_page *header;
+
+       if (buf_idx >= pb->cpu_cnt)
+               return libbpf_err(-EINVAL);
+
+       cpu_buf = pb->cpu_bufs[buf_idx];
+       if (!cpu_buf)
+               return libbpf_err(-ENOENT);
+
+       header = cpu_buf->base;
+       ring_buffer_write_tail(header, tail);
+       return 0;
+}
+
 int perf_buffer__epoll_fd(const struct perf_buffer *pb)
 {
        return pb->epoll_fd;
tools/lib/bpf/libbpf.h +6 -1
index 9e9a3fd3edd8..b6f6b6a12d70 100644
@@ -1381,6 +1381,12 @@ LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
+LIBBPF_API int perf_buffer__raw_ring_buf(const struct perf_buffer *pb,
+                                        size_t buf_idx, void **base,
+                                        size_t *buf_size, __u64 *head,
+                                        __u64 *tail);
+LIBBPF_API int perf_buffer__set_ring_buf_tail(const struct perf_buffer *pb,
+                                             size_t buf_idx, __u64 tail);
 
 typedef enum bpf_perf_event_ret
        (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
--

2.36.1

author
#3 lore raw

Re: [PATCH v1 1/1] libbpf: perfbuf: allow raw access to buffers

On 07/07/2022, Song Liu wrote:

Hi Song Liu, thank you for the fast reply, I'll add comments on top of your original email.

I will not be available through out next week, so sorry if I'll have a late reply for follow ups.

Thanks in advance,
-- Jon.

Quoted 4 lines · Jon Doron

Please prefix the patch with the target tree (bpf or bpf-next). For example, this patch should go via bpf-next.

Done

Quoted 8 lines
Quoted 4 lines

From: Jon Doron <[email protected]>

Add API for perfbuf to support writing a custom event reader.

This is too brief for such change. It is ok to duplicate text from the cover letter.

Done

Quoted 2 lines

Please also update libbpf.map.

Done

Quoted 2 lines

Also, we should add a selftest to use these new APIs.

I'm not sure I'm following here what were you had in mind, in practice I could just change **bpf_perf_event_read_simple** implementation to use these new APIs to initialize it's variables (data_head, data_tail, base, mmap_size) and it would act exactly the same.

Quoted 15 lines
Quoted 15 lines
---
 tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h |  6 ++++++
 2 files changed, 46 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index e89cc9c885b3..37299aa05185 100644
@@ -12433,6 +12433,46 @@ static int perf_buffer__process_records(struct perf_buffer *pb,
        return 0;
 }
 
Quoted 5 lines

tools/lib/bpf/libbpf.c static int perf_buffer__process_records(struct perf_buffer *pb, · line 12438

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+int perf_buffer__raw_ring_buf(const struct perf_buffer *pb, size_t buf_idx,
+                             void **base, size_t *buf_size, __u64 *head,
+                             __u64 *tail)

Please add comments to each API function.

Done

Quoted 60 lines
Quoted 60 lines
tools/lib/bpf/libbpf.c +37 -0
+{
+       struct perf_cpu_buf *cpu_buf;
+       struct perf_event_mmap_page *header;
+
+       if (buf_idx >= pb->cpu_cnt)
+               return libbpf_err(-EINVAL);
+
+       cpu_buf = pb->cpu_bufs[buf_idx];
+       if (!cpu_buf)
+               return libbpf_err(-ENOENT);
+
+       header = cpu_buf->base;
+       *head = ring_buffer_read_head(header);
+       *tail = header->data_tail;
+       *base = ((__u8 *)header) + pb->page_size;
+       *buf_size = pb->mmap_size;
+       return 0;
+}
+
+int perf_buffer__set_ring_buf_tail(const struct perf_buffer *pb, size_t buf_idx,
+                                  __u64 tail)
+{
+       struct perf_cpu_buf *cpu_buf;
+       struct perf_event_mmap_page *header;
+
+       if (buf_idx >= pb->cpu_cnt)
+               return libbpf_err(-EINVAL);
+
+       cpu_buf = pb->cpu_bufs[buf_idx];
+       if (!cpu_buf)
+               return libbpf_err(-ENOENT);
+
+       header = cpu_buf->base;
+       ring_buffer_write_tail(header, tail);
+       return 0;
+}
+
 int perf_buffer__epoll_fd(const struct perf_buffer *pb)
 {
        return pb->epoll_fd;
tools/lib/bpf/libbpf.h +6 -1
index 9e9a3fd3edd8..b6f6b6a12d70 100644
@@ -1381,6 +1381,12 @@ LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
 LIBBPF_API int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx);
 LIBBPF_API size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb);
 LIBBPF_API int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx);
+LIBBPF_API int perf_buffer__raw_ring_buf(const struct perf_buffer *pb,
+                                        size_t buf_idx, void **base,
+                                        size_t *buf_size, __u64 *head,
+                                        __u64 *tail);
+LIBBPF_API int perf_buffer__set_ring_buf_tail(const struct perf_buffer *pb,
+                                             size_t buf_idx, __u64 tail);
 
 typedef enum bpf_perf_event_ret
        (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
--

2.36.1