检查 ccache 调用是否为缓存命中



作为构建过程的一部分,我想获取有关构建时间以及 ccache 是否在缓存中找到该项目的统计信息。我知道ccache -s可以比较以前和当前的缓存命中计数。

但是,如果我有数百个编译线程并行运行,统计信息不会告诉我哪个文件导致了命中。

ccache 的返回代码是编译器的返回代码。有什么方法可以让 ccache 告诉我它是否成功?

有两个选项:

  1. 启用 ccache 日志文件:将配置(或环境变量 CCACHE_LOGFILE(中的log_file设置为文件路径。然后,您可以从日志数据中找出每次编译的结果。如果有许多并行 ccache 调用(日志文件在它们之间共享,因此来自不同进程的日志记录将交错(,这可能会有点乏味,但通过考虑每个日志行的 PID 部分来实现。
  2. 在 ccache 3.5 及更高版本中,最好启用调试模式:在配置(或环境变量 CCACHE_DEBUG=1(中设置debug = true。 然后,ccache 会将每个生成的对象文件的日志存储在 <objectfile>.ccache-log 中。有关详细信息,请参阅 ccache 手册中的缓存调试

我写了一个快速脏的脚本,告诉我哪些文件必须重建以及缓存未命中率是多少:

示例输出(截断(:

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 文件,而不仅仅是上次构建的文件。如果需要后者,只需先删除日志文件即可。

最新更新