Skip to contentSkip to conversation

libbpf: add API to consume the perf ring buffer content

PATCH Superseded Eelco Chaudron 2 participants 4 messages May 25, 2020 – May 26, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: add API to consume the perf ring buffer content

This new API, perf_buffer__consume, can be used as follows:
- When you have a perf ring where wakeup_events is higher than 1, and you have remaining data in the rings you would like to pull out on exit (or maybe based on a timeout).
- For low latency cases where you burn a CPU that constantly polls the queues.

---
 tools/lib/bpf/libbpf.c   |   23 +++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |    1 +
 tools/lib/bpf/libbpf.map |    1 +
 3 files changed, 25 insertions(+)
tools/lib/bpf/libbpf.c +23 -0
index fa04cbe547ed..cbef3dac7507 100644
@@ -8456,6 +8456,29 @@ int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
 	return cnt < 0 ? -errno : cnt;
 }
 
+int perf_buffer__consume(struct perf_buffer *pb)
+{
+	int i;
+
+	if (!pb)
+		return -EINVAL;
+
+	if (!pb->cpu_bufs)
+		return 0;
+
+	for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) {
+		int err;
+		struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
+
+		err = perf_buffer__process_records(pb, cpu_buf);
+		if (err) {
+			pr_warn("error while processing records: %d\n", err);
+			return err;
+		}
+	}
+	return 0;
+}
+
 struct bpf_prog_info_array_desc {
 	int	array_offset;	/* e.g. offset of jited_prog_insns */
 	int	count_offset;	/* e.g. offset of jited_prog_len */
tools/lib/bpf/libbpf.h +1 -0
index 8ea69558f0a8..1e2e399a5f2c 100644
@@ -533,6 +533,7 @@ perf_buffer__new_raw(int map_fd, size_t page_cnt,
 
 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
+LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
 
 typedef enum bpf_perf_event_ret
 	(*bpf_perf_event_print_t)(struct perf_event_header *hdr,
tools/lib/bpf/libbpf.map +1 -0
index 0133d469d30b..381a7342ecfc 100644
@@ -262,4 +262,5 @@ LIBBPF_0.0.9 {
 		bpf_link_get_fd_by_id;
 		bpf_link_get_next_id;
 		bpf_program__attach_iter;
+		perf_buffer__consume;
 } LIBBPF_0.0.8;
#1 lore raw
Quoted 2 lines · Eelco Chaudron

This new API, perf_buffer__consume, can be used as follows:

I wonder, was it inspired by yet-to-be committed ring_buffer__consume() or it's just a coincidence?

Quoted 18 lines

- When you have a perf ring where wakeup_events is higher than 1, and you have remaining data in the rings you would like to pull out on exit (or maybe based on a timeout).
- For low latency cases where you burn a CPU that constantly polls the queues.

---
 tools/lib/bpf/libbpf.c   |   23 +++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |    1 +
 tools/lib/bpf/libbpf.map |    1 +
 3 files changed, 25 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index fa04cbe547ed..cbef3dac7507 100644

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) · line 8464

Quoted 9 lines
tools/lib/bpf/libbpf.c +6 -0
@@ -8456,6 +8456,29 @@ int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
        return cnt < 0 ? -errno : cnt;
 }
 
+int perf_buffer__consume(struct perf_buffer *pb)
+{
+       int i;
+
+       if (!pb)
+               return -EINVAL;

we don't check this in perf_buffer__poll, IMO, checking this in every "method" is an overkill.

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) · line 8467

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+
+       if (!pb->cpu_bufs)
+               return 0;

no need to check. It's either non-NULL for valid perf_buffer, or calloc could return NULL if pb->cpu_cnt is zero (not sure it's possible, but still), but then loop below will never access pb->cpu_bufs[i].

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) · line 8469

Quoted 2 lines
tools/lib/bpf/libbpf.c +2 -0
+
+       for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) {

I think pb->cpu_bufs[i] check is wrong, it will stop iteration prematurely if cpu_bufs are sparsely populated. So move check inside and continue loop if NULL.

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) · line 8470

Quoted 1 line
tools/lib/bpf/libbpf.c +1 -0
+               int err;

nit: declare it together with "i" above, similar to how perf_buffer__poll does it

Quoted 37 lines
tools/lib/bpf/libbpf.c +11 -0
+               struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
+
+               err = perf_buffer__process_records(pb, cpu_buf);
+               if (err) {
+                       pr_warn("error while processing records: %d\n", err);
+                       return err;
+               }
+       }
+       return 0;
+}
+
 struct bpf_prog_info_array_desc {
        int     array_offset;   /* e.g. offset of jited_prog_insns */
        int     count_offset;   /* e.g. offset of jited_prog_len */
tools/lib/bpf/libbpf.h +1 -0
index 8ea69558f0a8..1e2e399a5f2c 100644
@@ -533,6 +533,7 @@ perf_buffer__new_raw(int map_fd, size_t page_cnt,
 
 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms);
+LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
 
 typedef enum bpf_perf_event_ret
        (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
tools/lib/bpf/libbpf.map +1 -0
index 0133d469d30b..381a7342ecfc 100644
@@ -262,4 +262,5 @@ LIBBPF_0.0.9 {
                bpf_link_get_fd_by_id;
                bpf_link_get_next_id;
                bpf_program__attach_iter;
+               perf_buffer__consume;
 } LIBBPF_0.0.8;
#2 lore raw
Quoted 6 lines · Andrii Nakryiko
Quoted 2 lines · Eelco Chaudron

This new API, perf_buffer__consume, can be used as follows:

I wonder, was it inspired by yet-to-be committed ring_buffer__consume() or it's just a coincidence?

Just coincidence, I was needing a function to flush the remaining ring entries, as I was using a larger wakeup_events value. Initially, I called the function ring_buffer_flush(), but once I noticed your patch I renamed it :)

Quoted 22 lines
Quoted 22 lines

- When you have a perf ring where wakeup_events is higher than 1, and you have remaining data in the rings you would like to pull out on exit (or maybe based on a timeout).
- For low latency cases where you burn a CPU that constantly polls the queues.

---
 tools/lib/bpf/libbpf.c   |   23 +++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |    1 +
 tools/lib/bpf/libbpf.map |    1 +
 3 files changed, 25 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index fa04cbe547ed..cbef3dac7507 100644
@@ -8456,6 +8456,29 @@ int perf_buffer__poll(struct perf_buffer *pb, 
 int timeout_ms)
        return cnt < 0 ? -errno : cnt;
 }
 
Quoted 9 lines

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) · line 8464

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+int perf_buffer__consume(struct perf_buffer *pb)
+{
+       int i;
+
+       if (!pb)
+               return -EINVAL;

we don't check this in perf_buffer__poll, IMO, checking this in every "method" is an overkill.

Ack, will fix in v2

Quoted 8 lines

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) · line 8467

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+
+       if (!pb->cpu_bufs)
+               return 0;

no need to check. It's either non-NULL for valid perf_buffer, or calloc could return NULL if pb->cpu_cnt is zero (not sure it's possible, but still), but then loop below will never access pb->cpu_bufs[i].

Agreed, was just adding some safety checks, but in the constantly poll mode this is a lot of overhead. Will remover in v2.

Quoted 6 lines

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, · line 8470

Quoted 2 lines
tools/lib/bpf/libbpf.c +2 -0
+
+       for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) {

I think pb->cpu_bufs[i] check is wrong, it will stop iteration prematurely if cpu_bufs are sparsely populated. So move check inside and continue loop if NULL.

Mimicked the behavior from other functions, however just to be safe I split it up.

Quoted 4 lines

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) · line 8470

Quoted 1 line
tools/lib/bpf/libbpf.c +1 -0
+               int err;

nit: declare it together with "i" above, similar to how perf_buffer__poll does it

Put it down here as it’s only used in the context of the for loop, but will move it up in the v2.

Quoted 26 lines
Quoted 26 lines
tools/lib/bpf/libbpf.c +11 -0
+               struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
+
+               err = perf_buffer__process_records(pb, cpu_buf);
+               if (err) {
+                       pr_warn("error while processing records: 
 %d\n", err);
+                       return err;
+               }
+       }
+       return 0;
+}
+
 struct bpf_prog_info_array_desc {
        int     array_offset;   /* e.g. offset of jited_prog_insns */
        int     count_offset;   /* e.g. offset of jited_prog_len */
tools/lib/bpf/libbpf.h +1 -0
index 8ea69558f0a8..1e2e399a5f2c 100644
@@ -533,6 +533,7 @@ perf_buffer__new_raw(int map_fd, size_t page_cnt,
 
 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int 
 timeout_ms);
+LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
 
Quoted 13 lines

tools/lib/bpf/libbpf.map LIBBPF_0.0.9 { · line 266

Quoted 13 lines
tools/lib/bpf/libbpf.h +0 -0
 typedef enum bpf_perf_event_ret

(*bpf_perf_event_print_t)(struct perf_event_header *hdr,

tools/lib/bpf/libbpf.map +1 -0
index 0133d469d30b..381a7342ecfc 100644
@@ -262,4 +262,5 @@ LIBBPF_0.0.9 {
                bpf_link_get_fd_by_id;
                bpf_link_get_next_id;
                bpf_program__attach_iter;
+               perf_buffer__consume;
 } LIBBPF_0.0.8;

Thanks for the review, will send out a v2 soon.

#3 lore raw
Quoted 10 lines · Eelco Chaudron
Quoted 5 lines · Andrii Nakryiko
Quoted 2 lines · Eelco Chaudron

This new API, perf_buffer__consume, can be used as follows:

Quoted 7 lines
Quoted 2 lines

I wonder, was it inspired by yet-to-be committed ring_buffer__consume() or it's just a coincidence?

Just coincidence, I was needing a function to flush the remaining ring entries, as I was using a larger wakeup_events value. Initially, I called the function ring_buffer_flush(), but once I noticed your patch I renamed it :)

Nice, thanks, I love consistent naming :)

Quoted 50 lines
Quoted 23 lines
Quoted 23 lines

- When you have a perf ring where wakeup_events is higher than 1, and you have remaining data in the rings you would like to pull out on exit (or maybe based on a timeout).
- For low latency cases where you burn a CPU that constantly polls the queues.

---
 tools/lib/bpf/libbpf.c   |   23 +++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |    1 +
 tools/lib/bpf/libbpf.map |    1 +
 3 files changed, 25 insertions(+)
tools/lib/bpf/libbpf.c +0 -0
index fa04cbe547ed..cbef3dac7507 100644
@@ -8456,6 +8456,29 @@ int perf_buffer__poll(struct perf_buffer *pb,
 int timeout_ms)
        return cnt < 0 ? -errno : cnt;
 }
 
