Files
mandy/tools/dstorage_census.py
Jan Trencansky 100fed1b3d #984's Python tools: encoding=, the stdout guard, and --help that writes no file
Three conventions main's tests enforce and #984's new tools missed (all three tests fail on #984's head):

- tests/test_text_encoding.py: tools/build_dstorage_history.py read its stamp and its base history, and tools/dstorage_census.py its trace, in the locale's code page. They name UTF-8 now.
- tests/test_tool_stdout_encoding.py: the __main__ blocks of build_dstorage_history.py, dstorage_census.py, gen_bcpack_runtime_tables.py and scripts/translate_pipeline_compute.py did not set errors='replace' on stdout.
- tests/test_tool_startup.py runs every tool with --help. gen_bcpack_runtime_tables.py took its first argument as the output path whatever it was, so the test passed and left a 61 KiB file named '--help' in the working directory. The tool has an argument parser now (the tables file, or --profile JSON OUT, as before); the generated tables are byte-identical.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-24 20:09:32 -05:00

98 lines
4.1 KiB
Python

"""Summarize a local LEONIDA_DSTORAGE_TRACE=1 run in one pass (no filenames in the report)."""
import argparse
from collections import Counter, defaultdict
import json
import re
import sys
def census(lines, from_file=None):
groups = defaultdict(Counter)
errors = Counter()
priorities = Counter()
stages = Counter()
paired = 0
active = not from_file
first_tick = last_tick = None
peak_reservation = drained_peak = None
for line in lines:
if "[rt] ds-staging " in line:
peak = re.search(r"\bpeak_bytes=(\d+)", line)
if peak:
peak_reservation = int(peak[1])
if "idle_bytes=0 " in line:
drained_peak = peak_reservation
continue
if not active:
if "[rt] ds-request " not in line:
continue
path = re.search(r"\bfile=(.*?) offset=", line)
if not path or from_file.casefold() not in path[1].casefold():
continue
active = True
if "[rt] ds-notify " in line:
m = re.search(r"\bus=(\d+)", line)
if m:
stages["notify_us"] += int(m[1])
if "[rt] ds-request " not in line:
continue
fields = dict(re.findall(r"\b(\w+)=([^\s]+)", line))
if "tick_ms" in fields:
tick = int(fields["tick_ms"])
first_tick = tick if first_tick is None else min(first_tick, tick)
last_tick = tick if last_tick is None else max(last_tick, tick)
o = int(fields["options"], 16)
codec = (o >> 1) & 15
tags = ["paged" if fields["dest"] == "pages" else "buffer"]
if int(fields["zlib"]):
tags.append("zlib")
if codec:
tags.append("BC6H" if codec == 6 else f"BC{codec}")
if int(fields["sw"]):
tags.append("swizzle" + fields["sw"])
if len(tags) == 1:
tags.append("plain")
g = groups[" ".join(tags)]
g["requests"] += 1
g["source_bytes"] += int(fields["src"])
g["destination_bytes"] += int(fields["dst"])
if int(fields["hr"], 16):
reason = fields["hr"] + " " + fields.get("reason", "unspecified")
errors[reason] += 1
g["rejected"] += 1
elif fields["cancelled"] == "1":
g["cancelled"] += 1
else:
g["successful"] += 1
g["written_bytes"] += int(fields["dst"])
if "priority" in fields:
priorities[fields["priority"]] += 1
paired += int(fields.get("paired", 0))
for key in ("read_us", "inflate_us", "bc_us", "sw_us", "copy_us"):
stages[key] += int(fields.get(key, 0))
totals = sum(groups.values(), Counter())
interval = None if first_tick is None else last_tick - first_tick
result = dict(shapes=dict(groups), totals=dict(totals), failures=dict(errors), worker_microseconds=dict(stages),
prepared_in_parallel=paired, queue_priorities=dict(priorities), retirement_window_ms=interval)
if peak_reservation is not None:
result["process_peak_staging_reservation_bytes"] = peak_reservation
result["process_drained_through_peak_bytes"] = drained_peak
if interval:
result["successful_requests_per_second"] = round(totals["successful"] * 1000 / interval, 2)
result["written_MiB_per_second"] = round(totals["written_bytes"] * 1000 / (1048576 * interval), 2)
return result
if __name__ == "__main__":
try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode
sys.stdout.reconfigure(errors="replace")
except AttributeError:
pass
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("trace")
parser.add_argument("--from-file", metavar="SUBSTRING",
help="start with the first matching source path, then include all queues and files")
args = parser.parse_args()
with open(args.trace, encoding="utf-8", errors="replace") as stream:
print(json.dumps(census(stream, args.from_file), indent=2, sort_keys=True))