CPU user/kernel time given the PID

Jay Loden freebsd at jayloden.com
Sun Mar 8 17:33:31 PDT 2009


Oliver Fromme wrote:
> ps(1) and top(1) both use ki_pctcpu, see the getpcpu()
> function in src/bin/ps/print.c and format_next_process()
> in src/usr.bin/top/machine.c

Hi Oliver, thanks for the reply. I noticed the same after some digging through
the source code for ps and top. While CPU usage % is a useful number also, I was
hoping to be able to get CPU time(s). Possibly that information simply isn't
available on FreeBSD like it is for other OSes.

> As far as I know, there is no distinction between user-
> mode and kernel-mode CPU time per process.  It should
> also be noted that the kernel's time cannot always be
> attributed to a certain userland process.  I would even
> guess is that the majority of the CPU time spent in the
> kernel is not on behalf of a specific userland process.

I would suspect the same, but I did notice that times() does return separate
values for user/system time on FreeBSD, so that implies that the system is able
to differentiate between the two somehow. If you can get it from within the
current running process the data must be there but I've no idea what interface
(if any) exists to read that information for other processes.

-Jay


More information about the freebsd-hackers mailing list