[fpc-pascal] Re: GetAffinity\SetAffinity

Brian vmst at golden.net
Tue Nov 19 22:01:24 CET 2013


After a bit of research , the issue of setting the cpu affinity has been
solved , which may be of use to other folks.

A bit of info here concerning the data type cpu_set_t , which as far as I
can determine (no help from the mess that is the c library source) is
essentially an array of DWORD. For my purposes running on an x86 CPU ,
allowance for 32 cores is sufficient , using cpu_set_1 as one unsigned long
word.

http://www.cyberciti.biz/tips/setting-processor-affinity-certain-task-or-process.html

Here are some code fragments which may be of help.


The function do_SysCall(0) basically doesn't work for setting the CPU core.

Although the Linux documentation claims sched_setaffinity() amd
sched_getaffinity() work for threads  , it only works for processes. 

pthread_getaffinity_np() and pthread_setaffinity_np() work properly for
threads.

FPC has some sloppy code as there is a note not to use PtrInt , instead use
Ptruint , but the function BeginThread() returns PtrInt , but as long as you
are not aritmetically manipulating the pointer it works ok.

With a dual core Intel CPU with all cores enabled in the BIOS ,
sched_getaffinity() returns $03 (0011) and if you set process 0 to use only
1 core , sched_getaffinity returns $01 correctly.

Same for thread_getaffinity_np() and  pthread_setaffinity_np() , although if
you set the CPU process 0 (current process) to one core , all the threads
run on that core.

// ------- external functions from c-library libc (not the UNIT libc !
)----------------------------------------
function sched_getaffinity(pid : Ptruint; cpusetsize : longint; cpuset :
pointer) : longint; cdecl; external;

function sched_setaffinity(pid : Ptruint; cpusetsize : longint; cpuset :
pointer) : longint; cdecl; external;

function pthread_setaffinity_np(pid : Ptruint; cpusetsize : longint; cpuset
: pointer) : longint; cdecl; external;
                                  
function pthread_getaffinity_np(pid : Ptruint; cpusetsize : longint; cpuset
: pointer) : longint; cdecl; external;
//-----------------------------------------------------------------------------------------------------------------------

 

procedure  Set_Thread_CPU_Core(ThreadID : PtrInt; CPU_Core : longword);

const
 cpu_SetSize = 4; // 32 cores max
 
var
 cpu_Mask : longword;
 cpu_set : longword;  //cpu_set_type sufficient for 32 core CPU
 ResultX : longint;
  
begin

 ResultX := pthread_getaffinity_np(ThreadID,cpu_SetSize, at cpu_set);

 writeln('Result ',ResultX,' ');
 writeln('Mask   ',Hexl(cpu_set));

 cpu_set := $01; // CPU 0
 ResultX := pthread_setaffinity_np(ThreadID,cpu_SetSize, at cpu_set) ;

 cpu_set := 0; // clear it in case pthread_setAffiniti_np() above = -1
(fail)
 ResultX := pthread_getaffinity_np(ThreadID,cpu_SetSize, at cpu_set) ;

 writeln('Result ',ResultX,' ');
 writeln('Mask   ',Hexl(cpu_set));
 
end;

procedure  Set_Process_CPU_Core(ProcessID : PtrInt; CPU_Core : longword);

const
 cpu_SetSize = 4; // 32 cores max
 
var
 cpu_Mask : longword;
 cpu_set : longword;  //cpu_set_type sufficient for 32 core CPU
 ResultX : longint;
  
begin
writeln('------ Process -----------');
 ResultX := sched_getaffinity(ProcessID,cpu_SetSize, at cpu_set);

 writeln('Result ',ResultX,' ');
 writeln('Mask   ',Hexl(cpu_set));

 cpu_set := CPU_Core; 
 ResultX := sched_setaffinity(ProcessID,cpu_SetSize, at cpu_set) ;

 cpu_set := 0; // clear it in case sched_setAffinity() above = -1 (fail)
 ResultX := sched_getaffinity(ProcessID,cpu_SetSize, at cpu_set) ;

 writeln('Result ',ResultX,' ');
 writeln('Mask   ',Hexl(cpu_set));
 writeln('-------------------------');
end;



--
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/GetAffinity-SetAffinity-tp3351231p5717539.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.



More information about the fpc-pascal mailing list