Skip to content

Commit

Permalink
Simplify benchmark probe code
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrik committed Jul 18, 2023
1 parent 1eb2cbf commit 15af506
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 267 deletions.
2 changes: 1 addition & 1 deletion benchmark/probes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ARCH := $(shell uname -m | sed -e 's/x86_64/x86/' -e 's/aarch64/arm64/')
SRC = ${wildcard *.bpf.c}
OBJ = ${patsubst %.bpf.c, %.bpf.o, $(SRC)}

$(OBJ): %.bpf.o: %.bpf.c ../../include/$(ARCH)/vmlinux.h
$(OBJ): %.bpf.o: %.bpf.c benchmark.bpf.h ../../include/$(ARCH)/vmlinux.h
$(CC) -mcpu=v3 -g -O2 -Wall -Werror -D__TARGET_ARCH_$(ARCH) -I../../include/$(ARCH) -c -target bpf $< -o $@

.PHONY: clean
Expand Down
102 changes: 102 additions & 0 deletions benchmark/probes/benchmark.bpf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#if defined(__TARGET_ARCH_x86)
#define FENTRY_SEC() SEC("fentry/__x64_sys_getpid")
#elif defined(__TARGET_ARCH_arm64)
#define FENTRY_SEC() SEC("fentry/__arm64_sys_getpid")
#else
#error Unknown target for this architecture
#endif

#if defined(__TARGET_ARCH_x86)
#define KPROBE_SEC() SEC("kprobe/__x64_sys_getpid")
#elif defined(__TARGET_ARCH_arm64)
#define KPROBE_SEC() SEC("kprobe/__arm64_sys_getpid")
#else
#error Unknown target for this architecture
#endif

#define TRACEPOINT_SEC() SEC("tp_btf/sys_enter")

#define BENCHMARK_PROBE(sec, impl) \
sec() int probe() \
{ \
return impl(); \
}

static u64 zero = 0;

#ifdef BENCHMARK_NO_MAP
static inline int empty_probe()
{
return 0;
}
#endif

#ifdef BENCHMARK_SIMPLE_MAP
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, u32);
__type(value, u64);
} counts SEC(".maps");

static inline int simple_probe()
{
u32 key = bpf_get_current_pid_tgid();
u64 *count;

count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
return 0;
}
}
__sync_fetch_and_add(count, 1);

return 0;
}
#endif

#ifdef BENCHMARK_COMPLEX_MAP
struct key_t {
u64 pid;
u64 random;
char command[32];
};

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, struct key_t);
__type(value, u64);
} counts SEC(".maps");

static inline int complex_probe()
{
u64 *count;
struct key_t key = {};

key.pid = bpf_get_current_pid_tgid();
key.random = bpf_ktime_get_ns() % 1024;
bpf_get_current_comm(&key.command, sizeof(key.command));

count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
return 0;
}
}
__sync_fetch_and_add(count, 1);

return 0;
}
#endif

char LICENSE[] SEC("license") = "GPL";
50 changes: 3 additions & 47 deletions benchmark/probes/fentry-complex.bpf.c
Original file line number Diff line number Diff line change
@@ -1,48 +1,4 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#define BENCHMARK_COMPLEX_MAP
#include "benchmark.bpf.h"

static u64 zero = 0;

struct key_t {
u64 pid;
u64 random;
char command[32];
};

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, struct key_t);
__type(value, u64);
} counts SEC(".maps");

#if defined(__TARGET_ARCH_x86)
SEC("fentry/__x64_sys_getpid")
#elif defined(__TARGET_ARCH_arm64)
SEC("fentry/__arm64_sys_getpid")
#else
#error Unknown target for this architecture
#endif
int probe(struct pt_regs *ctx)
{
u64 *count;
struct key_t key = {};

key.pid = bpf_get_current_pid_tgid();
key.random = bpf_ktime_get_ns() % 1024;
bpf_get_current_comm(&key.command, sizeof(key.command));

count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
return 0;
}
}
__sync_fetch_and_add(count, 1);

return 0;
}

char LICENSE[] SEC("license") = "GPL";
BENCHMARK_PROBE(FENTRY_SEC, complex_probe);
18 changes: 3 additions & 15 deletions benchmark/probes/fentry-empty.bpf.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#define BENCHMARK_NO_MAP
#include "benchmark.bpf.h"

#if defined(__TARGET_ARCH_x86)
SEC("fentry/__x64_sys_getpid")
#elif defined(__TARGET_ARCH_arm64)
SEC("fentry/__arm64_sys_getpid")
#else
#error Unknown target for this architecture
#endif
int probe(struct pt_regs *ctx)
{
return 0;
}

