Re: [PATCH bpf-next v4] selftests/bpf: use auto-dependencies for test objects

Andrii Nakryiko
2024-07-19T18:18:29Z

On Thu, Jul 18, 2024 at 3:57 PM Ihor Solodrai <ihor.solodrai@pm.me> wrote:
>
> Make use of -M compiler options when building .test.o objects to
> generate .d files and avoid re-building all tests every time.
>
> Previously, if a single test bpf program under selftests/bpf/progs/*.c
> has changed, make would rebuild all the *.bpf.o, *.skel.h and *.test.o
> objects, which is a lot of unnecessary work.
>
> A typical dependency chain is:
> progs/x.c -> x.bpf.o -> x.skel.h -> x.test.o -> trunner_binary
>
> However for many tests it's not a 1:1 mapping by name, and so far
> %.test.o have been simply dependent on all %.skel.h files, and
> %.skel.h files on all %.bpf.o objects.
>
> Avoid full rebuilds by instructing the compiler (via -MMD) to
> produce *.d files with real dependencies, and appropriately including
> them. Exploit make feature that rebuilds included makefiles if they
> were changed by setting %.test.d as prerequisite for %.test.o files.
>
> A couple of examples of compilation time speedup (after the first
> clean build):
>
> $ touch progs/verifier_and.c && time make -j8
> Before: real 0m16.651s
> After: real 0m2.245s
> $ touch progs/read_vsyscall.c && time make -j8
> Before: real 0m15.743s
> After: real 0m1.575s
>
> A drawback of this change is that now there is an overhead due to make
> processing lots of .d files, which potentially may slow down unrelated
> targets. However a time to make all from scratch hasn't changed
> significantly:
>
> $ make clean && time make -j8
> Before: real 1m31.148s
> After: real 1m30.309s
>
> Suggested-by: Eduard Zingerman <eddyz87@gmail.com>
> Signed-off-by: Ihor Solodrai <ihor.solodrai@pm.me>
>
> ---
> v3 -> v4: Make $(TRUNNER_BPF_OBJS) order only prereq of trunner binary
> v2 -> v3: Restore dependency on $(TRUNNER_BPF_OBJS)
> v1 -> v2: Make %.test.d prerequisite order only
> ---
> tools/testing/selftests/bpf/.gitignore | 1 +
> tools/testing/selftests/bpf/Makefile | 44 +++++++++++++++++++-------
> 2 files changed, 34 insertions(+), 11 deletions(-)
>

It seems to behave correctly, but it reports wrong flavor when
building .bpf.o, e.g.,:

$ touch progs/test_vmlinux.c
$ make -j90
CLNG-BPF [test_maps] test_vmlinux.bpf.o
CLNG-BPF [test_maps] test_vmlinux.bpf.o
CLNG-BPF [test_maps] test_vmlinux.bpf.o
GEN-SKEL [test_progs] test_vmlinux.skel.h
GEN-SKEL [test_progs-cpuv4] test_vmlinux.skel.h
GEN-SKEL [test_progs-no_alu32] test_vmlinux.skel.h
TEST-OBJ [test_progs] vmlinux.test.o
TEST-OBJ [test_progs-no_alu32] vmlinux.test.o
EXT-COPY [test_progs-no_alu32] urandom_read bpf_testmod.ko
bpf_test_no_cfi.ko liburandom_read.so xdp_synproxy sign-file
uprobe_multi ima_setup.sh verify_sig_setup.sh
btf_dump_test_case_bitfields.c btf_dump_test_case_multidim.c
btf_dump_test_case_namespacing.c btf_dump_test_case_ordering.c
btf_dump_test_case_packing.c btf_dump_test_case_padding.c
btf_dump_test_case_syntax.c
TEST-OBJ [test_progs-cpuv4] vmlinux.test.o
EXT-COPY [test_progs-cpuv4] urandom_read bpf_testmod.ko
bpf_test_no_cfi.ko liburandom_read.so xdp_synproxy sign-file
uprobe_multi ima_setup.sh verify_sig_setup.sh
btf_dump_test_case_bitfields.c btf_dump_test_case_multidim.c
btf_dump_test_case_namespacing.c btf_dump_test_case_ordering.c
btf_dump_test_case_packing.c btf_dump_test_case_padding.c
btf_dump_test_case_syntax.c
make[1]: Nothing to be done for 'docs'.
BINARY test_progs
BINARY test_progs-no_alu32
BINARY test_progs-cpuv4
$ ls -la test_vmlinux.bpf.o no_alu32/test_vmlinux.bpf.o cpuv4/test_vmlinux.bpf.o
-rw-r--r-- 1 andriin users 21344 Jul 19 11:08 cpuv4/test_vmlinux.bpf.o
-rw-r--r-- 1 andriin users 21408 Jul 19 11:08 no_alu32/test_vmlinux.bpf.o
-rw-r--r-- 1 andriin users 21408 Jul 19 11:08 test_vmlinux.bpf.o

Note [test_maps] for all three variants (I expected
test_maps/test_progs + no_alu32 + cpuv4, just like we see for skel.h).
Can you please double check what's going on? Looking at timestamps it
seems like they are actually regenerated, though.

BTW, if you get a chance, see if you can avoid unnecessary EXT-COPY as
well (probably a bit smarter rule dependency should be set up, e.g.,
phony target that then depends on actual files or something like
that).

Regardless, this is a massive improvement and seems to work correctly,
so I've applied this and will wait for follow ups. Thanks a lot!

BTW, are you planning to look into vmlinux.h optimization as well?

> diff --git a/tools/testing/selftests/bpf/.gitignore b/tools/testing/selftests/bpf/.gitignore
> index 5025401323af..4e4aae8aa7ec 100644
> --- a/tools/testing/selftests/bpf/.gitignore
> +++ b/tools/testing/selftests/bpf/.gitignore
> @@ -31,6 +31,7 @@ test_tcp_check_syncookie_user
> test_sysctl
> xdping
> test_cpp
> +*.d
> *.subskel.h
> *.skel.h
> *.lskel.h …