CPU user/kernel time given the PID

Oliver Fromme olli at lurza.secnetix.de
Sun Mar 8 14:56:54 PDT 2009


Jay Loden wrote:
 > I'm working on FreeBSD support for a Python library called psutil for reading
 > process information in a cross-platform fashion. Each platform-specific module
 > is written in C, so the majority of the FreeBSD code is a C interface to various
 > process information. I've been having some trouble working out how to get CPU
 > user/kernel time for a given PID. I took a look at the source to top and ps but
 > neither really helped since they don't seem to cover the info I was looking for
 > (or I missed it).
 > 
 > I'm not sure if there's a better way to go about this but I've been looking at
 > sysctl and the kinfo_proc struct - is there somewhere more appropriate to
 > retrieve this information? If the kinfo_proc struct is the way to go, then do I
 > want to use ki_runtime, ki_swtime or something else, and does that mean there's
 > no distinction between user/kern time for a process? If anyone has code samples
 > or recommended docs to get me pointed in the right direction that would be great.

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

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.

Best regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

"C is quirky, flawed, and an enormous success."
        -- Dennis M. Ritchie.


More information about the freebsd-hackers mailing list