Skip to content
16 changes: 11 additions & 5 deletions docs/game_gate.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ pwsh -NoProfile -File scripts\game-gate.ps1 stop -RunDir <run folder>
- **The play script's switches.** `-PlayArgs` passes them on as they are, for example `-SeriesS -NoFrameGen`.
- **Reaching the world.** The gate does not drive the menus. Pass key presses with `LEONIDA_KEYS`; they are timed from the start of the process, so allow for the precompile.
- **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).
- **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). It checks every 5 s. When two reasons come at the same check, the first of these wins: the title relaunched, the world time (`target`), the memory stop (below), the `STOP` file, the time limit. So a `STOP` file and the world time at the same check give `target`.
- **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: 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).

0 turns a limit off. The limits go into `run.json` (`memory_stop`) and the summary (`memory.stop_limits`). 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.

Expand All @@ -44,6 +50,7 @@ The run folder (under `-RunRoot`, by default `<Data>\gate-runs`, refused inside
| `events.jsonl` | What the gate did and saw: the game found, the stop asked for and why, a kill, the exit code |
| `data\` | The run's copy of the template, and the runtime's `crashes.log` |
| `summary.json` | The result, below |
| `memory.csv` | One row per runtime memory line (every 5 s, builds with diagnostics): time, private bytes, of them in physical memory, the PC's memory free and left to commit, the card's video memory budget and in use, the shared memory in use, the objects let go that wait for their submissions, and the reaper thread's queue. Its header is `t_s,private_mb,in_memory_mb,pc_free_mb,pc_commit_left_mb,vram_budget_mb,vram_in_use_mb,shared_in_use_mb,waiting_objects,reaper_queue`. The video memory columns come from the video memory line nearest the memory line, and the last two from the census line nearest it, each within 2 s; a cell is empty when there is none. Only when the run has memory lines: a summary made again from a log without them removes an older `memory.csv` |

## summary.json (`leonida-gate-summary/1`)

Expand All @@ -59,10 +66,10 @@ Times are seconds since the run started. The summary holds numbers, switch names
| `times.world_s` | Seconds from `world_at_s` to the stop request |
| `fps` | From the presents, after the first 60 s in the world: frames, median, p5, frame p95 (ms), median of the last 60 s (`end_median`), the first second with fewer than 2 frames, from 60 s in the world on too (`under_2_at_s`), the longest gap with no frame presented from the world on (`longest_present_gap_s`, `longest_gap_at_s`; on purpose from the world on: a freeze on pipeline compiles comes right after arrival, and 10 s with no frame is what a player sees), draws a frame. Without the present lines (a build without diagnostics), the median and p5 come from the frame lines, with a warning |
| `pipelines` | Waits that ran out (`missed_waits`), the most pipelines being made at once |
| `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 |
| `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. `stop_limits` holds the memory stop's limits from `run.json` (null from a gate without the memory stop). Also, from the runtime's census line, the reaper thread's queue at its longest and when (`reaper_queue_max`, `reaper_queue_max_at_s`) and the most objects let go that wait for their submissions (`waiting_objects_max`); how fast the private bytes grew in the world in MB a minute (`private_mb_per_min_in_world`, a least-squares slope over the memory lines from `world_at_s` to the stop request: on a short run it is the loading's climb, on a long one a pile-up's slope; null with fewer than 3 lines there); and `series`, the name of `memory.csv` (null without memory lines, or when the file could not be written, for example open in another program: `game_gate.py` says so on stderr and writes `summary.json` all the same). The video memory peak and the time over the budget count every video memory line: the one written with each memory line, and the ones the runtime writes right before and after it makes the swap chain, right after the swap chain's buffers are made again for another size or format, and right after the system changes the card's budget by 256 MB or more. `memory.csv` has only the one nearest each memory line, so the summary's peak can come from a line with no row there |
| `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`), 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
Expand All @@ -81,6 +88,7 @@ Times are seconds since the run started. The summary holds numbers, switch names
- `exited_before_stop`;
- `present_gap`;
- `world_time_short`: the time limit came first;
- `memory_stop`: the memory stop ended the run, which was on its way to running out. `world_time_short` and `world_not_reached` are not added to it: the stop is why the run was short;
- `not_alive_at_stop`;
- `world_not_reached`, when the game ended before reaching the world.

Expand All @@ -96,8 +104,6 @@ A crash after the stop request is a warning, except a device loss or a panic, wh

- A driver for the menus and a fixed route (a Lua mod).
- An ABBA runner over several runs.
- A time series beside the summary (memory, census, reaping queue).
- A "memory rising" warning.
- VRAM and RAM peaks.
- The distance walked.
- The Vulkan runtime (`gta6rt`).
44 changes: 36 additions & 8 deletions scripts/game-gate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
# 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 (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. 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 = '',
Expand All @@ -27,7 +32,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
Expand Down Expand Up @@ -96,7 +104,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 @{
Expand Down Expand Up @@ -140,6 +149,14 @@ $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)
$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]@{
name = $Name
Expand All @@ -155,6 +172,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')
Expand Down Expand Up @@ -193,9 +211,20 @@ 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
$memoryWhy = $null
try {
while ($true) {
Start-Sleep -Seconds 5
Expand All @@ -210,23 +239,22 @@ 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 (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 }
}

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
Add-Event 'killed'
$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 }
Expand Down
Loading