From owner-freebsd-hackers@FreeBSD.ORG Sun Mar 8 21:56:53 2009 Return-Path: Delivered-To: freebsd-hackers@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id AFA761065673 for ; Sun, 8 Mar 2009 21:56:53 +0000 (UTC) (envelope-from olli@lurza.secnetix.de) Received: from lurza.secnetix.de (lurza.secnetix.de [IPv6:2a01:170:102f::2]) by mx1.freebsd.org (Postfix) with ESMTP id 2D2768FC1D for ; Sun, 8 Mar 2009 21:56:52 +0000 (UTC) (envelope-from olli@lurza.secnetix.de) Received: from lurza.secnetix.de (localhost [127.0.0.1]) by lurza.secnetix.de (8.14.3/8.14.3) with ESMTP id n28LupOp085566; Sun, 8 Mar 2009 22:56:51 +0100 (CET) (envelope-from oliver.fromme@secnetix.de) Received: (from olli@localhost) by lurza.secnetix.de (8.14.3/8.14.3/Submit) id n28Lup7e085565; Sun, 8 Mar 2009 22:56:51 +0100 (CET) (envelope-from olli) Date: Sun, 8 Mar 2009 22:56:51 +0100 (CET) Message-Id: <200903082156.n28Lup7e085565@lurza.secnetix.de> From: Oliver Fromme To: freebsd-hackers@FreeBSD.ORG, freebsd@jayloden.com In-Reply-To: <49B3D01E.1010600@jayloden.com> X-Newsgroups: list.freebsd-hackers User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (FreeBSD/6.4-PRERELEASE-20080904 (i386)) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.1.2 (lurza.secnetix.de [127.0.0.1]); Sun, 08 Mar 2009 22:56:52 +0100 (CET) Cc: Subject: Re: CPU user/kernel time given the PID X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: freebsd-hackers@FreeBSD.ORG, freebsd@jayloden.com List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 08 Mar 2009 21:56:54 -0000 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.