Recent

Author Topic: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux  (Read 1132 times)

finalist

  • Jr. Member
  • **
  • Posts: 99
I am searching Components about 64 PC CPU usage and PC RAM usage example code for Linux.
If there is that kind of components, I will install Lazarus 64 on my MX 21.2 Linux.
Thanks !
« Last Edit: November 12, 2022, 07:10:09 pm by finalist »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #1 on: November 12, 2022, 09:25:52 pm »
You do not need a component for that (I highly recommend to not even look for it). In Unixes everything is a file, so:
Code: Pascal  [Select][+][-]
  1. program showcpuandmem;
  2. {$mode objfpc}{$h+}{$ifndef unix}{$error this is Unix flavors only}{$endif}
  3. uses classes;
  4. var
  5.   mem,cpu:TStrings; // this is a later edit, it read Tstringlist, but declaring it like this you can put the result in any Tstrings descendent
  6. begin
  7.   mem := TStringlist.Create;
  8.   try
  9.     mem.LoadFromFile('/proc/meminfo'); // edit: contained a typo
  10.     writeln(mem.Text);
  11.   finally
  12.     mem.Free;
  13.   end;
  14.   cpu := TStringlist.Create;
  15.   try
  16.     cpu.LoadFromFile('/proc/cpuinfo');
  17.     writeln(cpu.Text);
  18.   finally
  19.     cpu.Free;
  20.   end;
  21. end.
Is a simple example.....but it is the same logic as the higher level unix utilities use.
The info is very verbose, so you may want to filter the data somewhat to your needs.
And simply call this code periodically using a timer, a threadedtimer or OnIdle if you want near live updates.
The code is so basic that it does not warrant a component.
« Last Edit: November 12, 2022, 11:58:21 pm by Thaddy »
Specialize a type, not a var.

finalist

  • Jr. Member
  • **
  • Posts: 99
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #2 on: November 12, 2022, 09:58:15 pm »
You do not need a component for that (I highly recommend to not even look for it). In Unixes everything is a file, so:
Code: Pascal  [Select][+][-]
  1. program showcpuandmem;
  2. {$mode objfpc}{$h+}{$ifndef unix}{$error this is Unix flavors only}{$endif}
  3. uses classes;
  4. var
  5.   mem,cpu:TStringlist;
  6. begin
  7.   mem := TStringlist.Create;
  8.   try
  9.     mem.LoadFromFile('/proc/memifo');
  10.     writeln(mem.Text);
  11.   finally
  12.     mem.Free;
  13.   end;
  14.   cpu := TStringlist.Create;
  15.   try
  16.     cpu.LoadFromFile('/proc/cpuinfo');
  17.     writeln(cpu.Text);
  18.   finally
  19.     cpu.Free;
  20.   end;
  21. end.
Is a simple example.....but it is the same logic as the higher level unix utilities use.
The info is very verbose, so you may want to filter the data somewhat to your needs.
And simply call this code periodically using a timer, a threadedtimer or OnIdle if you want near live updates.
The code is so basic that it does not warrant a component.
Thank You very much.
I will transform above code may be into a function / procedure.
I wil call that function / procedure periodically (500 ms or 1000 ms or .... ms )

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #3 on: November 12, 2022, 10:24:03 pm »
I'm in 100% agreement with Thaddy here, but I think one point is worth amplifying. Things that "look like files" in the /proc and /sys trees are actually generated on demand by the kernel, the special filesystems are just a convenient abstraction that allows kernel (and individual process) state to be queried.

Back when Linux distreaux were somewhat less polished, it was possible to find oneself in an odd state where the kernel was running but /proc wasn't mounted. Because this was such an important interface to the kernel, when this happened utilities such as ps and top couldn't return any useful information.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #4 on: November 12, 2022, 10:33:10 pm »
Tnx for explaining how it actually works, Mark. At 2: I know those days too  :(
« Last Edit: November 12, 2022, 10:35:57 pm by Thaddy »
Specialize a type, not a var.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #5 on: November 12, 2022, 10:44:19 pm »
Tnx for explaining how it actually works, Mark. At 2: I know those days too  :(

:-) A lot of people assume that just because everything in unix looks like a file, it actually /is/ a file.

On a typical system I'd estimate that there are thousands or tens of thousands of items in the /proc and /sys trees, and if the kernel really had to write every detail to a file a hundred or so times a second it would leave little if any time for anything else.

Almost all the system state that utilities such as ps display come from those special filesystems. The rest comes from various sockets etc., into which application programs can- with care- tap.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #6 on: November 12, 2022, 10:54:54 pm »
And the kernel "files" are extremely fast compared to other options for that reason.
Anyway, @finalist should be able to write his own top clone by now  :D O:-)
Specialize a type, not a var.

