簡體   English   中英

檢查 ccache 調用是否是緩存命中

[英]Check if a ccache call was a cache hit

作為構建過程的一部分,我想獲取有關構建時間以及 ccache 是否在緩存中找到該項目的統計信息。 我知道ccache -s可以比較以前和當前的緩存命中計數。

但是,如果我有數百個並行運行的編譯線程,則統計信息不會告訴我是哪個文件導致了命中。

ccache的返回碼是編譯器的返回碼。 有什么辦法可以讓 ccache 告訴我它是否成功?

有兩種選擇:

  1. 啟用 ccache 日志文件:將配置中的log_file (或環境變量CCACHE_LOGFILE )設置為文件路徑。 然后你可以從日志數據中找出每次編譯的結果。 如果有許多並行 ccache 調用(日志文件在所有調用之間共享,因此來自不同進程的日志記錄將被交錯)可能會有點乏味,但通過考慮每個日志行的 PID 部分是可能的。
  2. 在 ccache 3.5 及更新版本中,最好啟用調試模式:在配置中設置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))

請注意,這會考慮所有 cca​​che-log 文件,而不僅僅是最后一次構建的文件。 如果您想要后者,只需先刪除日志文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM