0

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?

mmoment
  • 1,269
  • 14
  • 30
  • Would you mind to define _the performance_? – Yasushi Shoji Aug 17 '12 at 09:16
  • 3
    If you're just after CPU speed / memory, do you need a library? Can't you just look at /proc or call sysconf? – Rup Aug 17 '12 at 09:17
  • @YasushiShoji : I edited the question. I need detailsl about the CPU speed, current CPU usage and available memory of each device / system – mmoment Aug 17 '12 at 09:24
  • Even with this edit, can you look in /proc or not? There's almost everything you need in there – Jaffa Aug 17 '12 at 09:33
  • @Rup : /proc/cpuinfo only returns total static system values, not dynamic values such as current usage. – mmoment Aug 17 '12 at 09:48
  • @mmoment Actualy, if you refresh the file opening enough you ll get the actual values in real times. No files will give you a real time speed of the CPU so a way or another you have to refresh opening a /proc/file – C404 Aug 17 '12 at 09:52
  • This is not really a good way for systems with low resources. Correct me if I'm wrong, but reopening files periodically requires a load of memory and time itself? – mmoment Aug 17 '12 at 11:16
  • Yes reopening a `/proc` file requires some overhead. But if you want the information to be recalculated *that* requires some overhead. On Linux `/proc` or a syscall are your only options. – mpe Aug 17 '12 at 12:20

5 Answers5

3

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

Yasushi Shoji
  • 4,028
  • 1
  • 26
  • 47
  • The source code tells me the major part is GPL, though some parts of the code seem to be LGPL :/ – mmoment Aug 17 '12 at 10:34
  • 1
    @mmoment, does it? `git grep -l 'GNU General' **/*.[ch]` tells me four files, `free.c`, `pgrep.c`, `sysctl.c`, and `lib/strutils.c`. The commit clearly states that _"sysctl and pgrep are GPL 2+"_. Alos, it is clearly stated that it is LGPL in `free.c`. `lib/strutils.c`, providing two functions used in a few tool but not in the library, does not state what licese it is under. We can ask it on [procps-ng ml](http://www.freelists.org/archive/procps/) if we need. – Yasushi Shoji Aug 17 '12 at 10:53
  • Nice, my bad. I'll go after that. Reading the free.c source code it says: [...] under the terms of the GNU General Public License [...] – mmoment Aug 17 '12 at 11:04
  • 1
    @mmoment, you are right. I'm sorry that I misread `+` and `-` in `git log`. free.c is under _GPL_. I stand corrected. By the way, you do not need `free.c` for your purpose. – Yasushi Shoji Aug 17 '12 at 11:31
1

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.

Community
  • 1
  • 1
C404
  • 243
  • 4
  • 14
  • 1
    I could not have said it any better myself mate! – boby lapointe Aug 17 '12 at 10:30
  • 1
    #1 doesn't really help at all, #2 /proc/cpuinfo only returns total static system values, not dynamic values such as current usage. I need realtime values. #3 relates to #2 #4 They're using Windows. – mmoment Aug 17 '12 at 10:37
0

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).

dmon
  • 1,344
  • 1
  • 14
  • 29
0

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.

0

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

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177