*BSD News Article 30133


Return to BSD News archive

Xref: sserve comp.unix.programmer:16820 comp.unix.misc:12264 comp.unix.questions:49039 comp.unix.aix:37245 comp.unix.bsd:13870
Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!msuinfo!agate!ihnp4.ucsd.edu!usc!math.ohio-state.edu!news.acns.nwu.edu!ftpbox!mothost!mdisea!mmddvan!vanbc.wimsey.com!rwsys.wimsey.bc.ca!rw
From: rw@rwsys.wimsey.bc.ca (Randy Wright)
Newsgroups: comp.unix.programmer,comp.unix.misc,comp.unix.questions,comp.unix.aix,comp.unix.bsd
Distribution: world
Subject: Re: %CPU usage for a process ?
References: <2q9mee$jpe@vixen.cso.uiuc.edu>
Message-ID: <9405053173@rwsys.wimsey.bc.ca>
Organization: RW development, Surrey BC, Canada
Date: Thu,  5 May 94 18:09:23 PDT
Lines: 43

libor@rasputin.ncsa.uiuc.edu (Libor Michalek) writes:
> In article <CpAqxF.1w3E@austin.ibm.com>, accapadi@austin.ibm.com (Matt Accapadi) writes:
> |> I must be missing something.  How about  ps -eF "%c %u %p %C"|sort -rn +3
> 
> Sorry I wasn't specific enough. What I meant was... how do you do this in a
> C program (i.e. using the kernals process tables to find usful information
> from which %CPU can be calculated.), In otherwords: How does the ps command
> do this? Sorry about the confusion.
> 
> -Libor
> 
> |> In article <2q75oj$s1c@vixen.cso.uiuc.edu>,
> |> Libor Michalek <libor@rasputin.ncsa.uiuc.edu> wrote:
> |> >  I was wondering if anyone knows how to calculate %CPU usage for a process.
> |> >I know how to get information from the kernals memory, but the kernal does
> |> >NOT keep %CPU usage for a process (altough it does keep CPU time for a 
> |> >process), so I assume that you use some of the fields from the process table
> |> >to calculate this information. So does any one know how to do this 
> |> >calculation??? I don't care which unix you can help me with since the
> |> >process table for most unices are pretty similar, and I'm far too familiar
> |> >with alot of them :-) but if you really want to pick one heres a list of
> |> >OS's that I'll need the information for:
> 
I wrote a device driver that performs this sort of function by
walking the proc struct chain and summing the times given in
each proc struct. You can start with curproc and travel the list
by refering to p_nxt. On the os's I've tried this with, the
last p_nxt points to the first proc, so you just walk until
p_nxt is equal to the saved value of curporc.

Then you do some arithmatic to figure which proc is using which
percentage of the summed time for all procs. This can be done
at regular intervals to get a dynamic reading rather than a ps
style snapshot.

There may be better ways to do this, but this is a way I
have tried out myself.

--Randy
____________________________________________________________________________
rw@rwsys.wimsey.bc.ca (Randy Wright)                        (604) 581-0518
                ICBM address = 49d 12m 5s N    122d 51m 49s W
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--