okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-141-fixb: Python, docs and bench fixes and portsef8071303

main
2.3 KiB62 linesraw
1#!/usr/bin/env python3
2"""Turn a telemetry.jsonl from monitor.py into memory-summary.json.
3
4Reports the observed peaks and ranges of the engine's own memory counters, in the
5units it reports them in (bytes), so the report can state where each number came from.
6"""
7import argparse
8import json
9import statistics
10from pathlib import Path
11
12FIELDS = ("gpu_mem_used", "gpu_mem_total", "gpu_util", "gpu_temp_c", "gpu_power_w",
13          "gpu_pcie_gen", "gpu_pcie_width", "ram_used", "ram_total", "disk_read_mb",
14          "vram_free_mib", "expert_cache_mib")
15
16GIB = 1024 ** 3
17
18
19def main():
20    parser = argparse.ArgumentParser(description=__doc__)
21    parser.add_argument("telemetry", type=Path)
22    parser.add_argument("--out", type=Path, required=True)
23    args = parser.parse_args()
24
25    samples, broken = [], 0
26    for line in args.telemetry.read_text(encoding="utf-8").splitlines():
27        if not line.strip():
28            continue
29        try:
30            row = json.loads(line)
31        except json.JSONDecodeError:
32            broken += 1                     # one line can be lost where a sampler restarts
33            continue
34        if "error" not in row:
35            samples.append(row)
36
37    times = [row["t"] for row in samples]
38    summary = {
39        "source": str(args.telemetry.name),
40        "samples": len(samples),
41        "unparseable_lines": broken,
42        "sampled_seconds": round(max(times) - min(times), 1) if times else 0,
43        "note": ("Each sample is one GET /metrics, so the values are the engine's own view. "
44                 "Sampled peaks can miss shorter spikes between samples."),
45        "fields": {},
46    }
47    for field in FIELDS:
48        values = [row[field] for row in samples if row.get(field) is not None]
49        if not values:
50            continue
51        entry = {"median": statistics.median(values), "min": min(values), "max": max(values)}
52        if field in ("gpu_mem_used", "gpu_mem_total", "ram_used", "ram_total"):
53            entry["max_gib"] = round(max(values) / GIB, 2)
54            entry["median_gib"] = round(statistics.median(values) / GIB, 2)
55        summary["fields"][field] = entry
56
57    args.out.write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
58    print(json.dumps(summary["fields"], indent=2))
59
60
61if __name__ == "__main__":
62    main()