简体   繁体   中英

Adjusting the affinity of an process for high core counts

I want to adjust the affinity of my program, but the number of cores is high

Is there a way that I can set all cores on the app by PowerShell code?

The number of cores is 64, and sometimes we use 112 cores.

This is my code

PowerShell "$Process = Get-Process -Id 6048; $Process.ProcessorAffinity=All"

I searched all over google but couldn't find any code that uses all cores.

Note: I cannot personally verify that the following works.

The .ProcessorAffinity is a bit mask implemented as aSystem.IntPtr value.

However, the bit masks must only contain bits for cores that are actually available, so using [IntPtr]::MaxValue is not an option.

Assuming 112 available cores, you need to provide a bit mask with all possible bits within the range of available cores set to 1 , which you can calculate as follows:

$allCores = 
 [Math]::Pow(
   2,
   [Math]::Ceiling([Math]::Log2(112))
 ) - 1

(Get-Process -Id 6048).ProcessorAffinity = $allCores

Translated into a powershell.exe call from outside PowerShell:

powershell.exe "(Get-Process -Id 6048).ProcessorAffinity = [Math]::Pow(2, [Math]::Ceiling([Math]::Log2(112))) - 1"

Note: If you want to determine the number of available (logical) cores programmatically , use:

$totalLogicalCores = (
  (Get-CimInstance –ClassName Win32_Processor).NumberOfLogicalProcessors |
    Measure-Object -Sum
).Sum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM