Skip to contentSkip to conversation

libbpf: Add wakeup_events to creation options

PATCH Superseded Jon Doron 3 participants 8 messages Feb 2, 2023 – Feb 6, 2023

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jon Doron

author
#0 lore raw

PATCH v2 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 | 3 ++-
 2 files changed, 4 insertions(+), 3 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 +2 -1
index 8777ff21ea1d..e83c0a915dc7 100644
@@ -1246,8 +1246,9 @@ 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
+#define perf_buffer_opts__last_field wakeup_events
 
 /**
  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
Signature · 1 line
2.39.1
#1 lore raw
Quoted 22 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 | 3 ++-
 2 files changed, 4 insertions(+), 3 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 11723

Quoted 7 lines
tools/lib/bpf/libbpf.c +2 -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);
+       attr.wakeup_events = OPTS_GET(opts, wakeup_events, 1);

I suspect the case of

LIBBPF_OPTS(perf_buffer_opts, opts);

perf_buffer__new(...., &opts);

is not handled correctly and you end up with sample_period == wakeup_events == 0

Can you please add BPF selftests that's setting wakeup_events to zero and separately to >1?

Quoted 21 lines
tools/lib/bpf/libbpf.c +0 -0
        p.attr = &attr;
        p.sample_cb = sample_cb;
tools/lib/bpf/libbpf.h +2 -2
index 8777ff21ea1d..e83c0a915dc7 100644
@@ -1246,8 +1246,9 @@ 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
+#define perf_buffer_opts__last_field wakeup_events
 
 /**
  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
--

2.39.1

author
#2 lore raw
Quoted 33 lines · Andrii Nakryiko
Quoted 22 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 | 3 ++-
 2 files changed, 4 insertions(+), 3 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 11723

Quoted 7 lines
tools/lib/bpf/libbpf.c +2 -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);
+       attr.wakeup_events = OPTS_GET(opts, wakeup_events, 1);

I suspect the case of

Quoted 9 lines

LIBBPF_OPTS(perf_buffer_opts, opts);

perf_buffer__new(...., &opts);

is not handled correctly and you end up with sample_period == wakeup_events == 0

Can you please add BPF selftests that's setting wakeup_events to zero and separately to >1?

Hi Andrii,

I'm not sure what we are testing, when you have sample_period == wakeup_events == 0, it basically means to never wakeup, so let's say you would wait on the poll_fd infinitely it will never wake you up.

When you have let's say wakeup_event != 0, you will wakeup after the ring buffer in the perf buffer has more events than wakeup_events.

I do see your point that if someone is using the macro to build the opts they will end with something unexpected, would you like me to treat 0 as 1 in that case?

-- Jon.

Quoted 21 lines
Quoted 21 lines
tools/lib/bpf/libbpf.c +0 -0
        p.attr = &attr;
        p.sample_cb = sample_cb;
tools/lib/bpf/libbpf.h +2 -2
index 8777ff21ea1d..e83c0a915dc7 100644
@@ -1246,8 +1246,9 @@ 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
+#define perf_buffer_opts__last_field wakeup_events
 
 /**
  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
--

2.39.1

#4 lore raw
In reply to Jon Doron
Quoted 36 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 | 3 ++-
  2 files changed, 4 insertions(+), 3 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 +0 -0
index 8777ff21ea1d..e83c0a915dc7 100644

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

Quoted 4 lines
tools/lib/bpf/libbpf.h +1 -0
@@ -1246,8 +1246,9 @@ 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;

Since you are adding wakeup_events here, do you think it make sense
to add sample_period to struct perf_buffer_opts as well? In some cases,
users might want to have different values for sample_period and wakeup_events, e.g., smaller sample_period to accumulate data and larger wakeup_events to wakeup user space poll?

Quoted 6 lines
tools/lib/bpf/libbpf.h +1 -1
  };
-#define perf_buffer_opts__last_field sz
+#define perf_buffer_opts__last_field wakeup_events
 
  /**
   * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
#3 lore raw
In reply to Jon Doron
Quoted 51 lines · Jon Doron
Quoted 33 lines · Andrii Nakryiko
Quoted 22 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 | 3 ++-
 2 files changed, 4 insertions(+), 3 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 11723

Quoted 7 lines
tools/lib/bpf/libbpf.c +2 -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);
+       attr.wakeup_events = OPTS_GET(opts, wakeup_events, 1);

I suspect the case of

Quoted 9 lines

LIBBPF_OPTS(perf_buffer_opts, opts);

perf_buffer__new(...., &opts);

is not handled correctly and you end up with sample_period == wakeup_events == 0

Can you please add BPF selftests that's setting wakeup_events to zero and separately to >1?

Hi Andrii,

I'm not sure what we are testing, when you have sample_period == wakeup_events == 0, it basically means to never wakeup, so let's say you would wait on the poll_fd infinitely it will never wake you up.

Quoted 6 lines

When you have let's say wakeup_event != 0, you will wakeup after the ring buffer in the perf buffer has more events than wakeup_events.

I do see your point that if someone is using the macro to build the opts they will end with something unexpected, would you like me to treat 0 as 1 in that case?

Yes, exactly, I think we should treat zero as 1 and write a test that this happens. Otherwise it will be very confusing when someone use perf_buffer_opts for some other future option, and then suddenly starts getting no notification. If someone really needs wakeup == 0, they have a fallback plan to use perf_buffer__new_raw_opts(), which is probably justified for some very specific and advanced uses.

So yes, please add a test with few subtests where we test default opts (wakeup_after == 1), and wakeup_after > 1.

Quoted 24 lines

-- Jon.

Quoted 21 lines
Quoted 21 lines
tools/lib/bpf/libbpf.c +0 -0
        p.attr = &attr;
        p.sample_cb = sample_cb;
tools/lib/bpf/libbpf.h +2 -2
index 8777ff21ea1d..e83c0a915dc7 100644
@@ -1246,8 +1246,9 @@ 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
+#define perf_buffer_opts__last_field wakeup_events
 
 /**
  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
--

2.39.1

#5 lore raw
In reply to Yonghong Song
Quoted 42 lines · Yonghong Song
Quoted 38 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 | 3 ++-
  2 files changed, 4 insertions(+), 3 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 +0 -0
index 8777ff21ea1d..e83c0a915dc7 100644
@@ -1246,8 +1246,9 @@ typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
  /* common use perf buffer options */
  struct perf_buffer_opts {
Quoted 8 lines

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

Quoted 2 lines
tools/lib/bpf/libbpf.h +1 -0
      size_t sz;
+     __u32 wakeup_events;

Since you are adding wakeup_events here, do you think it make sense to add sample_period to struct perf_buffer_opts as well? In some cases, users might want to have different values for sample_period and wakeup_events, e.g., smaller sample_period to accumulate data and larger wakeup_events to wakeup user space poll?

It's not clear to me from reading perf_event_open manpage what "sample_period" means for perf buffer. What will happen when we have sample_period != wakeup_events?

Quoted 7 lines
Quoted 7 lines
tools/lib/bpf/libbpf.h +1 -1
  };
