Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

djberg96 (2603)

djberg96
  (email not shown publicly)

Journal of djberg96 (2603)

Monday January 27, 2003
10:31 PM

sys-top and NaN issues

[ #10224 ]
I've hit a snag with the 'top' interface with regards to %cpu - it's possible to end up with "inf" when the calling program computes its own %cpu.

How is this possible? Well, %cpu is calculated with "time(NULL) - start"* as the denominator. However, if the program takes <= 1 second to run, then time(NULL) - start ends up being 0, since Linux doesn't store fractional seconds in /proc.

I have a few potential solutions:
1) I could ignore the calling program as a process. This is cheating, and command line 'top' does report itself. How accurate it is on the first read is another story.
2) I could cheat and simply set the %cpu to 100 (or 0) in the event of a NaN.
3) I could let the programmer worry about the possibility of a NaN.
4) I could cheat even worse by throwing a "sleep 1" in the extension so that I'm always guaranteed to avoid a NaN. But, that could taint the other results.

Suggestions?

* Full formula for calculating %cpu on Linux:

start = btime + starttime/CLK_TCK;
pcpu = ( ((float)utime + (float)stime) / CLK_TCK ) / (time(NULL) - start);
pcpu *= 100;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.