okami.mom/strata

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

Niko1221Merge f-142b-serve into f-1426e1085c1d

main
2.0 KiB44 linesraw
1"""Read-only, one-second sysfs/proc snapshots for attributing benchmark drift."""
2from pathlib import Path
3import threading
4import time
5
6from single_gpu import BDF
7
8
9class ResourceSampler:
10    def __init__(self, pid):
11        device = Path("/sys/bus/pci/devices") / BDF
12        paths = [device / name for name in ("gpu_busy_percent", "mem_busy_percent", "current_link_speed",
13                                           "current_link_width", "pp_dpm_sclk", "pp_dpm_mclk")]
14        for hwmon in (device / "hwmon").glob("hwmon*"):
15            paths.extend(hwmon / name for name in ("temp1_input", "temp2_input", "temp3_input", "freq1_input",
16                                                   "freq2_input", "power1_average", "fan1_input"))
17        paths += [Path(f"/proc/{pid}/io"), Path(f"/proc/{pid}/schedstat"), Path(f"/proc/{pid}/stat"),
18                  Path("/proc/loadavg")]
19        paths += [Path("/proc/pressure") / kind for kind in ("cpu", "io", "memory")]
20        paths += list(Path("/sys/block").glob("nvme*/stat"))
21        self.paths = [p for p in paths if p.exists()]
22        self.samples = []
23        self.stop = threading.Event()
24        self.thread = threading.Thread(target=self.run, daemon=True)
25        self.thread.start()
26
27    def run(self):
28        while not self.stop.is_set():
29            begin = time.perf_counter()
30            values = {}
31            for path in self.paths:
32                try:
33                    values[str(path)] = path.read_text().strip()
34                except OSError as exc:
35                    values[str(path)] = {"error": str(exc)}
36            self.samples.append({"monotonic_s": begin, "unix_s": time.time(), "values": values,
37                                 "read_ms": (time.perf_counter() - begin) * 1000})
38            self.stop.wait(1)
39
40    def close(self):
41        self.stop.set()
42        self.thread.join()
43        return {"scope": "read-only system snapshots, raw sysfs units; shared host; no clock/power changes",
44                "interval_s": 1, "samples": self.samples}