-#define perf_buffer_opts__last_field sz
+#define perf_buffer_opts__last_field wakeup_events
 
  /**
   * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
#6 lore raw
Quoted 52 lines · Andrii Nakryiko
Quoted 42 lines · Yonghong Song
Quoted 38 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 | 3 ++-
   2 files changed, 4 insertions(+), 3 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 +0 -0
index 8777ff21ea1d..e83c0a915dc7 100644
@@ -1246,8 +1246,9 @@ typedef void (*perf_buffer_lost_fn)(void *ctx, int cpu, __u64 cnt);
   /* common use perf buffer options */
   struct perf_buffer_opts {
Quoted 8 lines

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

Quoted 2 lines
tools/lib/bpf/libbpf.h +1 -0
       size_t sz;
+     __u32 wakeup_events;

Since you are adding wakeup_events here, do you think it make sense to add sample_period to struct perf_buffer_opts as well? In some cases, users might want to have different values for sample_period and wakeup_events, e.g., smaller sample_period to accumulate data and larger wakeup_events to wakeup user space poll?

Quoted 3 lines

It's not clear to me from reading perf_event_open manpage what "sample_period" means for perf buffer. What will happen when we have sample_period != wakeup_events?

The following is my understanding. Let us sample_period = 10, wakeup_events = 100.

Every 10 samples, kernel overflow handler is called and the the sample data are written into ring buffer. Every 100 samples, the poll() syscall is waken up to give userspace
a chance to read the data.

In this particular case, it is possible that user space may miss some samples at the end if no special handling (I forgot what it is). But if the user space doesn't really care, it might be okay with such a configuration.

It would be great if some perf expert can clarify whether my understanding is correct or not.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
tools/lib/bpf/libbpf.h +1 -1
   };
-#define perf_buffer_opts__last_field sz
+#define perf_buffer_opts__last_field wakeup_events
 
   /**
    * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
#7 lore raw
Quoted 68 lines · Yonghong Song
Quoted 54 lines · Andrii Nakryiko
Quoted 44 lines · Yonghong Song
Quoted 40 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 | 3 ++-

   2 files changed, 4 insertions(+), 3 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 +0 -0
index 8777ff21ea1d..e83c0a915dc7 100644
@@ -1246,8 +1246,9 @@ typedef void (*perf_buffer_lost_fn)(void *ctx, 
 int cpu, __u64 cnt);
    /* common use perf buffer options */
    struct perf_buffer_opts {
Quoted 8 lines

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

Quoted 2 lines
tools/lib/bpf/libbpf.h +1 -0
        size_t sz;
+     __u32 wakeup_events;

Since you are adding wakeup_events here, do you think it make sense to add sample_period to struct perf_buffer_opts as well? In some cases, users might want to have different values for sample_period and wakeup_events, e.g., smaller sample_period to accumulate data and larger wakeup_events to wakeup user space poll?

Quoted 3 lines

It's not clear to me from reading perf_event_open manpage what "sample_period" means for perf buffer. What will happen when we have sample_period != wakeup_events?

The following is my understanding. Let us sample_period = 10, wakeup_events = 100.

Every 10 samples, kernel overflow handler is called and the the sample data are written into ring buffer. Every 100 samples, the poll() syscall is waken up to give userspace
a chance to read the data.

Quoted 7 lines

In this particular case, it is possible that user space may miss some samples at the end if no special handling (I forgot what it is). But if the user space doesn't really care, it might be okay with such a configuration.

It would be great if some perf expert can clarify whether my understanding is correct or not.

Think twice. I am okay with just one wakeup_events/sample_period in the perf_buffer_opts since wakeup_events == sample_period is the most common use case. The same as before wakeup_events = sample_period = 1.

But it is apparent that sample_period is much easier to understand than wakeup_events. Can we put sample_period in struct perf_buffer_opts instead of wakeup_events?

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
tools/lib/bpf/libbpf.h +1 -1
    };
-#define perf_buffer_opts__last_field sz
+#define perf_buffer_opts__last_field wakeup_events
 
    /**

    * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified