Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/scripts/build_tests/test_ccache.py
9498 views
1
#!/usr/bin/env python3
2
# test ccache efficiency building two similar boards
3
# AP_FLAKE8_CLEAN
4
5
import subprocess
6
import re
7
import argparse
8
import sys
9
import os
10
11
12
def ccache_stats():
13
'''return hits/misses from ccache -s'''
14
hits = 0
15
miss = 0
16
stats = str(subprocess.Popen(["ccache", "-s"], stdout=subprocess.PIPE).communicate()[0], encoding='ascii')
17
for line in stats.split('\n'):
18
m = re.match(r"cache.hit\D*(\d+)$", line)
19
if m is not None:
20
hits += int(m.group(1))
21
22
m = re.match(r"cache.miss\D*(\d+)", line)
23
if m is not None:
24
miss += int(m.group(1))
25
26
m = re.match(r"\s*Hits:\s*(\d+)", line)
27
if m is not None:
28
hits += int(m.group(1))
29
30
m = re.match(r"\s*Misses:\s*(\d+)", line)
31
if m is not None:
32
miss += int(m.group(1))
33
34
if line.startswith("Primary"):
35
break
36
return (hits, miss)
37
38
39
def build_board(boardname):
40
subprocess.run(["./waf", "configure", "--board", boardname, '--disable-networking'])
41
subprocess.run(["./waf", "clean", "copter"])
42
43
44
def main() -> None:
45
parser = argparse.ArgumentParser(description='test ccache performance')
46
parser.add_argument('--boards', default='MatekF405-bdshot,MatekF405-TE-bdshot', help='boards to test')
47
parser.add_argument('--min-cache-pct', type=int, default=75, help='minimum acceptable ccache hit rate')
48
parser.add_argument('--display', action='store_true', help='parse and show ccache stats')
49
50
args = parser.parse_args()
51
52
if args.display:
53
(hits, misses) = ccache_stats()
54
print("Hits=%u misses=%u" % (hits, misses))
55
sys.exit(0)
56
57
boards = args.boards.split(",")
58
if len(boards) != 2:
59
print(boards)
60
print("Must specify exactly 2 boards (comma separated)")
61
sys.exit(1)
62
63
os.environ['CCACHE_DIR'] = os.path.join(os.getcwd(), 'build', 'ccache')
64
subprocess.run(["ccache", "--version"])
65
subprocess.run(["ccache", "-C", "-z"])
66
build_board(boards[0])
67
subprocess.run(["ccache", "-z"])
68
build_board(boards[1])
69
result = subprocess.run(["ccache", "-s"], capture_output=True, text=True)
70
print(result.stdout)
71
72
# Get the GitHub Actions summary file path
73
summary_file = os.getenv('GITHUB_STEP_SUMMARY')
74
75
post = ccache_stats()
76
hit_pct = 100 * post[0] / float(post[0]+post[1])
77
print("ccache hit percentage: %.1f%% %s" % (hit_pct, post))
78
if summary_file:
79
# Append the output to the summary file
80
with open(summary_file, 'a') as f:
81
f.write(f"### ccache -s Output with {boards}\n")
82
f.write(f"```\n{result.stdout}\n```\n")
83
f.write(f"### ccache hit percentage (min {args.min_cache_pct})\n")
84
f.write("ccache hit percentage: %.1f%% %s\n" % (hit_pct, post))
85
if hit_pct < args.min_cache_pct:
86
print("ccache hits too low, need %d%%" % args.min_cache_pct)
87
sys.exit(1)
88
else:
89
print("ccache hits good")
90
91
92
if __name__ == "__main__":
93
main()
94
95