From c5f9ba3aa163a61eb0176b4fd5cbfa16c017faf6 Mon Sep 17 00:00:00 2001 From: Lorenz Bauer Date: Thu, 23 Nov 2023 14:49:27 +0000 Subject: [PATCH] program: add benchmark 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 --- prog_test.go | 16 ++++++++++++++++ testdata/kfunc-eb.elf | Bin 2400 -> 2960 bytes testdata/kfunc-el.elf | Bin 2400 -> 2960 bytes testdata/kfunc.c | 9 +++++++++ 4 files changed, 25 insertions(+) diff --git a/prog_test.go b/prog_test.go index a07d63ada..dbf2e04b3 100644 --- a/prog_test.go +++ b/prog_test.go @@ -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() diff --git a/testdata/kfunc-eb.elf b/testdata/kfunc-eb.elf index 7733de1a4106ed427c8dd7b950cd556efad65577..adb3f43a44f9fc7d2e5b6165f1c9643f48076ea9 100644 GIT binary patch literal 2960 zcmb_ePly|36o0eb?QYbrUEAs^2tyIcmL*Boi_kx$OSf)eh0re8R&X|%Ol(MY5;K#s zixi8XM-f!;q`ipXNiSw^+oK1;Yw;vr_0U@n9_sJ+&G#m=(}rI9;G6e;@BcURy)XG> zZR66^lp`rQ@(^-Hky;?k9W|(GpY!7EpE9z{x+0H2qe_DJR}(&}CHymFr2T}*6gB%v zCQl(A<>&K_|HIh7>hKQFnP{$xEh+zVq0$(x<99_f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644 --- a/testdata/kfunc.c +++ b/testdata/kfunc.c @@ -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); +}