Quoted 9 lines

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) · line 8464

Quoted 6 lines
tools/lib/bpf/libbpf.c +6 -0
+int perf_buffer__consume(struct perf_buffer *pb)
+{
+       int i;
+
+       if (!pb)
+               return -EINVAL;

we don't check this in perf_buffer__poll, IMO, checking this in every "method" is an overkill.

Ack, will fix in v2

Quoted 8 lines

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) · line 8467

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+
+       if (!pb->cpu_bufs)
+               return 0;

no need to check. It's either non-NULL for valid perf_buffer, or calloc could return NULL if pb->cpu_cnt is zero (not sure it's possible, but still), but then loop below will never access pb->cpu_bufs[i].

Agreed, was just adding some safety checks, but in the constantly poll mode this is a lot of overhead. Will remover in v2.

Quoted 3 lines
Quoted 2 lines
tools/lib/bpf/libbpf.c +2 -0
+
+       for (i = 0; i < pb->cpu_cnt && pb->cpu_bufs[i]; i++) {
Quoted 6 lines
Quoted 3 lines

I think pb->cpu_bufs[i] check is wrong, it will stop iteration prematurely if cpu_bufs are sparsely populated. So move check inside and continue loop if NULL.

Mimicked the behavior from other functions, however just to be safe I split it up.

You mean perf_buffer__poll() or perf_buffer__free() loop? In the perf_buffer__poll() case, first N events will always correspond to non-NULL buffers. It's very different from what you are doing here. But I think perf_buffer__free() actually is buggy similarly to how I pointed out in this case. We need to fix that.

Quoted 51 lines
Quoted 5 lines

tools/lib/bpf/libbpf.c int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms) · line 8470

Quoted 2 lines
tools/lib/bpf/libbpf.c +1 -0
+               int err;

nit: declare it together with "i" above, similar to how perf_buffer__poll does it

Put it down here as it’s only used in the context of the for loop, but will move it up in the v2.

Quoted 26 lines
Quoted 26 lines
tools/lib/bpf/libbpf.c +11 -0
+               struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
+
+               err = perf_buffer__process_records(pb, cpu_buf);
+               if (err) {
+                       pr_warn("error while processing records:
 %d\n", err);
+                       return err;
+               }
+       }
+       return 0;
+}
+
 struct bpf_prog_info_array_desc {
        int     array_offset;   /* e.g. offset of jited_prog_insns */
        int     count_offset;   /* e.g. offset of jited_prog_len */
tools/lib/bpf/libbpf.h +1 -0
index 8ea69558f0a8..1e2e399a5f2c 100644
@@ -533,6 +533,7 @@ perf_buffer__new_raw(int map_fd, size_t page_cnt,
 
 LIBBPF_API void perf_buffer__free(struct perf_buffer *pb);
 LIBBPF_API int perf_buffer__poll(struct perf_buffer *pb, int
 timeout_ms);
+LIBBPF_API int perf_buffer__consume(struct perf_buffer *pb);
 
Quoted 13 lines

tools/lib/bpf/libbpf.map LIBBPF_0.0.9 { · line 266

Quoted 13 lines
tools/lib/bpf/libbpf.h +0 -0
 typedef enum bpf_perf_event_ret

(*bpf_perf_event_print_t)(struct perf_event_header *hdr,

tools/lib/bpf/libbpf.map +1 -0
index 0133d469d30b..381a7342ecfc 100644
@@ -262,4 +262,5 @@ LIBBPF_0.0.9 {
                bpf_link_get_fd_by_id;
                bpf_link_get_next_id;
                bpf_program__attach_iter;
+               perf_buffer__consume;
 } LIBBPF_0.0.8;

Thanks for the review, will send out a v2 soon.