For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2006 > How to monitor certain resources in Linux









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to monitor certain resources in Linux
Chris

2006-08-29, 7:01 pm


I am curious about how to monitor the following resources in Linux from
within code and not on the command line. Are there system calls that
can be tapped into to get this information, or is the only solution to
use popen on the command that will give the output and parse this
information. While this is a solution, I tend to like to avoid it
since it may not work with every target machine that may or may not
have the given commands available:

1) The CPU percentage of the machine. I think I can get this with
popen("top n 1"...), but I would prefer a system call if available, in
case they ever take top off the machines we run.
2) The amount of space left in a partition. We do have the du command
which gives numerical output, but again, if there is a system call I
would prefer that to using popen() and parsing output.
3) The current stack margin of a specific process. I looked through
the /proc/<process_id>/status file looking for something that might fit
this description, but I wasn't sure if it could be found here or
somewhere else.

Any help with any of the above 3 would be greatly appreciated!

--Chris

bwaichu@yahoo.com

2006-08-29, 10:00 pm


Chris wrote:
> I am curious about how to monitor the following resources in Linux from
> within code and not on the command line. Are there system calls that
> can be tapped into to get this information, or is the only solution to
> use popen on the command that will give the output and parse this
> information. While this is a solution, I tend to like to avoid it
> since it may not work with every target machine that may or may not
> have the given commands available:
>
> 1) The CPU percentage of the machine. I think I can get this with
> popen("top n 1"...), but I would prefer a system call if available, in
> case they ever take top off the machines we run.
> 2) The amount of space left in a partition. We do have the du command
> which gives numerical output, but again, if there is a system call I
> would prefer that to using popen() and parsing output.
> 3) The current stack margin of a specific process. I looked through
> the /proc/<process_id>/status file looking for something that might fit
> this description, but I wasn't sure if it could be found here or
> somewhere else.
>
> Any help with any of the above 3 would be greatly appreciated!
>
> --Chris


Have you looked at the sysctl(3)?

I am not sure to what extent it is implemented in linux.

Chuck Dillon

2006-08-30, 8:00 am

Chris wrote:
> I am curious about how to monitor the following resources in Linux from
> within code and not on the command line. Are there system calls that
> can be tapped into to get this information, or is the only solution to
> use popen on the command that will give the output and parse this
> information. While this is a solution, I tend to like to avoid it
> since it may not work with every target machine that may or may not
> have the given commands available:


Actually I think you'll find that the command line mechanisms will be
more standardized than API calls when you dig into these kinds of
areas. For example, if you look at the code under programs like top
you'll see that the program provides a near standard interface to
system specific APIs. If you have open ended requirements in this kind
of information mining I suggest you look at top's code before deciding
to work at the API level.

-- ced

>
> 1) The CPU percentage of the machine. I think I can get this with
> popen("top n 1"...), but I would prefer a system call if available, in
> case they ever take top off the machines we run.
> 2) The amount of space left in a partition. We do have the du command
> which gives numerical output, but again, if there is a system call I
> would prefer that to using popen() and parsing output.
> 3) The current stack margin of a specific process. I looked through
> the /proc/<process_id>/status file looking for something that might fit
> this description, but I wasn't sure if it could be found here or
> somewhere else.
>
> Any help with any of the above 3 would be greatly appreciated!
>
> --Chris
>



--
Chuck Dillon
Manager of Software Development, Bioinformatics
NimbleGen Systems Inc.
Bo Yang

2006-08-30, 7:00 pm

I think top is the most one fittable !
If you should do that in your our code , top's
code is helpful !

Chris

2006-08-30, 7:00 pm

Hi, thanks for the responses!

Do you know how I could get my hand on the top source code? I remember
someone on here lending me the tail source code once which I found very
helpful for another project. While you're at it, I wonder if it would
be possible to see the df source code as well for partition sizes.

Do you happen to know about where stack margin data sits for a process?

--Chris

Chuck Dillon wrote:
> Chris wrote:
>
> Actually I think you'll find that the command line mechanisms will be
> more standardized than API calls when you dig into these kinds of
> areas. For example, if you look at the code under programs like top
> you'll see that the program provides a near standard interface to
> system specific APIs. If you have open ended requirements in this kind
> of information mining I suggest you look at top's code before deciding
> to work at the API level.
>
> -- ced
>
>
>
> --
> Chuck Dillon
> Manager of Software Development, Bioinformatics
> NimbleGen Systems Inc.


Juha Laiho

2006-09-02, 6:59 pm

"Chris" <foureightyeast@yahoo.com> said:
>I am curious about how to monitor the following resources in Linux from
>within code and not on the command line.


>1) The CPU percentage of the machine.


getloadavg

>2) The amount of space left in a partition.


statfs, fstatfs

>3) The current stack margin of a specific process.


Sorry, no clues for this.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Maxim Yegorushkin

2006-09-04, 3:59 am


Juha Laiho wrote:
> "Chris" <foureightyeast@yahoo.com> said:
>
>
> getloadavg


Which linux package provides this command? Does uptime not do the same
thing?

Juha Laiho

2006-09-04, 7:00 pm

"Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> said:
>
>Juha Laiho wrote:
>
>Which linux package provides this command? Does uptime not do the same
>thing?


It's not a command, it's a library call. As in:

#define GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>

int
main(int argc, char **argv) {
double avg[3];
getloadavg(avg,3);
printf(" 1-min avg: %f\n 5-min-avg: %f\n15-min avg: %f\n",
avg[0], avg[1], avg[2]);
exit(0);
}


uptime most probably calls getloadavg() when it is run, I'm too lazy
to check the source code at the moment.

On Linux (or, at least on my system) getloadavg() will just read the
values from /proc/loadavg. On other systems there may be a system
call to return the data.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Chris

2006-09-11, 7:00 pm

Hi, all:
Thanks for the reply for this information. I'm not too sure how the
output from this system call tells me anything about how much CPU % is
currently on-going? I tried some prototyping using this system call
running in a background task along with a task that ran in a tight loop
to cause the CPU to jump to about 97%. From what I can see, this just
gives the number of tasks currently in the running state as a
proportion to the number of processes all together. I'm not sure how
this can tell anything? It seems that theoretically there can be only
1 running task that is running at 100% CPU as well as 4 tasks running
at 25% each. How would I use this to tell the difference or calculate
the CPU % as a total?

--Chris

Juha Laiho wrote:
> "Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> said:
>
> It's not a command, it's a library call. As in:
>
> #define GNU_SOURCE
> #include <stdlib.h>
> #include <stdio.h>
>
> int
> main(int argc, char **argv) {
> double avg[3];
> getloadavg(avg,3);
> printf(" 1-min avg: %f\n 5-min-avg: %f\n15-min avg: %f\n",
> avg[0], avg[1], avg[2]);
> exit(0);
> }
>
>
> uptime most probably calls getloadavg() when it is run, I'm too lazy
> to check the source code at the moment.
>
> On Linux (or, at least on my system) getloadavg() will just read the
> values from /proc/loadavg. On other systems there may be a system
> call to return the data.
> --
> Wolf a.k.a. Juha Laiho Espoo, Finland
> (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
> PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
> "...cancel my subscription to the resurrection!" (Jim Morrison)


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com