In Windows, with
START /node 1 /affinity ff cmd /C "app.exe"
I can set the affinity of app.exe (number of cores used by app.exe).
With a windows script, How I can change the affinity of a running process ?
In Windows, with
START /node 1 /affinity ff cmd /C "app.exe"
I can set the affinity of app.exe (number of cores used by app.exe).
With a windows script, How I can change the affinity of a running process ?
PowerShell can do this task for you.
$Process = Get-Process app
$Process | Select-Object ProcessorAffinity
$Process = Get-Process app
$Process.ProcessorAffinity = 255
Core # | Value | BitMask |
---|---|---|
Core 1 | 1 | 0x00000001 |
Core 2 | 2 | 0x00000010 |
Core 3 | 4 | 0x00000100 |
Core 4 | 8 | 0x00001000 |
Core 5 | 16 | 0x00010000 |
Core 6 | 32 | 0x00100000 |
Core 7 | 64 | 0x01000000 |
Core 8 | 128 | 0x10000000 |
Just add the decimal values together for which core you want to use.
You can use the PowerShell code from within the cmd shell by calling the PowerShell process with the code as an argument. Separate commands with a semi-colon.
> powershell.exe "Get-Process notepad++ | Select-Object ProcessorAffinity"
ProcessorAffinity
-----------------
255
> powershell.exe "$p = Get-Process notepad++; $p.ProcessorAffinity = 13"
> powershell.exe "Get-Process notepad++ | Select-Object ProcessorAffinity"
ProcessorAffinity
-----------------
13
Here is a nicely detailed post on how to change a process's affinity:
Setting Processor Affinity @ EnergizedTech
archive.today / archive.org
(the broken images weren't important)
Get-Process java | ...
will change the affinity for all of the java processes. If you just want a specific instance of java, you will have to know and use the process ID. e.g. Get-Process -ID 8500
Otherwise, you will have to implement your own filtering. –
Mabellemable cmd /start "" /Affinity value
method, core affinity is actually hexadecimal. e.g., to launch a process using cores 13-16 on a 16-core CPU, the decimal value is 61440
and the hex is 0xF000
. Using 61440
will launch a process using cores 6,10 and 12 :-) rapidtables.com/convert/number/binary-to-hex.html is useful for quick conversion. –
Salmi The accepted answer works, but only for the first process in the list. The solution to that in the comments does not work for me.
To change affinity of all processes with the same name use this:
Powershell "ForEach($PROCESS in GET-PROCESS processname) { $PROCESS.ProcessorAffinity=255}"
Where 255
is the mask as given in the accepted answer.
Get-Process $processName | % { $_.ProcessorAffinity = $value; };
–
Blanche Get-Process $pn| %{ $_.ProcessorAffinity = $v}
/s –
Rochkind (ps $p).ProcessorAffinity=$v
/s –
Papeete Unexpected token 's'
, 2. The property 'ProcessorAffinity' cannot be found on this object.
. How is your command supposed to be used? –
Dehart ps $p | %{ $_.ProcessorAffinity = $v }
e.g. ps chrome | %{ $_.ProcessorAffinity = 63 }
–
Dehart /s
, which I did, I get the error number 2. Is it failing because the object is an array? –
Dehart ps
returns multiple processes. It's better to use your solution which handles both cases. No big deal because like I said I meant that comment more as a continuation of the "too verbose" rather than a real solution –
Papeete For anyone else looking for answers to this and not finding any, the solution I found was to use an app called WinAFC (or AffinityChanger). This is a partial GUI, partial command line app that allows you to specify profiles for certain executables, and will poll the process list for them. If it finds matching processes, it will change the affinity of those processes according to the settings in the loaded profile.
There is some documentation here: http://affinitychanger.sourceforge.net/
For my purposes, I created a profile that looked like this:
TestMode = 0
TimeInterval = 1
*\convert.exe := PAIR0+PAIR1
This profile sets any convert.exe process to use the first two CPU core pairs (CPU0, CPU1, CPU2, and CPU3), polling every second. TestMode
is a toggle that allows you to see if your profile is working without actually setting affinities.
Hope someone finds this useful!
If you really like enums, you can do it this way. ProcessorAffinity is an IntPtr, so it takes a little extra type casting.
[flags()] Enum Cores {
Core1 = 0x0001
Core2 = 0x0002
Core3 = 0x0004
Core4 = 0x0008
Core5 = 0x0010
Core6 = 0x0020
Core7 = 0x0040
Core8 = 0x0080
}
$a = get-process notepad
[cores][int]$a.Processoraffinity
Core1, Core2, Core3, Core4
$a.ProcessorAffinity = [int][cores]'core1,core2,core3,core4'
© 2022 - 2024 — McMap. All rights reserved.