Subject: Re: getting cpu utilization
To: None <netbsd-users@NetBSD.org>
From: George Georgalis <george@galis.org>
List: netbsd-users
Date: 03/26/2007 18:52:46
On Fri, Jan 12, 2007 at 12:58:41PM +1100, Simon Burge wrote:
>"Steven M. Bellovin" wrote:
>
>> On Thu, 11 Jan 2007 20:08:46 -0500
>> "George Georgalis" <george@galis.org> wrote:
>> 
>> > On Thu, Jan 11, 2007 at 06:46:30PM -0600, Jeremy C. Reed wrote:
>> > >> I'm using a /bin/sh function to generate a the cpu utilization 
>> > >> 
>> > >> util () { # CPU Utilization
>> > >>  idle=$(echo "2 k $(top -b -d2 | grep '^CPU states' | awk '{print
>> > >> $11}' | sed 's/%//') 1 + p" | dc) echo "2 k 1 $idle / p"  | dc ;}
>> > >> 
>> > >> That returns the inverse of the cpu idle % found in top.  I add
>> > >> 1 to the value before I invert it to prevent divide by zero, so
>> > >> output is pretty much between 0 and 1.
>> > >> 
>> > >> Running two top reports seems a pretty inefficient way to get the
>> > >> value.  I think I can tune top a bit, but is there a more direct
>> > >> way to get the measurement?
>> > >
>> > >Maybe "sysctl kern.cp_time" ?
>> > 
>> > hey that looks pretty good, but I cannot find any doc on it
>> > sysctl(8) mentions it; but no detail in (3) 
>> > 
>> > my best guess to the numbers is the number of 0.01 seconds elapsed
>> > per cpu, so idle athlon below increments idle time silghtly over
>> > 100 per second, while the idle 8 core opteron increments at a
>> > little more than 800 per second.
>> > 
>> Are you looking on -current?  Details are now in sysctl(7).  Anyway, I
>> don't think it's what you want, since it looks to be a cumulative time,
>> not the current one status.  You could, of course, take the difference
>> between two samples.
>
>This "difference between two samples" of kern.cp_time is exactly how
>top(1) gets its numbers.  If you only want a ratio, what the numbers
>represent isn't important, but it's the hardware clock rate.  See the
>"hz" field in kern.clockrate.  Note that this is not guaranteed to be
>100.
>
>
>This is a bit messy but does roughly what top does:
>
>#!/bin/sh
> 
>DELAY=1
>
>( sysctl kern.cp_time ; sleep $DELAY ; sysctl kern.cp_time ) | awk '
>{ 
>	a = $4 + $7 + $10 + $13
>	b = $16
> 
>	if (oldtot == 0) {
>		oldcpu = a
>		oldtot = a + b
>	} else {
>		newcpu = a
>		newtot = a + b
>		print "usage = ", (newcpu - oldcpu) / (newtot - oldtot)
>	}
>} 
>' 

Thanks. I ended up using the DERIVE option in rrdtool:

http://metrg.net/pub/script/rrd-load.sh
http://metrg.net/pub/script/rrd-disk.sh

both of those take one of  gen|db|graph|''
gen to generate (and purge any existing db),
update the db,
make the graphs,
or '', do a db and graph (useful for dugging)

eg, to test, install rrdtool, then

 rrd-load.sh gen | sh ; while :; sleep 15 ; rrd-load.sh; done

...and check your $htrr directory with a browser.

you may have to fix paths in top of scripts,
but they can both run as a user and should work
out of the box to detect your partitions etc.

I run the db function every minute from cron.

not sure the best way to overlay inode use with disk
bytes, but inode * 1000, seems reasonable....

// George


-- 
George Georgalis, systems architect, administrator <IXOYE><