浏览代码

+ linux implementation of System.GetCPUCount

git-svn-id: trunk@48106 -
florian 4 年之前
父节点
当前提交
d2b0bcdf1f
共有 3 个文件被更改,包括 37 次插入0 次删除
  1. 6 0
      rtl/linux/ossysc.inc
  2. 11 0
      rtl/linux/ostypes.inc
  3. 20 0
      rtl/linux/system.pp

+ 6 - 0
rtl/linux/ossysc.inc

@@ -732,3 +732,9 @@ begin
   fpsetrlimit:=do_syscall(syscall_nr_setrlimit,TSysParam(Resource),TSysParam(rlim));
 end;
 
+function FpSchedGetAffinity(pid : pid_t;cpusetsize : size_t;mask : pcpu_set_t) : cint;
+begin
+  FpSchedGetAffinity := do_syscall(syscall_nr_sched_getaffinity,TSysParam(pid),TSysParam(cpusetsize),TSysParam(mask));
+end;
+
+

+ 11 - 0
rtl/linux/ostypes.inc

@@ -430,6 +430,17 @@ type
   tiovec=iovec;
   piovec=^tiovec;
 
+  cpu_set_t = record
+{$ifdef CPU64}
+    __bits : array[0..15] of culong;
+{$else CPU64}
+    __bits : array[0..0] of culong;
+{$endif CPU64}
+  end;
+
+  tcpu_set_t = cpu_set_t;
+  pcpu_set_t = ^tcpu_set_t;
+
 {$if defined(cpupowerpc)}
 const
   { FP exception related constants for prctl(); PowerPC specific }

+ 20 - 0
rtl/linux/system.pp

@@ -113,6 +113,8 @@ procedure OsSetupEntryInformation(constref info: TEntryInformation); forward;
 
 {$endif FPC_LOAD_SOFTFPU}
 
+{$define HAS_GETCPUCOUNT}
+
 {$I system.inc}
 
 {$ifdef android}
@@ -458,6 +460,24 @@ Begin
   randseed:=longint(Fptime(nil));
 End;
 
+function GetCPUCount: LongWord;
+  var
+    cpus : tcpu_set_t;
+    BytesWritten,i : cint;
+  begin
+    Result := 1;
+    { same approach as nproc uses:
+      we return the number of available CPUs }
+    BytesWritten:=FpSchedGetAffinity(0,sizeof(cpus),@cpus);
+    if BytesWritten>0 then
+      begin
+        Result := 0;
+        for i:=0 to BytesWritten-1 do
+          Result:=Result+Popcnt((PByte(@cpus)+i)^);
+      end;
+  end;
+
+
 {*****************************************************************************
                                     cmdline
 *****************************************************************************}