Commit c6497df0 authored by Daniel T. Lee's avatar Daniel T. Lee Committed by Andrii Nakryiko

samples: bpf: Refactor test_overhead program with libbpf

This commit refactors the existing program with libbpf bpf loader.
Since the kprobe, tracepoint and raw_tracepoint bpf program can be
attached with single bpf_program__attach() interface, so the
corresponding function of libbpf is used here.

Rather than specifying the number of cpus inside the code, this commit
uses the number of available cpus with _SC_NPROCESSORS_ONLN.
Signed-off-by: default avatarDaniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20201124090310.24374-6-danieltimlee@gmail.com
parent 763af200
...@@ -78,7 +78,7 @@ lathist-objs := lathist_user.o ...@@ -78,7 +78,7 @@ lathist-objs := lathist_user.o
offwaketime-objs := 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 := test_overhead_user.o
test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o
test_cgrp2_attach-objs := test_cgrp2_attach.o test_cgrp2_attach-objs := test_cgrp2_attach.o
test_cgrp2_sock-objs := test_cgrp2_sock.o test_cgrp2_sock-objs := test_cgrp2_sock.o
......
...@@ -18,10 +18,14 @@ ...@@ -18,10 +18,14 @@
#include <time.h> #include <time.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include "bpf_load.h" #include <bpf/libbpf.h>
#define MAX_CNT 1000000 #define MAX_CNT 1000000
static struct bpf_link *links[2];
static struct bpf_object *obj;
static int cnt;
static __u64 time_get_ns(void) static __u64 time_get_ns(void)
{ {
struct timespec ts; struct timespec ts;
...@@ -115,20 +119,54 @@ static void run_perf_test(int tasks, int flags) ...@@ -115,20 +119,54 @@ static void run_perf_test(int tasks, int flags)
} }
} }
static int load_progs(char *filename)
{
struct bpf_program *prog;
int err = 0;
obj = bpf_object__open_file(filename, NULL);
err = libbpf_get_error(obj);
if (err < 0) {
fprintf(stderr, "ERROR: opening BPF object file failed\n");
return err;
}
/* load BPF program */
err = bpf_object__load(obj);
if (err < 0) {
fprintf(stderr, "ERROR: loading BPF object file failed\n");
return err;
}
bpf_object__for_each_program(prog, obj) {
links[cnt] = bpf_program__attach(prog);
err = libbpf_get_error(links[cnt]);
if (err < 0) {
fprintf(stderr, "ERROR: bpf_program__attach failed\n");
links[cnt] = NULL;
return err;
}
cnt++;
}
return err;
}
static void unload_progs(void) static void unload_progs(void)
{ {
close(prog_fd[0]); while (cnt)
close(prog_fd[1]); bpf_link__destroy(links[--cnt]);
close(event_fd[0]);
close(event_fd[1]); bpf_object__close(obj);
} }
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};
char filename[256]; int num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
int num_cpu = 8;
int test_flags = ~0; int test_flags = ~0;
char filename[256];
int err = 0;
setrlimit(RLIMIT_MEMLOCK, &r); setrlimit(RLIMIT_MEMLOCK, &r);
...@@ -145,38 +183,36 @@ int main(int argc, char **argv) ...@@ -145,38 +183,36 @@ int main(int argc, char **argv)
if (test_flags & 0xC) { if (test_flags & 0xC) {
snprintf(filename, sizeof(filename), snprintf(filename, sizeof(filename),
"%s_kprobe_kern.o", argv[0]); "%s_kprobe_kern.o", argv[0]);
if (load_bpf_file(filename)) {
printf("%s", bpf_log_buf);
return 1;
}
printf("w/KPROBE\n"); printf("w/KPROBE\n");
run_perf_test(num_cpu, test_flags >> 2); err = load_progs(filename);
if (!err)
run_perf_test(num_cpu, test_flags >> 2);
unload_progs(); unload_progs();
} }
if (test_flags & 0x30) { if (test_flags & 0x30) {
snprintf(filename, sizeof(filename), snprintf(filename, sizeof(filename),
"%s_tp_kern.o", argv[0]); "%s_tp_kern.o", argv[0]);
if (load_bpf_file(filename)) {
printf("%s", bpf_log_buf);
return 1;
}
printf("w/TRACEPOINT\n"); printf("w/TRACEPOINT\n");
run_perf_test(num_cpu, test_flags >> 4); err = load_progs(filename);
if (!err)
run_perf_test(num_cpu, test_flags >> 4);
unload_progs(); unload_progs();
} }
if (test_flags & 0xC0) { if (test_flags & 0xC0) {
snprintf(filename, sizeof(filename), snprintf(filename, sizeof(filename),
"%s_raw_tp_kern.o", argv[0]); "%s_raw_tp_kern.o", argv[0]);
if (load_bpf_file(filename)) {
printf("%s", bpf_log_buf);
return 1;
}
printf("w/RAW_TRACEPOINT\n"); printf("w/RAW_TRACEPOINT\n");
run_perf_test(num_cpu, test_flags >> 6); err = load_progs(filename);
if (!err)
run_perf_test(num_cpu, test_flags >> 6);
unload_progs(); unload_progs();
} }
return 0; return err;
} }
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