diff options
Diffstat (limited to 'scripts/game-gate.ps1')
| -rw-r--r-- | scripts/game-gate.ps1 | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/scripts/game-gate.ps1 b/scripts/game-gate.ps1 index 6ab0c11a9..1a153a8ae 100644 --- a/scripts/game-gate.ps1 +++ b/scripts/game-gate.ps1 | |||
| @@ -15,6 +15,11 @@ | |||
| 15 | # saved) when -WorldSeconds were spent in the world, when a STOP file appears in the run folder, or after | 15 | # saved) when -WorldSeconds were spent in the world, when a STOP file appears in the run folder, or after |
| 16 | # -MaxMinutes; it kills the game only if it has not ended 60 s later. Exit code: 0 PASS, 1 FAIL, 2 ERROR. | 16 | # -MaxMinutes; it kills the game only if it has not ended 60 s later. Exit code: 0 PASS, 1 FAIL, 2 ERROR. |
| 17 | # Options: -WorldSeconds (300), -MaxMinutes (45), -More 'LEONIDA_X=1;LEONIDA_Y=2', -PlayArgs '-SeriesS -NoFrameGen'. | 17 | # Options: -WorldSeconds (300), -MaxMinutes (45), -More 'LEONIDA_X=1;LEONIDA_Y=2', -PlayArgs '-SeriesS -NoFrameGen'. |
| 18 | # Memory stop (from the runtime's memory lines, every 5 s, builds with diagnostics): the run is stopped the same | ||
| 19 | # clean way, with the reason `memory`, when the game's private bytes pass -StopPrivateMB (the lower of 90 % of this | ||
| 20 | # PC's commit limit and 1.25 x its memory), when the PC has less than -StopCommitLeftMB left to commit for 20 s | ||
| 21 | # (the larger of 128 MB and 0.25 % of the limit), or when video memory stays over the system's budget for | ||
| 22 | # -StopOverBudgetSeconds (60). 0 turns one off. A run that reaches -WorldSeconds at the same poll stops with `target`. | ||
| 18 | param( | 23 | param( |
| 19 | [Parameter(Mandatory, Position = 0)][ValidateSet('run', 'start', 'stop')][string]$Verb, | 24 | [Parameter(Mandatory, Position = 0)][ValidateSet('run', 'start', 'stop')][string]$Verb, |
| 20 | [string]$Game = '', | 25 | [string]$Game = '', |
| @@ -27,7 +32,10 @@ param( | |||
| 27 | [int]$MaxMinutes = 45, | 32 | [int]$MaxMinutes = 45, |
| 28 | [string]$More = '', | 33 | [string]$More = '', |
| 29 | [string]$PlayArgs = '', | 34 | [string]$PlayArgs = '', |
| 30 | [string]$Python = '' | 35 | [string]$Python = '', |
| 36 | [int]$StopPrivateMB = -1, | ||
| 37 | [int]$StopCommitLeftMB = -1, | ||
| 38 | [int]$StopOverBudgetSeconds = 60 | ||
| 31 | ) | 39 | ) |
| 32 | $ErrorActionPreference = 'Stop' | 40 | $ErrorActionPreference = 'Stop' |
| 33 | $repo = Split-Path -Parent $PSScriptRoot | 41 | $repo = Split-Path -Parent $PSScriptRoot |
| @@ -96,7 +104,8 @@ if ($Verb -eq 'start') { | |||
| 96 | $line = @(& $quote $pwsh; '-NoProfile'; '-ExecutionPolicy'; 'Bypass'; '-File'; & $quote $PSCommandPath; 'run' | 104 | $line = @(& $quote $pwsh; '-NoProfile'; '-ExecutionPolicy'; 'Bypass'; '-File'; & $quote $PSCommandPath; 'run' |
| 97 | '-Game'; & $quote $Game; '-Data'; & $quote $Data; '-DataTemplate'; & $quote $DataTemplate; '-Name'; & $quote $Name | 105 | '-Game'; & $quote $Game; '-Data'; & $quote $Data; '-DataTemplate'; & $quote $DataTemplate; '-Name'; & $quote $Name |
| 98 | '-RunRoot'; & $quote $RunRoot; '-RunDir'; & $quote $RunDir; '-WorldSeconds'; $WorldSeconds; '-MaxMinutes'; $MaxMinutes | 106 | '-RunRoot'; & $quote $RunRoot; '-RunDir'; & $quote $RunDir; '-WorldSeconds'; $WorldSeconds; '-MaxMinutes'; $MaxMinutes |
| 99 | '-More'; & $quote $More; '-PlayArgs'; & $quote $PlayArgs; '-Python'; & $quote $Python) -join ' ' | 107 | '-More'; & $quote $More; '-PlayArgs'; & $quote $PlayArgs; '-Python'; & $quote $Python |
| 108 | '-StopPrivateMB'; $StopPrivateMB; '-StopCommitLeftMB'; $StopCommitLeftMB; '-StopOverBudgetSeconds'; $StopOverBudgetSeconds) -join ' ' | ||
| 100 | # Created by Windows, not as a child of this shell: an agent's or a console's end does not end the run. | 109 | # Created by Windows, not as a child of this shell: an agent's or a console's end does not end the run. |
| 101 | $startup = New-CimInstance -ClassName Win32_ProcessStartup -ClientOnly -Property @{ ShowWindow = [uint16]0 } | 110 | $startup = New-CimInstance -ClassName Win32_ProcessStartup -ClientOnly -Property @{ ShowWindow = [uint16]0 } |
| 102 | $made = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{ | 111 | $made = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{ |
| @@ -140,6 +149,14 @@ $moreText | |||
| 140 | & '$play' -Game '$Game' -Data '$Data' -More `$settings $PlayArgs @args | 149 | & '$play' -Game '$Game' -Data '$Data' -More `$settings $PlayArgs @args |
| 141 | "@ | 150 | "@ |
| 142 | 151 | ||
| 152 | # 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 | ||
| 153 | # normal run can leave a few hundred MB to commit: the floor is low and must hold for 20 s. | ||
| 154 | $commitLimitMB = [math]::Floor((Get-CimInstance Win32_OperatingSystem).TotalVirtualMemorySize / 1KB) | ||
| 155 | $physicalMB = [math]::Floor((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1MB) | ||
| 156 | # With a large page file the commit limit is far above what the PC survives: two Windows crashes (0x10E) came at | ||
| 157 | # 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. | ||
| 158 | if ($StopPrivateMB -lt 0) { $StopPrivateMB = [int][math]::Min($commitLimitMB * 0.9, $physicalMB * 1.25) } | ||
| 159 | if ($StopCommitLeftMB -lt 0) { $StopCommitLeftMB = [int][math]::Max(128, $commitLimitMB * 0.0025) } | ||
| 143 | $gpu = Get-CimInstance Win32_VideoController | Sort-Object { [uint64]$_.AdapterRAM } -Descending | Select-Object -First 1 | 160 | $gpu = Get-CimInstance Win32_VideoController | Sort-Object { [uint64]$_.AdapterRAM } -Descending | Select-Object -First 1 |
| 144 | [ordered]@{ | 161 | [ordered]@{ |
| 145 | name = $Name | 162 | name = $Name |
| @@ -155,6 +172,7 @@ $gpu = Get-CimInstance Win32_VideoController | Sort-Object { [uint64]$_.AdapterR | |||
| 155 | world_seconds = $WorldSeconds | 172 | world_seconds = $WorldSeconds |
| 156 | max_minutes = $MaxMinutes | 173 | max_minutes = $MaxMinutes |
| 157 | f1_settings_file = $hadMenuFile | 174 | f1_settings_file = $hadMenuFile |
| 175 | memory_stop = [ordered]@{ private_max_mb = $StopPrivateMB; commit_left_min_mb = $StopCommitLeftMB; over_budget_s = $StopOverBudgetSeconds } | ||
| 158 | } | ConvertTo-Json | Set-Content (Join-Path $RunDir 'run.json') | 176 | } | ConvertTo-Json | Set-Content (Join-Path $RunDir 'run.json') |
| 159 | 177 | ||
| 160 | & pwsh -NoProfile -ExecutionPolicy Bypass -File $launch -DryRun *> (Join-Path $RunDir 'settings.txt') | 178 | & pwsh -NoProfile -ExecutionPolicy Bypass -File $launch -DryRun *> (Join-Path $RunDir 'settings.txt') |
| @@ -193,9 +211,20 @@ public static class GateWindow { | |||
| 193 | '@ | 211 | '@ |
| 194 | } | 212 | } |
| 195 | 213 | ||
| 214 | # Why this poll stops the run, or nothing. The target comes before the memory stop: a run that reached its world time | ||
| 215 | # in the same poll as a memory limit is a measurement, and its memory peaks are in the summary all the same. | ||
| 216 | function Get-StopReason($Poll, [bool]$StopFile, [double]$Minutes, [int]$TargetSeconds, [int]$LimitMinutes) { | ||
| 217 | if ($Poll.relaunched) { 'relaunched' } | ||
| 218 | elseif ($Poll.world_s -ge $TargetSeconds) { 'target' } | ||
| 219 | elseif ($Poll.memory_stop) { 'memory' } | ||
| 220 | elseif ($StopFile) { 'stop_file' } | ||
| 221 | elseif ($Minutes -ge $LimitMinutes) { 'timeout' } | ||
| 222 | } | ||
| 223 | |||
| 196 | $began = Get-Date | 224 | $began = Get-Date |
| 197 | $title = $null # the game process ($Game, the same name in any case, is its folder) | 225 | $title = $null # the game process ($Game, the same name in any case, is its folder) |
| 198 | $stopReason = $null | 226 | $stopReason = $null |
| 227 | $memoryWhy = $null | ||
| 199 | try { | 228 | try { |
| 200 | while ($true) { | 229 | while ($true) { |
| 201 | Start-Sleep -Seconds 5 | 230 | Start-Sleep -Seconds 5 |
| @@ -210,15 +239,14 @@ while ($true) { | |||
| 210 | if ($title -and $title.HasExited) { break } | 239 | if ($title -and $title.HasExited) { break } |
| 211 | if (-not $title -and $launcher.HasExited) { break } | 240 | if (-not $title -and $launcher.HasExited) { break } |
| 212 | $state = & $Python -I $tool state $RunDir | ConvertFrom-Json | 241 | $state = & $Python -I $tool state $RunDir | ConvertFrom-Json |
| 213 | if ($state.relaunched) { $stopReason = 'relaunched' } | 242 | $stopReason = Get-StopReason -Poll $state -StopFile (Test-Path (Join-Path $RunDir 'STOP')) ` |
| 214 | elseif (Test-Path (Join-Path $RunDir 'STOP')) { $stopReason = 'stop_file' } | 243 | -Minutes ((Get-Date) - $began).TotalMinutes -TargetSeconds $WorldSeconds -LimitMinutes $MaxMinutes |
| 215 | elseif ($state.world_s -ge $WorldSeconds) { $stopReason = 'target' } | 244 | if ($stopReason -eq 'memory') { $memoryWhy = $state.memory_stop } |
| 216 | elseif (((Get-Date) - $began).TotalMinutes -ge $MaxMinutes) { $stopReason = 'timeout' } | ||
| 217 | if ($stopReason) { break } | 245 | if ($stopReason) { break } |
| 218 | } | 246 | } |
| 219 | 247 | ||
| 220 | if ($stopReason -and $title -and -not $title.HasExited) { | 248 | if ($stopReason -and $title -and -not $title.HasExited) { |
| 221 | Add-Event 'stop_requested' @{ reason = $stopReason } | 249 | Add-Event 'stop_requested' @{ reason = $stopReason; why = $memoryWhy } |
| 222 | if ([GateWindow]::Close($title.Id)) { Add-Event 'close_sent' } | 250 | if ([GateWindow]::Close($title.Id)) { Add-Event 'close_sent' } |
| 223 | if (-not $title.WaitForExit(60000)) { | 251 | if (-not $title.WaitForExit(60000)) { |
| 224 | Stop-Process -Id $title.Id -Force | 252 | Stop-Process -Id $title.Id -Force |
| @@ -226,7 +254,7 @@ if ($stopReason -and $title -and -not $title.HasExited) { | |||
| 226 | $title.WaitForExit() | 254 | $title.WaitForExit() |
| 227 | } | 255 | } |
| 228 | } elseif ($stopReason) { | 256 | } elseif ($stopReason) { |
| 229 | Add-Event 'stop_requested' @{ reason = $stopReason } | 257 | Add-Event 'stop_requested' @{ reason = $stopReason; why = $memoryWhy } |
| 230 | } | 258 | } |
| 231 | if ($title) { Add-Event 'game_exited' @{ exit_code = $title.ExitCode } } | 259 | if ($title) { Add-Event 'game_exited' @{ exit_code = $title.ExitCode } } |
| 232 | if (-not $launcher.WaitForExit(30000)) { Stop-Process -Id $launcher.Id -Force } | 260 | if (-not $launcher.WaitForExit(30000)) { Stop-Process -Id $launcher.Id -Force } |