Recent

Author Topic: Consumption of RAM and CPU in Linux  (Read 3732 times)

mosquito

  • Full Member
  • ***
  • Posts: 101
Consumption of RAM and CPU in Linux
« on: January 17, 2022, 01:00:41 pm »
I need to show in my program the consumption of RAM and CPU of the system (Linux). I plan to capture stdout of some well-known commands. My question is if we have something already prepared in the RTL / FCL. I have reviewed the units [Linux, BaseUnix, Unix], but I do not see anything.

I would also like to know how to show the consumption of RAM and CPU of the program itself (not debug, but release), also in Linux.
« Last Edit: January 17, 2022, 01:05:36 pm by mosquito »

Thaddy

  • Hero Member
  • *****
  • Posts: 11517
Re: Consumption of RAM and CPU in Linux
« Reply #1 on: January 17, 2022, 02:04:02 pm »
Easy: read the /proc/meminfo file and the /proc/cpuinfo file into a stringlist.
Code: Pascal  [Select][+][-]
  1. program meminfo;
  2. {$mode objfpc}
  3. {$if not defined(unix)}{$error 'this program is for unix and unix based OS's only'}{$ifend}
  4. uses classes;
  5. var
  6.   s:Tstrings;
  7. begin
  8.   s:=Tstringlist.create;
  9.   try
  10.     s.loadfromfile('/proc/meminfo');
  11.     writeln(s.text);
  12.   finally
  13.     s.free;
  14.   end;
  15.   s:=Tstringlist.create;
  16.   try
  17.     s.loadfromfile('/proc/cpuinfo');
  18.     writeln(s.text);
  19.   finally
  20.     s.free;
  21.   end;
  22. end.
In unix everything is a file, so this is a correct approach.
You can subsequently search the list for just the info you want.

Note that the program is very verbose, but this info is used throughout Nixes.
Note that both are dynamic and need regular updates.
« Last Edit: January 17, 2022, 02:20:22 pm by Thaddy »
Путин преступник. Россияне дезинформированы.

MarkMLl

  • Hero Member
  • *****
  • Posts: 4181
Re: Consumption of RAM and CPU in Linux
« Reply #2 on: January 17, 2022, 07:16:32 pm »
Couple of comments there if I may. First, it's possible to have a system in a state where nothing's mounted on /proc. It's reasonable to assume that things like ps and top wouldn't work in this case, but a custom program- particularly one that doesn't use a GUI- might have to handle this intelligently (i.e. rather than just crashing or locking up).

Second, the Linux maintainers are trying to stop new stuff going into /proc, and at some point they might be aggressive about moving existing stuff out even if doing so breaks things.

Third, when writing stuff for multiple unix variants in the past I've been able to use ps to get info even though there was much less in (e.g. SunOS/Solaris's) /proc tree than Linux users are used to. It might be worth researching whether there's e.g. a top for SunOS (possibly sourced by either UCB or Gnu), how it works efficiently, and possibly whether it's possible to pipe just the top couple of lines into ones own program.

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

mosquito

  • Full Member
  • ***
  • Posts: 101
Re: Consumption of RAM and CPU in Linux
« Reply #3 on: January 17, 2022, 08:03:44 pm »
@Thaddy.
Thanks a lot. I think this is the best approach. Not by avoiding pipes (free -h, lscpu, etc.), but because these files are not really files but descriptors mapped to memory. Now I'm going to compare the speed to parse with (grep-awk) vs FPC Internal (pos-posEx).

@Markmll.
Thank you for your comment. Developing in wide-spectrum for *nix is not easy, but not even within GNU/Linux variants. Nor do I think the matter improves. Systemd debate, variants with non-Linux kernels, sandbox applications of different moralities (FlatPak, Snap, Appimage). Nowadays, the simple fact of showing a system notification is no longer standardized.
As if that were not enough, assuming that "ps" will run magically on all systems, it seems that the data that draw are inaccurate (in multi-thread, much more).

Fortunately, my target for this topic is only pure Debian 9-11, with which I think reading from /proc will be the most appropriate at the moment.

I think this will be enough for me:

RAM:  /proc/meminfo
CPU:  /proc/cpuinfo
SELF: /proc/{myPID}/smaps

winni

  • Hero Member
  • *****
  • Posts: 3045
Re: Consumption of RAM and CPU in Linux
« Reply #4 on: January 17, 2022, 08:06:35 pm »
Hi!

Use the linux command free so you don't have to care how it is implemented and which system moves what  from /proc to /sys

free show something like this:

Code: Bash  [Select][+][-]
  1. free
  2.                total        used        free      shared  buff/cache   available
  3. Mem:        15283868     3586192      707712      123624    10989964    11238600
  4. Swap:        2097312      116736     1980576


Winni

Thaddy

  • Hero Member
  • *****
  • Posts: 11517
