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 7733de1a4..adb3f43a4 100644 Binary files a/testdata/kfunc-eb.elf and b/testdata/kfunc-eb.elf differ diff --git a/testdata/kfunc-el.elf b/testdata/kfunc-el.elf index 08f20f618..5b4913ede 100644 Binary files a/testdata/kfunc-el.elf and b/testdata/kfunc-el.elf differ diff --git a/testdata/kfunc.c b/testdata/kfunc.c index 36171b845..5a6d3a605 100644 --- 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); +}