upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/game_gate.py
diff options
context:
space:
mode:
authorMoonEyes <14891700+MoonEyes@users.noreply.github.com>2026-12-02 11:51:40 +0100
committerMoonEyes <14891700+MoonEyes@users.noreply.github.com>2026-12-02 11:51:40 +0100
commitfff970e616bf965729cd08a4e86bc6264494de73 (patch)
tree03cdc30677fbe1ddaaf33d160150c9afbcfe73c5 /tools/game_gate.py
parent7c245a359b8bdb6d9985c6d3ffd2bee0609de911 (diff)
parent895c472faa1c3d44a1358640f09c1f10e6a6a876 (diff)
Merge origin/main into carry/1346
Diffstat (limited to 'tools/game_gate.py')
-rw-r--r--tools/game_gate.py133
1 files changed, 126 insertions, 7 deletions
diff --git a/tools/game_gate.py b/tools/game_gate.py
index fee55866b..b88ec9535 100644
--- a/tools/game_gate.py
+++ b/tools/game_gate.py
@@ -8,6 +8,7 @@ script's output: the precompile lines), leonida.log (the runtime's log) and data
8numbers, switch names and verdicts only: no path, no user or machine name, nothing the title printed. 8numbers, switch names and verdicts only: no path, no user or machine name, nothing the title printed.
9Schema and rules: docs/game_gate.md. 9Schema and rules: docs/game_gate.md.
10""" 10"""
11import csv
11import datetime as dt 12import datetime as dt
12import glob 13import glob
13import json 14import json
@@ -31,6 +32,8 @@ END_WINDOW_S = 60.0
31GAP_FAIL_S = 10.0 32GAP_FAIL_S = 10.0
32# The last frame line before the stop request is at most this old for the run to count as alive. 33# The last frame line before the stop request is at most this old for the run to count as alive.
33ALIVE_S = 30.0 34ALIVE_S = 30.0
35# The memory stop: the PC under its commit floor this long (5 s memory lines).
36COMMIT_LOW_S = 20.0
34DEVICE_LOST = 0x887A0005 37DEVICE_LOST = 0x887A0005
35 38
36ANSI = re.compile(r"\x1b\[[0-9;]*m") 39ANSI = re.compile(r"\x1b\[[0-9;]*m")
@@ -49,6 +52,14 @@ MEMORY = re.compile(r"^memory: (\d+) MB private \((\d+) MB of it in memory; the
49 r"left to commit\)") 52 r"left to commit\)")
50VIDEO = re.compile(r"^video memory as the system counts it, [^:]*: the card's own: budget (\d+) MB, in use (\d+) MB, " 53VIDEO = re.compile(r"^video memory as the system counts it, [^:]*: the card's own: budget (\d+) MB, in use (\d+) MB, "
51 r".*?; the PC's shared with the card: budget (\d+) MB, in use (\d+) MB") 54 r".*?; the PC's shared with the card: budget (\d+) MB, in use (\d+) MB")
55# The census line with them: objects let go that wait for their submissions, and those handed to the reaper thread
56# and not let go yet (the reaper's queue, the number a pile-up shows first).
57WAITS = re.compile(r"^memory, what waits to be let go: (\d+) objects let go that wait for the submissions that may use "
58 r"them \(LEONIDA_BURY_LAG\), (\d+) retired objects, landed host lists and lists' fronts handed to the "
59 r"thread that lets them go")
60SERIES = "memory.csv"
61SERIES_COLUMNS = ("t_s", "private_mb", "in_memory_mb", "pc_free_mb", "pc_commit_left_mb", "vram_budget_mb",
62 "vram_in_use_mb", "shared_in_use_mb", "waiting_objects", "reaper_queue")
52 63
53# The runtime's own words this parser reads, and where they are written: tests/test_game_gate.py checks that 64# The runtime's own words this parser reads, and where they are written: tests/test_game_gate.py checks that
54# each is still there, so a reworded line fails the test, not the next gate. 65# each is still there, so a reworded line fails the test, not the next gate.
@@ -76,6 +87,11 @@ SOURCES = {
76 "d3d12/crates/leonida-gpu/src/host.rs", 87 "d3d12/crates/leonida-gpu/src/host.rs",
77 "the PC's shared with the card: budget {} MB, in use {} MB": "d3d12/crates/leonida-gpu/src/host.rs", 88 "the PC's shared with the card: budget {} MB, in use {} MB": "d3d12/crates/leonida-gpu/src/host.rs",
78 'h.log_video_memory("with the memory line")': "d3d12/crates/leonida-gpu/src/copystat.rs", 89 'h.log_video_memory("with the memory line")': "d3d12/crates/leonida-gpu/src/copystat.rs",
90 # One key over the whole part WAITS reads, the ", " between its two numbers too.
91 "memory, what waits to be let go: {waiting} objects let go that wait for the submissions that may use them "
92 "(LEONIDA_BURY_LAG), {handed} retired objects, landed host lists and lists' fronts handed to the thread that "
93 "lets them go": "d3d12/crates/leonida-gpu/src/census.rs",
94 "crate::census::log();": "d3d12/crates/leonida-gpu/src/copystat.rs",
79} 95}
80 96
81 97
@@ -124,6 +140,7 @@ class Run:
124 self.missed = [] # t of each pipeline not made within its wait 140 self.missed = [] # t of each pipeline not made within its wait
125 self.memory = [] # (t, private MB, in memory MB, PC free MB, PC left to commit MB) 141 self.memory = [] # (t, private MB, in memory MB, PC free MB, PC left to commit MB)
126 self.video = [] # (t, card budget MB, card in use MB, shared budget MB, shared in use MB) 142 self.video = [] # (t, card budget MB, card in use MB, shared budget MB, shared in use MB)
143 self.waits = [] # (t, objects waiting for their submissions, the reaper's queue)
127 self.settings = None # from the runtime's settings line 144 self.settings = None # from the runtime's settings line
128 self.suspend = None 145 self.suspend = None
129 self.ended_at = None 146 self.ended_at = None
@@ -155,6 +172,8 @@ class Run:
155 self.memory.append((t,) + tuple(int(x) for x in MEMORY.match(text).groups())) 172 self.memory.append((t,) + tuple(int(x) for x in MEMORY.match(text).groups()))
156 elif text.startswith("video memory as the system counts it") and VIDEO.match(text): 173 elif text.startswith("video memory as the system counts it") and VIDEO.match(text):
157 self.video.append((t,) + tuple(int(x) for x in VIDEO.match(text).groups())) 174 self.video.append((t,) + tuple(int(x) for x in VIDEO.match(text).groups()))
175 elif text.startswith("memory, what waits to be let go") and WAITS.match(text):
176 self.waits.append((t,) + tuple(int(x) for x in WAITS.match(text).groups()))
158 elif text.startswith("a pipeline not made within its wait"): 177 elif text.startswith("a pipeline not made within its wait"):
159 self.missed.append(t) 178 self.missed.append(t)
160 elif target == "settings": 179 elif target == "settings":
@@ -271,6 +290,37 @@ def read_precompile(lines):
271 return out 290 return out
272 291
273 292
293def since_end(rows, low):
294 """Seconds the last rows have all been `low`, from the first of them to the last row (0.0 if the last is not)."""
295 first = None
296 for row in reversed(rows):
297 if not low(row):
298 break
299 first = row[0]
300 return rows[-1][0] - first if first is not None else 0.0
301
302
303def memory_alarm(run):
304 """Why scripts/game-gate.ps1 must stop the run for memory now, or None, from the latest memory lines and the
305 limits it wrote in run.json (`memory_stop`; 0 or less turns one off)."""
306 limits = run.meta.get("memory_stop") or {}
307 private_max = limits.get("private_max_mb") or 0
308 left_min = limits.get("commit_left_min_mb") or 0
309 over_s = limits.get("over_budget_s") or 0
310 if run.memory:
311 private = run.memory[-1][1]
312 if private_max > 0 and private > private_max:
313 return f"private bytes {private} MB, over {private_max} MB"
314 low = since_end(run.memory, lambda m: m[4] < left_min) if left_min > 0 else 0.0
315 if low >= COMMIT_LOW_S:
316 return f"the PC had under {left_min} MB left to commit for {low:.0f} s"
317 if over_s > 0 and run.video:
318 over = since_end(run.video, lambda v: v[2] > v[1])
319 if over >= over_s:
320 return f"video memory over its budget for {over:.0f} s"
321 return None
322
323
274def state(run): 324def state(run):
275 world = run.world_at() 325 world = run.world_at()
276 last = run.last_at 326 last = run.last_at
@@ -282,6 +332,7 @@ def state(run):
282 "frame_lines": len(run.frames), 332 "frame_lines": len(run.frames),
283 "relaunched": run.relaunched, 333 "relaunched": run.relaunched,
284 "device_lost": any(c["kind"] == "device_lost" for c in run.crashes), 334 "device_lost": any(c["kind"] == "device_lost" for c in run.crashes),
335 "memory_stop": memory_alarm(run),
285 } 336 }
286 337
287 338
@@ -334,13 +385,76 @@ def fps_block(run, world, end):
334 return out 385 return out
335 386
336 387
337def memory_block(run): 388def slope_per_min(points):
338 """Peaks of the runtime's memory lines over the whole run (none from a build without diagnostics).""" 389 """Least-squares slope of (t in s, value) points, per minute; None under 3 points or with no time spread."""
390 if len(points) < 3:
391 return None
392 mt = sum(t for t, _ in points) / len(points)
393 mv = sum(v for _, v in points) / len(points)
394 spread = sum((t - mt) ** 2 for t, _ in points)
395 if spread == 0:
396 return None
397 return 60.0 * sum((t - mt) * (v - mv) for t, v in points) / spread
398
399
400def near(rows, t, within=2.0):
401 """The row of `rows` (sorted by time) nearest to t, if within `within` seconds."""
402 best = min(rows, key=lambda r: abs(r[0] - t), default=None)
403 return best if best is not None and abs(best[0] - t) <= within else None
404
405
406def memory_rows(run):
407 """One row per memory line, with the video memory and census lines written with it (`memory.csv`)."""
408 rows = []
409 for t, private, in_memory, free, left in run.memory:
410 v = near(run.video, t)
411 w = near(run.waits, t)
412 rows.append({"t_s": round(t, 3), "private_mb": private, "in_memory_mb": in_memory, "pc_free_mb": free,
413 "pc_commit_left_mb": left, "vram_budget_mb": v[1] if v else None,
414 "vram_in_use_mb": v[2] if v else None, "shared_in_use_mb": v[4] if v else None,
415 "waiting_objects": w[1] if w else None, "reaper_queue": w[2] if w else None})
416 return rows
417
418
419def write_series(run, folder):
420 """Writes memory.csv beside summary.json when the run has memory lines; returns its name or None. Without them
421 it removes a memory.csv already there (a folder summarized again), so no old rows sit beside `series: null`.
422 A file it cannot write or remove (open in another program) is said on stderr: the summary is written anyway."""
423 path = os.path.join(folder, SERIES)
424 rows = memory_rows(run)
425 try:
426 if not rows:
427 if os.path.exists(path):
428 os.remove(path)
429 return None
430 with open(path, "w", encoding="utf-8", newline="") as f:
431 out = csv.DictWriter(f, fieldnames=SERIES_COLUMNS, lineterminator="\n")
432 out.writeheader()
433 out.writerows(rows)
434 except OSError as e:
435 print(f"{SERIES} not written or removed: {e.__class__.__name__}: {e.strerror or e}", file=sys.stderr)
436 return None
437 return SERIES
438
439
440def memory_block(run, world=None, end=None):
441 """Peaks of the runtime's memory lines over the whole run (none from a build without diagnostics), the
442 reaper's queue at its longest, and how fast the private bytes grew in the world up to `end` (a pile-up's
443 slope; the lines written while the title ends after the stop request do not count)."""
339 out = {"source": None, "lines": 0, "private_max_mb": None, "private_max_at_s": None, "in_memory_max_mb": None, 444 out = {"source": None, "lines": 0, "private_max_mb": None, "private_max_at_s": None, "in_memory_max_mb": None,
340 "pc_commit_left_min_mb": None, "pc_commit_left_min_at_s": None, "vram_in_use_max_mb": None, 445 "pc_commit_left_min_mb": None, "pc_commit_left_min_at_s": None, "vram_in_use_max_mb": None,
341 "vram_max_at_s": None, "vram_budget_at_max_mb": None, "vram_over_budget_s": None, 446 "vram_max_at_s": None, "vram_budget_at_max_mb": None, "vram_over_budget_s": None,
342 "shared_in_use_max_mb": None} 447 "shared_in_use_max_mb": None, "reaper_queue_max": None, "reaper_queue_max_at_s": None,
448 "waiting_objects_max": None, "private_mb_per_min_in_world": None, "series": None}
449 if run.waits:
450 t, queue = max(((w[0], w[2]) for w in run.waits), key=lambda x: x[1])
451 out["reaper_queue_max"], out["reaper_queue_max_at_s"] = queue, round(t, 3)
452 out["waiting_objects_max"] = max(w[1] for w in run.waits)
453 if run.memory and world is not None:
454 slope = slope_per_min([(m[0], m[1]) for m in run.memory if world <= m[0] and (end is None or m[0] <= end)])
455 out["private_mb_per_min_in_world"] = round(slope, 1) if slope is not None else None
343 if run.memory: 456 if run.memory:
457 out["series"] = SERIES
344 out["source"], out["lines"] = "memory lines", len(run.memory) 458 out["source"], out["lines"] = "memory lines", len(run.memory)
345 t, private = max(((t, p) for t, p, *_ in run.memory), key=lambda x: x[1]) 459 t, private = max(((t, p) for t, p, *_ in run.memory), key=lambda x: x[1])
346 out["private_max_mb"], out["private_max_at_s"] = private, round(t, 3) 460 out["private_max_mb"], out["private_max_at_s"] = private, round(t, 3)
@@ -394,13 +508,16 @@ def summary(run):
394 fails.append("exited_before_stop") 508 fails.append("exited_before_stop")
395 if fps["longest_present_gap_s"] is not None and fps["longest_present_gap_s"] > GAP_FAIL_S: 509 if fps["longest_present_gap_s"] is not None and fps["longest_present_gap_s"] > GAP_FAIL_S:
396 fails.append("present_gap") 510 fails.append("present_gap")
511 if reason == "memory":
512 # Stopped before memory ran out: the run was on its way to a crash, not short by choice.
513 fails.append("memory_stop")
397 if run.relaunched: 514 if run.relaunched:
398 errors.append("relaunched") 515 errors.append("relaunched")
399 if run.event("game") is None and not run.frames: 516 if run.event("game") is None and not run.frames:
400 errors.append("not_started") 517 errors.append("not_started")
401 elif world is None: 518 elif world is None and reason != "memory":
402 (errors if not exited_before_stop else fails).append("world_not_reached") 519 (errors if not exited_before_stop else fails).append("world_not_reached")
403 elif world_s < min_world: 520 elif world_s < min_world and reason != "memory":
404 (errors if reason == "stop_file" else fails).append("stopped_early" if reason == "stop_file" else "world_time_short") 521 (errors if reason == "stop_file" else fails).append("stopped_early" if reason == "stop_file" else "world_time_short")
405 if stop is not None and not alive and not fails and not errors: 522 if stop is not None and not alive and not fails and not errors:
406 fails.append("not_alive_at_stop") 523 fails.append("not_alive_at_stop")
@@ -436,7 +553,7 @@ def summary(run):
436 }, 553 },
437 "fps": fps, 554 "fps": fps,
438 "pipelines": {"missed_waits": len(run.missed), "making_max": max((m for *_, m in run.frames), default=0)}, 555 "pipelines": {"missed_waits": len(run.missed), "making_max": max((m for *_, m in run.frames), default=0)},
439 "memory": memory_block(run), 556 "memory": dict(memory_block(run, world, end), stop_limits=run.meta.get("memory_stop")),
440 "crash": { 557 "crash": {
441 "kind": crash_kind, 558 "kind": crash_kind,
442 "at_s": round(first["at_s"], 3) if first and first["at_s"] is not None else None, 559 "at_s": round(first["at_s"], 3) if first and first["at_s"] is not None else None,
@@ -450,7 +567,7 @@ def summary(run):
450 "kind": ("device_lost" if code is not None and code & 0xFFFFFFFF == DEVICE_LOST else 567 "kind": ("device_lost" if code is not None and code & 0xFFFFFFFF == DEVICE_LOST else
451 "killed" if killed else "clean" if code == 0 else "other" if code is not None else None)}, 568 "killed" if killed else "clean" if code == 0 else "other" if code is not None else None)},
452 "stop": {"reason": reason, "killed": killed, "cache_saved": run.ended_at is not None if stop is not None else None, 569 "stop": {"reason": reason, "killed": killed, "cache_saved": run.ended_at is not None if stop is not None else None,
453 "suspend": run.suspend}, 570 "suspend": run.suspend, "memory_why": (run.event("stop_requested") or {}).get("why")},
454 "relaunched": run.relaunched, 571 "relaunched": run.relaunched,
455 "verdict": {"result": result, "reasons": fails + errors, "warnings": warnings, "min_world_s": min_world}, 572 "verdict": {"result": result, "reasons": fails + errors, "warnings": warnings, "min_world_s": min_world},
456 } 573 }
@@ -465,6 +582,8 @@ def main(argv):
465 print(json.dumps(state(run))) 582 print(json.dumps(state(run)))
466 return 0 583 return 0
467 out = summary(run) 584 out = summary(run)
585 # `series` names the file only when it was written.
586 out["memory"]["series"] = write_series(run, argv[2])
468 with open(os.path.join(argv[2], "summary.json"), "w", encoding="utf-8", newline="\n") as f: 587 with open(os.path.join(argv[2], "summary.json"), "w", encoding="utf-8", newline="\n") as f:
469 json.dump(out, f, indent=2) 588 json.dump(out, f, indent=2)
470 f.write("\n") 589 f.write("\n")