kbuild: Cache exploratory calls to the compiler
v3 cover kbuild: Cache exploratory calls to the compiler
This two-patch series attempts to speed incremental builds of the kernel up by a bit. How much of a speedup you get depends a lot on your environment, specifically the speed of your workstation and how fast it takes to invoke the compiler.
In the Chrome OS build environment you get a really big win. For an incremental build (via emerge) I measured a speedup from ~1 minute to ~35 seconds. ...but Chrome OS calls the compiler through a number of wrapper scripts and also calls the kernel make at least twice for an emerge (during compile stage and install stage), so it's a bit of a worst case.
Perhaps a more realistic measure of the speedup others might see is running "time make help > /dev/null" outside of the Chrome OS build environment on my system. When I do this I see that it took more than 1.0 seconds before and less than 0.2 seconds after. So presumably this has the ability to shave ~0.8 seconds off an incremental build for most folks out there. While 0.8 seconds savings isn't huge, it does make incremental builds feel a lot snappier.
Ingo Molnar also did some testing of this in his environment and found that an incremental build of his subsystem sped up from ~.44 seconds before to ~.15 seconds after. Clean builds also sped up by a marginal amount. :)
Changes in v3:
- Rule to prevent make from trying to generate the cache
- Rule to clean .cache.mk
- No more doc changes
- Moved cache stuff below cc-cross-prefix
- Removed duplicate documentation of try-run (oops)
- Add Tested-by for Ingo and Guenter since v2 and v3 are very similar
Changes in v2:
- Abstract at a different level (like shell-cached) per Masahiro Yamada
- Include ld-version, which I missed the first time
Douglas Anderson (2):
kbuild: Add a cache for generated variables
kbuild: Cache a few more calls to the compiler
Makefile | 5 +-- scripts/Kbuild.include | 87 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 76 insertions(+), 16 deletions(-)
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
PATCH v3 1/2 kbuild: Add a cache for generated variables
While timing a "no-op" build of the kernel (incrementally building the kernel even though nothing changed) in the Chrome OS build system I found that it was much slower than I expected.
Digging into things a bit, I found that quite a bit of the time was spent invoking the C compiler even though we weren't actually building anything. Currently in the Chrome OS build system the C compiler is called through a number of wrappers (one of which is written in python!) and can take upwards of 100 ms to invoke even if we're not doing anything difficult, so these invocations of the compiler were taking a lot of time. Worse the invocations couldn't seem to take advantage of the multiple cores on my system.
Certainly it seems like we could make the compiler invocations in the Chrome OS build system faster, but only to a point. Inherently invoking a program as big as a C compiler is a fairly heavy operation. Thus even if we can speed the compiler calls it made sense to track down what was happening.
It turned out that all the compiler invocations were coming from usages like this in the kernel's Makefile:
KBUILD_CFLAGS += $(call cc-option,-fno-delete-null-pointer-checks,)
Due to the way cc-option and similar statements work the above contains an implicit call to the C compiler. ...and due to the fact that we're storing the result in KBUILD_CFLAGS, a simply expanded variable, the call will happen every time the Makefile is parsed, even if there are no users of KBUILD_CFLAGS.
Rather than redoing this computation every time, it makes a lot of sense to cache the result of all of the Makefile's compiler calls just like we do when we compile a ".c" file to a ".o" file. Conceptually this is quite a simple idea. ...and since the calls to invoke the compiler and similar tools are centrally located in the Kbuild.include file this doesn't even need to be super invasive.
Implementing the cache in a simple-to-use and efficient way is not quite as simple as it first sounds, though. To get maximum speed we really want the cache in a format that make can natively understand and make doesn't really have an ability to load/parse files. ...but make _can_ import other Makefiles, so the solution is to store the cache in Makefile format. This requires coming up with a valid/unique Makefile variable name for each value to be cached, but that's solvable with some cleverness.
After this change, we'll automatically create a ".cache.mk" file that will contain our cached variables. We'll load this on each invocation of make and will avoid recomputing anything that's already in our cache. The cache is stored in a format that it shouldn't need any invalidation since anything that might change should affect the "key" and any old cached value won't be used.
NOTE: This change requires
<https://patchwork.kernel.org/patch/9998651/> ("kbuild: add forward declaration of default target to Makefile.asm-generic") for correctness.
---
Changes in v3:
- Rule to prevent make from trying to generate the cache
- Rule to clean .cache.mk
- No more doc changes
- Moved cache stuff below cc-cross-prefix
- Removed duplicate documentation of try-run (oops)
- Add Tested-by for Ingo and Guenter since v2 and v3 are very similar
Changes in v2:
- Abstract at a different level (like shell-cached) per Masahiro Yamada
- Include ld-version, which I missed the first time
Makefile | 1 + scripts/Kbuild.include | 87 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 74 insertions(+), 14 deletions(-)
Makefile +1 -0
@@ -1550,6 +1550,7 @@ clean: $(clean-dirs) -o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \ -o -name '*.symtypes' -o -name 'modules.order' \ -o -name modules.builtin -o -name '.tmp_*.o.*' \ + -o -name .cache.mk \ -o -name '*.c.[012]*.*' \ -o -name '*.ll' \ -o -name '*.gcno' \) -type f -print | xargs rm -f
scripts/Kbuild.include +73 -14
160 lines not on this page · raw message
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
PATCH v3 2/2 kbuild: Cache a few more calls to the compiler
In reply to Douglas AndersonThese are a few stragglers that I left out of the original patch to cache calls to the C compiler ("kbuild: Add a cache for generated variables") because they bleed out into the main Makefile and thus uglify things a little bit. The idea is the same here, though.
---
Changes in v3:
- Add Tested-by for Ingo and Guenter since v2 and v3 are very similar
Changes in v2:
- Abstract at a different level (like shell-cached) per Masahiro Yamada
Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Makefile +2 -2
@@ -652,7 +652,7 @@ KBUILD_CFLAGS += $(call cc-ifversion, -lt, 0409, \ KBUILD_CFLAGS += $(call cc-option,--param=allow-store-data-races=0) # check for 'asm goto' -ifeq ($(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y) +ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y) KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO endif
@@ -788,7 +788,7 @@ KBUILD_CFLAGS += $(call cc-option,-fdata-sections,) endif # arch Makefile may override CC so keep this after arch Makefile is included -NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) +NOSTDINC_FLAGS += -nostdinc -isystem $(call shell-cached,$(CC) -print-file-name=include) CHECKFLAGS += $(NOSTDINC_FLAGS) # warn about C99 declaration after statement
Signature · 1 line
2.15.0.rc0.271.g36b669edcc-goog
2017-10-12 1:19 GMT+09:00 Douglas Anderson <[email protected]>:
Quoted 37 lines
This two-patch series attempts to speed incremental builds of the kernel up by a bit. How much of a speedup you get depends a lot on your environment, specifically the speed of your workstation and how fast it takes to invoke the compiler.
In the Chrome OS build environment you get a really big win. For an incremental build (via emerge) I measured a speedup from ~1 minute to ~35 seconds. ...but Chrome OS calls the compiler through a number of wrapper scripts and also calls the kernel make at least twice for an emerge (during compile stage and install stage), so it's a bit of a worst case.
Perhaps a more realistic measure of the speedup others might see is running "time make help >
/dev/null" outside of the Chrome OS build environment on my system. When I do this I see that it took more than 1.0 seconds before and less than 0.2 seconds after. So presumably this has the ability to shave ~0.8 seconds off an incremental build for most folks out there. While 0.8 seconds savings isn't huge, it does make incremental builds feel a lot snappier.Ingo Molnar also did some testing of this in his environment and found that an incremental build of his subsystem sped up from ~.44 seconds before to ~.15 seconds after. Clean builds also sped up by a marginal amount. :)
Changes in v3:
- Rule to prevent make from trying to generate the cache
- Rule to clean .cache.mk
- No more doc changes
- Moved cache stuff below cc-cross-prefix
- Removed duplicate documentation of try-run (oops)
- Add Tested-by for Ingo and Guenter since v2 and v3 are very similarChanges in v2:
- Abstract at a different level (like shell-cached) per Masahiro Yamada
- Include ld-version, which I missed the first time
Quoted 7 lines
Douglas Anderson (2):
kbuild: Add a cache for generated variables
kbuild: Cache a few more calls to the compilerMakefile | 5 +-- scripts/Kbuild.include | 87 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 76 insertions(+), 16 deletions(-)
Series, applied to linux-kbuild/kbuild.
Signature · 2 lines
Best Regards Masahiro Yamada