Subject: Revising CPU usage calculations
To: None <tech-kern@netbsd.org>
From: None <dsieger@techfak.uni-bielefeld.de>
List: tech-kern
Date: 03/20/2007 21:19:48
Hi all,
one of the last missing pieces to make the new scheduler API fully
functional is a scheduler-independent implementation of CPU usage
calculations as used by ps(1) and friends.

I recently did some experiments to split p_pctcpu calculations from
the 4BSD scheduler. Unfortunately, this wasn't too successful and
caused me some headache. Even if I calculate p_pctcpu in exactly the
same manner as SCHED_4BSD does, ps(1) and top(1) always display 0.00
for CPU usage. In addition, load-average display in top(1) is also
broken (showing 0.00). I guess there's some dependency on p_estcpu
being calculated correctly which I haven't found up until now.

So, I'd be very glad if anyone could provide me with some pointer to 
what I might have missed. ;)

The other alternative, which might be a good idea anyway, would be to
completely rewrite those calculations. What we need to provide to
satisfy POSIX is as follows (see [1]):

"The ratio of CPU time used recently to CPU time available in the same
period, expressed as a percentage. The meaning of "recently" in this
context is unspecified. The CPU time available is determined in an
unspecified manner."

were "CPU time" is defined as (see [2])

"The time spent executing a process or thread, including the time
spent executing system services on behalf of that process or
thread. If the Threads option is supported, then the value of the
CPU-time clock for a process is implementation-defined. With this
definition the sum of all the execution times of all the threads in a
process might not equal the process execution time, even in a
single-threaded process, because implementations may differ in how
they account for time during context switches or for other reasons."

As said, any pointers to how to fix current calculations or
comments/suggestions about rewriting them are welcome.

Regards,
Daniel

[1]: http://www.opengroup.org/onlinepubs/009695399/utilities/ps.html
[2]: http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html

-- 
Daniel Sieger
Faculty of Technology
Bielefeld University
wwwhomes.uni-bielefeld.de/dsieger