Recent

Author Topic: Does anybody have CPU usage example code for Raspberry AND win32  (Read 572 times)

pascalbythree

  • Full Member
  • ***
  • Posts: 103
Hey i am looking for example code / fucntion or procedure. To read the CPU usage. I do have running versions from this forum, but they only see to work for Win32.

I am cross compiling from win32 to ARM. So they need to work both.

Further i do not like txt file reading or a TProcess

Anybody got help?

Greets, Wouter van Wegen

PS: It is for a Raspberry model 3
« Last Edit: May 24, 2022, 09:02:51 pm by pascalbythree »

PascalDragon

  • Hero Member
  • *****
  • Posts: 4123
  • Compiler Developer
Re: Does anybody have CPU usage example code for Raspberry AND win32
« Reply #1 on: May 25, 2022, 09:09:17 am »
It's less relevant that your compiling for ARM, but that your compiling for a different operating system, namely Linux (assuming you don't use Windows on ARM on your Pi), so you should search for how to retrieve the CPU usage on Linux (Hint: it's often called “system load” or similar, though that will be the total load across all cores).

And you'll simply have to ifdef your code and provide a suitable cross platform abstraction. Or maybe you'll find a cross platform abstraction, who knows. ;)


MarkMLl

  • Hero Member
  • *****
  • Posts: 4394
Re: Does anybody have CPU usage example code for Raspberry AND win32
« Reply #3 on: June 04, 2022, 10:17:54 am »
Please don't open duplicate posts. Somebody's already suggested a library in response to your earlier https://forum.lazarus.freepascal.org/index.php/topic,59344.msg442391.html#msg442391

I am cross compiling from win32 to ARM. So they need to work both.

Why? if you're /cross/ compiling then only the target matters so

Quote
PS: It is for a Raspberry model 3

...that's what matters, the RPi /does/ have capabilities for getting at voltages etc. but if you start messing around with those all hope of something portable goes out of the window.

Quote
Further i do not like txt file reading

Tough, since utilities like top and ps refer to items in the /proc filesystem rather than having a binary API. However that is NOT a real directory and you are NOT reading real text files.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BobDog

  • Sr. Member
  • ****
  • Posts: 266
Re: Does anybody have CPU usage example code for Raspberry AND win32
« Reply #4 on: June 04, 2022, 02:24:29 pm »

For windows, using the command line (wmic for example), itself takes a chunk of cpu every second.
The winapi method uses very little.
Sorry I don't have Linux, or know the Linux method, but I have a notion it might be easier than the Windows method.
But, if it is of any use to you, here is a winapi method.
Code: Pascal  [Select][+][-]
  1. uses
  2. windows,jwawinbase;
  3.  
  4. Function getsystemtime_CPU(var totalTime:qword;var idleTime:qword): boolean;
  5. var
  6. ftSysIdle, ftSysKernel, ftSysUser:filetime;
  7. sysKernel, sysUser, sysIdle:ularge_integer;
  8.         begin
  9.         If ((GetSystemTimes(@ftSysIdle, @ftSysKernel, @ftSysUser))=false) Then exit (false);
  10.         sysKernel.HighPart := ftSysKernel.dwHighDateTime;
  11.         sysKernel.LowPart := ftSysKernel.dwLowDateTime;
  12.         sysUser.HighPart := ftSysUser.dwHighDateTime;
  13.         sysUser.LowPart := ftSysUser.dwLowDateTime;
  14.         sysIdle.HighPart := ftSysIdle.dwHighDateTime;
  15.         sysIdle.LowPart := ftSysIdle.dwLowDateTime;
  16.         totalTime := sysKernel.QuadPart + sysUser.QuadPart;
  17.         idleTime := sysIdle.QuadPart;
  18.         exit(True);
  19. End;
  20.  
  21. procedure cpu;
  22. var
  23. totalprev:qword=0;
  24. totalcurr:qword=0;
  25. idleprev:qword=0;
  26. idlecurr:qword=0;
  27. total,idle:qword;
  28. usage:integer;
  29. s,use:ansistring;
  30. begin
  31. While True do
  32. begin
  33. s:='';
  34. total:=0;
  35. idle:=0;
  36.  Sleep(1000);
  37.  If ((getSystemtime_CPU(totalCurr, idleCurr))=true) Then
  38.         begin
  39.         total := totalCurr - totalPrev;
  40.         if (total>0) then
  41.         begin
  42.         idle := idleCurr - idlePrev;
  43.         usage:=trunc(100 * (total - idle) / (total));
  44.         Setlength(S,usage);
  45.           FillChar(S[1],usage,'*');
  46.            str(usage :3,use);
  47.         writeln('  ',use,' %','  ',s);
  48.         end;
  49.         totalPrev := totalCurr;
  50.         idlePrev := idleCurr;
  51.         end;   
  52. end;
  53. end;
  54.  
  55.  
  56.  
  57. begin
  58. cpu;
  59. end.
  60.  

Just click off the console when you are done.

 

TinyPortal © 2005-2018