"""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)}