selftests/bpf: convert test_get_stack_raw_tp to perf_buffer API

Convert test_get_stack_raw_tp test to new perf_buffer API.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Andrii Nakryiko 2019-07-23 14:34:41 -07:00 committed by Alexei Starovoitov
parent f2a3e4e95f
commit 58b8081536
2 changed files with 44 additions and 36 deletions

View File

@ -1,8 +1,15 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
#include <sys/socket.h>
#include <test_progs.h> #include <test_progs.h>
#define MAX_CNT_RAWTP 10ull #define MAX_CNT_RAWTP 10ull
#define MAX_STACK_RAWTP 100 #define MAX_STACK_RAWTP 100
static int duration = 0;
struct get_stack_trace_t { struct get_stack_trace_t {
int pid; int pid;
int kern_stack_size; int kern_stack_size;
@ -13,7 +20,7 @@ struct get_stack_trace_t {
struct bpf_stack_build_id user_stack_buildid[MAX_STACK_RAWTP]; struct bpf_stack_build_id user_stack_buildid[MAX_STACK_RAWTP];
}; };
static int get_stack_print_output(void *data, int size) static void get_stack_print_output(void *ctx, int cpu, void *data, __u32 size)
{ {
bool good_kern_stack = false, good_user_stack = false; bool good_kern_stack = false, good_user_stack = false;
const char *nonjit_func = "___bpf_prog_run"; const char *nonjit_func = "___bpf_prog_run";
@ -65,75 +72,76 @@ static int get_stack_print_output(void *data, int size)
if (e->user_stack_size > 0 && e->user_stack_buildid_size > 0) if (e->user_stack_size > 0 && e->user_stack_buildid_size > 0)
good_user_stack = true; good_user_stack = true;
} }
if (!good_kern_stack || !good_user_stack)
return LIBBPF_PERF_EVENT_ERROR;
if (cnt == MAX_CNT_RAWTP) if (!good_kern_stack)
return LIBBPF_PERF_EVENT_DONE; CHECK(!good_kern_stack, "kern_stack", "corrupted kernel stack\n");
if (!good_user_stack)
return LIBBPF_PERF_EVENT_CONT; CHECK(!good_user_stack, "user_stack", "corrupted user stack\n");
} }
void test_get_stack_raw_tp(void) void test_get_stack_raw_tp(void)
{ {
const char *file = "./test_get_stack_rawtp.o"; const char *file = "./test_get_stack_rawtp.o";
int i, efd, err, prog_fd, pmu_fd, perfmap_fd; const char *prog_name = "raw_tracepoint/sys_enter";
struct perf_event_attr attr = {}; int i, err, prog_fd, exp_cnt = MAX_CNT_RAWTP;
struct perf_buffer_opts pb_opts = {};
struct perf_buffer *pb = NULL;
struct bpf_link *link = NULL;
struct timespec tv = {0, 10}; struct timespec tv = {0, 10};
__u32 key = 0, duration = 0; struct bpf_program *prog;
struct bpf_object *obj; struct bpf_object *obj;
struct bpf_map *map;
cpu_set_t cpu_set;
err = bpf_prog_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno))
return; return;
efd = bpf_raw_tracepoint_open("sys_enter", prog_fd); prog = bpf_object__find_program_by_title(obj, prog_name);
if (CHECK(efd < 0, "raw_tp_open", "err %d errno %d\n", efd, errno)) if (CHECK(!prog, "find_probe", "prog '%s' not found\n", prog_name))
goto close_prog; goto close_prog;
perfmap_fd = bpf_find_map(__func__, obj, "perfmap"); map = bpf_object__find_map_by_name(obj, "perfmap");
if (CHECK(perfmap_fd < 0, "bpf_find_map", "err %d errno %d\n", if (CHECK(!map, "bpf_find_map", "not found\n"))
perfmap_fd, errno))
goto close_prog; goto close_prog;
err = load_kallsyms(); err = load_kallsyms();
if (CHECK(err < 0, "load_kallsyms", "err %d errno %d\n", err, errno)) if (CHECK(err < 0, "load_kallsyms", "err %d errno %d\n", err, errno))
goto close_prog; goto close_prog;
attr.sample_type = PERF_SAMPLE_RAW; CPU_ZERO(&cpu_set);
attr.type = PERF_TYPE_SOFTWARE; CPU_SET(0, &cpu_set);
attr.config = PERF_COUNT_SW_BPF_OUTPUT; err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set);
pmu_fd = syscall(__NR_perf_event_open, &attr, getpid()/*pid*/, -1/*cpu*/, if (CHECK(err, "set_affinity", "err %d, errno %d\n", err, errno))
-1/*group_fd*/, 0);
if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n", pmu_fd,
errno))
goto close_prog; goto close_prog;
err = bpf_map_update_elem(perfmap_fd, &key, &pmu_fd, BPF_ANY); link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
if (CHECK(err < 0, "bpf_map_update_elem", "err %d errno %d\n", err, if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n", PTR_ERR(link)))
errno))
goto close_prog; goto close_prog;
err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0); pb_opts.sample_cb = get_stack_print_output;
if (CHECK(err < 0, "ioctl PERF_EVENT_IOC_ENABLE", "err %d errno %d\n", pb = perf_buffer__new(bpf_map__fd(map), 8, &pb_opts);
err, errno)) if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb)))
goto close_prog;
err = perf_event_mmap(pmu_fd);
if (CHECK(err < 0, "perf_event_mmap", "err %d errno %d\n", err, errno))
goto close_prog; goto close_prog;
/* trigger some syscall action */ /* trigger some syscall action */
for (i = 0; i < MAX_CNT_RAWTP; i++) for (i = 0; i < MAX_CNT_RAWTP; i++)
nanosleep(&tv, NULL); nanosleep(&tv, NULL);
err = perf_event_poller(pmu_fd, get_stack_print_output); while (exp_cnt > 0) {
if (CHECK(err < 0, "perf_event_poller", "err %d errno %d\n", err, errno)) err = perf_buffer__poll(pb, 100);
goto close_prog; if (err < 0 && CHECK(err < 0, "pb__poll", "err %d\n", err))
goto close_prog;
exp_cnt -= err;
}
goto close_prog_noerr; goto close_prog_noerr;
close_prog: close_prog:
error_cnt++; error_cnt++;
close_prog_noerr: close_prog_noerr:
if (!IS_ERR_OR_NULL(link))
bpf_link__destroy(link);
if (!IS_ERR_OR_NULL(pb))
perf_buffer__free(pb);
bpf_object__close(obj); bpf_object__close(obj);
} }

View File

@ -55,7 +55,7 @@ struct {
__type(value, raw_stack_trace_t); __type(value, raw_stack_trace_t);
} rawdata_map SEC(".maps"); } rawdata_map SEC(".maps");
SEC("tracepoint/raw_syscalls/sys_enter") SEC("raw_tracepoint/sys_enter")
int bpf_prog1(void *ctx) int bpf_prog1(void *ctx)
{ {
int max_len, max_buildid_len, usize, ksize, total_size; int max_len, max_buildid_len, usize, ksize, total_size;