ruen-ui/Tools/Measure-RuenHardware.ps1

80 lines
6.1 KiB
PowerShell

[CmdletBinding()]
param([Parameter(Mandatory=$true)][string]$WoWPath,[ValidateRange(1,1800)][int]$Seconds=600,[switch]$Baseline,[ValidatePattern('^\d+$')][ValidateRange(20,240)][string]$FPS='60')
$ErrorActionPreference='Stop'
$game=(Get-Item -LiteralPath $WoWPath).FullName
if ((Split-Path -Leaf $game) -ne '_forever_') { $game=Join-Path $game '_forever_' }
$addon=Join-Path $game 'Interface/AddOns/RuenUI'
if (!(Test-Path -LiteralPath (Join-Path $addon 'RuenUI.toc'))) { throw 'Install Ruen UI in this Forever folder first.' }
$nvidia=Get-Command nvidia-smi.exe -ErrorAction SilentlyContinue
$nvidiaPath=if ($nvidia) {$nvidia.Source} else { Join-Path $env:ProgramFiles 'NVIDIA Corporation/NVSMI/nvidia-smi.exe' }
function Get-Nvidia {
if (!(Test-Path -LiteralPath $nvidiaPath)) { return $null }
$p=New-Object System.Diagnostics.Process
$p.StartInfo.FileName=$nvidiaPath
$p.StartInfo.Arguments='--query-gpu=name,temperature.gpu,utilization.gpu,clocks_event_reasons.hw_thermal_slowdown,clocks_event_reasons.sw_thermal_slowdown --format=csv,noheader,nounits'
$p.StartInfo.UseShellExecute=$false;$p.StartInfo.RedirectStandardOutput=$true;$p.StartInfo.RedirectStandardError=$true;$p.StartInfo.CreateNoWindow=$true
try {
if (!$p.Start()) { return $null }
if (!$p.WaitForExit(4000)) { $p.Kill();return $null }
$rows=@($p.StandardOutput.ReadToEnd() -split "`n" | Where-Object { $_.Trim() } | ConvertFrom-Csv -Header Name,Temp,Load,HW,SW)
if (!$rows.Count) {return $null}
$temps=@();$loads=@();$thermal=$false
foreach($r in $rows){
$x=0.0;if([double]::TryParse($r.Temp,[ref]$x)){$temps+=$x}
$x=0.0;if([double]::TryParse($r.Load,[ref]$x)){$loads+=$x}
if($r.HW.Trim() -eq 'Active' -or $r.SW.Trim() -eq 'Active'){$thermal=$true}
}
if (!$temps.Count) {return $null}
return @{temperature=($temps|Measure-Object -Maximum).Maximum;load=($loads|Measure-Object -Maximum).Maximum;thermal=$thermal}
} catch {return $null} finally {$p.Dispose()}
}
function Get-Temperatures {
# Use an already running hardware monitor, if present. Never install a driver.
foreach($ns in @('root/LibreHardwareMonitor','root/OpenHardwareMonitor')){
try {
$sensors=Get-CimInstance -Namespace $ns -ClassName Sensor -OperationTimeoutSec 2 -ErrorAction Stop
return @($sensors|Where-Object {$_.SensorType -eq 'Temperature' -and $_.Identifier -match 'cpu|gpu|intel|amd' -and $_.Value -gt 0 -and $_.Value -lt 130}|ForEach-Object {
@{sensor=[string]$_.Identifier;celsius=[double]$_.Value;limit=$(if($_.Identifier -match 'gpu'){83}else{90})}
})
}catch{}
}
return @()
}
$samples=New-Object 'System.Collections.Generic.List[object]'
$clock=[Diagnostics.Stopwatch]::StartNew();$hotCount=0;$thermal=$false;$limitedCount=0
Write-Host 'Play normally in WoW. Checking this PC for up to 10 minutes; no stress test or driver changes.'
do {
$temps=@(Get-Temperatures);$g=Get-Nvidia
$cpu=Get-CimInstance Win32_PerfFormattedData_Counters_ProcessorInformation -Filter "Name='_Total'" -OperationTimeoutSec 3 -ErrorAction SilentlyContinue
# Low clock under load is performance limiting, not proof of thermal throttling.
$limited=$cpu -and $cpu.PercentProcessorUtility -ge 80 -and $cpu.PercentPerformance -lt 80
if($limited){$limitedCount++}
$hot=@($temps|Where-Object {$_.celsius -ge $_.limit}).Count -gt 0
if($g){$hot=$hot -or $g.temperature -ge 83;$thermal=$thermal -or $g.thermal}
$hotCount=if($hot){$hotCount+1}else{0}
$samples.Add(@{time=[DateTimeOffset]::UtcNow.ToUnixTimeSeconds();temperatures=$temps;gpu=$g;performanceLimited=[bool]$limited;
cpuLoad=$(if($cpu){$cpu.PercentProcessorUtility}else{$null});gameRunning=[bool](Get-Process Wow,WowClassic -ErrorAction SilentlyContinue)})
if($clock.Elapsed.TotalSeconds -ge $Seconds -or $thermal -or $hotCount -ge 3){break}
Start-Sleep -Seconds ([Math]::Min(5,[Math]::Ceiling($Seconds-$clock.Elapsed.TotalSeconds)))
}while($true)
$underLoad=@($samples|Where-Object {$_.gameRunning -and ($_.cpuLoad -gt 15 -or ($_.gpu -and $_.gpu.load -gt 20))}).Count
$loaded=$clock.Elapsed.TotalSeconds -ge 180 -and $underLoad -ge $samples.Count*.6
$cpuSeen=@($temps|Where-Object {$_.sensor -match 'cpu'}).Count -gt 0
$gpuSeen=[bool]$g -or @($temps|Where-Object {$_.sensor -match 'gpu'}).Count -gt 0
$coverage=if($cpuSeen -and $gpuSeen){'full'}elseif($cpuSeen -or $gpuSeen){'partial'}else{'unavailable'}
$evidence=if($thermal){'thermal throttling observed'}elseif($hotCount -ge 3){'temperature headroom low'}elseif($g -or $temps.Count){'no thermal limit observed in sample; CPU coverage depends on available sensors'}else{'thermal sensors unavailable'}
$now=[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
$report=@{format=1;platform='Windows';checkedAt=$now;duration=[Math]::Round($clock.Elapsed.TotalSeconds,1);loaded=$loaded;targetFPS=[int]$FPS;
thermalCoverage=$coverage;thermalPressure=($thermal -or $hotCount -ge 3);performancePressure=($limitedCount -ge 6);evidence=$evidence;samples=@($samples.ToArray())}
$reports=Join-Path $game 'Ruen-UI-Reports';New-Item -ItemType Directory -Path $reports -Force|Out-Null
$dest=Join-Path $reports 'hardware.json'
if(Test-Path -LiteralPath $dest){Copy-Item -LiteralPath $dest -Destination (Join-Path $reports 'hardware.previous.json') -Force}
$utf8=New-Object Text.UTF8Encoding($false)
[IO.File]::WriteAllText($dest,($report|ConvertTo-Json -Depth 8),$utf8)
function Lua-Bool($v){if($v){'true'}else{'false'}}
$text="local _, P = ...`nP.hardwareProfile = { targetFPS = $([int]$FPS), thermalCoverage = `"$coverage`", checkedAt = $now, loaded = $(Lua-Bool $loaded), thermalPressure = $(Lua-Bool $report.thermalPressure), performancePressure = $(Lua-Bool $report.performancePressure), autoStart = $(Lua-Bool (!$Baseline)), evidence = `"$evidence`" }`n"
$tmp=Join-Path $addon 'HardwareProfile.lua.tmp';[IO.File]::WriteAllText($tmp,$text,$utf8)
Move-Item -LiteralPath $tmp -Destination (Join-Path $addon 'HardwareProfile.lua') -Force
Write-Host $evidence
Write-Host 'Report saved locally. /reload imports this PC report; a full evaluation also starts in-game calibration.'