Skip to contentSkip to conversation

Speedup LTP compile time by ~20% on multi-machine

PATCH In review Li Wang (Hieco) 2 participants 5 messages Nov 24 Kbuild

lore
Share & tools
lore
#0 lore raw

v4 cover Speedup LTP compile time by ~20% on multi-machine

The following results show how this optimization moves compile times really far from what we have now. With a single threaded build, LTP would take approximately 12 minutes to compile. With a multi-threaded build on 16 CPUs, this drops down to ~46 seconds.

time make -j16

Executed in   45.96 secs    fish           external
   usr time  519.11 secs    0.24 millis  519.11 secs
   sys time  167.13 secs    1.08 millis  167.13 secs

---
Changes in v4:
- Mark library targets .PHONY so library source changes are rebuilt
- Also build libkerntest.a when make is run from testcases/
- Remove the old $(LIBLTP) rule from testcases.mk (overriding recipe warning)
- Refactoring the patchset in clean code
- Link to v3: https://lore.kernel.org/all/[email protected]/

Changes in v3:
- added libs-all ordering to avoid races
- Link to v2: https://lore.kernel.org/[email protected]

Changes in v2:
- fix build race condition
- Link to v1: https://lore.kernel.org/[email protected]

Andrea Cervesato (2):
build: Fix library prerequisites in testcases.mk
build: Build shared libraries once when make runs in testcases/

Li Wang (Hieco) (1):
build: Recurse into trunk subdirectories in parallel

 Makefile                            |  2 ++
 include/mk/generic_leaf_target.inc  |  2 ++
 include/mk/generic_lib_deps.inc     | 34 ++++++++++++++++++++++++
 include/mk/generic_trunk_target.inc | 40 ++++++++++++++++++++++++-----
 include/mk/testcases.mk             | 15 +++++------
 testcases/kernel/Makefile           |  6 +++++
 testcases/kernel/include/lib.mk     |  6 +++--
 testcases/network/rpc/Makefile      |  5 ++++
 8 files changed, 93 insertions(+), 17 deletions(-)
 create mode 100644 include/mk/generic_lib_deps.inc
Signature · 5 lines
2.56.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v4 1/3 build: Fix library prerequisites in testcases.mk

From: Andrea Cervesato <[email protected]>

Fix LTPLIBS_FILES to match the actual libltp<name>.a archive names and only mark them .PHONY when MAKELEVEL == 0 so recursive leaf sub-makes do not redundantly rebuild them in parallel.

Also make directory prerequisites order-only so directory mtime updates do not trigger unnecessary archive rebuilds.

---
 include/mk/testcases.mk         | 14 ++++++++------
 testcases/kernel/include/lib.mk |  4 ++--
 2 files changed, 10 insertions(+), 8 deletions(-)
include/mk/testcases.mk +8 -6
index 2609535ce..5960d7485 100644
@@ -12,11 +12,11 @@ LIBLTP_DIR	:= $(abs_top_builddir)/lib
 
 LIBLTP		:= $(LIBLTP_DIR)/libltp.a
 
-$(APICMDS_DIR)/tst_kvercmp: $(APICMDS_DIR)
-	$(MAKE) -C "$^" -f "$(abs_top_srcdir)/tools/apicmds/Makefile" all
+$(APICMDS_DIR)/tst_kvercmp: | $(APICMDS_DIR)
+	$(MAKE) -C "$(APICMDS_DIR)" -f "$(abs_top_srcdir)/tools/apicmds/Makefile" all
 
-$(LIBLTP): $(LIBLTP_DIR)
-	$(MAKE) -C "$^" -f "$(abs_top_srcdir)/lib/Makefile" all
+$(LIBLTP): | $(LIBLTP_DIR)
+	$(MAKE) -C "$(LIBLTP_DIR)" -f "$(abs_top_srcdir)/lib/Makefile" all
 
 MAKE_DEPS	:= $(LIBLTP)
 
@@ -27,13 +27,15 @@ LDLIBS		+= -lltp
 ifdef LTPLIBS
 
 LTPLIBS_DIRS = $(addprefix $(abs_top_builddir)/libs/, $(LTPLIBS))
-LTPLIBS_FILES = $(addsuffix .a, $(addprefix $(abs_top_builddir)/libs/, $(foreach LIB,$(LTPLIBS),$(LIB)/lib$(LIB))))
+LTPLIBS_FILES = $(addsuffix .a, $(addprefix $(abs_top_builddir)/libs/, $(foreach LIB,$(LTPLIBS),$(LIB)/libltp$(LIB))))
 
 MAKE_DEPS += $(LTPLIBS_FILES)
 
