From owner-freebsd-hackers@FreeBSD.ORG Wed Mar 16 18:09:48 2011 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 E2DF1106566C for ; Wed, 16 Mar 2011 18:09:48 +0000 (UTC) (envelope-from dan@dan.emsphone.com) Received: from email2.allantgroup.com (email2.emsphone.com [199.67.51.116]) by mx1.freebsd.org (Postfix) with ESMTP id BF79B8FC1D for ; Wed, 16 Mar 2011 18:09:47 +0000 (UTC) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by email2.allantgroup.com (8.14.4/8.14.4) with ESMTP id p2GHuF1q098710 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 16 Mar 2011 12:56:16 -0500 (CDT) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (smmsp@localhost [127.0.0.1]) by dan.emsphone.com (8.14.4/8.14.4) with ESMTP id p2GHuFdL046049 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 16 Mar 2011 12:56:15 -0500 (CDT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.14.4/8.14.4/Submit) id p2GHuFJN046048; Wed, 16 Mar 2011 12:56:15 -0500 (CDT) (envelope-from dan) Date: Wed, 16 Mar 2011 12:56:14 -0500 From: Dan Nelson To: Thiago Damas Message-ID: <20110316175614.GB44561@dan.emsphone.com> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ZGiS0Q5IWpPtfppv" Content-Disposition: inline In-Reply-To: X-OS: FreeBSD 8.2-PRERELEASE User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: clamav-milter 0.97 at email2.allantgroup.com X-Virus-Status: Clean X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.2.6 (email2.allantgroup.com [199.67.51.78]); Wed, 16 Mar 2011 12:56:16 -0500 (CDT) X-Scanned-By: MIMEDefang 2.68 on 199.67.51.78 Cc: freebsd-hackers@freebsd.org Subject: Re: usertime and systime X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Mar 2011 18:09:49 -0000 --ZGiS0Q5IWpPtfppv Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In the last episode (Mar 16), Thiago Damas said: > Hi, > without procfs, there is a way to get usertime and systime from a > running process? Try applying the attached patch to ps. I've had it for a while but never submitted a PR. Heh. I've had it for a very long time. http://lists.freebsd.org/pipermail/freebsd-hackers/2009-March/027918.html -- Dan Nelson dnelson@allantgroup.com --ZGiS0Q5IWpPtfppv Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="ps.diff" Index: ps.1 =================================================================== --- ps.1 (revision 219700) +++ ps.1 (working copy) @@ -587,6 +587,8 @@ symbolic process state (alias saved gid from a setgid executable .It Cm svuid saved UID from a setuid executable +.It Cm systime +accumulated system CPU time .It Cm tdaddr thread address .It Cm tdev @@ -617,6 +619,8 @@ scheduling priority on return from system call (al .Cm usrpri ) .It Cm user user name (from UID) +.It Cm usertime +accumulated user CPU time .It Cm vsz virtual size in Kbytes (alias .Cm vsize ) Index: keyword.c =================================================================== --- keyword.c (revision 219700) +++ keyword.c (working copy) @@ -186,6 +186,7 @@ static VAR var[] = { UINT, UIDFMT, 0}, {"svuid", "SVUID", NULL, 0, kvar, NULL, UIDLEN, KOFF(ki_svuid), UINT, UIDFMT, 0}, + {"systime", "SYSTIME", NULL, USER, systime, NULL, 9, 0, CHAR, NULL, 0}, {"tdaddr", "TDADDR", NULL, 0, kvar, NULL, sizeof(void *) * 2, KOFF(ki_tdaddr), KPTR, "lx", 0}, {"tdev", "TDEV", NULL, 0, tdev, NULL, 5, 0, CHAR, NULL, 0}, @@ -207,6 +208,7 @@ static VAR var[] = { KOFF(ki_paddr), KPTR, "lx", 0}, {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN, 0, CHAR, NULL, 0}, + {"usertime", "USERTIME", NULL, USER, usertime, NULL, 9, 0, CHAR, NULL, 0}, {"usrpri", "", "upr", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, {"vsize", "", "vsz", 0, NULL, NULL, 0, 0, CHAR, NULL, 0}, {"vsz", "VSZ", NULL, 0, vsize, NULL, 5, 0, CHAR, NULL, 0}, Index: extern.h =================================================================== --- extern.h (revision 219700) +++ extern.h (working copy) @@ -79,12 +79,14 @@ int s_uname(KINFO *); void showkey(void); void started(KINFO *, VARENT *); void state(KINFO *, VARENT *); +void systime(KINFO *, VARENT *); void tdev(KINFO *, VARENT *); void tdnam(KINFO *, VARENT *); void tname(KINFO *, VARENT *); void ucomm(KINFO *, VARENT *); void uname(KINFO *, VARENT *); void upr(KINFO *, VARENT *); +void usertime(KINFO *, VARENT *); void vsize(KINFO *, VARENT *); void wchan(KINFO *, VARENT *); __END_DECLS Index: print.c =================================================================== --- print.c (revision 219700) +++ print.c (working copy) @@ -588,6 +588,79 @@ cputime(KINFO *k, VARENT *ve) } void +systime(KINFO *k, VARENT *ve) +{ + VAR *v; + long secs; + long psecs; /* "parts" of a second. first micro, then centi */ + char obuff[128]; + static char decimal_point; + + if (decimal_point == '\0') + decimal_point = localeconv()->decimal_point[0]; + v = ve->var; + if (!k->ki_valid) { + secs = 0; + psecs = 0; + } else { + /* + * This counts time spent handling interrupts. We could + * fix this, but it is not 100% trivial (and interrupt + * time fractions only work on the sparc anyway). XXX + */ + secs = k->ki_p->ki_rusage.ru_stime.tv_sec; + psecs = k->ki_p->ki_rusage.ru_stime.tv_usec; + if (sumrusage) { + secs += k->ki_p->ki_childstime.tv_sec; + psecs += k->ki_p->ki_childstime.tv_usec; + } + /* + * round and scale to 100's + */ + psecs = (psecs + 5000) / 10000; + secs += psecs / 100; + psecs = psecs % 100; + } + (void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld", + secs / 60, secs % 60, decimal_point, psecs); + (void)printf("%*s", v->width, obuff); +} + +void +usertime(KINFO *k, VARENT *ve) +{ + VAR *v; + long secs; + long psecs; /* "parts" of a second. first micro, then centi */ + char obuff[128]; + static char decimal_point; + + if (decimal_point == '\0') + decimal_point = localeconv()->decimal_point[0]; + v = ve->var; + if (!k->ki_valid) { + secs = 0; + psecs = 0; + } else { + secs = k->ki_p->ki_rusage.ru_utime.tv_sec; + psecs = k->ki_p->ki_rusage.ru_utime.tv_usec; + if (sumrusage) { + secs += k->ki_p->ki_childutime.tv_sec; + psecs += k->ki_p->ki_childutime.tv_usec; + } + /* + * round and scale to 100's + */ + psecs = (psecs + 5000) / 10000; + secs += psecs / 100; + psecs = psecs % 100; + } + (void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld", + secs / 60, secs % 60, decimal_point, psecs); + (void)printf("%*s", v->width, obuff); +} + +void elapsed(KINFO *k, VARENT *ve) { VAR *v; --ZGiS0Q5IWpPtfppv--