-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAffinity2.ps1
214 lines (180 loc) · 10.5 KB
/
Affinity2.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Start as administrator
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Start-Process powershell.exe "-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit
}
$MIN_CORES_ALLOWED = 4
# Core pre-check
$processorCounts = Get-WmiObject Win32_Processor | Select NumberOfCores, NumberOfLogicalProcessors
$coresAmount = $processorCounts.NumberOfCores
$threadsAmount = $processorCounts.NumberOfLogicalProcessors
$isHyperThreadingActive = $threadsAmount -gt $coresAmount
if ($coresAmount -lt $MIN_CORES_ALLOWED) {
Write-Host "To apply Interrupt Affinity tweaks, you must have $MIN_CORES_ALLOWED or more cores"
exit
}
Write-Host "Started applying Interrupt Affinity tweaks!"
[Environment]::NewLine
# Reset affinity and apply MSI tweaks
[PsObject[]]$allPnpDeviceIds = @()
Get-WmiObject Win32_VideoController | Where-Object PNPDeviceID -Match "PCI\\VEN*" | Select-Object -ExpandProperty PNPDeviceID | ForEach { $allPnpDeviceIds += $_ }
Get-WmiObject Win32_USBController | Where-Object PNPDeviceID -Match "PCI\\VEN*" | Select-Object -ExpandProperty PNPDeviceID | ForEach { $allPnpDeviceIds += $_ }
Get-WmiObject Win32_NetworkAdapter | Where-Object PNPDeviceID -Match "PCI\\VEN*" | Select-Object -ExpandProperty PNPDeviceID | ForEach { $allPnpDeviceIds += $_ }
Get-WmiObject Win32_IDEController | Where-Object PNPDeviceID -Match "PCI\\VEN*" | Select-Object -ExpandProperty PNPDeviceID | ForEach { $allPnpDeviceIds += $_ }
Get-WmiObject Win32_SoundDevice | Where-Object PNPDeviceID -Match "PCI\\VEN*" | Select-Object -ExpandProperty PNPDeviceID | ForEach { $allPnpDeviceIds += $_ }
Get-WmiObject Win32_DiskDrive | Where-Object PNPDeviceID -Match "PCI\\VEN*" | Select-Object -ExpandProperty PNPDeviceID | ForEach { $allPnpDeviceIds += $_ }
foreach ($devicePath in $allPnpDeviceIds) {
$affinityPath = "HKLM:\SYSTEM\CurrentControlSet\Enum\$devicePath\Device Parameters\Interrupt Management\Affinity Policy"
$msiPath = "HKLM:\SYSTEM\CurrentControlSet\Enum\$devicePath\Device Parameters\Interrupt Management\MessageSignaledInterruptProperties"
Remove-ItemProperty -Path $affinityPath -Name "AssignmentSetOverride" -Force -ErrorAction Ignore
Remove-ItemProperty -Path $affinityPath -Name "DevicePolicy" -Force -ErrorAction Ignore
Remove-ItemProperty -Path $affinityPath -Name "DevicePriority" -Force -ErrorAction Ignore
Set-ItemProperty -Path $msiPath -Name "MSISupported" -Value 1 -Force -Type Dword -ErrorAction Ignore
}
function Is-Empty-Str {
param ([string] $value)
[string]::IsNullOrWhiteSpace($value)
}
function Is-Even {
param ([int] $value)
$value % 2 -eq 0
}
function Apply-IRQ-Priotity-Optimization {
param ([string] $IRQValue)
$IRQSplit = $IRQValue.Trim().Split(" ")
foreach ($IRQ in $IRQSplit) {
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\PriorityControl" -Name "IRQ$($IRQ)Priority" -Value 1 -Force -Type Dword -ErrorAction Ignore
}
}
# ------------------------------------------------------
# Priorities - Where lowest number is first.
$priorities = @(
[PsObject]@{Class = 'Display'; Priority = 1; Enabled = $true; Description = 'GPU'; isUSB = $false},
[PsObject]@{Class = 'Mouse'; Priority = 2; Enabled = $true; Description = 'Mouse'; isUSB = $true},
[PsObject]@{Class = 'Net'; Priority = 3; Enabled = $true; Description = 'LAN / Ethernet'; isUSB = $false},
[PsObject]@{Class = 'Media'; Priority = 4; Enabled = $false; Description = 'Audio'; isUSB = $true},
[PsObject]@{Class = 'Keyboard'; Priority = 5; Enabled = $false; Description = 'Keyboard'; isUSB = $true}
)
# ------------------------------------------------------
$enabledClasses = $priorities | Where-Object { $_.Enabled -eq $true } | ForEach-Object { $_.Class }
$enabledUSBClasses = $priorities | Where-Object { $_.Enabled -eq $true -and $_.isUSB -eq $true } | ForEach-Object { $_.Class }
# Get all relevant child devices
$allDevices = Get-PnpDevice -PresentOnly -Class $enabledClasses -Status OK
$prioritizedDevices = $allDevices | ForEach-Object {
$device = $_
$priorityDevice = $priorities | Where-Object { $_.Class -eq $device.Class}
return [PsObject]@{
Class = $device.Class;
FriendlyName = $device.FriendlyName;
InstanceId = $device.InstanceId;
Priority = $priorityDevice.Priority;
Enabled = $priorityDevice.Enabled;
isUSB = $priorityDevice.isUSB
}
} | Sort-Object { $_.Priority }
[PsObject[]]$relevantData = @()
# Get all relevant devices data
for ($i=0; $i -lt $prioritizedDevices.Length; $i++) {
$childDevice = $prioritizedDevices[$i]
$childDeviceName = $childDevice.FriendlyName
$childDeviceInstanceId = $childDevice.InstanceId
$childPnpDevice = Get-PnpDeviceProperty -InstanceId $childDeviceInstanceId
$childDeviceClass = $childDevice.Class
$isUSB = $childDeviceClass -in $enabledUSBClasses
$childPnpDeviceLocationInfo = $childPnpDevice | Where KeyName -eq 'DEVPKEY_Device_LocationInfo' | Select -ExpandProperty Data
$childPnpDevicePDOName = $childPnpDevice | Where KeyName -eq 'DEVPKEY_Device_PDOName' | Select -ExpandProperty Data
$parentDeviceInstanceId = $childPnpDevice | Where KeyName -eq 'DEVPKEY_Device_Parent' | Select -ExpandProperty Data
$parentDevice = $null
$parentDeviceName = ""
$parentDeviceLocationInfo = ""
$parentDevicePDOName = ""
do {
$parentDevice = Get-PnpDeviceProperty -InstanceId $parentDeviceInstanceId
if (!$parentDevice) {
continue
}
$parentDeviceName = $parentDevice | Where KeyName -eq 'DEVPKEY_NAME' | Select -ExpandProperty Data
if ([string]::IsNullOrWhiteSpace($parentDeviceName)) {
continue
}
$parentDeviceLocationInfo = $parentDevice | Where KeyName -eq 'DEVPKEY_Device_LocationInfo' | Select -ExpandProperty Data
$parentDevicePDOName = $parentDevice | Where KeyName -eq 'DEVPKEY_Device_PDOName' | Select -ExpandProperty Data
if ($isUSB -and !$parentDeviceName.Contains('Controller')) {
$parentDeviceInstanceId = $parentDevice | Where KeyName -eq 'DEVPKEY_Device_Parent' | Select -ExpandProperty Data
}
} while (!$parentDeviceName.Contains('Controller') -and $isUSB)
if ([string]::IsNullOrWhiteSpace($parentDeviceName)) {
continue
}
$parentDeviceAllocatedResource = Get-CimInstance -ClassName Win32_PNPAllocatedResource | Where-Object { $_.Dependent.DeviceID -like "*$parentDeviceInstanceId*" } | Select-Object @{N="IRQ";E={$_.Antecedent.IRQNumber}}
$relevantData += [PsObject]@{
ChildDeviceName = $childDeviceName;
ChildDeviceInstanceId = $childDeviceInstanceId;
ChildDeviceLocationInfo = $childPnpDeviceLocationInfo;
ChildDevicePDOName = $childPnpDevicePDOName;
ParentDeviceName = $parentDeviceName;
ParentDeviceInstanceId = $parentDeviceInstanceId;
ParentDeviceLocationInfo = $parentDeviceLocationInfo;
ParentDevicePDOName = $parentDevicePDOName;
ClassType = $childDeviceClass;
ParentDeviceIRQ = $parentDeviceAllocatedResource.IRQ
}
}
$coresValues = if ($isHyperThreadingActive) { $threadsAmount } else { $coresAmount }
# Build masks per core
[System.Collections.ArrayList]$coresMask = @()
$tempDecimalValue = 1;
for ($i=0; $i -lt $coresValues; $i++) {
# https://poweradm.com/set-cpu-affinity-powershell/
[void]$coresMask.Add(@{ Core = $i; Decimal = $tempDecimalValue; })
$tempDecimalValue = $tempDecimalValue * 2
}
# Build cores to be used
[System.Collections.ArrayList]$coresToBeUsed = @()
foreach ($item in $relevantData) {
for ($i=1; $i -le $coresValues; $i++) {
$core = if ($isHyperThreadingActive) { if (Is-Even -value $i) { $i } else { $i+1 } } else { $i }
if (!($coresToBeUsed | Where-Object { $_.Core -eq $core })) {
if (!($coresToBeUsed | Where-Object { $_.ClassType -eq $item.ClassType })) {
$coreMask = $coresMask | Where-Object { $_.Core -in ($core) }
[void]$coresToBeUsed.Add(@{Core = $core; Decimal = $coreMask.Decimal; ClassType = $item.ClassType })
}
}
}
}
# ------------------------------------------------------
# Apply interrupt affinity tweaks
foreach ($item in $relevantData) {
if ($item.ClassType -eq 'Mouse' -or $item.ClassType -eq 'Keyboard') {
Apply-IRQ-Priotity-Optimization -IRQValue $item.ParentDeviceIRQ
}
$parentAffinityPath = "HKLM:\SYSTEM\CurrentControlSet\Enum\$($item.ParentDeviceInstanceId)\Device Parameters\Interrupt Management\Affinity Policy"
$childAffinityPath = "HKLM:\SYSTEM\CurrentControlSet\Enum\$($item.ChildDeviceInstanceId)\Device Parameters\Interrupt Management\Affinity Policy"
$parentMsiPath = "HKLM:\SYSTEM\CurrentControlSet\Enum\$($item.ParentDeviceInstanceId)\Device Parameters\Interrupt Management\MessageSignaledInterruptProperties"
$childMsiPath = "HKLM:\SYSTEM\CurrentControlSet\Enum\$($item.ChildDeviceInstanceId)\Device Parameters\Interrupt Management\MessageSignaledInterruptProperties"
Set-ItemProperty -Path $parentAffinityPath -Name "DevicePolicy" -Value 4 -Force -Type Dword -ErrorAction Ignore
Set-ItemProperty -Path $childAffinityPath -Name "DevicePolicy" -Value 4 -Force -Type Dword -ErrorAction Ignore
if ($item.ClassType -eq 'Net') {
Set-ItemProperty -Path $childAffinityPath -Name "DevicePriority" -Value 3 -Force -Type Dword -ErrorAction Ignore
Set-ItemProperty -Path $childMsiPath -Name "MessageNumberLimit" -Value 2048 -Force -Type Dword -ErrorAction Ignore
}
if ($item.ClassType -eq 'Mouse') {
Set-ItemProperty -Path $parentAffinityPath -Name "DevicePriority" -Value 3 -Force -Type Dword -ErrorAction Ignore
Set-ItemProperty -Path $parentMsiPath -Name "MessageNumberLimit" -Value 2048 -Force -Type Dword -ErrorAction Ignore
}
if ($item.ClassType -eq 'Display') {
Set-ItemProperty -Path $childMsiPath -Name "MessageNumberLimit" -Value 32 -Force -Type Dword -ErrorAction Ignore
}
$coreData = $coresToBeUsed | Where-Object { $item.ClassType -eq $_.ClassType }
Set-ItemProperty -Path $parentAffinityPath -Name "AssignmentSetOverride" -Value $coreData.Decimal -Force -Type Qword -ErrorAction Ignore
Set-ItemProperty -Path $childAffinityPath -Name "AssignmentSetOverride" -Value $coreData.Decimal -Force -Type Qword -ErrorAction Ignore
$ChildDeviceLocationInfo = if (Is-Empty-Str -value $item.ChildDeviceLocationInfo) { "None" } else { $item.ChildDeviceLocationInfo }
Write-Host "Assigned to Core $($coreData.Core)"
Write-Host "Device: $($item.ChildDeviceName) - $($item.ChildDeviceInstanceId)"
Write-Host "Location Info: $ChildDeviceLocationInfo"
Write-Host "PDO Name: $($item.ChildDevicePDOName)"
Write-Host "Parent Device: $($item.ParentDeviceName) - $($item.ParentDeviceInstanceId)"
Write-Host "Location Info: $($item.ParentDeviceLocationInfo)"
Write-Host "PDO Name: $($item.ParentDevicePDOName)"
[Environment]::NewLine
}
cmd /c pause