+ifeq ($(MAKELEVEL),0)
 .PHONY: $(LTPLIBS_FILES)
+endif
 
-$(LTPLIBS_FILES): $(LTPLIBS_DIRS)
+$(LTPLIBS_FILES): | $(LTPLIBS_DIRS)
 
 $(LTPLIBS_FILES): %:
 ifdef VERBOSE
testcases/kernel/include/lib.mk +2 -2
index 40a09b77e..c1a0c6d52 100644
@@ -29,8 +29,8 @@ LDFLAGS			+= -L$(LIBKERNTEST_DIR)
 $(LIBKERNTEST_DIR):
 	mkdir -p "$@"
 
-$(LIBKERNTEST): $(LIBKERNTEST_DIR)
-	$(MAKE) -C $^ -f "$(LIBKERNTEST_SRCDIR)/Makefile" all
+$(LIBKERNTEST): | $(LIBKERNTEST_DIR)
+	$(MAKE) -C "$(LIBKERNTEST_DIR)" -f "$(LIBKERNTEST_SRCDIR)/Makefile" all
 
 MAKE_DEPS		+= $(LIBKERNTEST)
 
Signature · 5 lines
2.56.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

PATCH v4 2/3 build: Build shared libraries once when make runs in testcases/

In reply to Li Wang (Hieco)

From: Andrea Cervesato <[email protected]>

Running make directly under testcases/ (for example
"make -C testcases/kernel/syscalls -j8") can start many sub-makes in parallel. Each leaf can then reach the $(LIBLTP) rule in testcases.mk and call "$(MAKE) -C lib" at the same time. This builds the same library more than once, and parallel builds can race on libltp.a. libkerntest.a and the libs/ libraries have the same problem.

