5

我运行一个 CI 服务器,用于构建自定义 linux 内核。CI 服务器并不强大,每次构建的时间限制为 3 小时。为了在这个限制内工作,我想到了使用 ccache 缓存内核构建。我希望我可以在每个次要版本发布时创建一个缓存,并将其重新用于补丁版本,例如我有一个为 4.18 制作的缓存,我想将它用于所有 4.18.x 内核。

删除构建时间戳后,这对于我正在构建的确切内核版本非常有用。对于上面提到的 4.18 内核,在 CI 上构建它会提供以下统计信息:

$ ccache -s
cache directory                     
primary config                      
secondary config      (readonly)    /etc/ccache.conf
stats zero time                     Thu Aug 16 14:36:22 2018
cache hit (direct)                 17812
cache hit (preprocessed)              38
cache miss                             0
cache hit rate                    100.00 %
called for link                        3
called for preprocessing           29039
unsupported code directive             4
no input file                       2207
cleanups performed                     0
files in cache                     53652
cache size                           1.4 GB
max cache size                       5.0 GB

100% 的缓存命中率和一个小时来完成构建,出色的统计数据和预期的一样。

不幸的是,当我尝试构建 4.18.1 时,我得到

cache directory                     
primary config                      
secondary config      (readonly)    /etc/ccache.conf
stats zero time                     Thu Aug 16 10:36:22 2018
cache hit (direct)                     0
cache hit (preprocessed)             233
cache miss                         17658
cache hit rate                      1.30 %
called for link                        3
called for preprocessing           29039
unsupported code directive             4
no input file                       2207
cleanups performed                     0
files in cache                     90418
cache size                           2.4 GB
max cache size                       5.0 GB

这是 1.30% 的命中率,而构建时间反映了这种糟糕的性能。即仅从单个补丁版本更改。

我本来预计缓存性能会随着时间的推移而下降,但不会降到这个程度,所以我唯一的想法是,除了简单的时间戳之外,还有更多的不确定性。例如,大多数/所有源文件是否包含完整的内核版本字符串?我的理解是,这样的事情会完全破坏缓存。有没有办法让缓存按我的意愿工作,还是不可能?

4

1 回答 1

1

include/generated/uapi/linux/version.h标题(在顶部 Makefile https://elixir.bootlin.com/linux/v4.16.18/source/Makefile中生成)

其中包括确切的内核版本作为宏:

version_h := include/generated/uapi/linux/version.h
old_version_h := include/linux/version.h

define filechk_version.h
    (echo \#define LINUX_VERSION_CODE $(shell                         \
    expr $(VERSION) \* 65536 + 0$(PATCHLEVEL) \* 256 + 0$(SUBLEVEL)); \
    echo '#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))';)
endef

$(version_h): $(srctree)/Makefile FORCE
    $(call filechk,version.h)
    $(Q)rm -f $(old_version_h)

因此,linux 4.16.18 的 version.h 将生成为 (266258 is (4 << 16) + (16 << 8) + 18 = 0x41012)

#define LINUX_VERSION_CODE 266258
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))

稍后,例如在模块构建中应该有办法读取 LINUX_VERSION_CODE 宏值https://www.tldp.org/LDP/lkmpg/2.4/html/lkmpg.html(4.1.6。为多个内核版本编写模块)

LINUX_VERSION_CODE将宏与宏进行比较的方法KERNEL_VERSION。在内核版本a.b.c中,此宏的值为2^{16}a+2^{8}b+c. 请注意,这个宏不是为内核 2.0.35 和更早版本定义的,所以如果你想编写支持真正旧内核的模块

如何包含 version.h?示例模块包含<linux/kernel.h> <linux/module.h>and <linux/modversions.h>,其中一个文件可能间接包含 global version.h。大多数甚至所有内核源代码都将包含 version.h。

比较构建时间戳时,可能会重新生成 version.h 并禁用 ccache。忽略时间戳时,LINUX_VERSION_CODE仅对于完全相同的 linux 内核版本相同,并且在下一个补丁级别更改。

更新:检查gcc -H一些内核对象编译的输出,会有另一个带有完整内核版本宏定义的头文件。例如:(include/generated/utsrelease.hUTS_RELEASE)、include/generated/autoconf.hCONFIG_VERSION_SIGNATURE)。

甚至gcc -E在两个补丁级别之间对相同的内核对象编译进行预处理并比较生成的文本。使用最简单的 linux 模块,我-include ./include/linux/kconfig.h直接在 gcc 命令行中使用它,它包含include/generated/autoconf.h(但这在-H输出中不可见,它是 gcc 的错误还是功能?)。

https://patchwork.kernel.org/patch/9326051/

...因为顶级 Makefile 强制将其包含在:

-include $(srctree)/include/linux/kconfig.h

它实际上是:https ://elixir.bootlin.com/linux/v4.16.18/source/Makefile

# Use USERINCLUDE when you must reference the UAPI directories only.
USERINCLUDE    := \
        -I$(srctree)/arch/$(SRCARCH)/include/uapi \
        -I$(objtree)/arch/$(SRCARCH)/include/generated/uapi \
        -I$(srctree)/include/uapi \
        -I$(objtree)/include/generated/uapi \
                -include $(srctree)/include/linux/kconfig.h

# Use LINUXINCLUDE when you must reference the include/ directory.
# Needed to be compatible with the O= option
LINUXINCLUDE    := \
        -I$(srctree)/arch/$(SRCARCH)/include \
        -I$(objtree)/arch/$(SRCARCH)/include/generated \
        $(if $(KBUILD_SRC), -I$(srctree)/include) \
        -I$(objtree)/include \
        $(USERINCLUDE)

LINUXINCLUDE 导出到 env 并用于 source/scripts/Makefile.lib定义编译器标志https://elixir.bootlin.com/linux/v4.16.18/source/scripts/Makefile.lib

  c_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)    
于 2018-08-25T21:06:35.720 回答