Here’s an uncommon one for your day. Ever have to set the “Processor Affinity” for an application?
First I heard the term my draw dropped. More so because it came from our CoOp student who was a heavy online gamer. He knew how to tweak and tune his PC like a Master.
Processor Affinity (to keep it simple, because that’s how I think) is the ability to delegate a SPECIFIC core to an application.
You see by default, if an application in Windows supports multiple cores, it will try to share and work with all of them. Which is fine 80% of them.
The other 20% that application is a resource monster (Like a Virtualized Machine on a laptop) and you need to cap it’s power without killing it.
Enter Processor Affinity. On a Multi Core Server or Workstation I can allocate just ONE core (or more) to an application. Usually it’ll be the case that I’m taking away cores.
Finding out the present Affinity of an application is very easy. Just use GET-PROCESS
GET-PROCESS VirtualBox | Select-Object ProcessorAffinity
What this will do is show me the Affinity of my application VirtualBox (Sun Virtual Box) on my Windows 7 laptop. But the number that comes back is well… vague.
I got a 15, which sounds a bit like Charlie Brown saying “I got a Rock”
The affinity is returned as the Decimal version of a binary number. That’s all. So depending on how many Cores you have will show you the default number. IE:
1111 in Binary or 15 in Decimal
11 in Binary or 3 in Decimal
1111111111111111 in Binary means you have a really nice boss who’s letting you deal with a honking high End server :)
So if I need to restrict the ProcessorAffinity to a single core for an application we just need to set that Property on the application. But since there is no “SET-PROCESS” cmdlet, I guess I’m up the creek without a paddle.
Or Am I?
It turns out the Property “ProcessorAffinity” has both a GET and a SET. “SET” means I can change it.
So if I do a GET-PROCESS on Powershell and Store it results away in say, Oh $RESULTS
I can access the value of ProcessorAffinity by doing this
Ready for the easy part? I just assign the value TO $RESULTS.ProcessorAffinity and it’s done!
Will assign only the first core to the Application in Question. Remember it’s a Binary value you’re computing so to figure out the Binary number to use, you must add it in your head or draw it out
0001 = 1 ( CPU 1)
0010 = 2 ( CPU 2)
0100 = 4 ( CPU 3 )
1000 = 8 ( CPU 4 )
Therefore if I want to assign Core 1 and 4 to the Application, Add up the Binary numbers ( 1000 (8d) ) and ( 0001) (1d) ) to get 9
I concur this is not a COMMON need but if you needed to be able to script your Accounting application or more IMPORTANTLY control a system remotely and limit it’s action on the cores, So the production web server doesn’t get dragged down?
You can. And all from Powershell.
The Power of Shell is in YOU
The Energized Tech