Files
javis_bot/scripts/m0/m0lib/gpucap.py
javis-bot 457b8636fa feat(m0): measure a YouTube video's dropped frames next to Jarvis
User decision (2026-11-11): the 95 % GPU cap is off by default because
gamemode_tune showed it did not lower nvidia-smi's utilisation and cost
6 to 10 % of the LLM's output. The worry behind it was a video stuttering
next to Jarvis, so the new on-demand stage video_drops measures that:

- A public Blender upload plays at 1080p60 in a throwaway Chrome/Edge
  profile (removed afterwards), driven over DevTools with a small
  standard-library WebSocket client, so no package is added to the
  already built venvs.
- Each window counts the video's dropped frames (the player's own
  counter) and the gaps over 50/100 ms between shown frames
  (requestVideoFrameCallback); windows with an ad, a pause, the end or a
  new video are kept but not counted.
- Rows: video alone, Jarvis idle (normal placement with the STT room and
  1 GB for other programs), Jarvis busy (LLM answering, STT transcribing
  ten speakers back to back) without and with the cap in A B B A order,
  then the video alone again. The cap pauses the engine and the STT
  worker only, never the browser.
- run_m0_video.bat runs just this stage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 00:55:26 +09:00

243 lines
8.7 KiB
Python

"""The GPU share cap the app can put on Jarvis (user decision, 2026-11-10: 95 %; 2026-11-11: off by
default, because gamemode_tune showed it did not lower the utilisation; video_drops measures whether
it keeps a video from stuttering).
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. A process
whose resume fails stays owed: it is not paused again on top (each suspend adds one to its
suspend count), its resume is retried every period and at the end, and one that still cannot
be resumed is reported as ``left_paused``."""
RESUME_TRIES_AT_END = 5
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.owed: dict[int, int] = {} # pid -> suspends not yet undone
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
@staticmethod
def _call(fn: Callable[[int], bool], pid: int) -> bool:
try:
return bool(fn(pid))
except Exception:
return False
def _resume(self, pid: int) -> None:
if self._call(self.resume, pid):
return
self.failed.add(pid)
self.owed[pid] = self.owed.get(pid, 0) + 1
def _settle_owed(self) -> None:
for pid, n in list(self.owed.items()):
while n and self._call(self.resume, pid):
n -= 1
if n:
self.owed[pid] = n
else:
del self.owed[pid]
def _loop(self) -> None:
while not self._stop.wait(self.period - self.pause):
self._settle_owed()
held = []
for p in self.pids:
if p in self.owed:
continue # still paused from a resume that failed
if self._call(self.suspend, p):
held.append(p)
else:
self.failed.add(p)
s0 = time.perf_counter()
try:
time.sleep(self.pause)
finally:
for p in held:
self._resume(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)
for _ in range(self.RESUME_TRIES_AT_END):
if not self.owed:
break
self._settle_owed()
if self.owed:
time.sleep(0.05)
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), "left_paused": len(self.owed)}