Determine CPU Speed / Memory in C/C++ (Linux), LGPL or BSD are a requirement

1.1k Views Asked by At

I'm writing a distributed, embedded, multi master / slave application for my college coursework and as a part of that I need a way to determine the CPU Speed, CPU usage and available Memory of each system in realtime without a lot of memory respectively cpu consumption, in order to automatically adjust the configuration of the network and the devices.

Triggering system calls periodically or reading out files (open-read-close ....) does not really pose a resource friendly option. I'm sure there must be some POSIX compliant solution to this?

The systems will all be using Linux as an OS and I'm supposed to exclusively use LGPL or BSD libraries for the release.

So far I haven't found any solutions that would fit the criteria.

Any ideas?

5

There are 5 best solutions below

0
On

hwloc could be what you are searching for. It is opensource and it looks that it even supports several platforms.

2
On

Here are few tips that can lead you to a solution :

1- kernel infos

2- You can get that information in /proc/cpuinfo. Google for 'Proc Filesystems'.

3- extract system infos

4- Another StackOverflow subject that might help here

From that i think you can lead yourself to the solution.

0
On

I had to do something like this before and my solution was to write a file parser for the system files /proc/cpuinfo and /proc/meminfo, but I don't know how portable this is (worked on both Ubuntu and Scientific Linux for me).

4
On

You can use libprocps. It is LGPL even though many web page says it's GPL. See the commit log

1
On

You'll need to follow three steps:

1) use system() or popen() to invoke shell command or utility from your program. (refer: this)

2) Decide on a utility from this list, to generate utilization data.

3) capture this data from program and parse it to gather relevant info.