finalist

  • Jr. Member
  • **
  • Posts: 99
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #7 on: November 12, 2022, 11:34:36 pm »
Error:
 
Code: Pascal  [Select][+][-]
  1. Unable to open file "/proc/memifo": No such file or directory


It is not so easy as it seems to be ...

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #8 on: November 12, 2022, 11:54:59 pm »
Error:
 
Code: Pascal  [Select][+][-]
  1. Unable to open file "/proc/memifo": No such file or directory


It is not so easy as it seems to be ...
typo: should be meminfo, I editted the source accordingly. At home I already corrected this. Sorry for posting buggy code, but if you go back, you will see the edit.
« Last Edit: November 12, 2022, 11:59:10 pm by Thaddy »
Specialize a type, not a var.

finalist

  • Jr. Member
  • **
  • Posts: 99
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #9 on: November 13, 2022, 12:10:42 am »
Thats fine: No more error !
Now have to find where are the results written ....  :)

finalist

  • Jr. Member
  • **
  • Posts: 99
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #10 on: November 13, 2022, 12:25:44 am »
Absolutely fantastic = it works  :)
@Thaddy, thank You very much !

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #11 on: November 13, 2022, 12:29:23 am »
In my example to a terminal/console, but if you replace writeln with writestr you can write it to a string instead.
See https://www.freepascal.org/docs-html/rtl/system/writestr.html
« Last Edit: November 13, 2022, 12:38:54 am by Thaddy »
Specialize a type, not a var.

finalist

  • Jr. Member
  • **
  • Posts: 99
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #12 on: November 13, 2022, 12:31:14 am »
Code: Text  [Select][+][-]
  1. MemTotal:        7992348 kB
  2. MemFree:         2064872 kB
  3. MemAvailable:    3792340 kB
  4. Buffers:          435644 kB
  5. Cached:          2260764 kB
  6. SwapCached:       265592 kB
  7. Active:          2290260 kB
  8. Inactive:        2990720 kB
  9. Active(anon):    1181992 kB
  10. Inactive(anon):  2221476 kB
  11. Active(file):    1108268 kB
  12. Inactive(file):   769244 kB
  13. Unevictable:      240136 kB
  14. Mlocked:           17252 kB
  15. SwapTotal:       8104956 kB
  16. SwapFree:        7096060 kB

Code: Text  [Select][+][-]
  1. processor       : 0
  2. vendor_id       : GenuineIntel
  3. cpu family      : 6
  4. model           : 76
  5. model name      : Intel(R) Celeron(R) CPU  J3160  @ 1.60GHz
  6. stepping        : 4
  7. microcode       : 0x411
  8. cpu MHz         : 1635.400
  9. cache size      : 1024 KB
  10. physical id     : 0
  11. siblings        : 4
  12. core id         : 0
  13. cpu cores       : 4
  14. apicid          : 0
  15. initial apicid  : 0
  16. fpu             : yes
  17. fpu_exception   : yes
  18. cpuid level     : 11
  19.  
« Last Edit: November 13, 2022, 08:30:38 am by finalist »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #13 on: November 13, 2022, 10:07:12 am »
Yup. Well done. See how simple that was?  O:-) 8-)
Specialize a type, not a var.

finalist

  • Jr. Member
  • **
  • Posts: 99
Re: Is into Lazarus 64 PC CPU usage and PC RAM usage Components for Linux
« Reply #14 on: November 13, 2022, 10:46:07 am »
Yeah!
Simple and fast working.
I am happy, may be November the 13 is my happy day.
Thanks again !

 

TinyPortal © 2005-2018