Subject: Re: getting cpu utilization
To: Johnny Billquist <bqt@softjar.se>
From: Johnny Billquist <bqt@softjar.se>
List: netbsd-users
Date: 01/12/2007 11:19:42
And to correct myself.. :)

Johnny Billquist wrote:
> George Georgalis 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.
>>
>> does that sound right?
> 
> 
> Yes, but slightly wrong. Your one-cpu system will increment the counter 
> according to the hz field in kern.clockrate. For your machine, that 
> would be 100. The "slightly over" you are observing is because your 
> sleep, followed by a command will not take exactly one second, but 
> slightly more.
> And your 8 cpu system also have a hz of 100, but since you have eight 
> cpus, you'll have to multiply by that, which gives 100.

Um... 800. :)

> So the rate per second is (no_cpu * hz)
> And to figure out the amount spent in different modes, just take the 
> delta from the last sample of the specific, divided with the total delta 
> of all the fields.

...last sample of the specific field, ...

	Johnny