fix(m0): keep the game in front for the whole frames window and record the game_frames results
On the game PC the game went behind another window right after Jarvis started and stayed there for three windows, drawing at its background rate (about 58 fps). The kit only checked the window in front at the end of a window, so the window in which the user clicked back into the game counted and showed yielding as a 47 % drop in 1 % low. - FrontWatch checks every second from the start of a window to its end and spoils the window if the game was behind anything at any check. - Before a window the kit asks the user, with a beep, to go back to the game and waits up to two minutes. - What was in front is kept only as a category (a Jarvis process, a console window, Explorer, another app), never an app name. Results: with the game in front, a busy Jarvis cost 0.2 % of the average fps and 3.3 % of the 1 % low with no frames over 50 ms, so by the pre-registered rule nothing is added and yielding is not adopted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -46,6 +46,12 @@ ROUNDS = ("busy", "busy_yield", "busy_yield_2", "busy_2")
|
||||
# The foreground window belongs to one of these when the user did not get back to the game.
|
||||
NOT_A_GAME = {"windowsterminal.exe", "openconsole.exe", "conhost.exe", "cmd.exe", "powershell.exe",
|
||||
"pwsh.exe", "explorer.exe"}
|
||||
# A game behind another window draws at its background rate, so the game must stay in front for the
|
||||
# whole window, not only at its end (2026-11-11: the game went behind a window right after Jarvis
|
||||
# started, and a window the user clicked back into midway still counted).
|
||||
FRONT_EVERY_S = 1.0 # how often the window in front is checked
|
||||
FRONT_WAIT_S = 120 # how long the kit waits for the user to get back to the game before a window
|
||||
BACK_SETTLE_S = 3 # the game settles this long after it is back in front
|
||||
# Windows' values: process priority classes and D3DKMT_SCHEDULINGPRIORITYCLASS.
|
||||
CPU_NORMAL, CPU_BELOW_NORMAL = 0x20, 0x4000
|
||||
GPU_NORMAL, GPU_BELOW_NORMAL = 2, 1
|
||||
@@ -98,6 +104,92 @@ def foreground_process() -> tuple[int, str] | None:
|
||||
return pid.value, name
|
||||
|
||||
|
||||
def front_label(fg: tuple[int, str], jarvis: set[int]) -> str:
|
||||
"""What was in front instead of the game, in words that name no app the user has open."""
|
||||
pid, exe = fg
|
||||
if pid in jarvis:
|
||||
return "a Jarvis process"
|
||||
if exe == "explorer.exe":
|
||||
return "Explorer (desktop or taskbar)"
|
||||
if exe in NOT_A_GAME:
|
||||
return "a console window"
|
||||
return "another app"
|
||||
|
||||
|
||||
class FrontWatch:
|
||||
"""Checks every FRONT_EVERY_S, from the start of a window to its end, that the game is in front.
|
||||
A moment with no window in front at all (Windows switching) is not counted either way."""
|
||||
|
||||
def __init__(self, game: int, jarvis_pids):
|
||||
self.game = game
|
||||
self.jarvis_pids = jarvis_pids
|
||||
self._jarvis: set[int] | None = None
|
||||
self.samples = 0
|
||||
self.away = 0
|
||||
self.to: set[str] = set()
|
||||
self._stop = threading.Event()
|
||||
self._thread = threading.Thread(target=self._loop, daemon=True)
|
||||
|
||||
def check(self) -> None:
|
||||
fg = foreground_process()
|
||||
if fg is None:
|
||||
return
|
||||
self.samples += 1
|
||||
if fg[0] != self.game:
|
||||
self.away += 1
|
||||
if self._jarvis is None:
|
||||
self._jarvis = set(self.jarvis_pids())
|
||||
self.to.add(front_label(fg, self._jarvis))
|
||||
|
||||
def _loop(self) -> None:
|
||||
while not self._stop.wait(FRONT_EVERY_S):
|
||||
self.check()
|
||||
|
||||
def __enter__(self) -> "FrontWatch":
|
||||
self.check()
|
||||
self._thread.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc) -> None:
|
||||
self._stop.set()
|
||||
self._thread.join()
|
||||
self.check()
|
||||
|
||||
def summary(self) -> dict:
|
||||
return {"checks": self.samples, "away": self.away, "to": sorted(self.to)}
|
||||
|
||||
|
||||
def _beep() -> None:
|
||||
try:
|
||||
import winsound
|
||||
winsound.MessageBeep()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def wait_for_game(game: int, jarvis_pids) -> dict | None:
|
||||
"""None when the game is in front. Otherwise asks the user to go back to it and waits up to
|
||||
FRONT_WAIT_S; says what was in front, how long it waited and whether the game came back."""
|
||||
fg = foreground_process()
|
||||
if fg is None or fg[0] == game:
|
||||
return None
|
||||
to = front_label(fg, set(jarvis_pids()))
|
||||
say(" 게임 화면이 앞에 없습니다. 게임 화면을 한 번 눌러 돌아가 주세요. 돌아오시면 이어서 잽니다.")
|
||||
_beep()
|
||||
t0 = time.monotonic()
|
||||
back = False
|
||||
while time.monotonic() - t0 < FRONT_WAIT_S:
|
||||
time.sleep(FRONT_EVERY_S)
|
||||
fg = foreground_process()
|
||||
if fg is not None and fg[0] == game:
|
||||
back = True
|
||||
break
|
||||
waited = round(time.monotonic() - t0, 1)
|
||||
if back:
|
||||
time.sleep(BACK_SETTLE_S)
|
||||
return {"to": to, "waited_s": waited, "back": back}
|
||||
|
||||
|
||||
_API: dict = {}
|
||||
|
||||
|
||||
@@ -405,19 +497,29 @@ def measure(r: StageResult, s: "llm.Strata", preset: dict, context: int, stt_pyt
|
||||
r.note("measured while the user's game was running (its name is not kept)")
|
||||
base = f"http://127.0.0.1:{llm.PORT}/v1"
|
||||
|
||||
def jarvis_pids() -> list[int]:
|
||||
return llm.cap_pids(s, stt)
|
||||
|
||||
def window(key: str, kind: str) -> dict:
|
||||
x: dict = {"key": key, "kind": kind}
|
||||
lost = wait_for_game(game, jarvis_pids)
|
||||
if lost:
|
||||
x["front_lost_before"] = lost
|
||||
r.note(f"{key}: the game was not in front before the window ({lost['to']}), "
|
||||
f"{'back after' if lost['back'] else 'not back after'} {lost['waited_s']} s")
|
||||
used = gpu_memory_mib()
|
||||
if used:
|
||||
x["vram_free_mib"] = used[1] - used[0]
|
||||
with gpucap.UtilSampler() as util:
|
||||
with gpucap.UtilSampler() as util, FrontWatch(game, jarvis_pids) as front:
|
||||
frames, err = capture(pm, game, WINDOW_S, work / "cache" / "frames.csv", pm_log)
|
||||
g = frame_stats(frames, WINDOW_S, MIN_FRAMES if kind in ("off", "idle") else 0)
|
||||
g["gpu_util"] = util.summary()
|
||||
now = foreground_process()
|
||||
if now is not None and now[0] != game:
|
||||
away = front.summary()
|
||||
if away["away"]:
|
||||
g["away_from_front"] = away
|
||||
g["valid"] = False
|
||||
g["invalid_because"] = g.get("invalid_because", []) + ["the game was not in front at the end"]
|
||||
g["invalid_because"] = g.get("invalid_because", []) + [
|
||||
f"the game was not in front for {away['away']} of {away['checks']} checks"]
|
||||
if err:
|
||||
# Frames from a capture that failed or was cut off do not cover the window.
|
||||
r.error(f"frames {key}", err)
|
||||
@@ -507,8 +609,4 @@ def measure(r: StageResult, s: "llm.Strata", preset: dict, context: int, stt_pyt
|
||||
stt.close()
|
||||
s.stop()
|
||||
say("\a 게임 프레임 측정이 끝났습니다. 게임을 꺼도 됩니다.")
|
||||
try:
|
||||
import winsound
|
||||
winsound.MessageBeep()
|
||||
except Exception:
|
||||
pass
|
||||
_beep()
|
||||
|
||||
Reference in New Issue
Block a user