okami.mom/strata

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

Niko1221Merge f-142c-small into f-142407ea8192

main
3.0 KiB76 linesraw
1"""Sample system RAM, swap and AMD GPU readings once per second during the trial.
2
3Radeon Pro VII / ROCm 6.2 adaptation of
4bench/results/2026-10-04-community-mi50/monitor_mi50.py:
5GPU memory, edge/junction/memory temperature and socket power via rocm-smi
6(ROCm 6.2 has no `--showpowercap`, so the power cap comes from the hwmon
7`power1_cap` file). Runs on the HOST (needs rocm-smi), writes telemetry.jsonl.
8
9    python3 monitor_rpv.py telemetry.jsonl
10"""
11import json
12import subprocess
13import sys
14import time
15from pathlib import Path
16
17
18def parse_rocm_smi():
19    """One rocm-smi call per sample: VRAM, temperatures, power, GPU use."""
20    out = {}
21    try:
22        text = subprocess.check_output(
23            ["rocm-smi", "--showmeminfo", "vram", "--showtemp", "--showpower", "--showuse"],
24            text=True, stderr=subprocess.DEVNULL)
25    except Exception as error:  # noqa: BLE001
26        return {"error": str(error)}
27    for line in text.splitlines():
28        # rocm-smi 的行形如 "GPU[0]\t\t: Temperature (Sensor edge) (C): 37.0" —— 指标名在**最后一个**
29        # 冒号之前,所以按最后一个冒号切(按第一个切会只拿到 "GPU[0]")。
30        if ":" not in line:
31            continue
32        name, _, value = line.rpartition(":")
33        name, value = name.strip(), value.strip()
34        try:
35            if name.endswith("VRAM Total Memory (B)"):
36                out["vram_total_bytes"] = int(value)
37            elif name.endswith("VRAM Total Used Memory (B)"):
38                out["vram_used_bytes"] = int(value)
39            elif "(Sensor edge) (C)" in name:
40                out["temp_edge_c"] = float(value)
41            elif "(Sensor junction) (C)" in name:
42                out["temp_junction_c"] = float(value)
43            elif "(Sensor memory) (C)" in name:
44                out["temp_memory_c"] = float(value)
45            elif "Socket Graphics Package Power (W)" in name:
46                out["power_w"] = float(value)
47            elif "GPU use (%)" in name:
48                out["gpu_use_pct"] = float(value)
49        except ValueError:
50            continue
51    return out
52
53
54def power_cap_uw():
55    for path in Path("/sys/class/drm").glob("card*/device/hwmon/hwmon*/power1_cap"):
56        try:
57            return int(path.read_text().strip()), str(path)
58        except OSError:
59            continue
60    return None, None
61
62
63cap, cap_path = power_cap_uw()
64target = Path(sys.argv[1] if len(sys.argv) > 1 else "telemetry.jsonl")
65with target.open("a") as output:
66    while True:
67        memory = {}
68        for line in Path("/proc/meminfo").read_text().splitlines():
69            key, value = line.split(":", 1)
70            if key in ("MemTotal", "MemAvailable", "SwapTotal", "SwapFree"):
71                memory[key + "_KiB"] = int(value.strip().split()[0])
72        output.write(json.dumps({"epoch_s": time.time(), "memory": memory,
73                                 "gpu": parse_rocm_smi(),
74                                 "power_cap_uw": cap, "power_cap_path": cap_path}) + "\n")
75        output.flush()
76        time.sleep(1)