Re: Consumption of RAM and CPU in Linux
« Reply #5 on: January 17, 2022, 08:37:06 pm »
That utility actually uses /proc/meminfo but and is a bit higher level.
Filtering is done with regular expressions. meminfo is not actually a file in the Windows sense but a kernel function. It is also regularly updated: it is dynamic.
My example does not need something like TProcess. I will write an example how to write something like free in pure pascal, but that will be tomorrow.
« Last Edit: January 17, 2022, 08:40:57 pm by Thaddy »
Путин преступник. Россияне дезинформированы.

AlexTP

  • Hero Member
  • *****
  • Posts: 1766
    • UVviewsoft
Re: Consumption of RAM and CPU in Linux
« Reply #6 on: January 17, 2022, 08:43:05 pm »
Thaddy,
better to have a wiki page with such example. Thanks in advance.

mosquito

  • Full Member
  • ***
  • Posts: 101
Re: Consumption of RAM and CPU in Linux
« Reply #7 on: January 17, 2022, 09:13:16 pm »
@winni
Thank you, this is what I had in mind, before seeing the possibility of reading from /proc, but now I do not see it so interesting:

The problem with the "free" command is that you are ready to do much more than I need.

By compiling the Thaddy program without optimizations and with debug-info is 20% faster than "free-h". But it also avoids 80% of the system calls  that makes free.

https://en.wikipedia.org/wiki/System_call.

Finally I see that under the hood, both use mmap, mprotect, etc. But reading directly from /proc, we avoided a ton of garbage that is evident in the execution trace of "free -h".

I am satisfied with a query every 1s., But for more intensive use, all this garbage generated by "free" would not help at all.

On the other hand, using "free" requires creating a pipe and capturing.

Taking into account that my program does not use dynamic libraries and I am not as crazy to use "valgrind" and install modules in the kernel, I think reading from /proc directly is the best solution for now.

« Last Edit: January 17, 2022, 09:23:12 pm by mosquito »

Kays

  • Sr. Member
  • ****
  • Posts: 423
  • Whasup!?
    • KaiBurghardt.de
Re: Consumption of RAM and CPU on Linux
« Reply #8 on: January 17, 2022, 09:45:37 pm »
I need to show in my program the consumption of RAM and CPU of the system (Linux). […] My question is if we have something already prepared in the RTL / FCL.
Linux.sysInfo enough?

I would also like to know how to show the consumption of RAM and CPU of the program itself (not debug, but release), also in Linux.
That’s getrusage(2), but there’s no wrapper for that [except on BSD], so you’ll need to do it yourself (use the sysCall unit).
« Last Edit: January 17, 2022, 09:49:30 pm by Kays »
Yours Sincerely
Kai Burghardt

mosquito

  • Full Member
  • ***
  • Posts: 101
Re: Consumption of RAM and CPU in Linux
« Reply #9 on: January 17, 2022, 09:57:35 pm »
@Kays
Checking, and buying glasses.
Thank you.

Thaddy

  • Hero Member
  • *****
  • Posts: 11517
Re: Consumption of RAM and CPU in Linux
« Reply #10 on: January 18, 2022, 06:32:20 am »
sysinfo seems not unix but just linux?

Btw, there is the fpUname function in BaseUnix that gives concise machine info:
Code: Pascal  [Select][+][-]
  1. program confinfo;
  2. {$mode objfpc}{$if not defined(unix)}{$error 'for unix and family only'}{$ifend}
  3. uses baseunix;
  4. var
  5.   info:UtsName;
  6.   res:cint;
  7.   s:string;
  8. begin
  9.   res := fpUname(info);
  10.   if res <> 0 then exit;
  11.   writeln('System: ':10,PChar(info.sysname));
  12.   writeln('Computer: ':10,PChar(info.nodename));
  13.   writeln('Release: ':10,PChar(info.release));
  14.   writeln('Version: ':10,PChar(info.version));
  15.   writeln('Machine: ':10,PChar(info.machine));
  16. {$if defined(linux)}
  17.   writeln('domain: ':10,PChar(info.domain));
  18. {$ifend}
  19. end.
https://www.freepascal.org/docs-html/rtl/baseunix/utsname.html
« Last Edit: January 18, 2022, 08:12:58 am by Thaddy »
Путин преступник. Россияне дезинформированы.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4011
  • Compiler Developer
Re: Consumption of RAM and CPU in Linux
« Reply #11 on: January 18, 2022, 08:57:48 am »
sysinfo seems not unix but just linux?

Well, mosquito wrote that they're looking only for Linux for now, so sysinfo is a valid solution for that.

Thaddy

  • Hero Member
  • *****
  • Posts: 11517
Re: Consumption of RAM and CPU in Linux
« Reply #12 on: January 18, 2022, 09:14:08 am »
True, but for my own requirements I prefer a generic UNIX solution if available.
Путин преступник. Россияне дезинформированы.

 

TinyPortal © 2005-2018