Posted by: Vinu Baby | August 25, 2008

How to run an app on one processor only

http://www.tuttomontefalco.it/forum/phpBB2/set-affinity-use-only-one-cpu-vt982.html

Abstract: Here an answer to a very interesting question. With this explanation you can choose programmatically the CPUs on which run a certain process.
Q. I was wondering if you knew of tools or a way to assign an application to a logical CPU? We want to optimize a new feature with the Intel(R) Xeon(TM) processor and hyper-threaded processors where logical CPU utilization is monitored.

A. If you are trying to specify which processor a process (application) should run on from a system administrator point of view, where you do not have the ability to modify the application itself, and you are using a Microsoft* OS, you can simply use Windows* Task Manager. When the process is running press Ctrl+Shift+ESC, click the Processes tab, select the name of the process, right click, and choose “Set Affinity…”. From there you can specify which processor you want the entire application to run on. I also know that there is a Microsoft* Windows* 2000 Datacenter Server version of their OS which has sophisticated controls over which process and threads run on which processors.

Programmatically, if you are using a Microsoft* OS, the Microsoft* Platform SDK contains APIs that allow developers to control which processor their process or threads will run on. The SetProcessAffinityMask http://msdn.microsoft.com/library/en-us/dllproc/base/setprocessaffinitymask.asp function is used to force all threads of the process to execute on the designated processor. The SetThreadAffinityMask http://msdn.microsoft.com/library/en-us/dllproc/base/setthreadaffinitymask.asp function forces a particular thread to only be scheduled on a specific processor. You will need to use GetProcessAffinityMask http://msdn.microsoft.com/library/en-us/dllproc/base/getprocessaffinitymask.asp to determine which processors are available to your process, since as we mentioned earlier, the system administrator can limit which processors are available to the process.

There is also an Platform SDK API called SetThreadIdealProcessor http://msdn.microsoft.com/library/en-us/dllproc/base/setthreadidealprocessor.asp which doesn’t force the thread to run on the specific processor, but rather strongly suggests it to the OS. The OS can still run that thread on other processors, but would attempt when possible to run it on the designated processor.

You can use the GetSystemInfo http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp function to determine the number of processors on the computer. However to determine which processors logical processors are associated with which physical processor, we recommend the Intel(R) Developer Services article “Detecting Support for Hyper-Threading Technology Enabled Processors” available at: http://www.intel.com/cd/ids/developer/asmo-na/eng/technologies/threading/hyperthreading/20416.htm
That article includes sample code that determines how many processors are on the system, whether they are HT enabled, and which logical processors belong to which physical processor. Also you will notice that to accomplish this, the sample uses SetProcessAffinityMask.

Programmatically on a Linux* OS, there are some thread affinity APIs included with Red Hat* Linux* 9.0 and a patch that can be applied to the Red Hat* Linux* 8.0 distribution. Details on the thread affinity patch are available at http://www.kernel.org/pub/linux/kernel/people/rml/cpu-affinity. The thread APIs:

int sched_setaffinity (pid_t pid, unsigned int len, unsigned long *new_mask_ptr)
int sched_getaffinity(pid_t pid, unsigned int len,unsigned long *user_mask_ptr)

 

Here’s a working code that sets and gets the affinity mask. It does this for the current process, but you can simply change it to use another process handle. It’s written in C++ and works only under Windows. There is a similar code for Linux/Unix too.
Hope it helps!!

 

Codice:
//Gets the current process handle
HANDLE hProc = GetCurrentProcess();
      DWORD procMask;
      DWORD sysMask;
      HANDLE hDup;
      DuplicateHandle(hProc,
                    hProc,
                    hProc,
                    &hDup,
                    0,
                    FALSE,
                    DUPLICATE_SAME_ACCESS);//Gets the current process affinity mask
      GetProcessAffinityMask(hDup,&procMask,&sysMask);//new Mask, uses only the first CPU
      DWORD newMask = 2;

//Set te affinity mask for the process
      BOOL res = SetProcessAffinityMask(hDup,(DWORD_PTR)newMask);

      if(res == 0 )
      {
         //Error setting affinity mask!!
      }

 

 

I’ve come across this problem several times in XP and Vista, there is a workaround but it’s a bit awkward.

Open Task Manager using ctrt+alt+del or by typing taskman in the start/run box, if i remember correctly for XP.

Go to the processes tab and find the process you want to to run on just 1 core, firefox.exe for example, right click the process and select Set Affinity. You will see a dialogue box saying “The Processor Affinity settings controls which CPUs the process will be allowed to execute on.”

With core2 you should see two checkboxes with CPU0 and CPU1 both checked, uncheck one and the process will only use one core.

Hope this helps, or at least points you in the right direction.

If anyone knows of an easier way to do this please post as this isn’t an ideal way of doing it and i believe there must be a better way.

 

 

http://www.mlin.net/SMPSeesaw.shtml

SMP Seesaw

SMP Seesaw is a utility for dual-CPU or dual-core computers that controls how Windows balances the compute load between the two processors. By default, Windows balances the processing load to both CPUs as evenly as possible among all programs. SMP Seesaw provides a convenient means to change the processor affinities for all running programs at once. In particular, the most common use is to dedicate one of your processors to a single program, thereby improving the performance or responsiveness of that program.

Full-screen games, software-based DVD playback, PVR programs like SageTV and BeyondTV, and VMware are examples of applications that might benefit from having exclusive access to one of your CPUs. Of course, not all programs would benefit: multi-threaded programs that are designed to take full advantage of several processors would actually be impeded. However, relatively few desktop applications are designed like this.

System Requirements: To use SMP Seesaw, your computer must have exactly two processors or cores, and you must be running Windows NT, 2000, 2003, or XP.

 

Start applications on specific CPU and priority

There is a great little utility that has been around for ages in called the start command. This dos command allows you to start any process with advanced settings. In Vista it is perfect if you want to start an application and tell to only run it on one CPU and at a high priority level. For example, let’s say that you want to start Microsoft Paint and have it run on your second CPU core at Above Normal priority. The command below will accomplish this:

start /affinity 2 /abovenormal mspaint.exe

You can customize the command above and replace the 2 with the processor number (in hex) that you want the processor to run on. You can also adjust the priority level by using one of the flags below:

/low

 

 

 


Leave a response

Your response:

Categories