Skip to contentSkip to conversation

libbpf: Add wakeup_events to creation options

PATCH Superseded Jon Doron 3 participants 4 messages Feb 1, 2023 – Feb 2, 2023

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jon Doron

author
#0 lore raw

PATCH 1/1 libbpf: Add wakeup_events to creation options

From: Jon Doron <[email protected]>

Add option to set when the perf buffer should wake up, by default the perf buffer becomes signaled for every event that is being pushed to it.

In case of a high throughput of events it will be more efficient to wake up only once you have X events ready to be read.

So your application can wakeup once and drain the entire perf buffer.

---
 tools/lib/bpf/libbpf.c | 4 ++--
 tools/lib/bpf/libbpf.h | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +2 -2
index eed5cec6f510..6b30ff13922b 100644
@@ -11719,8 +11719,8 @@ struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
 	attr.config = PERF_COUNT_SW_BPF_OUTPUT;
 	attr.type = PERF_TYPE_SOFTWARE;
 	attr.sample_type = PERF_SAMPLE_RAW;
-	attr.sample_period = 1;
-	attr.wakeup_events = 1;
+	attr.sample_period = OPTS_GET(opts, wakeup_events, 1);
+	attr.wakeup_events = OPTS_GET(opts, wakeup_events, 1);
 
 	p.attr = &attr;
 	p.sample_cb = sample_cb;
tools/lib/bpf/libbpf.h +1 -0
index 8777ff21ea1d..2e4bdfc58c82 100644
@@ -1246,6 +1246,7 @@ typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
 /* common use perf buffer options */
 struct perf_buffer_opts {
 	size_t sz;
+	__u32 wakeup_events;
 };
 #define perf_buffer_opts__last_field sz
 
Signature · 1 line
2.39.1
#1 lore raw
Quoted 21 lines · Jon Doron

From: Jon Doron <[email protected]>

Add option to set when the perf buffer should wake up, by default the perf buffer becomes signaled for every event that is being pushed to it.

In case of a high throughput of events it will be more efficient to wake up only once you have X events ready to be read.

So your application can wakeup once and drain the entire perf buffer.

---
 tools/lib/bpf/libbpf.c | 4 ++--
 tools/lib/bpf/libbpf.h | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +0 -0
index eed5cec6f510..6b30ff13922b 100644

tools/lib/bpf/libbpf.c struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt, · line 11722

Quoted 6 lines
tools/lib/bpf/libbpf.c +1 -2
@@ -11719,8 +11719,8 @@ struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
 	attr.config = PERF_COUNT_SW_BPF_OUTPUT;
 	attr.type = PERF_TYPE_SOFTWARE;
 	attr.sample_type = PERF_SAMPLE_RAW;
-	attr.sample_period = 1;
-	attr.wakeup_events = 1;
+	attr.sample_period = OPTS_GET(opts, wakeup_events, 1);

hm, but I think we still want every event.. setting sample_period to X would store only every X-th bpf-output event, no?

jirka

Quoted 19 lines
tools/lib/bpf/libbpf.c +1 -0
+	attr.wakeup_events = OPTS_GET(opts, wakeup_events, 1);
 
 	p.attr = &attr;
 	p.sample_cb = sample_cb;
tools/lib/bpf/libbpf.h +1 -0
index 8777ff21ea1d..2e4bdfc58c82 100644
@@ -1246,6 +1246,7 @@ typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
 /* common use perf buffer options */
 struct perf_buffer_opts {
 	size_t sz;
+	__u32 wakeup_events;
 };
 #define perf_buffer_opts__last_field sz
 
Signature · 1 line
2.39.1
#2 lore raw
Quoted 18 lines · Jiri Olsa
Quoted 16 lines · Jon Doron

From: Jon Doron <[email protected]>

Add option to set when the perf buffer should wake up, by default the perf buffer becomes signaled for every event that is being pushed to it.

In case of a high throughput of events it will be more efficient to wake up only once you have X events ready to be read.

So your application can wakeup once and drain the entire perf buffer.

---
 tools/lib/bpf/libbpf.c | 4 ++--
 tools/lib/bpf/libbpf.h | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)
Quoted 14 lines

tools/lib/bpf/libbpf.c struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt, · line 11722

