6

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

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

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

4

2 回答 2

7

有两种选择:

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

我写了一个 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))

请注意,这会考虑所有 cca​​che-log 文件,而不仅仅是上次构建的文件。如果您想要后者,只需先删除日志文件。

于 2019-11-28T17:31:37.657 回答