Files
VIctory-NVIDIA-2/scripts/watch-rig.ps1
gertvw23 abfc2bde3a Rig launcher: live watch, processes left behind, tests
- scripts\watch-rig.ps1: the live watch as a script of its own (-Watch
  opens it beside the game): fps, stalls, memory and GPU every minute;
  alerts at once on low commit, out-of-memory, log errors, stalls, GPU
  heat, and a hang (the game's log still for 60 s, or its window not
  responding for 15 s), as the start that froze on 2026-11-28 did.
- Processes left behind: every victory.exe is listed as game, preload or
  warm worker; only those left behind (a game without a window, a preload
  or worker whose parent is gone) are offered to be ended, with what they
  started. Work whose parent still runs is listed and left alone.
- An error in the watcher's runspace is now reported instead of lost.
  It found a bug in this change: the new -Watch switch and the watcher's
  $watch script block were one variable (names are not case-sensitive),
  so the runspace ran the word False. Renamed to $watchScript.
- scripts\tests\play-rig.Tests.ps1: 13 Pester tests (3.4, Windows 10's
  own) against a stand-in victory.exe in a temporary VICTORY_HOME.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 21:06:52 +01:00

187 lines
13 KiB
PowerShell

# Live performance watch for a VIctory run, in a window of its own (play-rig.ps1 -Watch opens it), read-only:
# the newest play log in %LOCALAPPDATA%\victory\logs, Windows' commit, the game's processes, nvidia-smi. It waits
# for victory.exe, reports every -ReportEvery seconds (rendered fps, stalls, memory, GPU), alerts at once on low
# commit, out-of-memory, errors in the log, stalls and GPU heat, and ends with a line for the whole run.
#
# .\scripts\watch-rig.ps1 until the game ends
# .\scripts\watch-rig.ps1 -ReportEvery 30
# .\scripts\watch-rig.ps1 -MaxMinutes 29 -StateFile x.json bounded; a later start with the same file carries on
#
# Nothing it does touches the game: it reads files and counters, at below-normal priority, every 5 s.
# Windows PowerShell 5.1 or PowerShell 7. ASCII only.
param([string]$StateFile = '', [int]$ReportEvery = 60, [int]$MaxMinutes = 0)
$ErrorActionPreference = 'SilentlyContinue'
try { (Get-Process -Id $PID).PriorityClass = 'BelowNormal' } catch { }
$vhome = if ($env:VICTORY_HOME) { $env:VICTORY_HOME } else { Join-Path $env:LOCALAPPDATA 'victory' }
$logsDir = Join-Path $vhome 'logs'
$cacheDir = if ($env:VICTORY_CACHE_DIR) { $env:VICTORY_CACHE_DIR } else { Join-Path $vhome 'cache' }
$inv = [Globalization.CultureInfo]::InvariantCulture
# Plain lines when the output is piped (a log, another program); coloured alerts in a console.
function Emit([string]$s) {
$line = '{0:HH:mm:ss} {1}' -f (Get-Date), $s
if ([Console]::IsOutputRedirected) { [Console]::Out.WriteLine($line); [Console]::Out.Flush() }
elseif ($s -like 'ALERT*') { Write-Host $line -ForegroundColor Yellow }
elseif ($s -like 'GAME ENDED*') { Write-Host $line -ForegroundColor Cyan }
else { Write-Host $line }
}
$s = $null
if ($StateFile -and (Test-Path -LiteralPath $StateFile)) { try { $s = Get-Content -LiteralPath $StateFile -Raw | ConvertFrom-Json } catch { } }
$fresh = -not $s
if ($fresh) {
$s = [pscustomobject]@{ Since = (Get-Date).AddMinutes(-2).ToString('o'); Log = ''; Offset = 0; Fps = @(); Stalls = 0; MinCommitMB = 999999
PeakVramMB = 0; PeakGameMB = 0; MaxTempC = 0; Errors = 0; Oom = 0; Pressure = 0; GameSeen = $false; Suspended = $false; Logs = 0; FgOn = '' }
}
$fps = New-Object System.Collections.Generic.List[double]; foreach ($v in @($s.Fps)) { if ($null -ne $v) { $fps.Add([double]$v) } }
$since = [datetime]::Parse($s.Since, $inv, [Globalization.DateTimeStyles]::RoundtripKind)
$lastAlert = @{}
function Alert([string]$kind, [string]$text, [int]$every = 120) {
$now = Get-Date
if (-not $lastAlert.ContainsKey($kind) -or ($now - $lastAlert[$kind]).TotalSeconds -ge $every) { $lastAlert[$kind] = $now; Emit "ALERT $text" }
}
function Save {
$s.Fps = @($fps | Select-Object -Last 20000)
if ($StateFile) { $s | ConvertTo-Json -Depth 3 -Compress | Set-Content -LiteralPath $StateFile -Encoding UTF8 }
}
function Pct($list, [double]$q) { $a = @($list | Sort-Object); if (-not $a.Count) { return $null }; $a[[math]::Min($a.Count - 1, [math]::Floor($q * $a.Count))] }
function CacheMB { [math]::Round(((Get-ChildItem $cacheDir -Recurse -File -ErrorAction SilentlyContinue | Measure-Object Length -Sum).Sum) / 1MB) }
if ($fresh) { Emit 'watching: waiting for victory.exe (preload, then the game)' } else { Emit 'watch re-armed; carrying on' }
$deadline = if ($MaxMinutes -gt 0) { (Get-Date).AddMinutes($MaxMinutes) } else { [datetime]::MaxValue }
$lastReport = Get-Date
$win = New-Object System.Collections.Generic.List[double] # in-game fps windows since the last report
$gpuUtil = New-Object System.Collections.Generic.List[double]
$cpuPct = New-Object System.Collections.Generic.List[double]
$cmdOf = @{}; $prevCpu = $null; $prevAt = $null; $goneSince = $null; $wasPreloading = $false; $preloadAt = $null; $preloadCache = 0
$lowRun = 0; $partial = ''; $logMovedAt = $null; $notResponding = 0
$logical = [Environment]::ProcessorCount
while ((Get-Date) -lt $deadline) {
# --- processes
$procs = @(Get-Process victory -ErrorAction SilentlyContinue)
foreach ($p in $procs) { if (-not $cmdOf.ContainsKey($p.Id)) { $cmdOf[$p.Id] = [string](Get-CimInstance Win32_Process -Filter "ProcessId=$($p.Id)").CommandLine } }
$preloading = @($procs | Where-Object { $cmdOf[$_.Id] -match '\spreload\s' }).Count -gt 0
if ($procs.Count) {
if (-not $s.GameSeen) { $s.GameSeen = $true; Emit "victory.exe started (pid $(($procs | ForEach-Object Id) -join ', '))" }
$goneSince = $null
} elseif ($s.GameSeen) {
if (-not $goneSince) { $goneSince = Get-Date }
elseif (((Get-Date) - $goneSince).TotalSeconds -ge 40) {
# The end: one line with the whole run.
$crash = ''
if ($s.Log -match '-(\d+)\.log$') {
$procId = $Matches[1]
foreach ($f in Get-ChildItem (Join-Path $vhome 'data\*\crashes.log')) {
$t = Get-Content -LiteralPath $f.FullName
for ($i = 0; $i -lt $t.Count - 1; $i++) { if ($t[$i] -match "^unix time \d+, process $procId\s*$") { $crash = $t[$i + 1] } }
}
}
$how = if ($crash) { "CRASHED: $crash" } elseif ($s.Suspended) { 'closed normally' } else { 'ended without the normal closing line (crash or killed?)' }
$med = Pct $fps 0.5; $low = Pct $fps 0.05
Emit ("GAME ENDED, {0}. Whole run: {1} s in game, rendered fps median {2} / slowest 5% {3}, {4} s under 10 fps; lowest commit free {5:N1} GB; peak game memory {6:N1} GB; peak VRAM {7:N1} GB; max GPU temp {8} C; {9} errors, {10} out-of-memory, {11} memory-pressure warnings; {12} run log(s)" -f `
$how, $fps.Count, $(if ($null -ne $med) { '{0:N1}' -f $med } else { '-' }), $(if ($null -ne $low) { '{0:N1}' -f $low } else { '-' }), $s.Stalls, ($s.MinCommitMB / 1024), ($s.PeakGameMB / 1024), ($s.PeakVramMB / 1024), $s.MaxTempC, $s.Errors, $s.Oom, $s.Pressure, $s.Logs)
if ($StateFile) { Remove-Item -LiteralPath $StateFile -ErrorAction SilentlyContinue }
exit 0
}
}
if ($preloading -and -not $wasPreloading) { $preloadAt = Get-Date; $preloadCache = CacheMB; Emit "shader preload running (cache $preloadCache MB)" }
if (-not $preloading -and $wasPreloading) { Emit ("shader preload finished after {0:N1} min (cache {1} MB); the game is starting" -f ((Get-Date) - $preloadAt).TotalMinutes, (CacheMB)) }
$wasPreloading = $preloading
# --- the newest play log since the watch began
$newest = Get-ChildItem $logsDir -Filter 'victory-nvidia-play-*.log' | Where-Object { $_.LastWriteTime -ge $since } | Sort-Object LastWriteTime | Select-Object -Last 1
if ($newest -and $newest.FullName -ne $s.Log) {
if ($s.Log) { Emit "new run log $($newest.Name): the game restarted (warm-up restart or F1 menu)" }
$s.Log = $newest.FullName; $s.Offset = 0; $s.Logs++; $partial = ''; $s.Suspended = $false
$head = Get-Content -LiteralPath $newest.FullName -TotalCount 1
if ($head -match 'VICTORY_MENU_FRAMEGEN=(\d)') { $s.FgOn = $Matches[1] }
}
if ($s.Log -and (Test-Path -LiteralPath $s.Log)) {
try {
$fs = [IO.File]::Open($s.Log, 'Open', 'Read', 'ReadWrite')
if ($fs.Length -gt $s.Offset) { $logMovedAt = Get-Date }
if ($fs.Length -gt $s.Offset) {
[void]$fs.Seek([int64]$s.Offset, 'Begin')
$sr = New-Object IO.StreamReader($fs)
$text = $partial + $sr.ReadToEnd()
$s.Offset = $fs.Length
$cut = $text.LastIndexOf("`n")
if ($cut -ge 0) { $partial = $text.Substring($cut + 1); $text = $text.Substring(0, $cut) } else { $partial = $text; $text = '' }
foreach ($line in $text -split "`r?`n") {
if ($line -match 'frame times: (\d+) frames, median ([\d.]+) ms') {
$ms = [double]::Parse($Matches[2], $inv)
if ($ms -ge 8.0) {
$f = 1000.0 / $ms; $fps.Add($f); $win.Add($f)
if ($f -lt 10) { $s.Stalls++; $lowRun++ } else { $lowRun = 0 }
if ($lowRun -eq 15) { Alert 'stall' 'under 10 fps for 15 s in a row (pipelines being made or streaming)' 180 }
}
}
elseif ($line -match 'host memory pressure.*available_commit_mib=(\d+)') { $s.Pressure++; Alert 'pressure' "the layer reports memory pressure: $($Matches[1]) MB of commit left" }
elseif ($line -match '0x8007000E') { $s.Oom++; Alert 'oom' "out-of-memory errors in the log ($($s.Oom) so far)" 60 }
elseif ($line -match '^\S+\s+ERROR\s+(.*)$') { $s.Errors++; $m = $Matches[1]; Alert 'error' ('ERROR in log: ' + $m.Substring(0, [math]::Min(180, $m.Length))) 90 }
elseif ($line -match 'DLSS Frame Generation (switched|is on with)|more frames asked than this card') { $m = $line -replace '^\S+\s+\S+\s+', ''; Emit ('frame generation: ' + $m.Substring(0, [math]::Min(160, $m.Length))) }
elseif ($line -match 'title suspended; ending the title') { $s.Suspended = $true }
}
$sr.Dispose()
}
$fs.Dispose()
} catch { }
}
# --- a hang: the game (not the preload) runs, but its log has not moved, or Windows finds its window not
# responding. A start froze like this on 2026-11-28 (log stopped 3 s in; Windows closed it 4 min later).
$games = @($procs | Where-Object { $cmdOf[$_.Id] -notmatch '\s(preload|warm)(\s|$)' })
if ($games.Count -and -not $preloading) {
if (-not $logMovedAt) { $logMovedAt = Get-Date }
$quiet = ((Get-Date) - $logMovedAt).TotalSeconds
# (A log set to warnings only, VICTORY_LOG=warn, is quiet anyway: no alert then.)
if ($quiet -ge 60 -and "$env:VICTORY_LOG" -notmatch '^\s*(warn|error)') { Alert 'quiet' ("the game's log has not moved for {0:N0} s: it may be frozen (if the picture is stuck too, close it and start again)" -f $quiet) 120 }
$stuck = @($games | Where-Object { $_.MainWindowHandle -ne [IntPtr]::Zero -and -not $_.Responding })
if ($stuck.Count) { $notResponding++ } else { $notResponding = 0 }
if ($notResponding -eq 3) { Alert 'hung' 'Windows reports the game window as not responding (15 s so far)' 120 }
} else { $logMovedAt = $null; $notResponding = 0 }
# --- the PC
$os = Get-CimInstance Win32_OperatingSystem
$commitMB = [int]($os.FreeVirtualMemory / 1KB)
if ($s.GameSeen -and $commitMB -lt $s.MinCommitMB) { $s.MinCommitMB = $commitMB }
if ($s.GameSeen -and $commitMB -lt 5120) { Alert 'commit' ("only {0:N1} GB of commit left: close other programs now or the game may crash" -f ($commitMB / 1024)) 90 }
# The game's memory: not the preload or its pipeline workers.
$gameMB = [int](($games | Measure-Object PrivateMemorySize64 -Sum).Sum / 1MB)
if ($gameMB -gt $s.PeakGameMB) { $s.PeakGameMB = $gameMB }
$cpuNow = ($procs | ForEach-Object { $_.TotalProcessorTime.TotalSeconds } | Measure-Object -Sum).Sum
$at = Get-Date
if ($null -ne $prevCpu -and $procs.Count -and $cpuNow -ge $prevCpu) { $cpuPct.Add(100 * ($cpuNow - $prevCpu) / (($at - $prevAt).TotalSeconds * $logical)) }
$prevCpu = $cpuNow; $prevAt = $at
$smi = nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu,power.draw --format=csv,noheader,nounits 2>$null | Select-Object -First 1
$vramMB = 0; $vramTotal = 0; $temp = 0; $watts = 0
# (Recent drivers give the temperature with decimals: "3, 1114, 16376, 41.32, 36.40".)
if ($smi -match '^\s*([\d.]+),\s*([\d.]+),\s*([\d.]+),\s*([\d.]+),\s*([\d.]+)') {
$gpuUtil.Add([double]::Parse($Matches[1], $inv)); $vramMB = [int][double]::Parse($Matches[2], $inv); $vramTotal = [int][double]::Parse($Matches[3], $inv)
$temp = [int][math]::Round([double]::Parse($Matches[4], $inv)); $watts = [double]::Parse($Matches[5], $inv)
if ($s.GameSeen -and $vramMB -gt $s.PeakVramMB) { $s.PeakVramMB = $vramMB }
if ($temp -gt $s.MaxTempC) { $s.MaxTempC = $temp }
if ($temp -ge 84) { Alert 'temp' "GPU at $temp C" 300 }
}
# --- the periodic report
if (((Get-Date) - $lastReport).TotalSeconds -ge $ReportEvery -and $procs.Count) {
$cpuAvg = if ($cpuPct.Count) { ($cpuPct | Measure-Object -Average).Average } else { 0 }
$gpuAvg = if ($gpuUtil.Count) { ($gpuUtil | Measure-Object -Average).Average } else { 0 }
$sys = "commit free {0:N1} GB | game {1:N1} GB RAM, CPU {2:N0}% of all threads | GPU {3:N0}% busy, VRAM {4:N1}/{5:N1} GB, {6} C, {7:N0} W" -f ($commitMB / 1024), ($gameMB / 1024), $cpuAvg, $gpuAvg, ($vramMB / 1024), ($vramTotal / 1024), $temp, $watts
if ($preloading) {
$c = CacheMB
Emit ("preloading shaders for {0:N0} min, cache {1} MB (+{2} MB) | {3}" -f ((Get-Date) - $preloadAt).TotalMinutes, $c, ($c - $preloadCache), $sys)
} elseif ($win.Count) {
$fg = if ($s.FgOn -and $s.FgOn -ne '0') { " (x$($s.FgOn) frame generation: about {0:N0} shown)" -f ((Pct $win 0.5) * [int]$s.FgOn) } else { '' }
Emit ("in game: rendered {0:N1} fps median, slowest 5% {1:N1}{2}; {3} s under 10 fps this stretch | {4}" -f (Pct $win 0.5), (Pct $win 0.05), $fg, @($win | Where-Object { $_ -lt 10 }).Count, $sys)
} else {
Emit "menus or loading (no in-game frames this stretch) | $sys"
}
$win.Clear(); $gpuUtil.Clear(); $cpuPct.Clear(); $lastReport = Get-Date
}
Save
Start-Sleep -Seconds 5
}
Save