Skip to content

Commit ba1e28e

Browse files
authored
Add codeSize metric (#8)
1 parent 87c6063 commit ba1e28e

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/rtlmeter/metrics.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def stepDescription(step: Step) -> str:
5151
"clocks",
5252
"traceSize",
5353
"ccacheHit",
54+
"codeSize",
5455
]
5556

5657

@@ -87,6 +88,8 @@ class MetricDef:
8788
"Size of trace dumps"),
8889
"ccacheHit" : MetricDef("ccache hit rate", "%", None, None, True,
8990
"ccache hit rate during C++ compilation"),
91+
"codeSize" : MetricDef("Code size", "MB", operator.add, 0, False,
92+
"Total size of all generated hot code"),
9093
}
9194
#fmt : on
9295

src/rtlmeter/verilator.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import itertools
1818
import json
1919
import os
20+
import re
21+
import subprocess
2022
from typing import Final, List, Tuple
2123

2224
from rtlmeter import metrics
@@ -96,7 +98,16 @@ def _cppbuild(cgraph: CGraph, descr: CompileDescriptor, compileDir: str) -> CNod
9698
step = "cppbuild"
9799

98100
def job() -> bool:
99-
cmd = ["make", "-j", str(len(CTX.usableCpus)), "-C", "obj_dir", "-f", f"{_PREFIX}.mk"]
101+
cmd = [
102+
"make",
103+
"-j",
104+
str(len(CTX.usableCpus)),
105+
"-C",
106+
"obj_dir",
107+
"-f",
108+
f"{_PREFIX}.mk",
109+
"VM_PARALLEL_BUILDS=1",
110+
]
100111
env = {"CCACHE_DEBUG": "1", "CCACHE_DEBUGLEVEL": "1"}
101112
# Run it
102113
if runcmd(cmd, step, env):
@@ -115,13 +126,37 @@ def job() -> bool:
115126
if "Succeeded getting cached result" in fd.read():
116127
ccacheHits += 1
117128
cData["ccacheHit"] = 100.0 * ccacheHits / max(objectFiles, 1)
129+
# Gather code size
130+
with subprocess.Popen(
131+
args=["objdump", "-w", "-h", os.path.join(compileDir, "obj_dir", "Vsim__ALL.a")],
132+
stdout=subprocess.PIPE,
133+
stderr=subprocess.STDOUT,
134+
text=True,
135+
encoding="utf-8",
136+
) as process:
137+
assert process.stdout, "stdout is None"
138+
filenameRe = re.compile(r"^Vsim.*?(Slow)?.o:")
139+
codesectionRe = re.compile(r"^\s*\d+\s+(\S+)\s+(\S+)\s+.*\bCODE\b.*")
140+
slowFile: bool = False
141+
hotCodeSize: int = 0
142+
for line in process.stdout:
143+
if filenameMatch := filenameRe.match(line):
144+
slowFile = filenameMatch.group(1) is not None
145+
continue
146+
if slowFile:
147+
continue
148+
if codesectionMatch := codesectionRe.match(line):
149+
hotCodeSize += int(codesectionMatch.group(2), base=16)
150+
cData["codeSize"] = hotCodeSize * 1e-6
151+
# Result data
118152
data = {descr.case: {step: cData}}
119153
# Add combined 'verilate' + 'cppbuild'
120154
with open("_verilate/time.json", "r", encoding="utf-8") as fd:
121155
tData = json.load(fd)
122156
for k, v in cData.items():
123-
if (accumulate := metrics.metricDef(k).accumulate) is not None:
124-
tData[k] = accumulate(tData[k], v)
157+
mDef = metrics.metricDef(k)
158+
if (accumulate := mDef.accumulate) is not None:
159+
tData[k] = accumulate(tData.get(k, mDef.identityValue), v)
125160
data[descr.case]["compile"] = tData
126161
with open(f"_{step}/metrics.json", "w", encoding="utf-8") as fd:
127162
json.dump(data, fd)

0 commit comments

Comments
 (0)