* [PATCH v1 0/1] libbpf: perfbuf custom event reader @ 2022-07-07 7:13 Jon Doron 2022-07-07 7:13 ` [PATCH v1 1/1] libbpf: perfbuf: allow raw access to buffers Jon Doron 0 siblings, 1 reply; 4+ messages in thread From: Jon Doron @ 2022-07-07 7:13 UTC (permalink / raw) To: bpf, ast, andrii, daniel; +Cc: Jon Doron From: Jon Doron <jond@wiz.io> 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(+) -- 2.36.1 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v1 1/1] libbpf: perfbuf: allow raw access to buffers 2022-07-07 7:13 [PATCH v1 0/1] libbpf: perfbuf custom event reader Jon Doron @ 2022-07-07 7:13 ` Jon Doron 2022-07-08 5:26 ` Song Liu 0 siblings, 1 reply; 4+ messages in thread From: Jon Doron @ 2022-07-07 7:13 UTC (permalink / raw) To: bpf, ast, andrii, daniel; +Cc: Jon Doron From: Jon Doron <jond@wiz.io> Add API for perfbuf to support writing a custom event reader. Signed-off-by: Jon Doron <jond@wiz.io> --- tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 6 ++++++ 2 files changed, 46 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index e89cc9c885b3..37299aa05185 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -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; diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 9e9a3fd3edd8..b6f6b6a12d70 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -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 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v1 1/1] libbpf: perfbuf: allow raw access to buffers 2022-07-07 7:13 ` [PATCH v1 1/1] libbpf: perfbuf: allow raw access to buffers Jon Doron @ 2022-07-08 5:26 ` Song Liu 2022-07-08 6:08 ` Jon Doron 0 siblings, 1 reply; 4+ messages in thread From: Song Liu @ 2022-07-08 5:26 UTC (permalink / raw) To: Jon Doron Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, Jon Doron On Thu, Jul 7, 2022 at 12:14 AM Jon Doron <arilou@gmail.com> wrote: Please prefix the patch with the target tree (bpf or bpf-next). For example, this patch should go via bpf-next. > > From: Jon Doron <jond@wiz.io> > > 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. > > Signed-off-by: Jon Doron <jond@wiz.io> > --- > tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++ > tools/lib/bpf/libbpf.h | 6 ++++++ > 2 files changed, 46 insertions(+) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index e89cc9c885b3..37299aa05185 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -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. > +{ > + 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; > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h > index 9e9a3fd3edd8..b6f6b6a12d70 100644 > --- a/tools/lib/bpf/libbpf.h > +++ b/tools/lib/bpf/libbpf.h > @@ -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 > ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1 1/1] libbpf: perfbuf: allow raw access to buffers 2022-07-08 5:26 ` Song Liu @ 2022-07-08 6:08 ` Jon Doron 0 siblings, 0 replies; 4+ messages in thread From: Jon Doron @ 2022-07-08 6:08 UTC (permalink / raw) To: Song Liu Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, Jon Doron 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. >On Thu, Jul 7, 2022 at 12:14 AM Jon Doron <arilou@gmail.com> wrote: > >Please prefix the patch with the target tree (bpf or bpf-next). For example, >this patch should go via bpf-next. Done >> >> From: Jon Doron <jond@wiz.io> >> >> 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 >Please also update libbpf.map. > Done >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. >> >> Signed-off-by: Jon Doron <jond@wiz.io> >> --- >> tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++ >> tools/lib/bpf/libbpf.h | 6 ++++++ >> 2 files changed, 46 insertions(+) >> >> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c >> index e89cc9c885b3..37299aa05185 100644 >> --- a/tools/lib/bpf/libbpf.c >> +++ b/tools/lib/bpf/libbpf.c >> @@ -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. Done >> +{ >> + 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; >> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h >> index 9e9a3fd3edd8..b6f6b6a12d70 100644 >> --- a/tools/lib/bpf/libbpf.h >> +++ b/tools/lib/bpf/libbpf.h >> @@ -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 >> ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-07-08 6:08 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2022-07-07 7:13 [PATCH v1 0/1] libbpf: perfbuf custom event reader Jon Doron 2022-07-07 7:13 ` [PATCH v1 1/1] libbpf: perfbuf: allow raw access to buffers Jon Doron 2022-07-08 5:26 ` Song Liu 2022-07-08 6:08 ` Jon Doron
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox