Skip to content

Commit c17bec5

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
samples/bpf: switch trace_output sample to perf_buffer API
Convert trace_output sample to libbpf's perf_buffer API. Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Song Liu <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f58a4d5 commit c17bec5

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

samples/bpf/trace_output_user.c

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
#include <libbpf.h>
1919
#include "bpf_load.h"
2020
#include "perf-sys.h"
21-
#include "trace_helpers.h"
22-
23-
static int pmu_fd;
2421

2522
static __u64 time_get_ns(void)
2623
{
@@ -31,12 +28,12 @@ static __u64 time_get_ns(void)
3128
}
3229

3330
static __u64 start_time;
31+
static __u64 cnt;
3432

3533
#define MAX_CNT 100000ll
3634

37-
static int print_bpf_output(void *data, int size)
35+
static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
3836
{
39-
static __u64 cnt;
4037
struct {
4138
__u64 pid;
4239
__u64 cookie;
@@ -45,38 +42,22 @@ static int print_bpf_output(void *data, int size)
4542
if (e->cookie != 0x12345678) {
4643
printf("BUG pid %llx cookie %llx sized %d\n",
4744
e->pid, e->cookie, size);
48-
return LIBBPF_PERF_EVENT_ERROR;
45+
return;
4946
}
5047

5148
cnt++;
5249

5350
if (cnt == MAX_CNT) {
5451
printf("recv %lld events per sec\n",
5552
MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
56-
return LIBBPF_PERF_EVENT_DONE;
53+
return;
5754
}
58-
59-
return LIBBPF_PERF_EVENT_CONT;
60-
}
61-
62-
static void test_bpf_perf_event(void)
63-
{
64-
struct perf_event_attr attr = {
65-
.sample_type = PERF_SAMPLE_RAW,
66-
.type = PERF_TYPE_SOFTWARE,
67-
.config = PERF_COUNT_SW_BPF_OUTPUT,
68-
};
69-
int key = 0;
70-
71-
pmu_fd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
72-
73-
assert(pmu_fd >= 0);
74-
assert(bpf_map_update_elem(map_fd[0], &key, &pmu_fd, BPF_ANY) == 0);
75-
ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
7655
}
7756

7857
int main(int argc, char **argv)
7958
{
59+
struct perf_buffer_opts pb_opts = {};
60+
struct perf_buffer *pb;
8061
char filename[256];
8162
FILE *f;
8263
int ret;
@@ -88,16 +69,20 @@ int main(int argc, char **argv)
8869
return 1;
8970
}
9071

91-
test_bpf_perf_event();
92-
93-
if (perf_event_mmap(pmu_fd) < 0)
72+
pb_opts.sample_cb = print_bpf_output;
73+
pb = perf_buffer__new(map_fd[0], 8, &pb_opts);
74+
ret = libbpf_get_error(pb);
75+
if (ret) {
76+
printf("failed to setup perf_buffer: %d\n", ret);
9477
return 1;
78+
}
9579

9680
f = popen("taskset 1 dd if=/dev/zero of=/dev/null", "r");
9781
(void) f;
9882

9983
start_time = time_get_ns();
100-
ret = perf_event_poller(pmu_fd, print_bpf_output);
84+
while ((ret = perf_buffer__poll(pb, 1000)) >= 0 && cnt < MAX_CNT) {
85+
}
10186
kill(0, SIGINT);
10287
return ret;
10388
}

0 commit comments

Comments
 (0)