Commit f0c328f8 authored by Daniel T. Lee's avatar Daniel T. Lee Committed by Alexei Starovoitov

samples: bpf: Refactor tracepoint tracing programs with libbpf

For the problem of increasing fragmentation of the bpf loader programs,
instead of using bpf_loader.o, which is used in samples/bpf, this
commit refactors the existing tracepoint tracing programs with libbbpf
bpf loader.

    - Adding a tracepoint event and attaching a bpf program to it was done
    through bpf_program_attach().
    - Instead of using the existing BPF MAP definition, MAP definition
    has been refactored with the new BTF-defined MAP format.
Signed-off-by: default avatarDaniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200823085334.9413-4-danieltimlee@gmail.com
parent 3677d0a1
...@@ -74,7 +74,7 @@ tracex7-objs := tracex7_user.o ...@@ -74,7 +74,7 @@ tracex7-objs := tracex7_user.o
test_probe_write_user-objs := test_probe_write_user_user.o test_probe_write_user-objs := test_probe_write_user_user.o
trace_output-objs := trace_output_user.o $(TRACE_HELPERS) trace_output-objs := trace_output_user.o $(TRACE_HELPERS)
lathist-objs := lathist_user.o lathist-objs := lathist_user.o
offwaketime-objs := bpf_load.o offwaketime_user.o $(TRACE_HELPERS) offwaketime-objs := offwaketime_user.o $(TRACE_HELPERS)
spintest-objs := spintest_user.o $(TRACE_HELPERS) spintest-objs := spintest_user.o $(TRACE_HELPERS)
map_perf_test-objs := map_perf_test_user.o map_perf_test-objs := map_perf_test_user.o
test_overhead-objs := bpf_load.o test_overhead_user.o test_overhead-objs := bpf_load.o test_overhead_user.o
...@@ -100,8 +100,8 @@ xdp_redirect_map-objs := xdp_redirect_map_user.o ...@@ -100,8 +100,8 @@ xdp_redirect_map-objs := xdp_redirect_map_user.o
xdp_redirect_cpu-objs := bpf_load.o xdp_redirect_cpu_user.o xdp_redirect_cpu-objs := bpf_load.o xdp_redirect_cpu_user.o
xdp_monitor-objs := bpf_load.o xdp_monitor_user.o xdp_monitor-objs := bpf_load.o xdp_monitor_user.o
xdp_rxq_info-objs := xdp_rxq_info_user.o xdp_rxq_info-objs := xdp_rxq_info_user.o
syscall_tp-objs := bpf_load.o syscall_tp_user.o syscall_tp-objs := syscall_tp_user.o
cpustat-objs := bpf_load.o cpustat_user.o cpustat-objs := cpustat_user.o
xdp_adjust_tail-objs := xdp_adjust_tail_user.o xdp_adjust_tail-objs := xdp_adjust_tail_user.o
xdpsock-objs := xdpsock_user.o xdpsock-objs := xdpsock_user.o
xdp_fwd-objs := xdp_fwd_user.o xdp_fwd-objs := xdp_fwd_user.o
......
...@@ -51,28 +51,28 @@ static int cpu_opps[] = { 208000, 432000, 729000, 960000, 1200000 }; ...@@ -51,28 +51,28 @@ static int cpu_opps[] = { 208000, 432000, 729000, 960000, 1200000 };
#define MAP_OFF_PSTATE_IDX 3 #define MAP_OFF_PSTATE_IDX 3
#define MAP_OFF_NUM 4 #define MAP_OFF_NUM 4
struct bpf_map_def SEC("maps") my_map = { struct {
.type = BPF_MAP_TYPE_ARRAY, __uint(type, BPF_MAP_TYPE_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(u64), __type(value, u64);
.max_entries = MAX_CPU * MAP_OFF_NUM, __uint(max_entries, MAX_CPU * MAP_OFF_NUM);
}; } my_map SEC(".maps");
/* cstate_duration records duration time for every idle state per CPU */ /* cstate_duration records duration time for every idle state per CPU */
struct bpf_map_def SEC("maps") cstate_duration = { struct {
.type = BPF_MAP_TYPE_ARRAY, __uint(type, BPF_MAP_TYPE_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(u64), __type(value, u64);
.max_entries = MAX_CPU * MAX_CSTATE_ENTRIES, __uint(max_entries, MAX_CPU * MAX_CSTATE_ENTRIES);
}; } cstate_duration SEC(".maps");
/* pstate_duration records duration time for every operating point per CPU */ /* pstate_duration records duration time for every operating point per CPU */
struct bpf_map_def SEC("maps") pstate_duration = { struct {
.type = BPF_MAP_TYPE_ARRAY, __uint(type, BPF_MAP_TYPE_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(u64), __type(value, u64);
.max_entries = MAX_CPU * MAX_PSTATE_ENTRIES, __uint(max_entries, MAX_CPU * MAX_PSTATE_ENTRIES);
}; } pstate_duration SEC(".maps");
/* /*
* The trace events for cpu_idle and cpu_frequency are taken from: * The trace events for cpu_idle and cpu_frequency are taken from:
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <linux/bpf.h>
#include <locale.h> #include <locale.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -18,7 +17,9 @@ ...@@ -18,7 +17,9 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include "bpf_load.h" #include <bpf/libbpf.h>
static int cstate_map_fd, pstate_map_fd;
#define MAX_CPU 8 #define MAX_CPU 8
#define MAX_PSTATE_ENTRIES 5 #define MAX_PSTATE_ENTRIES 5
...@@ -181,21 +182,50 @@ static void int_exit(int sig) ...@@ -181,21 +182,50 @@ static void int_exit(int sig)
{ {
cpu_stat_inject_cpu_idle_event(); cpu_stat_inject_cpu_idle_event();
cpu_stat_inject_cpu_frequency_event(); cpu_stat_inject_cpu_frequency_event();
cpu_stat_update(map_fd[1], map_fd[2]); cpu_stat_update(cstate_map_fd, pstate_map_fd);
cpu_stat_print(); cpu_stat_print();
exit(0); exit(0);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct bpf_link *link = NULL;
struct bpf_program *prog;
struct bpf_object *obj;
char filename[256]; char filename[256];
int ret; int ret;
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
obj = bpf_object__open_file(filename, NULL);
if (libbpf_get_error(obj)) {
fprintf(stderr, "ERROR: opening BPF object file failed\n");
return 0;
}
if (load_bpf_file(filename)) { prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
printf("%s", bpf_log_buf); if (!prog) {
return 1; printf("finding a prog in obj file failed\n");
goto cleanup;
}
/* load BPF program */
if (bpf_object__load(obj)) {
fprintf(stderr, "ERROR: loading BPF object file failed\n");
goto cleanup;
}
cstate_map_fd = bpf_object__find_map_fd_by_name(obj, "cstate_duration");
pstate_map_fd = bpf_object__find_map_fd_by_name(obj, "pstate_duration");
if (cstate_map_fd < 0 || pstate_map_fd < 0) {
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
goto cleanup;
}
link = bpf_program__attach(prog);
if (libbpf_get_error(link)) {
fprintf(stderr, "ERROR: bpf_program__attach failed\n");
link = NULL;
goto cleanup;
} }
ret = cpu_stat_inject_cpu_idle_event(); ret = cpu_stat_inject_cpu_idle_event();
...@@ -210,10 +240,13 @@ int main(int argc, char **argv) ...@@ -210,10 +240,13 @@ int main(int argc, char **argv)
signal(SIGTERM, int_exit); signal(SIGTERM, int_exit);
while (1) { while (1) {
cpu_stat_update(map_fd[1], map_fd[2]); cpu_stat_update(cstate_map_fd, pstate_map_fd);
cpu_stat_print(); cpu_stat_print();
sleep(5); sleep(5);
} }
cleanup:
bpf_link__destroy(link);
bpf_object__close(obj);
return 0; return 0;
} }
...@@ -28,38 +28,38 @@ struct key_t { ...@@ -28,38 +28,38 @@ struct key_t {
u32 tret; u32 tret;
}; };
struct bpf_map_def SEC("maps") counts = { struct {
.type = BPF_MAP_TYPE_HASH, __uint(type, BPF_MAP_TYPE_HASH);
.key_size = sizeof(struct key_t), __type(key, struct key_t);
.value_size = sizeof(u64), __type(value, u64);
.max_entries = 10000, __uint(max_entries, 10000);
}; } counts SEC(".maps");
struct bpf_map_def SEC("maps") start = { struct {
.type = BPF_MAP_TYPE_HASH, __uint(type, BPF_MAP_TYPE_HASH);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(u64), __type(value, u64);
.max_entries = 10000, __uint(max_entries, 10000);
}; } start SEC(".maps");
struct wokeby_t { struct wokeby_t {
char name[TASK_COMM_LEN]; char name[TASK_COMM_LEN];
u32 ret; u32 ret;
}; };
struct bpf_map_def SEC("maps") wokeby = { struct {
.type = BPF_MAP_TYPE_HASH, __uint(type, BPF_MAP_TYPE_HASH);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(struct wokeby_t), __type(value, struct wokeby_t);
.max_entries = 10000, __uint(max_entries, 10000);
}; } wokeby SEC(".maps");
struct bpf_map_def SEC("maps") stackmap = { struct {
.type = BPF_MAP_TYPE_STACK_TRACE, __uint(type, BPF_MAP_TYPE_STACK_TRACE);
.key_size = sizeof(u32), __uint(key_size, sizeof(u32));
.value_size = PERF_MAX_STACK_DEPTH * sizeof(u64), __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
.max_entries = 10000, __uint(max_entries, 10000);
}; } stackmap SEC(".maps");
#define STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP) #define STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP)
......
...@@ -5,19 +5,19 @@ ...@@ -5,19 +5,19 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
#include <linux/bpf.h>
#include <string.h>
#include <linux/perf_event.h> #include <linux/perf_event.h>
#include <errno.h> #include <errno.h>
#include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include "bpf_load.h" #include <bpf/bpf.h>
#include "trace_helpers.h" #include "trace_helpers.h"
#define PRINT_RAW_ADDR 0 #define PRINT_RAW_ADDR 0
/* counts, stackmap */
static int map_fd[2];
static void print_ksym(__u64 addr) static void print_ksym(__u64 addr)
{ {
struct ksym *sym; struct ksym *sym;
...@@ -52,14 +52,14 @@ static void print_stack(struct key_t *key, __u64 count) ...@@ -52,14 +52,14 @@ static void print_stack(struct key_t *key, __u64 count)
int i; int i;
printf("%s;", key->target); printf("%s;", key->target);
if (bpf_map_lookup_elem(map_fd[3], &key->tret, ip) != 0) { if (bpf_map_lookup_elem(map_fd[1], &key->tret, ip) != 0) {
printf("---;"); printf("---;");
} else { } else {
for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--) for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
print_ksym(ip[i]); print_ksym(ip[i]);
} }
printf("-;"); printf("-;");
if (bpf_map_lookup_elem(map_fd[3], &key->wret, ip) != 0) { if (bpf_map_lookup_elem(map_fd[1], &key->wret, ip) != 0) {
printf("---;"); printf("---;");
} else { } else {
for (i = 0; i < PERF_MAX_STACK_DEPTH; i++) for (i = 0; i < PERF_MAX_STACK_DEPTH; i++)
...@@ -96,23 +96,54 @@ static void int_exit(int sig) ...@@ -96,23 +96,54 @@ static void int_exit(int sig)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
struct bpf_object *obj = NULL;
struct bpf_link *links[2];
struct bpf_program *prog;
int delay = 1, i = 0;
char filename[256]; char filename[256];
int delay = 1;
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
setrlimit(RLIMIT_MEMLOCK, &r);
signal(SIGINT, int_exit); if (setrlimit(RLIMIT_MEMLOCK, &r)) {
signal(SIGTERM, int_exit); perror("setrlimit(RLIMIT_MEMLOCK)");
return 1;
}
if (load_kallsyms()) { if (load_kallsyms()) {
printf("failed to process /proc/kallsyms\n"); printf("failed to process /proc/kallsyms\n");
return 2; return 2;
} }
if (load_bpf_file(filename)) { snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
printf("%s", bpf_log_buf); obj = bpf_object__open_file(filename, NULL);
return 1; if (libbpf_get_error(obj)) {
fprintf(stderr, "ERROR: opening BPF object file failed\n");
obj = NULL;
goto cleanup;
}
/* load BPF program */
if (bpf_object__load(obj)) {
fprintf(stderr, "ERROR: loading BPF object file failed\n");
goto cleanup;
}
map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counts");
map_fd[1] = bpf_object__find_map_fd_by_name(obj, "stackmap");
if (map_fd[0] < 0 || map_fd[1] < 0) {
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
goto cleanup;
}
signal(SIGINT, int_exit);
signal(SIGTERM, int_exit);
bpf_object__for_each_program(prog, obj) {
links[i] = bpf_program__attach(prog);
if (libbpf_get_error(links[i])) {
fprintf(stderr, "ERROR: bpf_program__attach failed\n");
links[i] = NULL;
goto cleanup;
}
i++;
} }
if (argc > 1) if (argc > 1)
...@@ -120,5 +151,10 @@ int main(int argc, char **argv) ...@@ -120,5 +151,10 @@ int main(int argc, char **argv)
sleep(delay); sleep(delay);
print_stacks(map_fd[0]); print_stacks(map_fd[0]);
cleanup:
for (i--; i >= 0; i--)
bpf_link__destroy(links[i]);
bpf_object__close(obj);
return 0; return 0;
} }
...@@ -18,19 +18,19 @@ struct syscalls_exit_open_args { ...@@ -18,19 +18,19 @@ struct syscalls_exit_open_args {
long ret; long ret;
}; };
struct bpf_map_def SEC("maps") enter_open_map = { struct {
.type = BPF_MAP_TYPE_ARRAY, __uint(type, BPF_MAP_TYPE_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(u32), __type(value, u32);
.max_entries = 1, __uint(max_entries, 1);
}; } enter_open_map SEC(".maps");
struct bpf_map_def SEC("maps") exit_open_map = { struct {
.type = BPF_MAP_TYPE_ARRAY, __uint(type, BPF_MAP_TYPE_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(u32), __type(value, u32);
.max_entries = 1, __uint(max_entries, 1);
}; } exit_open_map SEC(".maps");
static __always_inline void count(void *map) static __always_inline void count(void *map)
{ {
......
...@@ -5,16 +5,12 @@ ...@@ -5,16 +5,12 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h>
#include <linux/bpf.h>
#include <string.h> #include <string.h>
#include <linux/perf_event.h> #include <linux/perf_event.h>
#include <errno.h> #include <errno.h>
#include <assert.h>
#include <stdbool.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <bpf/libbpf.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include "bpf_load.h"
/* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*. /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*.
* This requires kernel CONFIG_FTRACE_SYSCALLS to be set. * This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
...@@ -49,16 +45,44 @@ static void verify_map(int map_id) ...@@ -49,16 +45,44 @@ static void verify_map(int map_id)
static int test(char *filename, int num_progs) static int test(char *filename, int num_progs)
{ {
int i, fd, map0_fds[num_progs], map1_fds[num_progs]; int map0_fds[num_progs], map1_fds[num_progs], fd, i, j = 0;
struct bpf_link *links[num_progs * 4];
struct bpf_object *objs[num_progs];
struct bpf_program *prog;
for (i = 0; i < num_progs; i++) { for (i = 0; i < num_progs; i++) {
if (load_bpf_file(filename)) { objs[i] = bpf_object__open_file(filename, NULL);
fprintf(stderr, "%s", bpf_log_buf); if (libbpf_get_error(objs[i])) {
return 1; fprintf(stderr, "opening BPF object file failed\n");
objs[i] = NULL;
goto cleanup;
} }
printf("prog #%d: map ids %d %d\n", i, map_fd[0], map_fd[1]);
map0_fds[i] = map_fd[0]; /* load BPF program */
map1_fds[i] = map_fd[1]; if (bpf_object__load(objs[i])) {
fprintf(stderr, "loading BPF object file failed\n");
goto cleanup;
}
map0_fds[i] = bpf_object__find_map_fd_by_name(objs[i],
"enter_open_map");
map1_fds[i] = bpf_object__find_map_fd_by_name(objs[i],
"exit_open_map");
if (map0_fds[i] < 0 || map1_fds[i] < 0) {
fprintf(stderr, "finding a map in obj file failed\n");
goto cleanup;
}
bpf_object__for_each_program(prog, objs[i]) {
links[j] = bpf_program__attach(prog);
if (libbpf_get_error(links[j])) {
fprintf(stderr, "bpf_program__attach failed\n");
links[j] = NULL;
goto cleanup;
}
j++;
}
printf("prog #%d: map ids %d %d\n", i, map0_fds[i], map1_fds[i]);
} }
/* current load_bpf_file has perf_event_open default pid = -1 /* current load_bpf_file has perf_event_open default pid = -1
...@@ -80,6 +104,12 @@ static int test(char *filename, int num_progs) ...@@ -80,6 +104,12 @@ static int test(char *filename, int num_progs)
verify_map(map1_fds[i]); verify_map(map1_fds[i]);
} }
cleanup:
for (j--; j >= 0; j--)
bpf_link__destroy(links[j]);
for (i--; i >= 0; i--)
bpf_object__close(objs[i]);
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment