Recent

Author Topic: GetCPUCount  (Read 7380 times)

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
GetCPUCount
« on: June 25, 2016, 05:03:08 am »
The System unit function GetCPUCount should "Return the number of cores on the system".

I am using a Dell notebook with a: Intel® Core™ i7-4500U CPU @ 1.80GHz × 4.  This processor has 2 cores (4 threads).

But the line

Code: Pascal  [Select][+][-]
  1. Writeln('the system has ',GetCPUCount(),' cores')

prints:

Quote
the system has 1 cores

shobits1

  • Sr. Member
  • ****
  • Posts: 278
  • .
Re: GetCPUCount
« Reply #1 on: June 25, 2016, 05:41:35 am »
works perfectly with my Core2Quad Q9400 and reports the correct number of cores

Quote
the system has 4 cores

Thaddy

  • Hero Member
  • *****
  • Posts: 10721
Re: GetCPUCount
« Reply #2 on: June 25, 2016, 07:44:37 am »
What OS? since the code used to determine the number of cores differs a lot. Windows? Linux?

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: GetCPUCount
« Reply #3 on: June 25, 2016, 08:34:18 am »
What OS? since the code used to determine the number of cores differs a lot. Windows? Linux?

Ubuntu 14.04 64-bit  - Free Pascal Compiler version 3.0.0 [2015/11/20] for x86_64

bytebites

  • Sr. Member
  • ****
  • Posts: 393
Re: GetCPUCount
« Reply #4 on: June 25, 2016, 08:38:44 am »
This code is executed on linux (from thread.inc)
Code: Pascal  [Select][+][-]
  1. {$ifndef HAS_GETCPUCOUNT}
  2.     function GetCPUCount: LongWord;
  3.       begin
  4.         Result := 1;
  5.       end;
  6. {$endif}

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: GetCPUCount
« Reply #5 on: June 25, 2016, 09:44:14 am »
This code is executed on linux (from thread.inc)
Code: Pascal  [Select][+][-]
  1. {$ifndef HAS_GETCPUCOUNT}
  2.     function GetCPUCount: LongWord;
  3.       begin
  4.         Result := 1;
  5.       end;
  6. {$endif}


Thanks! 

rvk

  • Hero Member
  • *****
  • Posts: 4472
Re: GetCPUCount
« Reply #6 on: June 25, 2016, 10:05:25 am »
On Linux you might try

Code: Pascal  [Select][+][-]
  1. uses UTF8Process;
  2.  
  3. var
  4.   cores: integer;
  5. begin
  6.   cores := GetSystemThreadCount;

Quote
function GetSystemThreadCount: integer; // guess number of cores

Abelisto

  • Jr. Member
  • **
  • Posts: 91
Re: GetCPUCount
« Reply #7 on: June 25, 2016, 11:35:43 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.     Unix;
  5.  
  6. procedure ExecAndEcho(const ACmd: string);
  7. var
  8.     t: Text;
  9.     s: string;
  10. begin
  11.     POpen(t, ACmd, 'R');
  12.     while not Eof(t) do
  13.     begin
  14.         Readln(t, s);
  15.         Writeln(s);
  16.     end;
  17.     PClose(t);
  18. end;
  19.  
  20. procedure GetCoreCount;
  21. begin
  22.     ExecAndEcho('nproc');
  23. end;
  24.  
  25. procedure GetProcInfo;
  26. begin
  27.     ExecAndEcho('lscpu');
  28. end;
  29.  
  30. procedure GetProcInfoEx;
  31. begin
  32.     ExecAndEcho('cat /proc/cpuinfo');
  33. end;
  34.  
  35. function get_nprocs:longint; cdecl; external 'c' name 'get_nprocs';
  36.  
  37. begin
  38.     Writeln('Core count from libc: ', get_nprocs);
  39.     Writeln('Core count ============');
  40.     GetCoreCount;
  41.     Writeln('Proc info =============');
  42.     GetProcInfo;
  43.     Writeln('Proc info ex ==========');
  44.     GetProcInfoEx;
  45. end.
  46.  
« Last Edit: June 25, 2016, 01:54:41 pm by Abelisto »
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: GetCPUCount
« Reply #8 on: June 27, 2016, 04:18:49 am »
On Linux you might try

Code: Pascal  [Select][+][-]
  1. uses UTF8Process;
  2.  
  3. var
  4.   cores: integer;
  5. begin
  6.   cores := GetSystemThreadCount;

Quote
function GetSystemThreadCount: integer; // guess number of cores

I am trying to compile this with FPC 3.0.0 and Lazarus 1.0.10 and I get an error:

Quote
PPU Loading /usr/lib/lazarus/1.0.10/lcl/units/x86_64-linux/utf8process.ppu
PPU Invalid Version 135
get_cores.pp(5,5) Fatal: Can't find unit UTF8Process used by get_cores
Fatal: Compilation aborted
Error: /usr/local/bin/ppcx64 returned an error exitcode

