Skip to content

Commit

Permalink
program: add benchmark
Browse files Browse the repository at this point in the history
Add a benchmark which excercises two complicated code paths:
finding a function in the kernel to attach to, and resolving a
reference to a kfunc.

Both currently force a new parse of vmlinux BTF which is why
this is excruciatingly slow.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb committed Nov 24, 2023
1 parent 4f9c666 commit c0ce1af
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,22 @@ func TestProgramInstructions(t *testing.T) {
}
}

func BenchmarkNewProgram(b *testing.B) {
testutils.SkipOnOldKernel(b, "5.18", "kfunc support")
spec, err := LoadCollectionSpec(fmt.Sprintf("testdata/kfunc-%s.elf", internal.ClangEndian))
qt.Assert(b, err, qt.IsNil)

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := NewProgram(spec.Programs["benchmark"])
if !errors.Is(err, unix.EACCES) {
b.Fatal("Unexpected error:", err)
}
}
}

func createProgramArray(t *testing.T) *Map {
t.Helper()

Expand Down
Binary file modified testdata/kfunc-eb.elf
Binary file not shown.
Binary file modified testdata/kfunc-el.elf
Binary file not shown.
9 changes: 9 additions & 0 deletions testdata/kfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ __section("tc") int call_kfunc(void *ctx) {
}
return 1;
}

extern int bpf_fentry_test1(int) __ksym;

__section("fentry/bpf_fentry_test2") int benchmark() {
// bpf_fentry_test1 is a valid kfunc but not allowed to be called from
// TC context. We use this to avoid loading a gajillion programs into
// the kernel when benchmarking the loader.
return bpf_fentry_test1(0);
}

0 comments on commit c0ce1af

Please sign in to comment.