feat(m0): measure the 1 GB room for other programs and a 95 % GPU cap
User decision (2026-11-10, room to watch a video next to Jarvis): the normal placement leaves 1 GB of VRAM for programs opened later, and Jarvis's GPU work is capped at 95 %. - gamemode_tune's normal rows take OTHER_ROOM_MIB off the expert budget. - gpucap.DutyCap pauses the Strata engine (strata.exe --serve under the kit) and the STT worker with their children together for 10 ms of every 200 ms (NtSuspendProcess), and always resumes them. - Every row samples nvidia-smi utilization.gpu; capped rows have an uncapped twin (normal with and without the STT, game 2 GB with 2 s partials) so the run shows the cost and what the GPU really gets back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
201
scripts/m0/m0lib/gpucap.py
Normal file
201
scripts/m0/m0lib/gpucap.py
Normal file
@@ -0,0 +1,201 @@
|
||||
"""The GPU share cap the app would put on Jarvis (user decision, 2026-11-10: 95 %).
|
||||
|
||||
Jarvis's GPU work runs in two processes: the Strata engine and the voice worker (here the STT
|
||||
load worker). A GPU cannot be told to give one process only part of its time, so the cap pauses
|
||||
both processes together for a short slice of every period (Windows NtSuspendProcess /
|
||||
NtResumeProcess): 10 ms of every 200 ms is 95 %. Work already queued on the GPU still finishes
|
||||
during the pause, so what the GPU really gets back is measured (``UtilSampler``), not assumed.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Callable, Iterable
|
||||
|
||||
from m0lib.common import IS_WINDOWS, nvidia_smi, stats
|
||||
|
||||
PERIOD_S = 0.2
|
||||
|
||||
|
||||
def gpu_util_pct() -> int | None:
|
||||
"""nvidia-smi's utilization.gpu of GPU 0: the share of the last sample period in which any
|
||||
kernel ran (what the app would read through NVML), or None."""
|
||||
exe = nvidia_smi()
|
||||
if not exe:
|
||||
return None
|
||||
try:
|
||||
out = subprocess.run([exe, "--query-gpu=utilization.gpu", "--format=csv,noheader,nounits"],
|
||||
capture_output=True, text=True, timeout=10).stdout.strip().splitlines()[0]
|
||||
return int(out.strip())
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
class UtilSampler:
|
||||
"""Samples GPU utilisation in the background while a measurement runs."""
|
||||
|
||||
def __init__(self, interval: float = 0.5, read: Callable[[], int | None] | None = None):
|
||||
self.interval = interval
|
||||
self.read = read or gpu_util_pct
|
||||
self.samples: list[int] = []
|
||||
self._stop = threading.Event()
|
||||
self._t: threading.Thread | None = None
|
||||
|
||||
def __enter__(self) -> "UtilSampler":
|
||||
self._t = threading.Thread(target=self._loop, daemon=True)
|
||||
self._t.start()
|
||||
return self
|
||||
|
||||
def _loop(self) -> None:
|
||||
while not self._stop.is_set():
|
||||
v = self.read()
|
||||
if v is not None:
|
||||
self.samples.append(v)
|
||||
self._stop.wait(self.interval)
|
||||
|
||||
def __exit__(self, *exc) -> None:
|
||||
self._stop.set()
|
||||
if self._t:
|
||||
self._t.join(timeout=15)
|
||||
|
||||
def summary(self, cap: float | None = None) -> dict:
|
||||
out = {"pct": stats(self.samples, 1)}
|
||||
if cap is not None and self.samples:
|
||||
out["over_cap_share"] = round(sum(1 for v in self.samples if v > cap * 100) / len(self.samples), 3)
|
||||
return out
|
||||
|
||||
|
||||
def process_table() -> list[dict]:
|
||||
"""Every process with its id, parent, executable and command line (Windows only)."""
|
||||
if not IS_WINDOWS:
|
||||
return []
|
||||
ps = ("Get-CimInstance Win32_Process | ForEach-Object { \"$($_.ProcessId)`t$($_.ParentProcessId)`t"
|
||||
"$($_.ExecutablePath)`t$($_.CommandLine)\" }")
|
||||
try:
|
||||
out = subprocess.run(["powershell", "-NoProfile", "-Command", ps], capture_output=True, text=True,
|
||||
encoding="utf-8", errors="replace", timeout=60).stdout
|
||||
except Exception:
|
||||
return []
|
||||
return parse_process_table(out)
|
||||
|
||||
|
||||
def parse_process_table(text: str) -> list[dict]:
|
||||
rows = []
|
||||
for line in text.splitlines():
|
||||
parts = line.rstrip("\r").split("\t", 3)
|
||||
if len(parts) < 4 or not parts[0].strip().isdigit():
|
||||
continue
|
||||
rows.append({"pid": int(parts[0]), "ppid": int(parts[1]) if parts[1].strip().isdigit() else None,
|
||||
"exe": parts[2], "cmd": parts[3]})
|
||||
return rows
|
||||
|
||||
|
||||
def with_children(procs: list[dict], roots: Iterable[int]) -> list[int]:
|
||||
"""The roots and everything they started (a venv python.exe on Windows is a launcher that
|
||||
runs the real interpreter as its child)."""
|
||||
out, todo = [], [r for r in roots if r is not None]
|
||||
while todo:
|
||||
pid = todo.pop()
|
||||
if pid in out:
|
||||
continue
|
||||
out.append(pid)
|
||||
todo += [p["pid"] for p in procs if p["ppid"] == pid]
|
||||
return out
|
||||
|
||||
|
||||
def engine_roots(procs: list[dict], home: Path) -> list[int]:
|
||||
"""The Strata engine serving the model: its strata.exe --serve under our components folder
|
||||
(not the strata terminal that started it)."""
|
||||
where = str(home / "components").lower()
|
||||
return [p["pid"] for p in procs
|
||||
if p["exe"].lower().startswith(where) and p["exe"].lower().endswith("strata.exe")
|
||||
and "--serve" in p["cmd"]]
|
||||
|
||||
|
||||
_API: dict = {}
|
||||
|
||||
|
||||
def _nt(name: str, pid: int) -> bool:
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
if not _API:
|
||||
k32 = ctypes.WinDLL("kernel32", use_last_error=True)
|
||||
ntdll = ctypes.WinDLL("ntdll")
|
||||
k32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
|
||||
k32.OpenProcess.restype = ctypes.c_void_p
|
||||
k32.CloseHandle.argtypes = [ctypes.c_void_p]
|
||||
for fn in (ntdll.NtSuspendProcess, ntdll.NtResumeProcess):
|
||||
fn.argtypes = [ctypes.c_void_p]
|
||||
fn.restype = ctypes.c_long
|
||||
_API.update(k32=k32, ntdll=ntdll)
|
||||
k32 = _API["k32"]
|
||||
h = k32.OpenProcess(0x0800, False, pid) # PROCESS_SUSPEND_RESUME
|
||||
if not h:
|
||||
return False
|
||||
try:
|
||||
return getattr(_API["ntdll"], name)(h) == 0
|
||||
finally:
|
||||
k32.CloseHandle(h)
|
||||
|
||||
|
||||
def suspend_pid(pid: int) -> bool:
|
||||
return _nt("NtSuspendProcess", pid)
|
||||
|
||||
|
||||
def resume_pid(pid: int) -> bool:
|
||||
return _nt("NtResumeProcess", pid)
|
||||
|
||||
|
||||
class DutyCap:
|
||||
"""Pause ``pids`` together for (1 - cap) of every ``period`` s while the block runs; every
|
||||
paused process is resumed before the block ends, also on an error."""
|
||||
|
||||
def __init__(self, pids: list[int], cap: float, period: float | None = None,
|
||||
suspend: Callable[[int], bool] | None = None, resume: Callable[[int], bool] | None = None):
|
||||
self.pids, self.cap, self.period = list(pids), cap, period or PERIOD_S
|
||||
self.suspend, self.resume = suspend or suspend_pid, resume or resume_pid
|
||||
self.pause = self.period * (1 - cap)
|
||||
self.paused_s = 0.0
|
||||
self.cycles = 0
|
||||
self.failed: set[int] = set()
|
||||
self._stop = threading.Event()
|
||||
self._t: threading.Thread | None = None
|
||||
self._t0 = 0.0
|
||||
self._t1 = 0.0
|
||||
|
||||
def __enter__(self) -> "DutyCap":
|
||||
self._t0 = time.perf_counter()
|
||||
if self.pids and self.pause > 0:
|
||||
self._t = threading.Thread(target=self._loop, daemon=True)
|
||||
self._t.start()
|
||||
return self
|
||||
|
||||
def _loop(self) -> None:
|
||||
while not self._stop.wait(self.period - self.pause):
|
||||
held = [p for p in self.pids if self.suspend(p)]
|
||||
self.failed.update(p for p in self.pids if p not in held)
|
||||
s0 = time.perf_counter()
|
||||
try:
|
||||
time.sleep(self.pause)
|
||||
finally:
|
||||
for p in held:
|
||||
if not self.resume(p):
|
||||
self.failed.add(p)
|
||||
self.paused_s += time.perf_counter() - s0
|
||||
self.cycles += 1
|
||||
|
||||
def __exit__(self, *exc) -> None:
|
||||
self._stop.set()
|
||||
if self._t:
|
||||
self._t.join(timeout=10)
|
||||
self._t1 = time.perf_counter()
|
||||
|
||||
def summary(self) -> dict:
|
||||
span = (self._t1 or time.perf_counter()) - self._t0
|
||||
return {"cap": self.cap, "period_s": self.period, "pause_s": round(self.pause, 4),
|
||||
"processes": len(self.pids), "cycles": self.cycles,
|
||||
"paused_share": round(self.paused_s / span, 3) if span > 0 else None,
|
||||
"failed_processes": len(self.failed)}
|
||||
Reference in New Issue
Block a user