Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests/bpf: Use auto-dependencies for test objects

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>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/VJihUTnvtwEgv_mOnpfy7EgD9D2MPNoHO-MlANeLIzLJPGhDeyOuGKIYyKgk0O6KPjfM-MuhtvPwZcngN8WFqbTnTRyCSMc2aMZ1ODm1T_g=@pm.me

authored by

Ihor Solodrai and committed by
Andrii Nakryiko
844f7315 f157f9cb

+33 -11
+1
tools/testing/selftests/bpf/.gitignore
··· 31 31 test_sysctl 32 32 xdping 33 33 test_cpp 34 + *.d 34 35 *.subskel.h 35 36 *.skel.h 36 37 *.lskel.h
+32 -11
tools/testing/selftests/bpf/Makefile
··· 477 477 xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o 478 478 xdp_features.skel.h-deps := xdp_features.bpf.o 479 479 480 - LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps))) 480 + LINKED_BPF_OBJS := $(foreach skel,$(LINKED_SKELS),$($(skel)-deps)) 481 + LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(LINKED_BPF_OBJS)) 481 482 482 483 # Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on 483 484 # $eval()) and pass control to DEFINE_TEST_RUNNER_RULES. ··· 557 556 $(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@ 558 557 $(Q)rm -f $$(<:.o=.llinked1.o) $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o) 559 558 560 - $(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT) 559 + $(LINKED_BPF_OBJS): %: $(TRUNNER_OUTPUT)/% 560 + 561 + # .SECONDEXPANSION here allows to correctly expand %-deps variables as prerequisites 562 + .SECONDEXPANSION: 563 + $(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_OUTPUT)/%: $$$$(%-deps) $(BPFTOOL) | $(TRUNNER_OUTPUT) 561 564 $$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.bpf.o)) 562 565 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps)) 563 566 $(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o) ··· 571 566 $(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@ 572 567 $(Q)$$(BPFTOOL) gen subskeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$(@:.skel.h=.subskel.h) 573 568 $(Q)rm -f $$(@:.skel.h=.linked1.o) $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o) 569 + 570 + # When the compiler generates a %.d file, only skel basenames (not 571 + # full paths) are specified as prerequisites for corresponding %.o 572 + # file. This target makes %.skel.h basename dependent on full paths, 573 + # linking generated %.d dependency with actual %.skel.h files. 574 + $(notdir %.skel.h): $(TRUNNER_OUTPUT)/%.skel.h 575 + @true 576 + 574 577 endif 575 578 576 579 # ensure we set up tests.h header generation rule just once ··· 596 583 # Note: we cd into output directory to ensure embedded BPF object is found 597 584 $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ 598 585 $(TRUNNER_TESTS_DIR)/%.c \ 599 - $(TRUNNER_EXTRA_HDRS) \ 600 - $(TRUNNER_BPF_OBJS) \ 601 - $(TRUNNER_BPF_SKELS) \ 602 - $(TRUNNER_BPF_LSKELS) \ 603 - $(TRUNNER_BPF_SKELS_LINKED) \ 604 - $$(BPFOBJ) | $(TRUNNER_OUTPUT) 586 + $(TRUNNER_OUTPUT)/%.test.d 605 587 $$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@) 606 - $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F) 588 + $(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -MMD -MT $$@ -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F) 589 + 590 + $(TRUNNER_TEST_OBJS:.o=.d): $(TRUNNER_OUTPUT)/%.test.d: \ 591 + $(TRUNNER_TESTS_DIR)/%.c \ 592 + $(TRUNNER_EXTRA_HDRS) \ 593 + $(TRUNNER_BPF_SKELS) \ 594 + $(TRUNNER_BPF_LSKELS) \ 595 + $(TRUNNER_BPF_SKELS_LINKED) \ 596 + $$(BPFOBJ) | $(TRUNNER_OUTPUT) 597 + 598 + include $(wildcard $(TRUNNER_TEST_OBJS:.o=.d)) 607 599 608 600 $(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \ 609 601 %.c \ ··· 625 607 $$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES)) 626 608 $(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/ 627 609 endif 610 + 611 + # some X.test.o files have runtime dependencies on Y.bpf.o files 612 + $(OUTPUT)/$(TRUNNER_BINARY): | $(TRUNNER_BPF_OBJS) 628 613 629 614 $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \ 630 615 $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \ ··· 789 768 790 769 EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ 791 770 prog_tests/tests.h map_tests/tests.h verifier/tests.h \ 792 - feature bpftool \ 793 - $(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h \ 771 + feature bpftool \ 772 + $(addprefix $(OUTPUT)/,*.o *.d *.skel.h *.lskel.h *.subskel.h \ 794 773 no_alu32 cpuv4 bpf_gcc bpf_testmod.ko \ 795 774 bpf_test_no_cfi.ko \ 796 775 liburandom_read.so)