Add include/mk/generic_lib_deps.inc and include it from both the generic leaf and trunk targets. When make is started inside testcases/ (MAKELEVEL == 0), the file adds libltp.a, the libs/ libraries and, for testcases/ and testcases/kernel/*, libkerntest.a to MAKE_DEPS. Leaf targets and trunk subdirectories depend on MAKE_DEPS as order-only prerequisites. As a result, the libraries are built once, in the top-level make process, before any subdirectory is entered.

The library targets are .PHONY, so lib/, libs/ and testcases/kernel/lib/ are always entered and their own Makefiles decide what to rebuild. A change to the library sources is therefore picked up. Because the prerequisites are order-only, tests that are already linked are not relinked.

Remove the old $(LIBLTP) rule and "MAKE_DEPS := $(LIBLTP)" from include/mk/testcases.mk, which the new file replaces. Builds started from the top-level directory are not affected.

---
 include/mk/generic_leaf_target.inc  |  2 ++
 include/mk/generic_lib_deps.inc     | 34 +++++++++++++++++++++++++++++
 include/mk/generic_trunk_target.inc |  8 +++++++
 include/mk/testcases.mk             |  5 +----
 testcases/kernel/include/lib.mk     |  2 ++
 5 files changed, 47 insertions(+), 4 deletions(-)
 create mode 100644 include/mk/generic_lib_deps.inc
include/mk/generic_leaf_target.inc +2 -0
index 7c685fea7..533381b9f 100644
@@ -76,6 +76,8 @@
 # INSTALL_DIR			:= $(libdir)
 #
 
+include $(top_srcdir)/include/mk/generic_lib_deps.inc
+
 .PHONY: all clean install check
 
 ifneq ($(strip $(MAKE_TARGETS)),)
include/mk/generic_lib_deps.inc new file +34 -0
new file mode 100644
index 000000000..666b3f9ef
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) Linux Test Project, 2026
+
+ifeq ($(MAKELEVEL),0)
+
+ifneq ($(filter $(abs_top_builddir)/testcases%,$(CURDIR)),)
+
+MAKE_DEPS += $(abs_top_builddir)/lib/libltp.a
+MAKE_DEPS += $(abs_top_builddir)/libs-all
+
+.PHONY: $(abs_top_builddir)/lib/libltp.a
+$(abs_top_builddir)/lib/libltp.a:
+	mkdir -m 00755 -p "$(@D)"
+	$(MAKE) -C "$(@D)" -f "$(abs_top_srcdir)/lib/Makefile" all
+
+.PHONY: $(abs_top_builddir)/libs-all
+$(abs_top_builddir)/libs-all: $(LTPLIBS_FILES) | $(abs_top_builddir)/lib/libltp.a
+	mkdir -m 00755 -p "$(abs_top_builddir)/libs"
+	$(MAKE) -C "$(abs_top_builddir)/libs" -f "$(abs_top_srcdir)/libs/Makefile" all
+
+endif
+
+ifneq ($(filter $(abs_top_builddir)/testcases $(abs_top_builddir)/testcases/kernel%,$(filter-out $(abs_top_builddir)/testcases/kernel/lib,$(CURDIR))),)
+
+MAKE_DEPS += $(abs_top_builddir)/testcases/kernel/lib/libkerntest.a
+
+.PHONY: $(abs_top_builddir)/testcases/kernel/lib/libkerntest.a
+$(abs_top_builddir)/testcases/kernel/lib/libkerntest.a: | $(abs_top_builddir)/lib/libltp.a
+	mkdir -m 00755 -p "$(@D)"
+	$(MAKE) -C "$(@D)" -f "$(abs_top_srcdir)/testcases/kernel/lib/Makefile" all
+
+endif
+
+endif
include/mk/generic_trunk_target.inc +8 -0
index f9db7896a..4186ee9d9 100644
@@ -36,6 +36,8 @@ RECURSIVE_TARGETS		?= all install check
 
 $(eval $(get_make_dirs))
 
+include $(top_srcdir)/include/mk/generic_lib_deps.inc
+
 .PHONY: $(RECURSIVE_TARGETS) $(addprefix trunk-,$(RECURSIVE_TARGETS))
 
 $(SUBDIRS): %:
@@ -78,6 +80,12 @@ endif
 
 install: trunk-install
 
+ifneq ($(strip $(MAKE_DEPS)),)
+ifneq ($(strip $(filter all install,$(RECURSIVE_TARGETS))),)
+$(filter all install,$(RECURSIVE_TARGETS)): | $(MAKE_DEPS)
+endif
+endif
+
 # Print out CURDIR to check for a recursion issue.
 ifeq ($(strip $(SUBDIRS)),)
 	$(warning CURDIR is: $(CURDIR))
include/mk/testcases.mk +1 -4
index 5960d7485..f9ff93928 100644
@@ -15,10 +15,7 @@ LIBLTP		:= $(LIBLTP_DIR)/libltp.a
 $(APICMDS_DIR)/tst_kvercmp: | $(APICMDS_DIR)
 	$(MAKE) -C "$(APICMDS_DIR)" -f "$(abs_top_srcdir)/tools/apicmds/Makefile" all
 
-$(LIBLTP): | $(LIBLTP_DIR)
-	$(MAKE) -C "$(LIBLTP_DIR)" -f "$(abs_top_srcdir)/lib/Makefile" all
-
-MAKE_DEPS	:= $(LIBLTP)
+MAKE_DEPS	:=
 
 INSTALL_DIR	:= testcases/bin
 
testcases/kernel/include/lib.mk +2 -0
index c1a0c6d52..8f1ec5313 100644
@@ -29,8 +29,10 @@ LDFLAGS			+= -L$(LIBKERNTEST_DIR)
 $(LIBKERNTEST_DIR):
 	mkdir -p "$@"
 
+ifneq ($(MAKELEVEL),0)
 $(LIBKERNTEST): | $(LIBKERNTEST_DIR)
 	$(MAKE) -C "$(LIBKERNTEST_DIR)" -f "$(LIBKERNTEST_SRCDIR)/Makefile" all
+endif
 
 MAKE_DEPS		+= $(LIBKERNTEST)
 
Signature · 5 lines
2.56.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v4 3/3 build: Recurse into trunk subdirectories in parallel

In reply to Li Wang (Hieco)

Currently, generic_trunk_target traverses $(SUBDIRS) using a serial shell 'for' loop. This serializes directory traversal and prevents 'make -jN' from building sibling directories in parallel.

Replace the shell loop with target-based recursion:

1. Map each subdirectory to phony targets <dir>-all, <dir>-install and <dir>-check, implemented as static pattern rules using $* as the directory name.
2. Make 'all', 'install' and 'check' depend on these targets so GNU Make can dispatch sibling directories concurrently across available job slots.
3. Keep $(MAKE) literal in the recipes so that sub-makes keep access to the jobserver and 'make -n' still recurses.
4. Keep $(MAKE_DEPS) as order-only prerequisite. Together with the previous patch, libltp.a and libs/ are built exactly once at the top-level make invocation before any subdirectory is entered, so parallel leaves never race on the library build.
5. Add explicit ordering where a subdirectory produces a library used by its siblings (tools before testcases, kernel/lib before the other kernel subdirectories, rpc-tirpc before basic_tests), guarded against the directories being filtered out.

This cuts full build time by over 20% on multi-core systems.

---
 Makefile                            |  2 ++
 include/mk/generic_trunk_target.inc | 32 ++++++++++++++++++++++-------
 testcases/kernel/Makefile           |  6 ++++++
 testcases/network/rpc/Makefile      |  5 +++++
 4 files changed, 38 insertions(+), 7 deletions(-)
Makefile +2 -0
index 2efbe8c6c..0abb5c6b4 100644
@@ -63,6 +63,8 @@ CHECK_TARGETS		:= $(addsuffix -check,testcases lib)
 all: $(addsuffix -all,$(COMMON_TARGETS)) Version
 
 $(MAKE_TARGETS): lib-all libs-all
+# tools contains apicmds/tst_kvercmp required by some testcases
+testcases-all: tools-all
 
 .PHONY: include-all include-install
 include-install: $(top_builddir)/include/config.h include/mk/config.mk include-all
include/mk/generic_trunk_target.inc +25 -7
index 4186ee9d9..4c86fe1ec 100644
@@ -91,16 +91,34 @@ ifeq ($(strip $(SUBDIRS)),)
 	$(warning CURDIR is: $(CURDIR))
 	$(error SUBDIRS empty -- did you want generic_leaf_target instead?)
 else
-$(RECURSIVE_TARGETS): %: | $(SUBDIRS)
 ifdef VERBOSE
-	@set -e; for dir in $(SUBDIRS); do \
-	    $(MAKE) -C $$dir -f "$(abs_srcdir)/$$dir/Makefile" $@; \
-	done
+TRUNK_Q		:=
+SUBMAKE_OPTS	:=
 else
-	@set -e; for dir in $(SUBDIRS); do \
-	    $(MAKE) --no-print-directory -C $$dir -f "$(abs_srcdir)/$$dir/Makefile" $@; \
-	done
+TRUNK_Q		:= @
+SUBMAKE_OPTS	:= --no-print-directory
 endif
+
+# Recurse into subdirectories via per-directory phony targets
+# (<dir>-all, <dir>-install, <dir>-check) so that GNU Make can build
+# sibling directories in parallel under -jN. $(MAKE) must appear
+# literally in the recipes so that sub-makes join the jobserver.
+all:     $(addsuffix -all,$(SUBDIRS))
+install: $(addsuffix -install,$(SUBDIRS))
+check:   $(addsuffix -check,$(SUBDIRS))
+
+.PHONY: $(addsuffix -all,$(SUBDIRS)) \
+	$(addsuffix -install,$(SUBDIRS)) \
+	$(addsuffix -check,$(SUBDIRS))
+
+$(addsuffix -all,$(SUBDIRS)): %-all: trunk-all | $(SUBDIRS) $(MAKE_DEPS)
+	$(TRUNK_Q)$(MAKE) $(SUBMAKE_OPTS) -C "$*" -f "$(abs_srcdir)/$*/Makefile" all
+
+$(addsuffix -install,$(SUBDIRS)): %-install: trunk-install | $(SUBDIRS) $(MAKE_DEPS)
+	$(TRUNK_Q)$(MAKE) $(SUBMAKE_OPTS) -C "$*" -f "$(abs_srcdir)/$*/Makefile" install
+
+$(addsuffix -check,$(SUBDIRS)): %-check: trunk-check | $(SUBDIRS) $(MAKE_DEPS)
+	$(TRUNK_Q)$(MAKE) $(SUBMAKE_OPTS) -C "$*" -f "$(abs_srcdir)/$*/Makefile" check
 endif
 
 check: trunk-check
