Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-02

How to get info about CPU and Memory usage?

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Krzysztof

Hi,

I need system info such CPU (by PID), memory usage (by PID), free memory etc. Target platform: Linux.
Linux have command line tools like ps, top, memstat, but I'm just wondering if Free Pascal has wrappers for these commands. If not, then I just get these information by execution external process.

Regards

Ludo Brands
to Krzysztof
Krzysztof wrote 8 lines
Hi, I need system info such CPU (by PID), memory usage (by PID), free memory etc. Target platform: Linux. Linux have command line tools like ps, top, memstat, but I'm just wondering if Free Pascal has wrappers for these commands. If not, then I just get these information by execution external process.

On newer linux kernels, instead of running external processes you can better read and parse /proc/pid/stat (replace pid with the PID). If you need per thread info you can read /proc/pid/task/tid/stat. These are single line pseudo files with space separated values. Much easier than parsing the free format output from ps, top, etc.
http://www.kernel.org/doc/man-pages/online/pages/man5/proc.5.html

Ludo

Michael Van Canneyt
to Krzysztof: I need system info such CPU (by PID), memory usage (by PID), free memory etc. Target…
Krzysztof wrote
Hi, I need system info such CPU (by PID), memory usage (by PID), free memory etc. Target platform: Linux. Linux have command line tools like ps, top, memstat, but I'm just wondering if Free Pascal has wrappers for these commands. If not, then I just get these information by execution external process.

This is becoming a FAQ. :-)

There are no such units AFAIK, and executing an external process for this is not recommended.

It is better to simply parse
/proc/cpuinfo
and
/proc/PID/*

Michael.

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 10 lines
On 2013-02-27 16:49, Michael Van Canneyt wrote: > > It is better to simply parse > /proc/cpuinfo > and > /proc/PID/* .... and hope all distros behave the same. My experience with OnGuard work is that your mileage will vary.

I happen to know the code will have to run only on 1 type of system. Even so, I would still advise this over running an external program.

Michael.

Mark Morgan Lloyd
to Graeme Geldenhuys: .... and hope all distros behave the same. My experience with OnGuard work is that your…
Graeme Geldenhuys wrote 9 lines
On 2013-02-27 16:49, Michael Van Canneyt wrote: > It is better to simply parse > /proc/cpuinfo > and > /proc/PID/* .... and hope all distros behave the same. My experience with OnGuard work is that your mileage will vary.

For this sort of thing /proc/pid is the way to go, since- I'm pretty sure- it's what's used by ps so there are certain things that can't be changed.

The non-pid stuff, specifically things like /proc/cpuinfo, is far more of a problem.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]