sys-top and NaN issues

djberg96 on 2003-01-28T03:31:59

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;