I assume that the Lazarus 1.0.10 unit is not compatible with the 3.0.0 compiler, but I am wondering what took place to make it incompatible.

I was able to compile the UTF8Process unit with 2.6.2 FPC, but it says it can't find GetSystemThreadCount, and looking at
/usr/lib/lazarus/1.0.10/lcl/utf8process.pp

I don't see that routine.
« Last Edit: June 27, 2016, 09:00:19 am by Pasqualish »

Pasqualish

  • Jr. Member
  • **
  • Posts: 68
Re: GetCPUCount
« Reply #9 on: June 27, 2016, 04:21:24 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.     Unix;
  5.  
  6. procedure ExecAndEcho(const ACmd: string);
  7. var
  8.     t: Text;
  9.     s: string;
  10. begin
  11.     POpen(t, ACmd, 'R');
  12.     while not Eof(t) do
  13.     begin
  14.         Readln(t, s);
  15.         Writeln(s);
  16.     end;
  17.     PClose(t);
  18. end;
  19.  
  20. procedure GetCoreCount;
  21. begin
  22.     ExecAndEcho('nproc');
  23. end;
  24.  
  25. procedure GetProcInfo;
  26. begin
  27.     ExecAndEcho('lscpu');
  28. end;
  29.  
  30. procedure GetProcInfoEx;
  31. begin
  32.     ExecAndEcho('cat /proc/cpuinfo');
  33. end;
  34.  
  35. function get_nprocs:longint; cdecl; external 'c' name 'get_nprocs';
  36.  
  37. begin
  38.     Writeln('Core count from libc: ', get_nprocs);
  39.     Writeln('Core count ============');
  40.     GetCoreCount;
  41.     Writeln('Proc info =============');
  42.     GetProcInfo;
  43.     Writeln('Proc info ex ==========');
  44.     GetProcInfoEx;
  45. end.
  46.  

I have no idea what it is doing in the first procedure, but that works. Although it gives 4 - making it a GetHardwareThreadCount on my machine but I guess hardware threads are considered CPUs in most cases.
lscpu lists (for my 2 core - 2 thread per core processor):
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 69
Stepping:              1
CPU MHz:               768.000
BogoMIPS:              4788.93
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              4096K
NUMA node0 CPU(s):     0-3
« Last Edit: June 27, 2016, 03:49:14 pm by Pasqualish »

shobits1

  • Sr. Member
  • ****
  • Posts: 278
  • .
Re: GetCPUCount
« Reply #10 on: June 27, 2016, 04:56:34 am »
Quote
http://www.unix.com/man-page/linux/3/get_nprocs/
....
The function get_nprocs() returns the number of processors currently available in the system.
.....
....
The current implementation of these functions is rather expensive, since they open and parse files in the /sys filesystem each time they are called.
get_nprocs() parses "/sys/devices/system/cpu/online" , "/proc/stat" and "/proc/cpuinfo"
source https://sourceware.org/git/?p=glibc.git
« Last Edit: June 27, 2016, 05:44:28 am by shobits1 »

Thaddy

  • Hero Member
  • *****
  • Posts: 10721
Re: GetCPUCount
« Reply #11 on: June 27, 2016, 09:54:11 am »
Well:
this does not work and returns 1 on my Pi 3 (quad core)
Code: Pascal  [Select][+][-]
  1. program cpuc;
  2. begin
  3. writeln(getcpucount);
  4. end.

But this works under linux:
Code: Pascal  [Select][+][-]
  1. program testcores;
  2. uses initc,ctypes;
  3. function sysconf(i:cint):clong;cdecl;external name 'sysconf';
  4.  
  5. begin
  6. writeln('We have ',sysconf(83),' cores'); //83 should be beautified into a nice const
  7. end.
This returns proper core counts on all my linux boxes, both arm and x86_64
So should I submit a patch?
« Last Edit: June 27, 2016, 10:01:33 am by Thaddy »

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4000
  • I like bugs.
Re: GetCPUCount
« Reply #12 on: June 27, 2016, 10:31:45 am »
This returns proper core counts on all my linux boxes, both arm and x86_64
So should I submit a patch?

function GetSystemThreadCount: integer;
in unit UTF8Process in LazUtils implements it also for Solaris, FreeBSD, Darwin and WinCE.
It is made by Mattias.
A patch for FPC sources implementing those platforms would be good. I hope they would apply it.
Apparently FPC now implements GetCPUCount only for Windows and OS/2.
« Last Edit: June 27, 2016, 11:32:11 am by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 10721
Re: GetCPUCount
« Reply #13 on: June 27, 2016, 10:58:30 am »
IMO it is definitely not a high level functionality so it should be in the FPC provided libraries, even in system.
For OP this should work, however.
[edit]
Note that sysconf seems to be posix, but:
http://linux.die.net/man/3/sysconf says the cpu count is not a standardized option.
« Last Edit: June 27, 2016, 11:06:35 am by Thaddy »

 

TinyPortal © 2005-2018