char LICENSE[] SEC("license") = "GPL";
BENCHMARK_PROBE(FENTRY_SEC, empty_probe);
40 changes: 3 additions & 37 deletions benchmark/probes/fentry-simple.bpf.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,4 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#define BENCHMARK_SIMPLE_MAP
#include "benchmark.bpf.h"

static u64 zero = 0;

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, u32);
__type(value, u64);
} counts SEC(".maps");

#if defined(__TARGET_ARCH_x86)
SEC("fentry/__x64_sys_getpid")
#elif defined(__TARGET_ARCH_arm64)
SEC("fentry/__arm64_sys_getpid")
#else
#error Unknown target for this architecture
#endif
int probe(struct pt_regs *ctx)
{
u32 key = bpf_get_current_pid_tgid();
u64 *count;

count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
return 0;
}
}
__sync_fetch_and_add(count, 1);

return 0;
}

char LICENSE[] SEC("license") = "GPL";
BENCHMARK_PROBE(FENTRY_SEC, simple_probe);
50 changes: 3 additions & 47 deletions benchmark/probes/kprobe-complex.bpf.c
Original file line number Diff line number Diff line change
@@ -1,48 +1,4 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#define BENCHMARK_COMPLEX_MAP
#include "benchmark.bpf.h"

static u64 zero = 0;

struct key_t {
u64 pid;
u64 random;
char command[32];
};

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, struct key_t);
__type(value, u64);
} counts SEC(".maps");

#if defined(__TARGET_ARCH_x86)
SEC("kprobe/__x64_sys_getpid")
#elif defined(__TARGET_ARCH_arm64)
SEC("kprobe/__arm64_sys_getpid")
#else
#error Unknown target for this architecture
#endif
int probe(struct pt_regs *ctx)
{
u64 *count;
struct key_t key = {};

key.pid = bpf_get_current_pid_tgid();
key.random = bpf_ktime_get_ns() % 1024;
bpf_get_current_comm(&key.command, sizeof(key.command));

count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
return 0;
}
}
__sync_fetch_and_add(count, 1);

return 0;
}

char LICENSE[] SEC("license") = "GPL";
BENCHMARK_PROBE(KPROBE_SEC, complex_probe);
40 changes: 3 additions & 37 deletions benchmark/probes/kprobe-simple.bpf.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,4 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#define BENCHMARK_SIMPLE_MAP
#include "benchmark.bpf.h"

static u64 zero = 0;

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, u32);
__type(value, u64);
} counts SEC(".maps");

#if defined(__TARGET_ARCH_x86)
SEC("kprobe/__x64_sys_getpid")
#elif defined(__TARGET_ARCH_arm64)
SEC("kprobe/__arm64_sys_getpid")
#else
#error Unknown target for this architecture
#endif
int probe(struct pt_regs *ctx)
{
u32 key = bpf_get_current_pid_tgid();
u64 *count;

count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
return 0;
}
}
__sync_fetch_and_add(count, 1);

return 0;
}

char LICENSE[] SEC("license") = "GPL";
BENCHMARK_PROBE(KPROBE_SEC, simple_probe);
45 changes: 3 additions & 42 deletions benchmark/probes/tracepoint-complex.bpf.c
Original file line number Diff line number Diff line change
@@ -1,43 +1,4 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#define BENCHMARK_COMPLEX_MAP
#include "benchmark.bpf.h"

static u64 zero = 0;

struct key_t {
u64 pid;
u64 random;
char command[32];
};

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, struct key_t);
__type(value, u64);
} counts SEC(".maps");

SEC("tp_btf/sys_enter")
int BPF_PROG(probe)
{
u64 *count;
struct key_t key = {};

key.pid = bpf_get_current_pid_tgid();
key.random = bpf_ktime_get_ns() % 1024;
bpf_get_current_comm(&key.command, sizeof(key.command));

count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
count = bpf_map_lookup_elem(&counts, &key);
if (!count) {
return 0;
}
}
__sync_fetch_and_add(count, 1);

return 0;
}

char LICENSE[] SEC("license") = "GPL";
BENCHMARK_PROBE(TRACEPOINT_SEC, complex_probe);
12 changes: 3 additions & 9 deletions benchmark/probes/tracepoint-empty.bpf.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
#include "vmlinux.h"
#include <bpf/bpf_tracing.h>
#define BENCHMARK_NO_MAP
#include "benchmark.bpf.h"

SEC("tp_btf/sys_enter")
int BPF_PROG(probe)
{
return 0;
}

char LICENSE[] SEC("license") = "GPL";
BENCHMARK_PROBE(TRACEPOINT_SEC, empty_probe);
Loading

0 comments on commit 15af506

Please sign in to comment.