Files
mandy/scripts/game-abba.ps1
TheGeogeo af89a79c10 scripts: game-abba.ps1 takes a value the settings line hides as an arm, keeps a name's last value, refuses a checkout in -Out (review)
- arm_settings applies the space and length rules only to a value the runtime's settings line shows as it is:
  one of ASCII letters, digits and " ._,:+=*-", the `plain` test of d3d12/apps/leonida/src/leonida.rs line 65,
  mirrored and quoted in d3d12_ab_compare.py's new SOURCES, which a contract test checks against leonida.rs.
  Any other value (a folder with a space, a long path) shows as "<hidden, N chars>", which `check` takes as
  shown, so it can be an arm.
- A name given twice in one arm counts with its last value, as the gate keeps it.
- `settings` names an item by its position (and its LEONIDA_ name), never by its text, so no path is said back.
- The runner refuses a checkout inside -Out, where a re-run's clean-up could reach it.
- The files the runner writes are written beside themselves and moved over, so abba.json, rewritten after every
  run, is never found cut short; an -Out whose abba.json exists but is not readable as this runner's is refused
  as such ("no readable abba.json of this runner").
- docs/measuring_fps.md and the script's header say the same.
2026-12-02 12:04:48 +01:00

245 lines
14 KiB
PowerShell

