Recent

Author Topic: FPC commandline read CPU utilization  (Read 1063 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 255
FPC commandline read CPU utilization
« on: May 14, 2022, 06:47:32 pm »
Does anybody have a component name or example code to monitor the CPU utilization of my Raspberry for FPC in the command line?

Maybe i can strip a component ?

NumCPULib4Pascal only reads the processor count, and RPI_HAL does not have utilization percentages in it.

https://www.pragmaticlinux.com/2020/06/check-the-raspberry-pi-cpu-temperature/

This URL is only for temperature.

Does anybody have alternatives?

Greets, Wouter van Wegen

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: FPC commandline read CPU utilization
« Reply #1 on: May 15, 2022, 01:32:28 pm »
Maybe you can make use: https://www.idnt.net/en-GB/kb/941772

Linux say: everything is file, so can open /proc/stat like file, read text, convert like number, make math as link write and print answer.

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: FPC commandline read CPU utilization
« Reply #2 on: June 04, 2022, 03:49:07 pm »
May be for you translate difficult ?

This literal translate:
Code: Pascal  [Select][+][-]
  1. program linux_cpu; // https://www.idnt.net/en-GB/kb/941772
  2. {$MODE OBJFPC}{$H+}
  3. uses sysutils;
  4.  
  5. function givehead:string;
  6. var
  7.   head: string absolute givehead;
  8.   procstatfile:Text;
  9.   oldfilemode:longint;
  10. begin
  11.   head:='';
  12.   oldfilemode:=FileMode;
  13.   Filemode:=fmOpenread;
  14.   Assign(procstatfile,'/proc/stat');
  15.   Reset(procstatfile);
  16.   ReadLn(procstatfile,head);
  17.   Close(procstatfile);
  18.   FileMode:=oldfilemode
  19. end;
  20.  
  21. procedure cpu_utilization(times:integer=100);
  22. type
  23.   tcpu_values=array[1..9]of longint;
  24. var
  25.   i,n:integer;
  26.   timesleft:boolean;
  27.   head:string;
  28.  
  29.   cpu_now,
  30.   cpu_last:tcpu_values;
  31.   cpu_sum,
  32.   cpu_last_sum,
  33.   cpu_delta,
  34.   cpu_idle,
  35.   cpu_used:longint;
  36.   cpu_usage:single;
  37. begin
  38.   n:=0;
  39.   timesleft:=n<times;
  40.   cpu_last:=default(tcpu_values);
  41.   cpu_last_sum:=0;
  42.  
  43.   while timesleft do
  44.   begin
  45.     // loop timesleft
  46.     inc(n);
  47.     timesleft:=n<times;
  48.  
  49.     // Get the first line with aggregate of all CPUs
  50.     head:=givehead;
  51.  
  52.     // Get all columns but skip first (which is the "cpu" string)
  53.     head:=stringreplace(head,'cpu','',[]);
  54.     readstr(head,cpu_now[1],cpu_now[2],cpu_now[3],cpu_now[4],cpu_now[5],cpu_now[6],cpu_now[7],cpu_now[8],cpu_now[9]);
  55.  
  56.     // Sum cpu_sum
  57.     cpu_sum:=0;
  58.     for i:=low(cpu_now)to high(cpu_now)do
  59.       cpu_sum:=cpu_sum+cpu_now[i];
  60.  
  61.     // Get the delta between two reads
  62.     cpu_delta:=cpu_sum-cpu_last_sum;
  63.  
  64.     // Get the idle time Delta
  65.     cpu_idle:=cpu_now[4]-cpu_last[4];
  66.  
  67.     // Calc time spent working
  68.     cpu_used:=cpu_delta-cpu_idle;
  69.  
  70.     // Calc percentage
  71.     cpu_usage:=100*cpu_used/cpu_delta;
  72.  
  73.     // Keep this as last for our next read
  74.     cpu_last:=cpu_now;
  75.     cpu_last_sum:=cpu_sum;
  76.  
  77.     writeln('CPU usage at ',cpu_usage:3:2,'%');
  78.  
  79.     // wait a second before the next read
  80.     sleep(1*1000)
  81.   end
  82. end;
  83.  
  84. begin
  85.   cpu_utilization(10)
  86. end.
  87.  

 

TinyPortal © 2005-2018