Skip to contentSkip to conversation

libbpf: Add sample_period to creation options

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

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jon Doron

author
#0 lore raw

PATCH v3 1/1 libbpf: Add sample_period 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 | 9 +++++++--
 tools/lib/bpf/libbpf.h | 3 ++-
 2 files changed, 9 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +7 -2
index eed5cec6f510..cd0bce5482b2 100644
@@ -11710,17 +11710,22 @@ struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
 	const size_t attr_sz = sizeof(struct perf_event_attr);
 	struct perf_buffer_params p = {};
 	struct perf_event_attr attr;
+	__u32 sample_period;
 
 	if (!OPTS_VALID(opts, perf_buffer_opts))
 		return libbpf_err_ptr(-EINVAL);
 
+	sample_period = OPTS_GET(opts, sample_period, 1);
+	if (!sample_period)
+		sample_period = 1;
+
 	memset(&attr, 0, attr_sz);
 	attr.size = attr_sz;
 	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 = sample_period;
+	attr.wakeup_events = sample_period;
 
 	p.attr = &attr;
 	p.sample_cb = sample_cb;
tools/lib/bpf/libbpf.h +2 -1
index 8777ff21ea1d..5d3b75a5acde 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 sample_period;
 };
-#define perf_buffer_opts__last_field sz
+#define perf_buffer_opts__last_field sample_period
 
 /**
  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
Signature · 1 line
2.39.1
Acked-by on v3 1/1
#1 lore raw
Quoted 11 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.

LGTM with one possible change below.

Quoted 39 lines
---
  tools/lib/bpf/libbpf.c | 9 +++++++--
  tools/lib/bpf/libbpf.h | 3 ++-
  2 files changed, 9 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +7 -2
index eed5cec6f510..cd0bce5482b2 100644
@@ -11710,17 +11710,22 @@ struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
  	const size_t attr_sz = sizeof(struct perf_event_attr);
  	struct perf_buffer_params p = {};
  	struct perf_event_attr attr;
+	__u32 sample_period;
 
  	if (!OPTS_VALID(opts, perf_buffer_opts))
  		return libbpf_err_ptr(-EINVAL);
 
+	sample_period = OPTS_GET(opts, sample_period, 1);
+	if (!sample_period)
+		sample_period = 1;
+
  	memset(&attr, 0, attr_sz);
  	attr.size = attr_sz;
  	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 = sample_period;
+	attr.wakeup_events = sample_period;
 
  	p.attr = &attr;
  	p.sample_cb = sample_cb;
tools/lib/bpf/libbpf.h +0 -0
index 8777ff21ea1d..5d3b75a5acde 100644

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

Quoted 5 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 sample_period;
  };

The data structure now may be 16 bytes for 64bit system and we have 4 byte padding at the end which could be arbitrary value. The libbpf convention is to add "size_t :0;" at the end of structure to zero out tail padding during declaration.

Quoted 5 lines
tools/lib/bpf/libbpf.h +1 -1
-#define perf_buffer_opts__last_field sz
+#define perf_buffer_opts__last_field sample_period
 
  /**
   * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified
author
#2 lore raw
Quoted 57 lines · Yonghong Song
Quoted 11 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.

LGTM with one possible change below.

Quoted 38 lines
---
 tools/lib/bpf/libbpf.c | 9 +++++++--
 tools/lib/bpf/libbpf.h | 3 ++-
 2 files changed, 9 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +7 -2
index eed5cec6f510..cd0bce5482b2 100644
@@ -11710,17 +11710,22 @@ struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
 	const size_t attr_sz = sizeof(struct perf_event_attr);
 	struct perf_buffer_params p = {};
 	struct perf_event_attr attr;
+	__u32 sample_period;
 	if (!OPTS_VALID(opts, perf_buffer_opts))
 		return libbpf_err_ptr(-EINVAL);
+	sample_period = OPTS_GET(opts, sample_period, 1);
+	if (!sample_period)
+		sample_period = 1;
+
 	memset(&attr, 0, attr_sz);
 	attr.size = attr_sz;
 	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 = sample_period;
+	attr.wakeup_events = sample_period;
 	p.attr = &attr;
 	p.sample_cb = sample_cb;
tools/lib/bpf/libbpf.h +0 -0
index 8777ff21ea1d..5d3b75a5acde 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 9 lines

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

Quoted 3 lines
tools/lib/bpf/libbpf.h +1 -0
 	size_t sz;
+	__u32 sample_period;
 };

The data structure now may be 16 bytes for 64bit system and we have 4 byte padding at the end which could be arbitrary value. The libbpf convention is to add "size_t :0;" at the end of structure to zero out tail padding during declaration.

Done

Quoted 4 lines
Quoted 4 lines
tools/lib/bpf/libbpf.h +1 -1
-#define perf_buffer_opts__last_field sz
+#define perf_buffer_opts__last_field sample_period
 /**
  * @brief **perf_buffer__new()** creates BPF perfbuf manager for a specified