Skip to content

Commit a3cc56c

Browse files
theihoranakryiko
authored andcommitted
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 <[email protected]> Signed-off-by: Ihor Solodrai <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/VJihUTnvtwEgv_mOnpfy7EgD9D2MPNoHO-MlANeLIzLJPGhDeyOuGKIYyKgk0O6KPjfM-MuhtvPwZcngN8WFqbTnTRyCSMc2aMZ1ODm1T_g=@pm.me
1 parent 51f1bb9 commit a3cc56c

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

tools/testing/selftests/bpf/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test_tcp_check_syncookie_user
3131
test_sysctl
3232
xdping
3333
test_cpp
34+
*.d
3435
*.subskel.h
3536
*.skel.h
3637
*.lskel.h

tools/testing/selftests/bpf/Makefile

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,8 @@ xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o
477477
xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o
478478
xdp_features.skel.h-deps := xdp_features.bpf.o
479479

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))
481482

482483
# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
483484
# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
@@ -556,7 +557,11 @@ $(TRUNNER_BPF_LSKELS): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
556557
$(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@
557558
$(Q)rm -f $$(<:.o=.llinked1.o) $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o)
558559

559-
$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT)
560+
$(LINKED_BPF_OBJS): %: $(TRUNNER_OUTPUT)/%
561+
562+
# .SECONDEXPANSION here allows to correctly expand %-deps variables as prerequisites
563+
.SECONDEXPANSION:
564+
$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_OUTPUT)/%: $$$$(%-deps) $(BPFTOOL) | $(TRUNNER_OUTPUT)
560565
$$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.bpf.o))
561566
$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps))
562567
$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o)
@@ -566,6 +571,14 @@ $(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT)
566571
$(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@
567572
$(Q)$$(BPFTOOL) gen subskeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$(@:.skel.h=.subskel.h)
568573
$(Q)rm -f $$(@:.skel.h=.linked1.o) $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o)
574+
575+
# When the compiler generates a %.d file, only skel basenames (not
576+
# full paths) are specified as prerequisites for corresponding %.o
577+
# file. This target makes %.skel.h basename dependent on full paths,
578+
# linking generated %.d dependency with actual %.skel.h files.
579+
$(notdir %.skel.h): $(TRUNNER_OUTPUT)/%.skel.h
580+
@true
581+
569582
endif
570583

571584
# ensure we set up tests.h header generation rule just once
@@ -583,14 +596,19 @@ endif
583596
# Note: we cd into output directory to ensure embedded BPF object is found
584597
$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \
585598
$(TRUNNER_TESTS_DIR)/%.c \
586-
$(TRUNNER_EXTRA_HDRS) \
587-
$(TRUNNER_BPF_OBJS) \
588-
$(TRUNNER_BPF_SKELS) \
589-
$(TRUNNER_BPF_LSKELS) \
590-
$(TRUNNER_BPF_SKELS_LINKED) \
591-
$$(BPFOBJ) | $(TRUNNER_OUTPUT)
599+
$(TRUNNER_OUTPUT)/%.test.d
592600
$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
593-
$(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
601+
$(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -MMD -MT $$@ -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
602+
603+
$(TRUNNER_TEST_OBJS:.o=.d): $(TRUNNER_OUTPUT)/%.test.d: \
604+
$(TRUNNER_TESTS_DIR)/%.c \
605+
$(TRUNNER_EXTRA_HDRS) \
606+
$(TRUNNER_BPF_SKELS) \
607+
$(TRUNNER_BPF_LSKELS) \
608+
$(TRUNNER_BPF_SKELS_LINKED) \
609+
$$(BPFOBJ) | $(TRUNNER_OUTPUT)
610+
611+
include $(wildcard $(TRUNNER_TEST_OBJS:.o=.d))
594612

595613
$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
596614
%.c \
@@ -608,6 +626,9 @@ ifneq ($2:$(OUTPUT),:$(shell pwd))
608626
$(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
609627
endif
610628

629+
# some X.test.o files have runtime dependencies on Y.bpf.o files
630+
$(OUTPUT)/$(TRUNNER_BINARY): | $(TRUNNER_BPF_OBJS)
631+
611632
$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
612633
$(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \
613634
$(RESOLVE_BTFIDS) \
@@ -768,8 +789,8 @@ $(OUTPUT)/uprobe_multi: uprobe_multi.c
768789

769790
EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \
770791
prog_tests/tests.h map_tests/tests.h verifier/tests.h \
771-
feature bpftool \
772-
$(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h \
792+
feature bpftool \
793+
$(addprefix $(OUTPUT)/,*.o *.d *.skel.h *.lskel.h *.subskel.h \
773794
no_alu32 cpuv4 bpf_gcc bpf_testmod.ko \
774795
bpf_test_no_cfi.ko \
775796
liburandom_read.so)

0 commit comments

Comments
 (0)