Last active
August 29, 2015 14:19
-
-
Save brianddk/7890f3ea9543a588edaa to your computer and use it in GitHub Desktop.
This file contains 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
# [rights] Copyright Dan B. (brianddk) 2015 https://github.com/brianddk | |
# [license] Licensed under Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 | |
# [repo] https://gist.github.com/brianddk/7890f3ea9543a588edaa | |
# [tips] 18MDTTiqPM8ZEo29Cig1wfGdkLNtvyorW5 | |
$REAL = 0x100 | |
$HIGH = 0x80 | |
$ABOVE = 0x8000 | |
$NORM = 0x20 | |
$BELOW = 0x4000 | |
$IDLE = 0x40 | |
$ALLP = 0x01 | |
$PROC1 = 0x01 | |
$keys = | |
"% Processor Time", | |
"% User Time", | |
"% Privileged Time", | |
"Page Faults/sec", | |
"IO Read Operations/sec", | |
"IO Write Operations/sec", | |
"IO Data Operations/sec", | |
"IO Other Operations/sec", | |
"IO Read Bytes/sec", | |
"IO Write Bytes/sec", | |
"IO Data Bytes/sec", | |
"IO Other Bytes/sec" | |
$ids = | |
"ID Process", | |
"Creating Process ID" | |
$excludes = | |
"\\$env:COMPUTERNAME\process(idle)".ToLower(), | |
"\\$env:COMPUTERNAME\process(_total)".ToLower(), | |
"\\$env:COMPUTERNAME\process(system)".ToLower(), | |
"\\$env:COMPUTERNAME\process(smss)".ToLower(), | |
"\\$env:COMPUTERNAME\process(explorer)".ToLower() | |
$counters=@() | |
foreach ($key in ($keys+$ids)) | |
{ | |
$counters += "\Process(*)\$key" | |
} | |
$x = Get-Counter $counters -ErrorAction SilentlyContinue | |
$wps = Get-WmiObject Win32_Process | |
foreach($wp in $wps) | |
{ | |
if ($wp.Handle -eq $pid) | |
{ | |
$wp.SetPriority($REAL) | Out-Null | |
} | |
} | |
$pps = Get-Process | |
$h = @{} | |
foreach ($s in $x.CounterSamples) | |
{ | |
$p = $s.Path | |
$key = $p.Substring(0, $p.LastIndexOf("\")) | |
$val = $p.Substring($p.LastIndexOf("\") + 1) | |
if (-not $h[$key]) | |
{ | |
$h[$key] = @{} | |
} | |
$h[$key][$val] = $s.CookedValue | |
if($s.CookedValue -eq $pid -and $val -eq "ID Process") | |
{ | |
$excludes += $key | |
} | |
} | |
foreach ($x in $excludes) { $h.Remove($x) } | |
$depth = 2 | |
$caps = @{} | |
foreach ($key in $keys) | |
{ | |
$caps[$key] = @() | |
foreach ($val in ($h.values.$key | Sort-Object -Descending)) | |
{ | |
if($val -eq 0 -or $caps[$key].Count -eq $depth) { break } | |
$caps[$key] += $val | |
} | |
} | |
$squash_pids = @() | |
$squash_keys = @() | |
foreach ($item in $h.GetEnumerator()) | |
{ | |
foreach($cap in $caps.GetEnumerator()) | |
{ | |
if($cap.Value -contains $item.Value[$cap.Key]) | |
{ | |
$squash_pids += $item.Value["ID Process"] | |
$squash_keys += $item.Key | |
} | |
} | |
} | |
$squash_pids = $squash_pids | Sort-Object | Get-Unique | |
$squash_keys = $squash_keys | Sort-Object | Get-Unique | |
$numProc = $env:NUMBER_OF_PROCESSORS | |
if($numProc -gt 1) | |
{ | |
$ALLP = [Math]::Pow(2, $numProc) - 1 | |
$PROC1 = 0x02 | |
} | |
foreach ($pp in $pps) | |
{ | |
if($squash_pids -contains $pp.Id) | |
{ | |
$pp.ProcessorAffinity = $PROC1 | |
} | |
} | |
$ea_bef = $ErrorActionPreference | |
$ErrorActionPreference = "Stop" | |
foreach ($wp in $wps) | |
{ | |
if($squash_pids -contains $wp.Handle) | |
{ | |
try | |
{ | |
$wp.SetPriority($IDLE) | Out-Null | |
} | |
catch | |
{ | |
Write-Host "Other exception" | |
} | |
} | |
} | |
$ErrorActionPreference = $ea_bef |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment