iis - How to calculate Processor Affinity Mask for an Application Pool using Powershell? -


i able select cpus iis app pool using. possible set affinity mask in pool settings able calculate mask programmatically depending available cores. example run on available cores except 1 , 2.

i wrote 2 helpers convert mask list of cpu numbers , list of cpu numbers mask feels should using bitwise operators. curious how it, there easier way or if there built in functions or modules can use achieve same.

https://serverfault.com/questions/471105/formula-for-processor-affinity-mask-iis

my example:

function get-cpus($mask){      $binary = [convert]::tostring($mask,2).tochararray()     $i = $binary.length - 1     $cpus = @()      for(;$i -ge 0; $i--){         if($binary[$i] -eq "1"){             $cpus += $binary.length - $i - 1         }     }      return $cpus | sort-object }  function get-mask($cpus){     $cpus = $cpus | sort-object     $i = $cpus[$cpus.length -1]     $binary = ""      for(; $i -ge 0; $i--){         if($cpus -contains $i){             $binary += "1"         }         else{             $binary += "0"         }     }      return [convert]::toint64($binary, 2) }  

i tested helpers converting , forth , checking if same values.

[string]::join(",", (get-cpus ([convert]::toint64("00000000111111111111000000000000", 2)))) [string]::join(",", (get-cpus 4294967295)) [string]::join(",", (get-cpus (get-mask 2,9,5,23,4,7,31))) [string]::join(",", (get-cpus (get-mask 2,4,5,7,9,23,31))) [string]::join(",", (get-cpus (get-mask 1,2,30,31))) [string]::join(",", (get-cpus (get-mask 30,2,1,31))) [string]::join(",", (get-cpus (get-mask @(0..31))))  

well can simplify get-mask without bitwise ops:

function get-mask($cpus) {     $cpus | foreach -begin {$mask=0} -process {$mask += [math]::pow(2,$_)} -end {$mask} } 

in get-cpus use shift-right simplify. divide 2 powershell return doubles non-integer results.

function get-cpus($mask) {     ($i=0; $mask -gt 0; $i++) {         if ($mask % 2 -eq 1) {             $i         }         $mask = $mask -shr 1     } } 

Comments