127 lines
5.1 KiB
Python
127 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
||
"""Turn `sweep.sh`'s `/usr/bin/time -l` logs into the document's §7 table.
|
||
|
||
python3 tools/perf/sweeptable.py <out-dir> [--labels 0.49.0,0.43.0,0.39.0]
|
||
|
||
Reads every `<label>__<tree>__<command>__<cache>.txt` in `<out-dir>`, pulls the
|
||
`real` seconds and the `maximum resident set size` out of each try's block, and
|
||
prints a Markdown table with one row per (tree, command, cache) and one column
|
||
per label — first try, then the range over the repeats, then peak RSS.
|
||
|
||
It also prints the summary line each run reported (`N files would be
|
||
reformatted`, the finding count), because a timing that moved because the work
|
||
moved is not a timing finding.
|
||
|
||
Since take 5 §P0 it prints the **cache hit count per try** — the `N files from
|
||
cache` the formatter reported, or 0 when it reported none. A "cold" row whose
|
||
tries report hits is not cold, which is exactly the bug §P0 fixed in
|
||
`sweep.sh`; printing the number per try makes the next one visible instead of
|
||
inferred. The untimed priming run a warm cell now starts with is excluded:
|
||
only the blocks after a `--- try N ---` marker are read.
|
||
"""
|
||
|
||
import argparse
|
||
import os
|
||
import re
|
||
import sys
|
||
|
||
REAL = re.compile(r"([0-9.]+)\s+real")
|
||
RSS = re.compile(r"(\d+)\s+maximum resident set size")
|
||
HITS = re.compile(r"(\d+) files? from cache")
|
||
TRY = re.compile(r"^--- try \d+ ---$", re.MULTILINE)
|
||
COMMANDS = [("format", "cold"), ("format", "warm"), ("check", "default"), ("check", "select")]
|
||
COMMAND_NAMES = {
|
||
("format", "cold"): "`format --check`, cold cache",
|
||
("format", "warm"): "`format --check`, warm cache",
|
||
("check", "default"): "`check` (default rules)",
|
||
("check", "select"): "`check --select error,warning,style`",
|
||
}
|
||
TREES = ["hw/ip", "hw/top_earlgrey/ip", "hw/top_darjeeling/ip", "hw/dv", "hw/vendor"]
|
||
|
||
|
||
def cell(directory, label, tree, command, cache):
|
||
"""(reals, peak_rss_mb, summary_lines, hits) for a cell, or None."""
|
||
name = "%s__%s__%s__%s.txt" % (label, tree.replace("/", "_"), command, cache)
|
||
path = os.path.join(directory, name)
|
||
if not os.path.exists(path):
|
||
return None
|
||
text = open(path, encoding="utf-8", errors="replace").read()
|
||
# Everything before the first `--- try 1 ---` is a warm cell's untimed
|
||
# priming run: it is not a trial and is not counted anywhere.
|
||
tries = TRY.split(text)[1:]
|
||
timed = "".join(tries) if tries else text
|
||
reals = [float(v) for v in REAL.findall(timed)]
|
||
rss = [int(v) for v in RSS.findall(timed)]
|
||
# The run's own total line, not its per-file lines: a timing that moved
|
||
# because the *work* moved is not a timing finding.
|
||
summary = [
|
||
line.strip()
|
||
for line in timed.splitlines()
|
||
if re.match(r"^(\d+ files? would be reformatted|found \d+ violations|"
|
||
r"no violations|All \d+)", line.strip())
|
||
]
|
||
hits = [int(HITS.search(one)[1]) if HITS.search(one) else 0 for one in tries]
|
||
return reals, (max(rss) / 1048576.0 if rss else 0.0), summary, hits
|
||
|
||
|
||
def render(reals):
|
||
if not reals:
|
||
return "—"
|
||
if len(reals) == 1:
|
||
return "%.2f" % reals[0]
|
||
rest = reals[1:]
|
||
if abs(max(rest) - min(rest)) < 0.005:
|
||
return "%.2f / %.2f" % (reals[0], min(rest))
|
||
return "%.2f / %.2f–%.2f" % (reals[0], min(rest), max(rest))
|
||
|
||
|
||
def main():
|
||
parser = argparse.ArgumentParser()
|
||
parser.add_argument("directory")
|
||
parser.add_argument("--labels", default="0.49.0,0.43.0,0.39.0")
|
||
args = parser.parse_args()
|
||
labels = args.labels.split(",")
|
||
|
||
print("| Tree | command | " + " | ".join(labels) + " | peak RSS (MB) |")
|
||
print("|---|---|" + "---:|" * (len(labels) + 1))
|
||
for tree in TREES:
|
||
for command, cache in COMMANDS:
|
||
cells = [cell(args.directory, label, tree, command, cache) for label in labels]
|
||
if not any(cells):
|
||
continue
|
||
times = " | ".join(render(c[0]) if c else "—" for c in cells)
|
||
rss = " → ".join("%.0f" % c[1] for c in cells if c)
|
||
print("| `%s` | %s | %s | %s |" % (tree, COMMAND_NAMES[(command, cache)], times, rss))
|
||
|
||
print()
|
||
print("Summary lines, per cell (what the run reported):")
|
||
for tree in TREES:
|
||
for command, cache in COMMANDS:
|
||
for label in labels:
|
||
found = cell(args.directory, label, tree, command, cache)
|
||
if not found or not found[2]:
|
||
continue
|
||
print(" %-22s %-8s %-8s %-8s %s" % (
|
||
tree, command, cache, label, found[2][0]))
|
||
|
||
print()
|
||
print("Format-cache hits per try (a cold row with a hit is not cold):")
|
||
for tree in TREES:
|
||
for command, cache in COMMANDS:
|
||
if command != "format":
|
||
continue
|
||
for label in labels:
|
||
found = cell(args.directory, label, tree, command, cache)
|
||
if not found:
|
||
continue
|
||
flag = " <-- COLD ROW WITH HITS" if (
|
||
cache == "cold" and any(found[3])) else ""
|
||
print(" %-22s %-8s %-8s %s%s" % (
|
||
tree, cache, label,
|
||
" / ".join(str(v) for v in found[3]) or "—", flag))
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|