作为构建过程的一部分,我想获取有关构建时间以及 ccache 是否在缓存中找到该项目的统计信息。我知道在ccache -s
哪里可以比较以前和当前的缓存命中计数。
但是,如果我有数百个编译线程并行运行,则统计数据不会告诉我是哪个文件导致了命中。
的返回码ccache
是编译器的返回码。有什么办法可以让 ccache 告诉我它是否成功?
作为构建过程的一部分,我想获取有关构建时间以及 ccache 是否在缓存中找到该项目的统计信息。我知道在ccache -s
哪里可以比较以前和当前的缓存命中计数。
但是,如果我有数百个编译线程并行运行,则统计数据不会告诉我是哪个文件导致了命中。
的返回码ccache
是编译器的返回码。有什么办法可以让 ccache 告诉我它是否成功?
有两种选择:
log_file
在配置(或环境变量CCACHE_LOGFILE
)中设置文件路径。然后你可以从日志数据中找出每次编译的结果。如果有许多并行的 ccache 调用(日志文件在所有这些调用之间共享,因此来自不同进程的日志记录将被交错)可能会有点乏味,但可以通过考虑每个日志行的 PID 部分来实现。debug = true
在配置(或环境变量CCACHE_DEBUG=1
)中设置。然后,ccache 会将每个生成的对象文件的日志存储在<objectfile>.ccache-log
. 在 ccache 手册中的缓存调试中阅读更多内容。我写了一个 quick-n-dirty 脚本,告诉我哪些文件必须被重建以及缓存未命中率是多少:
样本输出(截断):
ccache hit: lib/expression/unary_minus_expression.cpp
ccache miss: lib/expression/in_expression.cpp
ccache miss: lib/expression/arithmetic_expression.cpp
=== 249 files, 248 cache misses (0.995984 %)===
脚本:
#!/usr/bin/env python3
from pathlib import Path
import re
import os
files = {}
for filename in Path('src').rglob('*.ccache-log'):
with open(filename, 'r') as file:
for line in file:
source_file_match = re.findall(r'Source file: (.*)', line)
if source_file_match:
source_file = source_file_match[0]
result_match = re.findall(r'Result: cache (.*)', line)
if result_match:
result = result_match[0]
files[source_file] = result
break
if len(files) == 0:
print("No *.ccache-log files found. Did you compile with ccache and the environment variable CCACHE_DEBUG=1?")
sys.exit(1)
common_path_prefix = os.path.commonprefix(list(files.keys()))
files_shortened = {}
misses = 0
for file in files:
shortened = file.replace(common_path_prefix, '')
if files[file] == 'miss':
misses += 1
print("ccache miss: %s" % (shortened))
print("\n=== %i files, %i cache misses (%f %%)===\n" % (len(files), misses, float(misses) / len(files) * 100))
请注意,这会考虑所有 ccache-log 文件,而不仅仅是上次构建的文件。如果您想要后者,只需先删除日志文件。