testcases/kernel/Makefile +6 -0
index 29e26742d..90615fbb4 100644
@@ -54,3 +54,9 @@ FILTER_OUT_DIRS		+= containers controllers device-drivers fs io ipc mem \
 endif
 
 include $(top_srcdir)/include/mk/generic_trunk_target.mk
+
+# libkerntest.a in lib/ must be built before its consumers
+ifneq ($(filter lib,$(SUBDIRS)),)
+KERNTEST_USERS	:= $(filter-out lib,$(SUBDIRS))
+$(addsuffix -all,$(KERNTEST_USERS)) $(addsuffix -install,$(KERNTEST_USERS)): lib-all
+endif
testcases/network/rpc/Makefile +5 -0
index 6ecb1a665..d5ed5814c 100644
@@ -9,3 +9,8 @@ include $(top_srcdir)/include/mk/env_pre.mk
 INSTALL_TARGETS		:= *.sh
 
 include $(top_srcdir)/include/mk/generic_trunk_target.mk
+
+# librpc-tirpc.a in rpc-tirpc/ must be built before basic_tests
+ifneq ($(filter rpc-tirpc,$(SUBDIRS)),)
+basic_tests-all basic_tests-install: rpc-tirpc-all
+endif
Signature · 5 lines
2.56.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp