March 17, 2011

Real top is coming

Up til now I have been either faking the statistics in top, or pulling statistics directly from the kernel (by reading kernel variables). Since top runs in user space, it can't really get information directly from the kernel, so it needs to get it another way. In Linux (and not in FreeBSD), top uses the /proc filesystem. Punix doesn't have so much as a filesystem working, let alone a /proc filesystem, so I followed 4.4BSD's and FreeBSD's model: use the sysctl() system call. (Technically speaking, top in Linux could use sysctl() as well, since sysctl() maps directly (?) to the /proc FS there).

The BSD sysctl() syscall has a top-level "directory" called "kern", and one of the second-level directories under that is "proc". This is used for getting process information and has several third-level directories for filtering which processes are included. In my version of top I use the "all" value for retrieving information for all processes.

Here is what "top" currently looks like (r377 in the trunk):
(sh is the top process because top runs in the same process as the shell, à la BusyBox)

As you can see, some values are -1. These are statistics that I have not gathered yet, either in the kernel or in top. I still have to add memory statistics in the kernel, for example.

Also, the CPU usage reported for top itself is much lower than before (0.5 vs 2.5). This is because the older top code used getrusage() to calculate CPU usage (as change in CPU time divided by change in real time), while this latest version uses the kp_pcpu field in struct kinfo_proc from sysctl(). This field is maintained in the kernel as an exponential moving average of CPU usage rather than a linear moving average, so it will naturally be different from the old method.

3 comments:

Unknown said...
This comment has been removed by the author.
dTal said...

For reference, FreeBSD also uses sysctl, not /proc. In fact /proc is completely optional on FreeBSD.

Christopher said...

Thanks for the information, dTal. I didn't look into how modern BSD systems supported process statistics. I just assumed they might also use the /proc filesystem.