feat(m0): measure the game's own frame times and try yielding by priority instead of a GPU cap

The 95 % GPU cap is gone: it did not lower utilisation, slowed the LLM and
a YouTube video did not stutter without it. What it was meant to protect
is now measured directly in a game. The new on-demand game_frames stage
records the game's frame times with PresentMon (pinned by hash, game
process only, CSV deleted after reading) with Jarvis off, idle and busy in
the game placement, and busy while Jarvis yields: its processes at
below-normal CPU and GPU scheduling priority, read back to confirm, then
restored. Busy rows run A B B A. The design docs record the candidate and
the pre-registered rule for adopting it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 09:05:55 +09:00
parent 726fca3377
commit dff18c5c7e
9 changed files with 717 additions and 11 deletions

View File

@@ -1655,3 +1655,189 @@ def test_a_player_that_never_comes_back_is_tried_again_per_window_and_the_stage_
assert cdp.navigated >= len(rows) * (video.ATTEMPTS - 1)
assert not any("seekTo" in e["error"] for e in r["errors"])
assert closed and running["now"] is None and stt.loaded is None and not running["held"]
# --- game_frames: the user's game's frame times next to Jarvis, plain and yielding by priority --
_PM_HEAD = ("Application,ProcessID,SwapChainAddress,Runtime,SyncInterval,PresentFlags,Dropped,TimeInSeconds,"
"msInPresentAPI,msBetweenPresents\n")
def _pm_csv(frames_ms, chain="0x1", pid=7000, dropped=(), head=_PM_HEAD):
t, lines = 0.0, []
for i, ms in enumerate(frames_ms):
t += ms / 1000
lines.append(f"game.exe,{pid},{chain},DXGI,0,0,{1 if i in dropped else 0},{t:.4f},0.1,{ms}")
return head + "\n".join(lines) + "\n"
def test_frame_stats_read_the_main_swap_chain_with_rate_one_percent_low_and_hitches():
from m0lib import frames
main = [10.0] * 200 + [60.0, 120.0]
text = _pm_csv(main, dropped={5}) + _pm_csv([500.0] * 5, chain="0x2", head="")
g = frames.frame_stats(frames.parse_csv(text), 3.0)
counted = main[1:] # the first row's interval reaches back before the capture
assert g["valid"] and g["frames"] == len(counted)
assert g["fps"] == round(len(counted) / (sum(counted) / 1000), 1)
p99 = sorted(counted)[int(len(counted) * 0.99)]
assert g["low1_fps"] == round(1000 / p99, 1) and g["max_ms"] == 120.0
assert g["hitches_over_ms"] == {"50": 2, "100": 1} and g["not_shown"] == 1
def test_frame_stats_read_presentmon_2_column_names_too():
from m0lib import frames
text = _pm_csv([16.7] * 100).replace("msBetweenPresents", "MsBetweenPresents")
assert frames.frame_stats(frames.parse_csv(text), 1.5)["fps"] == round(1000 / 16.7, 1)
def test_a_window_where_the_game_drew_nothing_or_almost_nothing_is_not_counted():
from m0lib import frames
assert not frames.frame_stats([], 60)["valid"]
few = frames.frame_stats(frames.parse_csv(_pm_csv([1000.0] * 30)), 60)
assert not few["valid"] and "frames" in few["invalid_because"][0]
def test_compare_pools_the_valid_windows_against_the_game_alone():
from m0lib import frames
def row(kind, fps, low, valid=True):
return {"kind": kind, "game": {"valid": valid, "fps": fps, "low1_fps": low,
"hitches_over_ms": {str(h): 1 for h in frames.HITCH_MS}}}
c = frames.compare([row("off", 100, 80), row("idle", 99, 79), row("busy", 90, 60), row("busy_yield", 98, 76),
row("busy_yield", 10, 5, valid=False), row("busy", 90, 60), row("off", 100, 80)])
assert c["off"]["windows"] == 2 and c["busy_yield"]["windows"] == 1
assert c["busy"]["vs_off_pct"] == {"fps": -10.0, "low1_fps": -25.0}
assert c["busy_yield"]["vs_off_pct"] == {"fps": -2.0, "low1_fps": -5.0}
class _FakePriorities:
"""Process priorities as Windows keeps them; ``ignored`` pids accept nothing."""
def __init__(self, ignored=()):
self.now: dict = {}
self.ignored = set(ignored)
self.touched: set = set()
def get(self, pid):
return self.now.get(pid, (0x20, 2))
def set(self, pid, cpu, gpu):
self.touched.add(pid)
if pid in self.ignored:
return False, False
self.now[pid] = (cpu, gpu)
return True, True
def test_yield_lowers_cpu_and_gpu_priority_and_puts_back_what_each_process_had(monkeypatch):
from m0lib import frames
pr = _FakePriorities(ignored={3})
pr.now[2] = (0x8000, 2) # already above normal: it gets that back, not plain normal
monkeypatch.setattr(frames, "get_priority", pr.get)
monkeypatch.setattr(frames, "set_priority", pr.set)
with frames.Yield([1, 2, 3]) as y:
assert pr.now[1] == pr.now[2] == (frames.CPU_BELOW_NORMAL, frames.GPU_BELOW_NORMAL)
assert pr.now[1] == (0x20, 2) and pr.now[2] == (0x8000, 2)
s = y.summary()
assert s == {"processes": 3, "cpu_lowered": 2, "gpu_lowered": 2, "restored": 2, "not_restored": 1}
assert frames.yield_problems(s) == ["1 process(es) kept the lowered priority"]
assert frames.yield_problems(frames.Yield([]).summary()) == ["yield found no Jarvis process"]
def test_presentmon_records_only_the_game_for_a_fixed_time_and_a_bad_download_is_refused(tmp_path, monkeypatch):
from m0lib import frames
cmd = frames.presentmon_cmd(Path("pm.exe"), 7000, 60, Path("f.csv"))
assert cmd[cmd.index("--process_id") + 1] == "7000" and cmd[cmd.index("--timed") + 1] == "60"
assert "--terminate_after_timed" in cmd and "--output_file" in cmd
class _Resp:
def __enter__(self):
return self
def __exit__(self, *a):
return None
def read(self):
return b"not presentmon"
monkeypatch.setattr(frames.urllib.request, "urlopen", lambda url, timeout=0: _Resp())
with pytest.raises(RuntimeError, match="hash"):
frames.ensure_presentmon(tmp_path)
assert not list((tmp_path / "tools").iterdir())
GAME_PID = 7000
def _run_frames_stage(tmp_path, monkeypatch, fake_server, admin=True, front=(GAME_PID, "game.exe"), pr=None):
from m0lib import frames
pr = _FakePriorities() if pr is None else pr
captured = []
monkeypatch.setattr(frames, "is_admin", lambda: admin)
monkeypatch.setattr(frames, "ensure_presentmon", lambda work: Path("pm.exe"))
monkeypatch.setattr(frames, "ask_for_a_game", lambda: True)
monkeypatch.setattr(frames, "foreground_process", lambda: front)
monkeypatch.setattr(frames, "get_priority", pr.get)
monkeypatch.setattr(frames, "set_priority", pr.set)
monkeypatch.setattr(frames, "SETTLE_S", 0)
def capture(exe, pid, seconds, out_csv, log):
captured.append((pid, dict(pr.now)))
return frames.parse_csv(_pm_csv([16.0] * 4000, pid=pid)), None
monkeypatch.setattr(frames, "capture", capture)
stt = _FakeStt()
r, started, running = _run_levels_stage(tmp_path, monkeypatch, fake_server, _vram, only="frames",
extra_opts=["stt_python=py"], stt=stt)
return r, started, running, stt, pr, captured
def test_frames_stage_measures_the_game_alone_next_to_jarvis_idle_busy_and_yielding(
tmp_path, monkeypatch, fake_server):
from m0lib import frames
r, started, running, stt, pr, captured = _run_frames_stage(tmp_path, monkeypatch, fake_server)
assert r["stage"] == "game_frames" and r["status"] == "ok"
rows = r["frame_rows"]
# Alone first and last; idle before busy; plain and yielding rows alternate A B B A.
assert [x["kind"] for x in rows] == ["off", "idle", "busy", "busy_yield", "busy_yield", "busy", "off"]
assert all(x["game"]["valid"] for x in rows) and {pid for pid, _ in captured} == {GAME_PID}
for x in rows:
if x["kind"].startswith("busy"):
assert x["llm"]["decode_tok_s"]["n"] > 0 # the LLM answered during the window
# Jarvis runs the app's game placement, the STT doing finals only (no partials while it answers).
assert len(started) == 1 and started[0]["sets"] == frames.GAME_LEVEL["sets"]
assert stt.paces == [llm._FINALS_ONLY] and stt.events[:3] == ["stt_loaded", "llm_start", "stt_go"]
# Yielding lowers Jarvis's processes during those windows only, never the game, and puts them back.
jarvis = {20, 21, STT_LAUNCHER_PID, STT_LAUNCHER_PID + 1}
low = (frames.CPU_BELOW_NORMAL, frames.GPU_BELOW_NORMAL)
for (pid, prio), x in zip(captured, rows):
lowered = {p for p, v in prio.items() if v == low}
assert lowered == (jarvis if x["kind"] == "busy_yield" else set())
assert GAME_PID not in pr.touched and all(v == (0x20, 2) for v in pr.now.values())
assert set(r["comparison"]) == {"off", "busy", "busy_yield"} and r["comparison"]["busy_yield"]["windows"] == 2
assert running["now"] is None and stt.loaded is None
def test_frames_stage_needs_administrator_rights_and_the_game_in_front_before_starting_jarvis(
tmp_path, monkeypatch, fake_server):
r, started, _, stt, _, captured = _run_frames_stage(tmp_path, monkeypatch, fake_server, admin=False)
assert r["status"] == "failed" and not started and not stt.events and not captured
assert any("관리자" in e["error"] for e in r["errors"])
r, started, _, stt, _, captured = _run_frames_stage(tmp_path, monkeypatch, fake_server,
front=(1234, "windowsterminal.exe"))
assert r["status"] == "failed" and not started and not stt.events and not captured
def test_a_yield_that_lowered_nothing_does_not_count_as_a_yielding_window(tmp_path, monkeypatch, fake_server):
pr = _FakePriorities(ignored={20, 21, STT_LAUNCHER_PID, STT_LAUNCHER_PID + 1})
r, *_ = _run_frames_stage(tmp_path, monkeypatch, fake_server, pr=pr)
assert r["comparison"]["busy_yield"]["windows"] == 0 and r["comparison"]["busy"]["windows"] == 2
assert r["status"] == "partial" and any("lowered no priority" in e["error"] for e in r["errors"])
def test_frames_stage_is_on_demand_with_the_stt_python_and_an_elevating_one_click_runner():
import m0
assert "game_frames" in m0.ON_DEMAND and "game_frames" in m0.NEEDS_STT
bat = (Path(m0.KIT) / "run_m0_game_frames.bat").read_text(encoding="utf-8")
assert "-Only game_frames" in bat and "-Verb RunAs" in bat