CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

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