Indice del forum
 FAQ   Cerca   Lista utenti   Gruppi   Registrati   Profilo   Messaggi privati   Log in 
Set affinity: use only one CPU

 
Nuovo argomento   Rispondi    Indice del forum -> Programmazione: c#, c++, Java, HTML, PHP, Javascript...
Precedente :: Successivo  

Autore

Messaggio

cali1981
Site Admin


Registrato: 16/01/06 22:01
Messaggi: 827

MessaggioInviato: Mer Feb 07, 2007 10:11 am    Oggetto: Set affinity: use only one CPU

Rispondi citando


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)
_________________
Visita anche il sito Agriturismo Umbria per maggiori informazioni sull'Umbria!

Realizzazione siti web e applicazioni ASp.NEt, C/C++, C#


L'ultima modifica di cali1981 il Mer Feb 07, 2007 11:25 am, modificato 1 volta

Top

Profilo Invia messaggio privato Invia e-mail

cali1981
Site Admin


Registrato: 16/01/06 22:01
Messaggi: 827

MessaggioInviato: Mer Feb 07, 2007 11:22 am    Oggetto:

Rispondi citando


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!!
      }

_________________
Visita anche il sito Agriturismo Umbria per maggiori informazioni sull'Umbria!

Realizzazione siti web e applicazioni ASp.NEt, C/C++, C#

Top

Profilo Invia messaggio privato Invia e-mail

Mostra prima i messaggi di:   
Nuovo argomento   Rispondi    Indice del forum -> Programmazione: c#, c++, Java, HTML, PHP, Javascript... Tutti i fusi orari sono GMT
Pagina 1 di 1

 
Vai a:  
Non puoi inserire nuovi argomenti
Non puoi rispondere a nessun argomento
Non puoi modificare i tuoi messaggi
Non puoi cancellare i tuoi messaggi
Non puoi votare nei sondaggi
Forum del sito TuttoMontefalco.it - Umbria - Italy topic RSS feed 


Torna al sito TuttoMontefalco.it


Powered by phpBB © 2001, 2005 phpBB Group
phpbb.it

SoftGreen 1.1 phpBB theme by DaTutorials.com
Copyright © DaTutorials 2005