Recent

Author Topic: NumCPULib4Pascal  (Read 1902 times)

Xor-el

  • Sr. Member
  • ****
  • Posts: 384
NumCPULib4Pascal
« on: August 03, 2019, 11:34:48 am »
NumCPULib4Pascal is a Library that enables you to Count the number of CPUs on the current machine.
This Library aims to provide support for those Oses that are not supported by System.CPUCount ( or those in which System.CPUCount defaults to 1).

Code: Pascal  [Select]
  1. uses
  2.   NumCPULib;
  3.  
  4. var
  5.   lcc, pcc: Int32;
  6. begin
  7. // count logical cpus (aka logical processors)
  8.  lcc := TNumCPULib.GetLogicalCPUCount();
  9. // count physical cpus (aka cores)
  10.  pcc := TNumCPULib.GetPhysicalCPUCount();
  11. end;
  12.  

Supported Oses
  • Linux
  • Raspberry PI (thanks to Thaddy for testing).
  • MSWindows
  • Mac OSX
  • Android
  • iOS
  • FreeBSD
  • NetBSD
  • OpenBSD
  • DragonFlyBSD

https://github.com/Xor-el/NumCPULib4Pascal
« Last Edit: August 07, 2019, 06:37:41 pm by Xor-el »

totya

  • Hero Member
  • *****
  • Posts: 577
Re: NumCPULib4Pascal
« Reply #1 on: August 03, 2019, 11:51:15 am »
NumCPULib4Pascal is a Library that enables you to Count the number of CPUs on the current machine.
This Library aims to provide support for those Oses that are not supported by System.CPUCount ( or those in which System.CPUCount

Hi, thank you for this new library!

The UsageSamples.lpr (in FreePascal.Samples lol?) works without any problem on Win7 x64, compiled either x86 or x64, and show my proc core count: 2.

Ony few unimportant messages come in under compile (as usually):
Quote
Compile Project, Target: UsageSamples.exe: Success, Hints: 3
NumCPULib.pas(367,24) Hint: Local variable "LSystemAffinityMask" does not seem to be initialized
NumCPULib.pas(366,68) Hint: Local variable "LProcessAffinityMask" does not seem to be initialized
NumCPULib.pas(382,30) Hint: Local variable "LSystemInfo" does not seem to be initialized
501 lines compiled, 0.2 sec, 72560 bytes code, 4196 bytes data
« Last Edit: August 03, 2019, 11:56:29 am by totya »

Xor-el

  • Sr. Member
  • ****
  • Posts: 384
Re: NumCPULib4Pascal
« Reply #2 on: August 03, 2019, 12:01:29 pm »
@totya, Thanks, have fixed the compiler warnings with a commit just now.
« Last Edit: August 03, 2019, 12:08:25 pm by Xor-el »

totya

  • Hero Member
  • *****
  • Posts: 577
Re: NumCPULib4Pascal
« Reply #3 on: August 03, 2019, 12:08:35 pm »
No warning messages come in with rev3, when I compile the UsageSamples.lpr (lazarus project file) under FreePascal.Samples directory, thanks!

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7639
Re: NumCPULib4Pascal
« Reply #4 on: August 03, 2019, 01:32:22 pm »
Hmm, it shows 8 on my 4 core (and probably will show 12 on my 6 core) due to hyperthreading.  The logical count number is not very useful, usually the number of worker threads to spawn is based on physical count (and in case of hyperthreading a slightly higher percentage on top of that)

is there some way to get the physical count?


Xor-el

  • Sr. Member
  • ****
  • Posts: 384
Re: NumCPULib4Pascal
« Reply #5 on: August 03, 2019, 02:08:21 pm »
Hmm, it shows 8 on my 4 core (and probably will show 12 on my 6 core) due to hyperthreading.  The logical count number is not very useful, usually the number of worker threads to spawn is based on physical count (and in case of hyperthreading a slightly higher percentage on top of that)

is there some way to get the physical count?

for now, only Logical CPU Count is supported by this library.
Physical CPU Count is possible but is supported only on Linux, mac OS and Windows platforms (at least from the little research I have made).

totya

  • Hero Member
  • *****
  • Posts: 577
Re: NumCPULib4Pascal
« Reply #6 on: August 03, 2019, 02:22:40 pm »
is there some way to get the physical count?

I thinking on it about few years ago... the REAL CORE (OS usable) detect method is very simple on any OS.. Just open and execute a new thread with any time-measurable operation. About: if the exists thread(s) execution time significantly decreased or the new thread speed is significantly ower than previous thread speeds, then the new thread can't assign to new cpu core, so you get the real cpu core count.

Certainly, when run this measure, no other app running with significant cpu usage... but to tell the truth, when your app is started, you need to know, what is the FREE and usable CPU core count, isn't it? ;)

Xor-el

  • Sr. Member
  • ****
  • Posts: 384
Re: NumCPULib4Pascal
« Reply #7 on: August 07, 2019, 12:31:14 pm »
v1.2 Update

added support for physical CPU Count.

Supported Oses for Physical CPU Count are same as that of Logical CPU Count with the exception of the BSD family (FreeBSD, NetBSD, OpenBSD, DragonFlyBSD).

Linux boxes are supported but I have not been able to test on RPI. if anyone can do that, I will be grateful.

also @marcov, can you please confirm that the Physical CPU Count are corrected reported on your Boxes with this latest update?

Thaddy

  • Hero Member
  • *****
  • Posts: 9303
Re: NumCPULib4Pascal
« Reply #8 on: August 07, 2019, 01:31:22 pm »
I will test the RPi and will report back.
also related to equus asinus.

Xor-el

  • Sr. Member
  • ****
  • Posts: 384
Re: NumCPULib4Pascal
« Reply #9 on: August 07, 2019, 01:40:03 pm »
I will test the RPi and will report back.

Thanks. :)

