Change a process priority and affinity with PowerShell

Yesterday I was using a virtual machine from my Hyper-V server and I noticed it became sluggish at one point. A quick look at the task manager and it was clearly Windows Update who was slurping away my CPU power. There is no real point in killing the process but it should not interfere as much with my work as it did.

To tame the Windows Update task inside my 2 vCPU virtual machine I wanted to lower the priority of the Windows Update process and let it run on 1 core instead of both cores. PowerShell to the rescue!

Why change Affinity ?

You can always change the priority of a process with just a few clicks in the task manager but if you have a multi-core system fiddling with affinity probably will have more impact on your experience. Why?

Lets assume Task A will consume 237 units of time. Changing priority or affinity will not reduce the 237 units it takes to do the task. That’s just not gonna happen, it needs the 237 units to calculate the job one way or the other. How fast those units are calculated and what room is left for other tasks if a different story.

To keep things simple lets say your CPU is spinning on the same speed and waiting for tasks to do. Your browser or something else you are doing (lets call it Task B) needs 42 units of time to do his job you just instructed it to do. 237 vs 42 units. You can guess with which task your system will be the most active if priority is equal and both CPU’s are working on both tasks.

In theory you should not really notice a slow down because tasks are spread around both cores and the workload of Task A is given an amount of time before switching to Task B and back again to A, etc. But who here can say they never noticed Windows Update is working on the background and is slowing down your pc. If your pc is not really powerful and has plenty of headroom you will notice your pc feels a bit slower.

With changing the affinity you are disallowing a process to spread across all available cores. By limiting Windows Update to one core you will free the other core of this task. Your Task B won’t have to share one core with Task A. This means your Task B can use one CPU (or more if your pc has them) without the interruption of Windows Update. Because we haven’t changed the affinity of other processes on our system other tasks will still use all CPU’s. Tasks will take a bit longer on the busy CPU but they still will get some time to do their job there and combined with the Windows Update free CPU it will feel better than without changing affinity.

This can make your pc feel a lot smoother if it’s running low on ‘unused speed’ when updating Windows.

Change the priority and affinity of Windows Update

 

Open a PowerShell window with elevated permissions and paste the following:

Get-Process TiWorker,TrustedInstaller | ForEach-Object { 
$_.ProcessorAffinity=2 
$_.PriorityClass='BelowNormal'
}

This will make the current session of Windows Update running on a lower priority and limiting it to the 2nd core of your system.

Affinity

Explaining affinity is hard. Binary CPU masking, converting to hexadecimal and so on. Pretty sure not many people would be interested in this.

Here is a table on with values you can use for a 8 core processor.

CPU ID

Associated value (n)

Formula (2n-1)

Affinity in Hex (h)

CPU 0

1

1

1

CPU 0 and 1

2

3

3

CPU 0, 1, and 2

4

7

7

CPU 0, 1, 2, and 3

8

15

F

CPU 0, 1, 2, 3, and 4

16

31

1F

CPU 0, 1, 2, 3, 4, and 5

32

63

3F

CPU 0, 1, 2, 3, 4, 5, and 6

64

127

7F

CPU 0, 1, 2, 3, 4, 5, 6, and 7

128

255

FF

If you want to make other combinations the easiest way is to open the Windows 10 Calculator and change it to Programmer mode (Alt- 3)

Here are some screenshots on how to get desired Hex value with the calculator app.

CPU-Cacl-1CPU-Cacl-2CPU-Cacl-3

Where there is a 1 on your binary CPU layout the process will be active on that CPU.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s