#Requires -Version 7.0
# An ABBA comparison of the D3D12 runtime in game (docs/measuring_fps.md, section 3): gate runs
# (scripts\game-gate.ps1) in a balanced order, then tools\d3d12_ab_compare.py over the measured ones.
#
# One exe, two settings (this checkout's exe, one data folder):
# pwsh -NoProfile -File scripts\game-abba.ps1 -Game <dir> -DataTemplate <dir> -Out <dir> -Data <dir> -MoreA 'LEONIDA_X=0' -MoreB 'LEONIDA_X=1'
# Two builds (each arm runs its own checkout's gate and exe, with a data folder of its own: their caches differ):
# pwsh -NoProfile -File scripts\game-abba.ps1 -Game <dir> -DataTemplate <dir> -Out <dir> -CheckoutA <dir> -DataA <dir> -CheckoutB <dir> -DataB <dir>
#
# The order comes from `d3d12_ab_compare.py plan`: warm-up runs of each arm, discarded runs, then -Pairs pairs in
# ABBA order. A warm-up or discarded run whose gate fails stops everything at once: the measured runs would fail the
# same way. Each measured run is checked with `d3d12_ab_compare.py check`: it counts for the comparison (its gate
# passed, its summary.json reads, it has a frame rate), the pipeline cache was warm before the measured window, its
# exe is the one its arm started with, and its arm's own -MoreA/-MoreB settings show in the runtime's settings line
# when the runtime writes one. A run that fails a check gets an ab-excluded.txt (the comparison leaves it out) and its
# slot runs again, -Retry times (1). Runs go to <Out>\runs; <Out> gets abba.json (the arms' inputs, then each run as it
# ends, what was left out and why), comparison.txt and comparison.json.
#
# Refused before any run: -Out in a checkout or holding one, or one folder with, inside or around -Game,
# -DataTemplate or a data folder; a -MoreA/-MoreB item the settings line could never show (`d3d12_ab_compare.py
# settings`; a value the line hides, such as a folder, counts as shown); a missing folder or a checkout without
# scripts\game-gate.ps1. A run into an -Out whose abba.json this runner wrote first deletes what the earlier run wrote
# there (runs, abba.json, comparison.*); an -Out holding any of those without a readable one is refused.
# Exit code: 0 with a comparison ("no clear difference" is one); 1 without one, either because the runs left do not
# give one (comparison.txt says why) or on an error (nothing compared).
# Options: -Pairs (4; 3 at least: the comparison needs 3 runs a side, or 4 pairs for a verdict from the pairs
# themselves), -WorldSeconds (300), -MaxMinutes (45), -More (both arms), -PlayArgs, -Retry (1, 0 or more), -Python,
# -DryRun (prints the plan and every gate command, checks no folder, starts and deletes nothing). Two arms at 4 pairs
# and 300 s are 11 or 12 runs: about an hour.
param(
[string]$Game = '',
[string]$DataTemplate = '',
[string]$Out = '',
[string]$Data = '',
[string]$DataA = '',
[string]$DataB = '',
[string]$CheckoutA = '',
[string]$CheckoutB = '',
[string]$MoreA = '',
[string]$MoreB = '',
[string]$More = '',
[int]$Pairs = 4,
[int]$WorldSeconds = 300,
[int]$MaxMinutes = 45,
[string]$PlayArgs = '',
[int]$Retry = 1,
[string]$Python = '',
[switch]$DryRun
)
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent $PSScriptRoot
$compare = Join-Path $repo 'tools\d3d12_ab_compare.py'
if (-not $Python) { $Python = (Get-Command python -ErrorAction Stop).Source }
$pwsh = (Get-Process -Id $PID).Path
$schema = 'leonida-abba-run/1'
# One spelling per folder: its full path without an ending separator, compared without case on Windows.
function Get-FolderPath([string]$Path) { [IO.Path]::TrimEndingDirectorySeparator([IO.Path]::GetFullPath($Path)) }
$pathCase = if ($IsWindows) { [StringComparison]::OrdinalIgnoreCase } else { [StringComparison]::Ordinal }
$sep = [IO.Path]::DirectorySeparatorChar
# Whether the folder $Inner is the folder $Outer or inside it.
function Test-Within([string]$Inner, [string]$Outer) { ($Inner + $sep).StartsWith($Outer + $sep, $pathCase) }
# The text files this runner writes: UTF-8 without a byte order mark, LF line ends, one final newline.
function Write-TextFile([string]$Path, [string]$Content) {
# Written beside the file and moved over it: a reader, or a run stopped halfway, never finds it cut short.
$part = "$Path.$PID.part"
[IO.File]::WriteAllText($part, ((($Content -replace "`r`n", "`n") -replace '\n+\z', '') + "`n"))
[IO.File]::Move($part, $Path, $true)
}
foreach ($need in 'Game', 'DataTemplate', 'Out') {
if (-not (Get-Variable $need -ValueOnly)) { throw "-$need is needed" }
}
if (-not $CheckoutA) { $CheckoutA = $repo }
if (-not $CheckoutB) { $CheckoutB = $repo }
$CheckoutA = Get-FolderPath (Resolve-Path $CheckoutA).Path
$CheckoutB = Get-FolderPath (Resolve-Path $CheckoutB).Path
$builds = -not [string]::Equals($CheckoutA, $CheckoutB, $pathCase)
if ($builds) {
if (-not $DataA -or -not $DataB) { throw 'two builds need -DataA and -DataB: their caches differ' }
if ([string]::Equals((Get-FolderPath $DataA), (Get-FolderPath $DataB), $pathCase)) {
throw '-DataA and -DataB are one folder: two builds would share their caches'
}
} else {
if (-not $Data) { throw 'one exe with two settings needs -Data' }
if (-not $MoreA -and -not $MoreB) { throw 'one exe and neither -MoreA nor -MoreB: the arms would be the same' }
$DataA = $Data
$DataB = $Data
}
# Refused before any run: 2 pairs are 2 runs a side, which never give a comparison, and only after an hour of runs.
if ($Pairs -lt 3) { throw '-Pairs is 3 or more: the comparison needs 3 runs a side (with fewer than 4 pairs it fits each side and a linear trend), and 4 pairs give a verdict from the pairs themselves' }
if ($Retry -lt 0) { throw '-Retry is 0 or more: how many times a measured run left out runs again' }
$Out = Get-FolderPath $Out
# This runner's own checkout too: in two-build mode it is neither arm's.
foreach ($checkout in $CheckoutA, $CheckoutB, (Get-FolderPath $repo)) {
if (Test-Within $Out $checkout) { throw "-Out is inside a checkout: $Out" }
# A checkout inside -Out (at <Out>\runs, say) could be deleted by a re-run's clean-up.
if (Test-Within $checkout $Out) { throw "a checkout is inside -Out: $checkout" }
}
# The folders every gate run reads or writes: the runs' folders would mix with them, and a re-run's clean-up with them.
$inputs = [ordered]@{ '-Game' = (Get-FolderPath $Game); '-DataTemplate' = (Get-FolderPath $DataTemplate) }
if ($builds) {
$inputs['-DataA'] = Get-FolderPath $DataA
$inputs['-DataB'] = Get-FolderPath $DataB
} else {
$inputs['-Data'] = Get-FolderPath $Data
}
foreach ($name in $inputs.Keys) {
if ((Test-Within $Out $inputs[$name]) -or (Test-Within $inputs[$name] $Out)) {
throw "-Out and $name are one folder, or one is inside the other: $Out"
}
}
# Each arm's own settings are looked for in its runs' settings line, read the way the gate reads -More: an item that
# line could never show would leave every run of the arm out, an hour later.
foreach ($arm in 'A', 'B') {
$armMore = if ($arm -eq 'A') { $MoreA } else { $MoreB }
$said = & $Python -I $compare settings "--more=$armMore" | Out-String
if ($LASTEXITCODE) { throw "d3d12_ab_compare.py settings failed (exit $LASTEXITCODE)" }
$unshowable = @(($said | ConvertFrom-Json).unshowable)
if ($unshowable.Count) { throw "-More${arm}: $($unshowable -join '; '): the runtime's settings line could never show it" }
}
$root = Join-Path $Out 'runs'
$planArgs = @('-I', $compare, 'plan', '--pairs', $Pairs, '--json')
if (-not $builds) { $planArgs += '--switch' }
$planText = & $Python @planArgs | Out-String
if ($LASTEXITCODE) { throw "d3d12_ab_compare.py plan failed (exit $LASTEXITCODE)" }
$order = $planText | ConvertFrom-Json
$slots = @($order.warm_up_and_discarded | ForEach-Object { [pscustomobject]@{ Kind = ($_ -split ' ')[0]; Arm = ($_ -split ' ')[-1] } }) +
@($order.measured | ForEach-Object { [pscustomobject]@{ Kind = 'measured'; Arm = $_ } })
"mode: $(if ($builds) { 'two builds' } else { 'one exe, two settings' }); $($slots.Count) runs, $Pairs pairs"
# What ran with what, written at the start and after each run, so that an error leaves it too. The paths are this
# machine's: the record is a local file, like the runs' own run.json.
$record = [ordered]@{ schema = $schema; builds = $builds; pairs = $Pairs; world_seconds = $WorldSeconds;
max_minutes = $MaxMinutes; retry = $Retry; more = $More; more_a = $MoreA; more_b = $MoreB; play_args = $PlayArgs;
game = $inputs['-Game']; data_template = $inputs['-DataTemplate']; checkout_a = $CheckoutA; checkout_b = $CheckoutB;
data_a = (Get-FolderPath $DataA); data_b = (Get-FolderPath $DataB); runs = @() }
function Save-Record { Write-TextFile (Join-Path $Out 'abba.json') ($record | ConvertTo-Json -Depth 5) }
if (-not $DryRun) {
# Everything a gate run needs is checked before anything is deleted.
foreach ($name in $inputs.Keys) {
if (-not (Test-Path -LiteralPath $inputs[$name] -PathType Container)) { throw "$name is not a folder: $($inputs[$name])" }
}
foreach ($checkout in $CheckoutA, $CheckoutB) {
if (-not (Test-Path -LiteralPath (Join-Path $checkout 'scripts\game-gate.ps1') -PathType Leaf)) {
throw "no scripts\game-gate.ps1 in $checkout`: each arm runs its own checkout's gate"
}
}
# The run folders' names repeat from one run to the next: a folder kept from an earlier run would keep its
# ab-excluded.txt (a good run left out) and its data copy, and an old comparison would pass for this one's. So
# what an earlier run wrote is deleted, but only when its abba.json says this runner wrote it.
$recordFile = Join-Path $Out 'abba.json'
$outputs = @($root, $recordFile, (Join-Path $Out 'comparison.txt'), (Join-Path $Out 'comparison.json'))
$ours = $false
if (Test-Path -LiteralPath $recordFile -PathType Leaf) {
try { $ours = (Get-Content -LiteralPath $recordFile -Raw | ConvertFrom-Json).schema -eq $schema } catch { $ours = $false }
}
if (-not $ours) {
$held = @($outputs | Where-Object { Test-Path -LiteralPath $_ } |
Where-Object { $_ -ne $root -or @(Get-ChildItem -LiteralPath $_ -Force).Count })
if ($held.Count) {
$whose = if (Test-Path -LiteralPath $recordFile) { 'no readable abba.json of this runner' } else { 'no abba.json of this runner' }
throw "-Out holds runs, abba.json or comparison files that this runner did not write ($whose): $Out"
}
}
foreach ($old in $outputs) {
if (Test-Path -LiteralPath $old) { Remove-Item -LiteralPath $old -Recurse -Force }
}
New-Item -ItemType Directory -Force $Out | Out-Null
Save-Record
}
$exes = @{}
$measured = @()
$index = 0
foreach ($slot in $slots) {
$index++
$own = if ($slot.Arm -eq 'A') { $MoreA } else { $MoreB }
$settings = (@($More, $own) | Where-Object { $_ }) -join ';'
$gate = Join-Path $(if ($slot.Arm -eq 'A') { $CheckoutA } else { $CheckoutB }) 'scripts\game-gate.ps1'
$armData = if ($slot.Arm -eq 'A') { $DataA } else { $DataB }
for ($attempt = 0; $attempt -le $Retry; $attempt++) {
$label = '{0:00}-{1}-{2}' -f $index, $slot.Kind, $slot.Arm
if ($attempt) { $label += "-retry$attempt" }
$dir = Join-Path $root $label
$gateArgs = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $gate, 'run', '-Game', $Game, '-Data', $armData,
'-DataTemplate', $DataTemplate, '-Name', $label, '-RunRoot', $root, '-RunDir', $dir, '-WorldSeconds', $WorldSeconds,
'-MaxMinutes', $MaxMinutes, '-Python', $Python)
# PowerShell 7.0 to 7.2 drop an empty argument to a program: the gate would read the next name as its value.
if ($settings) { $gateArgs += @('-More', $settings) }
if ($PlayArgs) { $gateArgs += @('-PlayArgs', $PlayArgs) }
if ($DryRun) {
"$index $($slot.Kind) $($slot.Arm): $gate run -Name $label -Data $armData -More '$settings'"
if ($slot.Kind -eq 'measured') { $measured += "$($slot.Arm):$dir" }
break
}
"run $index/$($slots.Count) $($slot.Kind) $($slot.Arm)$(if ($attempt) { ", again" })..."
& $pwsh @gateArgs | Out-Host
$gateExit = $LASTEXITCODE
$runFile = Join-Path $dir 'run.json'
if (-not (Test-Path $runFile)) { throw "the gate did not start $label (exit $gateExit)" }
$meta = Get-Content $runFile -Raw | ConvertFrom-Json
$exe = $meta.exe_sha16
if (-not $exes.ContainsKey($slot.Arm)) { $exes[$slot.Arm] = $exe }
$entry = [ordered]@{ position = $index; kind = $slot.Kind; arm = $slot.Arm; folder = $label; exe = $exe; commit = $meta.commit; left_out = $null }
$record.runs += $entry
Save-Record
if ($slot.Kind -ne 'measured') {
# A warm-up or discarded run is not compared, but a gate that fails here (0 PASS, 1 FAIL, 2 ERROR) would
# fail the measured runs after it too: stop now rather than an hour later with too few runs.
if ($gateExit) { throw "$label failed its gate (exit $gateExit, its summary.json says why): stopped before the measured runs" }
break
}
$checkText = & $Python -I $compare check $dir "--exe=$($exes[$slot.Arm])" "--more=$own" | Out-String
if ($LASTEXITCODE) { throw "d3d12_ab_compare.py check failed on $label (exit $LASTEXITCODE)" }
$why = ($checkText | ConvertFrom-Json).excluded
$entry.left_out = $why
Save-Record
if ($why) {
Write-TextFile (Join-Path $dir 'ab-excluded.txt') $why
" left out: $why"
if ($attempt -lt $Retry) { continue }
}
$measured += "$($slot.Arm):$dir"
break
}
}
if ($DryRun) {
"then: $Python -I $compare abba $($measured -join ' ')"
exit 0
}
# Each output is used only once its command has succeeded: no empty comparison passes for one.
$json = & $Python -I $compare abba @measured --json | Out-String
if ($LASTEXITCODE) { throw "d3d12_ab_compare.py abba failed (exit $LASTEXITCODE): no comparison" }
$text = & $Python -I $compare abba @measured | Out-String
if ($LASTEXITCODE) { throw "d3d12_ab_compare.py abba failed (exit $LASTEXITCODE): no comparison" }
Write-TextFile (Join-Path $Out 'comparison.json') $json
Write-TextFile (Join-Path $Out 'comparison.txt') $text
$text
# Too few runs left, or unbalanced pairs: the files say why, and the exit code says there is no comparison.
if ($null -eq ($json | ConvertFrom-Json).result) { exit 1 }
exit 0