Created
July 10, 2020 06:26
-
-
Save JohnLBevan/8aab069a6656a624b26623e3be1cebae to your computer and use it in GitHub Desktop.
Dirty hack to prevent 1 user's Excel from hogging CPU and impacting all other users on the same RDP session. Proper solution would be to implement auto-scaling (that's in the pipeline, but this gets us around the immediate issue)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [CmdletBinding()] | |
| Param() | |
| $noProcessors = (Get-WmiObject -class 'Win32_Processor' -Namespace 'root\cimv2').NumberOfLogicalProcessors | |
| if ($noProcessors -gt 1) { | |
| $maxAffinity = [Math]::Pow(2, $noProcessors) - 1 # i.e. this signfies that all processors are assigned to a process | |
| $procsPerUser = [Math]::Ceiling($noProcessors / 3) # i.e. allow users to use 1/3rd the available processors (rounded up) | |
| while ($true) { | |
| # now get all running instances of Excel and associate them with a random subset of the available processors (random so users are evenly spread to limit competition), ignoring the first (cpu 0) so that this can be dedicated to other tasks (in case all users are hammering excel) | |
| # ... also included WmiPrvSe and CCR, since those consume lots of resources | |
| foreach ($excel in (Get-Process @('Excel', 'WmiPrvSe', 'CCR') -IncludeUserName)) { | |
| if (([int]$excel.ProcessorAffinity) -eq $maxAffinity) { # only tackle those processes where we've not previously amended the number of assigned processors | |
| [int]$newAffinity = 0 | |
| [int[]]$procsToAssign = 2..$noProcessors | Sort-Object @{E={(get-random)}} | Select-Object -First $procsPerUser | |
| foreach ($procToAssign in $procsToAssign) { | |
| $newAffinity += [Math]::Pow(2, $procToAssign - 1) | |
| } | |
| $excel.ProcessorAffinity = $newAffinity | |
| Write-Verbose "$('{0:dd HH:mm:ss}' -f (Get-Date).ToUniversalTime()) Set $($excel.Name) for $($excel.username) to run on cores $($procsToAssign -join ', ')" | |
| } | |
| } | |
| Start-Sleep -Seconds 5 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment