启用 sccache

跳转到:导航搜索

在您的项目中启用 Sccache

简介

Sccache 类似于 ccache,它允许缓存构建工件,以便在重新构建时,如果源代码没有更改,则可以从缓存中访问它们。在某些情况下,这可以将重新构建时间缩短超过 85%,从而为您节省时间,允许更多作业在 OBS/IBS 中进行,并节省构建农场的能源。

Sccache 支持缓存 C、C++ 和 Rust,使其成为 ccache 的“即插即用”替代品。但是,您必须小心确保在使用时环境变量和路径相同,否则 sccache 无法正常工作。

要在 sccache 中启用此支持,您需要编辑您的项目配置(而不是软件包配置)。

CLI (osc)

您可以使用以下命令执行此操作

osc meta prjconf <name of project> -e

在其中,您需要指定缓存类型以及应用此配置的软件包名称。

BuildFlags: ccachetype:sccache
BuildFlags: useccache:<package name>
# if the package uses multibuild you need to specify the flavors
BuildFlags: useccache:<package name>:<multibuild flavor>

您可以多次指定 useccache。例如,要将其应用于 kanidm 和 nss_synth 项目,配置如下所示

BuildFlags: ccachetype:sccache
BuildFlags: useccache:kanidm
BuildFlags: useccache:nss_synth

Webui

导航到您的项目页面并选择“项目配置”。然后,您可以指定与 CLI 相同的构建标志。


检查是否有效

您可以通过检查您的构建日志来查看它是否有效。如果有效,您应该在构建开始时看到

[   27s] Unpacking sccache archive
[   28s] Compile requests                      0
[   28s] Compile requests executed             0
[   28s] Cache hits                            0
[   28s] Cache misses                          0
[   28s] Cache timeouts                        0
[   28s] Cache read errors                     0
[   28s] Forced recaches                       0
[   28s] Cache write errors                    0
[   28s] Compilation failures                  0
[   28s] Cache errors                          0
[   28s] Non-cacheable compilations            0
[   28s] Non-cacheable calls                   0
[   28s] Non-compilation calls                 0
[   28s] Unsupported compiler calls            0
[   28s] Average cache write               0.000 s
[   28s] Average cache read miss           0.000 s
[   28s] Average cache read hit            0.000 s
[   28s] Failed distributed compilations       0
[   28s] Cache location                  Local disk: "/.sccache"
[   28s] Cache size                          177 MiB
[   28s] Max cache size                        1 GiB

在构建结束时,您将看到有关缓存效果的统计信息。

[  621s] Compile requests                    963
[  621s] Compile requests executed           680
[  621s] Cache hits                           13
[  621s] Cache hits (C/C++)                   13
[  621s] Cache misses                        660
[  621s] Cache misses (C/C++)                516
[  621s] Cache misses (Rust)                 144
[  621s] Cache timeouts                        0
[  621s] Cache read errors                     0
[  621s] Forced recaches                       0
[  621s] Cache write errors                    0
[  621s] Compilation failures                  5
[  621s] Cache errors                          2
[  621s] Cache errors (C/C++)                  2
[  621s] Non-cacheable compilations            0
[  621s] Non-cacheable calls                 114
[  621s] Non-compilation calls               169
[  621s] Unsupported compiler calls            0
[  621s] Average cache write               0.001 s
[  621s] Average cache read miss           1.157 s
[  621s] Average cache read hit            0.000 s
[  621s] Failed distributed compilations       0
[  621s] 
[  621s] Non-cacheable reasons:
[  621s] crate-type                           51
[  621s] -E                                   32
[  621s] -                                    21
[  621s] argument parse                       10
[  621s] 
[  621s] Cache location                  Local disk: "/.sccache"
[  621s] Cache size                          149 MiB
[  621s] Max cache size                        1 GiB

这是来自“初始”构建的,因此我们可以看到缓存缺失“很高”。如果我们重新构建,缓存将生效,我们可以在统计信息中看到这一点。如果您在重新构建时经常看到缓存缺失,这可能表明您的环境变量正在发生变化。您可以使用 percent 构建阶段中的“env”命令来转储这些变量进行调查。

一个热构建应该如下所示

[  249s] Compile requests                    963
[  249s] Compile requests executed           680
[  249s] Cache hits                          673
[  249s] Cache hits (C/C++)                  529
[  249s] Cache hits (Rust)                   144
[  249s] Cache misses                          0
[  249s] Cache timeouts                        0
[  249s] Cache read errors                     0
[  249s] Forced recaches                       0
[  249s] Cache write errors                    0
[  249s] Compilation failures                  5
[  249s] Cache errors                          2
[  249s] Cache errors (C/C++)                  2
[  249s] Non-cacheable compilations            0
[  249s] Non-cacheable calls                 114
[  249s] Non-compilation calls               169
[  249s] Unsupported compiler calls            0
[  249s] Average cache write               0.000 s
[  249s] Average cache read miss           0.000 s
[  249s] Average cache read hit            0.002 s
[  249s] Failed distributed compilations       0
[  249s] 
[  249s] Non-cacheable reasons:
[  249s] crate-type                           51
[  249s] -E                                   32
[  249s] -                                    21
[  249s] argument parse                       10
[  249s] 
[  249s] Cache location                  Local disk: "/.sccache"
[  249s] Cache size                          149 MiB
[  249s] Max cache size                        1 GiB

我们还可以看到执行时间的差异(621s 冷 vs 249s 热)