winni

  • Hero Member
  • *****
  • Posts: 607
Re: NumCPULib4Pascal
« Reply #10 on: August 07, 2019, 05:12:40 pm »
Hi!

There was an example how to count CPUs  in the wiki, now it is an empty page.  It was at
http://wiki.freepascal.org/Example_of_multi-threaded_application

It could count only logic CPUs, not physical. It worked on Linux, Darwin, Windows, Solaris and FreeBSD as I remember.

If it helps I can start a search in the deepth of my hardisk - must be somewhere.

Winni

Xor-el

  • Sr. Member
  • ****
  • Posts: 384
Re: NumCPULib4Pascal
« Reply #11 on: August 07, 2019, 05:39:28 pm »
Hi!

There was an example how to count CPUs  in the wiki, now it is an empty page.  It was at
http://wiki.freepascal.org/Example_of_multi-threaded_application

It could count only logic CPUs, not physical. It worked on Linux, Darwin, Windows, Solaris and FreeBSD as I remember.

If it helps I can start a search in the deepth of my hardisk - must be somewhere.

Winni

@Winni, I really don't know about that but my library aim to support more than just logical CPUs count, it supports Physical CPUs count too.
Also it is Delphi and FPC compatible too.
« Last Edit: August 07, 2019, 06:43:26 pm by Xor-el »

totya

  • Hero Member
  • *****
  • Posts: 577
Re: NumCPULib4Pascal
« Reply #12 on: August 07, 2019, 05:39:43 pm »
Windows 7 result:

Quote
Logical CPU Count is 2
Physical CPU Count is 2

It's okay. But to tell the truth, I have one CPU with two cores. ;)

Xor-el

  • Sr. Member
  • ****
  • Posts: 384
Re: NumCPULib4Pascal
« Reply #13 on: August 07, 2019, 05:40:34 pm »
Windows 7 result:

Quote
Logical CPU Count is 2
Physical CPU Count is 2

It's okay. But to tell the truth, I have one CPU with two cores. ;)

Was this by any chance run in a VM?
By the way, this is strange, it works beautifully for me on my windows box reporting 2 physical CPUs and 4 logical CPUs.
« Last Edit: August 07, 2019, 05:53:37 pm by Xor-el »

Thaddy

  • Hero Member
  • *****
  • Posts: 9303
Re: NumCPULib4Pascal
« Reply #14 on: August 07, 2019, 05:47:02 pm »
Works perfectly fine on the Raspberry Pi (all versions)
Used this code, because I could not stand the superfluous format() use  ;D ;D
Code: Pascal  [Select]
  1. program cpulibtest;
  2. uses
  3.   numcpulib;
  4. begin
  5.   writeLn('Logical CPU Count is ', TNumCPULib.GetLogicalCPUCount);
  6.   writeLn('Physical CPU Count is ', TNumCPULib.GetPhysicalCPUCount);
  7. end.

Outputs on RPi4 (and 2 and 3):
Code: Bash  [Select]
  1. Logical CPU Count is 4
  2. Physical CPU Count is 4

Raspberry Pi 1 B and zero/w:
Code: Bash  [Select]
  1. Logical CPU Count is 1
  2. Physical CPU Count is 1
« Last Edit: August 07, 2019, 05:57:59 pm by Thaddy »
also related to equus asinus.