Skip to content

Instantly share code, notes, and snippets.

@RReverser
Last active February 9, 2025 15:57
Show Gist options
  • Save RReverser/43c95e1b810d5f8d6e4eb7d9fd0ce620 to your computer and use it in GitHub Desktop.
Save RReverser/43c95e1b810d5f8d6e4eb7d9fd0ce620 to your computer and use it in GitHub Desktop.
PowerShell script to isolate benchmarks as much as possible
[target.x86_64-pc-windows-msvc]
runner = ["pwsh", "D:\\run_bench.ps1"]
# Stop script on first error
$ErrorActionPreference = "Stop"
$AffinityMask = 0xFFF0 # Mask for cores 4-15
# Extract executable and arguments from $args
$Executable = $args[0]
$Arguments = $args[1..$args.Length]
# Reserve CPU cores 8-15 for benchmark
Write-Host "Reserving CPU cores 8-15 for the command..."
# Set the processor affinity for the OS to exclude cores 8-15
$processes = Get-Process | ? {
$process = $_
try {
$process.ProcessorAffinity = $process.ProcessorAffinity -band -bnot $AffinityMask
$true
}
catch {
Write-Warning "Failed to set processor affinity for process $($process.Name): $_"
$false
}
}
try {
# Run the command passed to the script on cores 8-15 (affinity mask 0xFF00)
Write-Host "Running command on isolated cores 8-15..."
$process = Start-Process -FilePath $Executable -ArgumentList $Arguments -NoNewWindow -PassThru
$process.ProcessorAffinity = $AffinityMask # Mask for cores 4-15
$process.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::RealTime
$process.WaitForExit()
}
catch {
Write-Error "Failed to run command: $_"
}
finally {
# Restore CPU allocation to the OS (set affinity back to all cores)
Write-Host "Restoring CPU cores to OS..."
$processes | ? { -not $_.HasExited } | % {
$process = $_
try {
$process.ProcessorAffinity = $process.ProcessorAffinity -bor $AffinityMask
}
catch {
Write-Warning "Failed to restore processor affinity for process $($process): $_"
}
}
}
Write-Host "Command completed and CPU cores restored."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment