From aa75f13197627391579e7c282b353cfa85f466ff Mon Sep 17 00:00:00 2001 From: TheGeogeo <57634077+TheGeogeo@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:39:51 +0100 Subject: gate: a memory stop ends a run before memory runs out (D3D12-GATE-MEMORY-STOP) A memory pile-up has taken a PC's whole Windows down, not only the device, and a fixed 80 GB cap never triggers on a PC with a 40 GB commit limit. The supervisor now asks `game_gate.py state` for a memory stop, read from the runtime's latest memory lines: the game's private bytes past -StopPrivateMB (default 90 % of this PC's commit limit), the PC under -StopCommitLeftMB left to commit for 20 s (default the larger of 128 MB and 0.25 % of the limit), or video memory over the system's budget for -StopOverBudgetSeconds (60). 0 turns one off. The run then stops the same clean way, with the reason `memory`; the limits go into run.json and the summary, the limit passed into `stop.memory_why`, and the verdict is FAIL `memory_stop`. `start` forwards the limits to the detached run. --- scripts/game-gate.ps1 | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'scripts/game-gate.ps1') diff --git a/scripts/game-gate.ps1 b/scripts/game-gate.ps1 index 6ab0c11a9..1192cd096 100644 --- a/scripts/game-gate.ps1 +++ b/scripts/game-gate.ps1 @@ -15,6 +15,10 @@ # saved) when -WorldSeconds were spent in the world, when a STOP file appears in the run folder, or after # -MaxMinutes; it kills the game only if it has not ended 60 s later. Exit code: 0 PASS, 1 FAIL, 2 ERROR. # Options: -WorldSeconds (300), -MaxMinutes (45), -More 'LEONIDA_X=1;LEONIDA_Y=2', -PlayArgs '-SeriesS -NoFrameGen'. +# Memory stop (from the runtime's memory lines, every 5 s, builds with diagnostics): the run is stopped the same +# clean way, with the reason `memory`, when the game's private bytes pass -StopPrivateMB (90 % of this PC's commit +# limit), when the PC has less than -StopCommitLeftMB left to commit for 20 s (the larger of 128 MB and 0.25 % of +# the limit), or when video memory stays over the system's budget for -StopOverBudgetSeconds (60). 0 turns one off. param( [Parameter(Mandatory, Position = 0)][ValidateSet('run', 'start', 'stop')][string]$Verb, [string]$Game = '', @@ -27,7 +31,10 @@ param( [int]$MaxMinutes = 45, [string]$More = '', [string]$PlayArgs = '', - [string]$Python = '' + [string]$Python = '', + [int]$StopPrivateMB = -1, + [int]$StopCommitLeftMB = -1, + [int]$StopOverBudgetSeconds = 60 ) $ErrorActionPreference = 'Stop' $repo = Split-Path -Parent $PSScriptRoot @@ -96,7 +103,8 @@ if ($Verb -eq 'start') { $line = @(& $quote $pwsh; '-NoProfile'; '-ExecutionPolicy'; 'Bypass'; '-File'; & $quote $PSCommandPath; 'run' '-Game'; & $quote $Game; '-Data'; & $quote $Data; '-DataTemplate'; & $quote $DataTemplate; '-Name'; & $quote $Name '-RunRoot'; & $quote $RunRoot; '-RunDir'; & $quote $RunDir; '-WorldSeconds'; $WorldSeconds; '-MaxMinutes'; $MaxMinutes - '-More'; & $quote $More; '-PlayArgs'; & $quote $PlayArgs; '-Python'; & $quote $Python) -join ' ' + '-More'; & $quote $More; '-PlayArgs'; & $quote $PlayArgs; '-Python'; & $quote $Python + '-StopPrivateMB'; $StopPrivateMB; '-StopCommitLeftMB'; $StopCommitLeftMB; '-StopOverBudgetSeconds'; $StopOverBudgetSeconds) -join ' ' # Created by Windows, not as a child of this shell: an agent's or a console's end does not end the run. $startup = New-CimInstance -ClassName Win32_ProcessStartup -ClientOnly -Property @{ ShowWindow = [uint16]0 } $made = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{ @@ -140,6 +148,11 @@ $moreText & '$play' -Game '$Game' -Data '$Data' -More `$settings $PlayArgs @args "@ +# The memory stop's limits, from this PC's commit limit unless given (0 turns one off). On a PC with no page file a +# normal run can leave a few hundred MB to commit: the floor is low and must hold for 20 s. +$commitLimitMB = [math]::Floor((Get-CimInstance Win32_OperatingSystem).TotalVirtualMemorySize / 1KB) +if ($StopPrivateMB -lt 0) { $StopPrivateMB = [int]($commitLimitMB * 0.9) } +if ($StopCommitLeftMB -lt 0) { $StopCommitLeftMB = [int][math]::Max(128, $commitLimitMB * 0.0025) } $gpu = Get-CimInstance Win32_VideoController | Sort-Object { [uint64]$_.AdapterRAM } -Descending | Select-Object -First 1 [ordered]@{ name = $Name @@ -155,6 +168,7 @@ $gpu = Get-CimInstance Win32_VideoController | Sort-Object { [uint64]$_.AdapterR world_seconds = $WorldSeconds max_minutes = $MaxMinutes f1_settings_file = $hadMenuFile + memory_stop = [ordered]@{ private_max_mb = $StopPrivateMB; commit_left_min_mb = $StopCommitLeftMB; over_budget_s = $StopOverBudgetSeconds } } | ConvertTo-Json | Set-Content (Join-Path $RunDir 'run.json') & pwsh -NoProfile -ExecutionPolicy Bypass -File $launch -DryRun *> (Join-Path $RunDir 'settings.txt') @@ -196,6 +210,7 @@ public static class GateWindow { $began = Get-Date $title = $null # the game process ($Game, the same name in any case, is its folder) $stopReason = $null +$memoryWhy = $null try { while ($true) { Start-Sleep -Seconds 5 @@ -211,6 +226,7 @@ while ($true) { if (-not $title -and $launcher.HasExited) { break } $state = & $Python -I $tool state $RunDir | ConvertFrom-Json if ($state.relaunched) { $stopReason = 'relaunched' } + elseif ($state.memory_stop) { $stopReason = 'memory'; $memoryWhy = $state.memory_stop } elseif (Test-Path (Join-Path $RunDir 'STOP')) { $stopReason = 'stop_file' } elseif ($state.world_s -ge $WorldSeconds) { $stopReason = 'target' } elseif (((Get-Date) - $began).TotalMinutes -ge $MaxMinutes) { $stopReason = 'timeout' } @@ -218,7 +234,7 @@ while ($true) { } if ($stopReason -and $title -and -not $title.HasExited) { - Add-Event 'stop_requested' @{ reason = $stopReason } + Add-Event 'stop_requested' @{ reason = $stopReason; why = $memoryWhy } if ([GateWindow]::Close($title.Id)) { Add-Event 'close_sent' } if (-not $title.WaitForExit(60000)) { Stop-Process -Id $title.Id -Force @@ -226,7 +242,7 @@ if ($stopReason -and $title -and -not $title.HasExited) { $title.WaitForExit() } } elseif ($stopReason) { - Add-Event 'stop_requested' @{ reason = $stopReason } + Add-Event 'stop_requested' @{ reason = $stopReason; why = $memoryWhy } } if ($title) { Add-Event 'game_exited' @{ exit_code = $title.ExitCode } } if (-not $launcher.WaitForExit(30000)) { Stop-Process -Id $launcher.Id -Force } -- cgit v1.2.3 From cb5bcfc2058e44cc4dea3197d6cf18d855f173ff Mon Sep 17 00:00:00 2001 From: TheGeogeo <57634077+TheGeogeo@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:42:44 +0100 Subject: gate: the private-bytes stop also follows the PC's memory (review) With a large page file the commit limit is far above what a PC survives: two Windows crashes (0x10E) came at about 100 GB private on a PC with 64 GB of memory and a 175 GB limit, where 90 % of the limit is 158 GB. The default is now the lower of 90 % of the commit limit and 1.25 x the physical memory (80 GB there, 36 GB here); the video memory stop stays on by default. --- docs/game_gate.md | 2 +- scripts/game-gate.ps1 | 12 ++++++++---- tests/test_game_gate.py | 3 +++ 3 files changed, 12 insertions(+), 5 deletions(-) (limited to 'scripts/game-gate.ps1') diff --git a/docs/game_gate.md b/docs/game_gate.md index 1178a5dc2..2d5677e1e 100644 --- a/docs/game_gate.md +++ b/docs/game_gate.md @@ -29,7 +29,7 @@ pwsh -NoProfile -File scripts\game-gate.ps1 stop -RunDir - **The caches.** `-Data` keeps its caches (`cache-2`); only the saves are copied. The first start after a translator change translates every shader again: run once before measuring. - **Ending the run.** It stops when the time in the world reaches `-WorldSeconds`, when a `STOP` file appears in the run folder (`stop`), or after `-MaxMinutes` (45). - **Memory stop.** It also stops the run, the same clean way and with the reason `memory`, before memory runs out. A memory pile-up has taken a PC's whole Windows down, not only the device. It reads the runtime's memory lines (every 5 s, builds with diagnostics) and stops when: - - the game's private bytes pass `-StopPrivateMB` (default: 90 % of this PC's commit limit); + - the game's private bytes pass `-StopPrivateMB` (default: the lower of 90 % of this PC's commit limit and 1.25 times its physical memory; with a large page file the limit is far above what a PC survives, and two Windows crashes came at about 100 GB private on a PC with 64 GB); - the PC has less than `-StopCommitLeftMB` left to commit for 20 s (default: the larger of 128 MB and 0.25 % of the limit; on a PC with no page file a normal run can leave a few hundred MB); - video memory stays over the system's budget for `-StopOverBudgetSeconds` (60). diff --git a/scripts/game-gate.ps1 b/scripts/game-gate.ps1 index 1192cd096..b2a3c88c1 100644 --- a/scripts/game-gate.ps1 +++ b/scripts/game-gate.ps1 @@ -16,9 +16,10 @@ # -MaxMinutes; it kills the game only if it has not ended 60 s later. Exit code: 0 PASS, 1 FAIL, 2 ERROR. # Options: -WorldSeconds (300), -MaxMinutes (45), -More 'LEONIDA_X=1;LEONIDA_Y=2', -PlayArgs '-SeriesS -NoFrameGen'. # Memory stop (from the runtime's memory lines, every 5 s, builds with diagnostics): the run is stopped the same -# clean way, with the reason `memory`, when the game's private bytes pass -StopPrivateMB (90 % of this PC's commit -# limit), when the PC has less than -StopCommitLeftMB left to commit for 20 s (the larger of 128 MB and 0.25 % of -# the limit), or when video memory stays over the system's budget for -StopOverBudgetSeconds (60). 0 turns one off. +# clean way, with the reason `memory`, when the game's private bytes pass -StopPrivateMB (the lower of 90 % of this +# PC's commit limit and 1.25 x its memory), when the PC has less than -StopCommitLeftMB left to commit for 20 s +# (the larger of 128 MB and 0.25 % of the limit), or when video memory stays over the system's budget for +# -StopOverBudgetSeconds (60). 0 turns one off. param( [Parameter(Mandatory, Position = 0)][ValidateSet('run', 'start', 'stop')][string]$Verb, [string]$Game = '', @@ -151,7 +152,10 @@ $moreText # The memory stop's limits, from this PC's commit limit unless given (0 turns one off). On a PC with no page file a # normal run can leave a few hundred MB to commit: the floor is low and must hold for 20 s. $commitLimitMB = [math]::Floor((Get-CimInstance Win32_OperatingSystem).TotalVirtualMemorySize / 1KB) -if ($StopPrivateMB -lt 0) { $StopPrivateMB = [int]($commitLimitMB * 0.9) } +$physicalMB = [math]::Floor((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1MB) +# With a large page file the commit limit is far above what the PC survives: two Windows crashes (0x10E) came at +# about 100 GB private on a PC with 64 GB of memory and a 175 GB limit. So also no more than 1.25 x the memory. +if ($StopPrivateMB -lt 0) { $StopPrivateMB = [int][math]::Min($commitLimitMB * 0.9, $physicalMB * 1.25) } if ($StopCommitLeftMB -lt 0) { $StopCommitLeftMB = [int][math]::Max(128, $commitLimitMB * 0.0025) } $gpu = Get-CimInstance Win32_VideoController | Sort-Object { [uint64]$_.AdapterRAM } -Descending | Select-Object -First 1 [ordered]@{ diff --git a/tests/test_game_gate.py b/tests/test_game_gate.py index c73c2485d..8bba0d756 100644 --- a/tests/test_game_gate.py +++ b/tests/test_game_gate.py @@ -381,6 +381,9 @@ class Script(unittest.TestCase): # `start` rebuilds the command line of the detached `run`: a limit left out would be lost there. self.assertIn(f"'-{name}'; ${name}", text) self.assertIn("memory_stop = [ordered]@{", text) + # The private default follows the PC's memory too, not only its commit limit (a large page file). + self.assertIn("TotalPhysicalMemory", text) + self.assertIn("[math]::Min($commitLimitMB * 0.9, $physicalMB * 1.25)", text) self.assertIn("$stopReason = 'memory'", text) def test_no_variable_takes_a_parameters_name_in_another_case(self): -- cgit v1.2.3 From eac2b61d2f012c764c08eacda411ef76a5581d88 Mon Sep 17 00:00:00 2001 From: TheGeogeo <57634077+TheGeogeo@users.noreply.github.com> Date: Wed, 2 Dec 2026 02:52:41 +0100 Subject: gate: the world time comes before the memory stop, the commit floor tested at 20 s (review) A run that reached -WorldSeconds at the same poll as a memory limit was reported FAIL `memory_stop`. It is a measurement, and its memory peaks are in the summary all the same, so the supervisor now checks the target first. The poll's order is one function, Get-StopReason, which the test takes out of the script and puts to PowerShell 7 with synthetic polls. The commit floor's boundary is now tested at exactly 20 s, and the docs say what `stop.memory_why` holds: the gate's whole sentence. --- docs/game_gate.md | 4 ++-- scripts/game-gate.ps1 | 20 ++++++++++++++------ tests/test_game_gate.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 9 deletions(-) (limited to 'scripts/game-gate.ps1') diff --git a/docs/game_gate.md b/docs/game_gate.md index 2d5677e1e..9f0722cfc 100644 --- a/docs/game_gate.md +++ b/docs/game_gate.md @@ -33,7 +33,7 @@ pwsh -NoProfile -File scripts\game-gate.ps1 stop -RunDir - the PC has less than `-StopCommitLeftMB` left to commit for 20 s (default: the larger of 128 MB and 0.25 % of the limit; on a PC with no page file a normal run can leave a few hundred MB); - video memory stays over the system's budget for `-StopOverBudgetSeconds` (60). - 0 turns a limit off. The limits go into `run.json` (`memory_stop`) and the summary (`memory.stop_limits`). + 0 turns a limit off. The limits go into `run.json` (`memory_stop`) and the summary (`memory.stop_limits`). The gate checks every 5 s, and the world time comes first: a run that reaches `-WorldSeconds` at the same check as a limit stops with `target`. It is a measurement, and its memory peaks are in the summary all the same. - **How it stops.** With `WM_SYSCOMMAND`/`SC_CLOSE`, the runtime's own end: the title is suspended and the pipeline cache saved. It kills the game only if it has not ended 60 s later. - **Exit code.** 0 PASS, 1 FAIL, 2 ERROR. `start` exits when the detached run is created. @@ -68,7 +68,7 @@ Times are seconds since the run started. The summary holds numbers, switch names | `memory` | Peaks over the whole run, from the runtime's memory line and video memory line (every 5 s; builds with diagnostics only, otherwise `source` is null): the process's private bytes and when (`private_max_mb`, `private_max_at_s`), the most of them in physical memory, the least the PC had left to commit and when, the card's video memory in use at its most and when, with the budget the system gave then (`vram_in_use_max_mb`, `vram_max_at_s`, `vram_budget_at_max_mb`), the seconds spent over that budget, and the most of the PC's memory shared with the card in use. The video memory is what the system counts for this process, less than a tool like `nvidia-smi` shows for the whole card | | `crash` | `kind` is `none`, `device_lost`, `panic` or `fault`. It also gives the time, the code, the module kind (`runtime`, `title`, `driver`, `system`, `unknown`) and the access (read/write). `title_watchdog` marks a write at address 0 in the title more than 25 s after the last frame line. `count` is the number of crashes | | `exit` | The game's exit code, and its kind: `clean`, `device_lost` (0x887A0005), `killed` or `other` | -| `stop` | Why it was stopped (`target`, `stop_file`, `timeout`, `relaunched`, `memory` with the limit passed in `memory_why`), whether it was killed, whether the cache was saved, whether the suspend was in time | +| `stop` | Why it was stopped (`target`, `stop_file`, `timeout`, `relaunched` or `memory`), whether it was killed, whether the cache was saved, whether the suspend was in time. After a memory stop, `memory_why` is the gate's whole sentence for it, with its numbers: `private bytes 31000 MB, over 30000 MB`, `the PC had under 200 MB left to commit for 25 s` or `video memory over its budget for 60 s`, for example. It is null for the other reasons | | `verdict` | `result`, `reasons`, `warnings`, `min_world_s` | ## The verdict diff --git a/scripts/game-gate.ps1 b/scripts/game-gate.ps1 index b2a3c88c1..1a153a8ae 100644 --- a/scripts/game-gate.ps1 +++ b/scripts/game-gate.ps1 @@ -19,7 +19,7 @@ # clean way, with the reason `memory`, when the game's private bytes pass -StopPrivateMB (the lower of 90 % of this # PC's commit limit and 1.25 x its memory), when the PC has less than -StopCommitLeftMB left to commit for 20 s # (the larger of 128 MB and 0.25 % of the limit), or when video memory stays over the system's budget for -# -StopOverBudgetSeconds (60). 0 turns one off. +# -StopOverBudgetSeconds (60). 0 turns one off. A run that reaches -WorldSeconds at the same poll stops with `target`. param( [Parameter(Mandatory, Position = 0)][ValidateSet('run', 'start', 'stop')][string]$Verb, [string]$Game = '', @@ -211,6 +211,16 @@ public static class GateWindow { '@ } +# Why this poll stops the run, or nothing. The target comes before the memory stop: a run that reached its world time +# in the same poll as a memory limit is a measurement, and its memory peaks are in the summary all the same. +function Get-StopReason($Poll, [bool]$StopFile, [double]$Minutes, [int]$TargetSeconds, [int]$LimitMinutes) { + if ($Poll.relaunched) { 'relaunched' } + elseif ($Poll.world_s -ge $TargetSeconds) { 'target' } + elseif ($Poll.memory_stop) { 'memory' } + elseif ($StopFile) { 'stop_file' } + elseif ($Minutes -ge $LimitMinutes) { 'timeout' } +} + $began = Get-Date $title = $null # the game process ($Game, the same name in any case, is its folder) $stopReason = $null @@ -229,11 +239,9 @@ while ($true) { if ($title -and $title.HasExited) { break } if (-not $title -and $launcher.HasExited) { break } $state = & $Python -I $tool state $RunDir | ConvertFrom-Json - if ($state.relaunched) { $stopReason = 'relaunched' } - elseif ($state.memory_stop) { $stopReason = 'memory'; $memoryWhy = $state.memory_stop } - elseif (Test-Path (Join-Path $RunDir 'STOP')) { $stopReason = 'stop_file' } - elseif ($state.world_s -ge $WorldSeconds) { $stopReason = 'target' } - elseif (((Get-Date) - $began).TotalMinutes -ge $MaxMinutes) { $stopReason = 'timeout' } + $stopReason = Get-StopReason -Poll $state -StopFile (Test-Path (Join-Path $RunDir 'STOP')) ` + -Minutes ((Get-Date) - $began).TotalMinutes -TargetSeconds $WorldSeconds -LimitMinutes $MaxMinutes + if ($stopReason -eq 'memory') { $memoryWhy = $state.memory_stop } if ($stopReason) { break } } diff --git a/tests/test_game_gate.py b/tests/test_game_gate.py index 8bba0d756..bae6302bf 100644 --- a/tests/test_game_gate.py +++ b/tests/test_game_gate.py @@ -257,6 +257,9 @@ class Measures(unittest.TestCase): cases = { "private bytes 31000 MB, over 30000 MB": calm + [(31000, 7000, 3000)], "the PC had under 200 MB left to commit for 25 s": calm + [(24000, 7000, 150)] * 6, + # Exactly 20 s under the floor (five lines 5 s apart) is enough: the floor's boundary, as 60 s is the + # video memory's. + "the PC had under 200 MB left to commit for 20 s": calm + [(24000, 7000, 150)] * 5, "video memory over its budget for 60 s": calm + [(24000, 9000, 3000)] * 13, } for why, rows in cases.items(): @@ -384,7 +387,49 @@ class Script(unittest.TestCase): # The private default follows the PC's memory too, not only its commit limit (a large page file). self.assertIn("TotalPhysicalMemory", text) self.assertIn("[math]::Min($commitLimitMB * 0.9, $physicalMB * 1.25)", text) - self.assertIn("$stopReason = 'memory'", text) + + # What one poll of the supervisor reads (`game_gate.py state`, a STOP file, the minutes since the start) and the + # reason it stops the run for, with -WorldSeconds 300 and -MaxMinutes 45. + POLLS = [ + # The world time reached in the same poll as a memory limit: a measurement, not a memory stop. + ({"relaunched": False, "world_s": 300.0, "memory_stop": "private bytes 31000 MB, over 30000 MB"}, False, 20, + "target"), + ({"relaunched": False, "world_s": 295.0, "memory_stop": "private bytes 31000 MB, over 30000 MB"}, False, 20, + "memory"), + ({"relaunched": True, "world_s": 300.0, "memory_stop": "private bytes 31000 MB, over 30000 MB"}, False, 20, + "relaunched"), + ({"relaunched": False, "world_s": 100.0, "memory_stop": "video memory over its budget for 60 s"}, True, 20, + "memory"), + ({"relaunched": False, "world_s": 100.0, "memory_stop": None}, True, 20, "stop_file"), + ({"relaunched": False, "world_s": 0.0, "memory_stop": None}, False, 45, "timeout"), + ({"relaunched": False, "world_s": 100.0, "memory_stop": None}, False, 20, "none"), + ] + + def test_a_poll_stops_for_the_target_before_the_memory_stop(self): + # The supervisor's own function is taken out of the script and put to PowerShell 7 with each poll above. + with open(os.path.join(ROOT, "scripts", "game-gate.ps1"), encoding="utf-8") as f: + text = f.read() + self.assertIn("$stopReason = Get-StopReason -Poll $state ", text) + self.assertEqual(text.count("$stopReason = "), 2) # set to $null before the loop, then only from the function + pwsh = shutil.which("pwsh") + if not pwsh: + self.skipTest("no PowerShell 7 here") + script = ("$ErrorActionPreference = 'Stop'; " + "$language = 'System.Management.Automation.Language'; " + "$ast = ($language + '.Parser' -as [type])::ParseFile($env:GATE_SCRIPT, [ref]$null, [ref]$null); " + "$fn = $ast.Find({ $args[0].GetType().Name -eq 'FunctionDefinitionAst' -and " + "$args[0].Name -eq 'Get-StopReason' }, $true); " + "if (-not $fn) { throw 'the gate has no Get-StopReason' }; " + ". ([scriptblock]::Create($fn.Extent.Text)); " + "foreach ($p in ($env:GATE_POLLS | ConvertFrom-Json)) " + "{ $r = Get-StopReason -Poll $p[0] -StopFile $p[1] -Minutes $p[2] -TargetSeconds 300 " + "-LimitMinutes 45; if ($r) { $r } else { 'none' } }") + env = dict(os.environ, GATE_SCRIPT=os.path.join(ROOT, "scripts", "game-gate.ps1"), + GATE_POLLS=json.dumps([p[:3] for p in self.POLLS])) + done = subprocess.run([pwsh, "-NoProfile", "-NonInteractive", "-Command", script], capture_output=True, + text=True, timeout=120, env=env) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertEqual(done.stdout.split(), [p[3] for p in self.POLLS]) def test_no_variable_takes_a_parameters_name_in_another_case(self): # PowerShell names ignore case: a `$game` process held in the `[string]$Game` parameter became its name, -- cgit v1.2.3