Quoted 11 lines
tools/lib/bpf/libbpf.c +1 -2
index eed5cec6f510..6b30ff13922b 100644
@@ -11719,8 +11719,8 @@ struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
      attr.config = PERF_COUNT_SW_BPF_OUTPUT;
      attr.type = PERF_TYPE_SOFTWARE;
      attr.sample_type = PERF_SAMPLE_RAW;
-     attr.sample_period = 1;
-     attr.wakeup_events = 1;
+     attr.sample_period = OPTS_GET(opts, wakeup_events, 1);

hm, but I think we still want every event.. setting sample_period to X would store only every X-th bpf-output event, no?

seems like benchs/bench_ringbufs.c sets both sample_period and wakeup_events, but it would be nice to make sure we do not lose events with such configuration.

Let's add a selftest for this feature.

Quoted 5 lines

jirka

Quoted 2 lines
tools/lib/bpf/libbpf.c +1 -0
+     attr.wakeup_events = OPTS_GET(opts, wakeup_events, 1);
 
Quoted 13 lines

tools/lib/bpf/libbpf.h typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt); · line 1251

Quoted 13 lines
tools/lib/bpf/libbpf.c +0 -0
      p.attr = &attr;
      p.sample_cb = sample_cb;
tools/lib/bpf/libbpf.h +1 -0
index 8777ff21ea1d..2e4bdfc58c82 100644
@@ -1246,6 +1246,7 @@ typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
 /* common use perf buffer options */
 struct perf_buffer_opts {
      size_t sz;
+     __u32 wakeup_events;
 };
 #define perf_buffer_opts__last_field sz

you need to update perf_buffer_opts__last_field to wakeup_events as well

Quoted 4 lines
Quoted 4 lines
tools/lib/bpf/libbpf.h +0 -1
--

2.39.1

author
#3 lore raw
Quoted 34 lines · Andrii Nakryiko
Quoted 18 lines · Jiri Olsa
Quoted 16 lines · Jon Doron

From: Jon Doron <[email protected]>

Add option to set when the perf buffer should wake up, by default the perf buffer becomes signaled for every event that is being pushed to it.

In case of a high throughput of events it will be more efficient to wake up only once you have X events ready to be read.

So your application can wakeup once and drain the entire perf buffer.

---
 tools/lib/bpf/libbpf.c | 4 ++--
 tools/lib/bpf/libbpf.h | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)
Quoted 14 lines

tools/lib/bpf/libbpf.c struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt, · line 11722

Quoted 11 lines
tools/lib/bpf/libbpf.c +1 -2
index eed5cec6f510..6b30ff13922b 100644
@@ -11719,8 +11719,8 @@ struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
      attr.config = PERF_COUNT_SW_BPF_OUTPUT;
      attr.type = PERF_TYPE_SOFTWARE;
      attr.sample_type = PERF_SAMPLE_RAW;
-     attr.sample_period = 1;
-     attr.wakeup_events = 1;
+     attr.sample_period = OPTS_GET(opts, wakeup_events, 1);

hm, but I think we still want every event.. setting sample_period to X would store only every X-th bpf-output event, no?

Quoted 6 lines

seems like benchs/bench_ringbufs.c sets both sample_period and wakeup_events, but it would be nice to make sure we do not lose events with such configuration.

Let's add a selftest for this feature.

That's how it works, we wont be losing any events

Quoted 5 lines
Quoted 5 lines

jirka

Quoted 2 lines
tools/lib/bpf/libbpf.c +1 -0
+     attr.wakeup_events = OPTS_GET(opts, wakeup_events, 1);
 
Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
tools/lib/bpf/libbpf.c +0 -0
      p.attr = &attr;
      p.sample_cb = sample_cb;
tools/lib/bpf/libbpf.h +0 -1
index 8777ff21ea1d..2e4bdfc58c82 100644
--- a/tools/lib/bpf/libbpf.h
Quoted 8 lines

tools/lib/bpf/libbpf.h typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt); · line 1251

Quoted 8 lines

+++ b/tools/lib/bpf/libbpf.h

tools/lib/bpf/libbpf.h +1 -0
@@ -1246,6 +1246,7 @@ typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
 /* common use perf buffer options */
 struct perf_buffer_opts {
      size_t sz;
+     __u32 wakeup_events;
 };
 #define perf_buffer_opts__last_field sz

you need to update perf_buffer_opts__last_field to wakeup_events as well

Done

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
tools/lib/bpf/libbpf.h +0 -1
--

2.39.1