Thoughts from the EPS Windows Server Performance Team
Useful Microsoft Blogs
Given that multi-core / multi-processor systems are the way of the future, this might seem like an odd post for us to be writing. However, we have run into a few instances where 32-bit applications run with no issues on single processor systems, but fail to run, or perform poorly on multiprocessor systems. I know that seems counterintuitive, but if an application is not multi-processor aware you may experience issues. In cases such as these, there are a couple of different approaches you might try:
Method 1: Setting processor affinity via Task Manager
If the application is already running, then you can use the “Set Affinity” feature within Task Manager on Windows XP and later operating systems. To use this feature, highlight the process within Task Manager, right-click and select the “Set Affinity” option as shown below:
You are then presented with the option to select your processor affinity:
Remember that this option is only available on Windows XP and later operating systems.
Method 2: Use IMAGECFG.EXE to permanently set the processor affinity for the executable (Windows 2000 Systems)
Using this method you can permanently bind an executable to a processor. IMAGECFG.EXE was available on the Windows 2000 Server Resource Kit (Supplement One). To set the processor affinity, use the following syntax from a Command Prompt: imagecfg –a 0xn <full path to the executable>. In our sample syntax, 0xn is the processor affinity mask. This mask is what restricts the application to a specific CPU. To determine the correct mask, use the table below:
You can use this syntax to set the affinity all the way up from processors 0 through 31. Remember that this would be the preferred method on a Windows 2000 system, where you cannot dynamically change the processor affinity via Task Manager.
Method 3: Use PsExec to set the Processor Affinity for an executable on Windows XP and later OS versions
In Method 1 we used Task Manager to set the processor affinity for an application that had already been launched. But, what happens if you need to launch the application with a specific processor affinity? You could certainly use Method 2, but you might have to hunt around for a copy of IMAGECFG.EXE. Instead you can use PsExec to pre-set your processor affinity. We normally use PsExec to execute programs on remote systems, but we can also use it to set processor affinity. The syntax is as follows: PsExec.exe –a 0 <path to executable>. The –a switch sets the affinity of the process – in this case to Processor 0. This isn't a permanent method of setting the affinity, although you could create a .cmd or .bat file that launched the executable using these parameters.
Method 4: Use the /AFFINITY switch (Windows Server 2003 and later)
You can also use the START command with the /AFFINITY switch to start the program with whichever processor affinity you wish to set. This is not a persistent method (unlike Method 2) – you would need to use the command each time, or launch the program via a batch file or a similar mechanism. The syntax for this command is as follows: START /AFFINITY <processor number> <program>. The processor number is defined by the same mask we used above in Method 2.
To verify that your process is started with the proper processor affinity, launch Task Manager once the process has launched. Right click on the process name and click on “Set Affinity” – the processor number that you set should be the one that is checked. Here’s an example from my Windows Server 2008 system – using Notepad as my test application I want to constrain Notepad to CPU 0.
Notepad launches successfully – now let’s check Task Manager:
And as we can see, Notepad was launched and is constrained to CPU 0.
With that we’ve come to the end of this post. As always – thanks for stopping by! Until next time …
- CC Hameed
I am not sure if you have tried to find IMAGECFG.EXE recently, but since Window 2000's Mainstream Support ended in 2005, it is very difficult to hunt down this tool. Is there a possibility to post a location to the current tool or have it "updated" so it could be available in the current OS's Resource Kit?
can i use imagecfg to sent affinity to more than one processor when using a quad core processor on WinXP sp3?
if yes, can you please tell me the command syntax.
I've noticed that some apps appear to jump back and forth between idle processors even when the system load is low. I've used affinity to deal with that.
Near as I can tell, it goes like this : a process on processor 1 takes 50% if its capacity. System notices processor is busier than processor 2 which is mostly idle and moves the process to processor 2. Next time it looks, processor 2 is busy and 1 is next to idle it moves the task back. It seems to waste a lot of time and resources playing process ping-pong. Setting the affinity makes the behaviour stop.
The end result is that you manage to keep 2 processors busy with a single threaded task. The appearance is that the scheduler needs changed. IE: only start moving tasks between processors one a processor gets fully loaded and tasks on it are clearly wait blocked.
I'm going to guess that the problem you describle of single threaded apps having problems is related to this game of process ping pong. Affinity might reduce the symptoms, but rewriting the scheduler properly would probably be the real solution.
While they are in there, once you get past 4 cores, it might not be such a bad idea to completely reserve one of the cores for OS only tasks..
My preferred method to permanently set the processor affinity for the executable is to use the Microsoft Application Compatibility Toolkit. It works on XP, Server 2003, and Vista.
I wrote a complete how-to with screen shots here:
very well writen but can you add some pictures?