Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 16 Mar 2011 22:11:47 +0200
From:      Kostik Belousov <kostikbel@gmail.com>
To:        Dan Nelson <dnelson@allantgroup.com>
Cc:        Thiago Damas <tdamas@gmail.com>, freebsd-hackers@freebsd.org
Subject:   Re: usertime and systime
Message-ID:  <20110316201147.GS78089@deviant.kiev.zoral.com.ua>
In-Reply-To: <20110316175614.GB44561@dan.emsphone.com>
References:  <AANLkTikZi66aU7n%2BOiBg1bjx4Qv6JjY3_0GugwM4WSmh@mail.gmail.com> <20110316175614.GB44561@dan.emsphone.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--wbPyWLaTzb4uthOk
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Mar 16, 2011 at 12:56:14PM -0500, Dan Nelson wrote:
> 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?
>=20
> Try applying the attached patch to ps.  I've had it for a while but never
> submitted a PR.
>=20
> Heh. I've had it for a very long time.=20
> http://lists.freebsd.org/pipermail/freebsd-hackers/2009-March/027918.html
Yes, apparently, this is often requested feature.

I dislike the copying of the existing code, sincere up to the comment
that is not quite relevant (about interrupts in systime). I slightly
reorganized the patch to reduce the copy/paste part of it.

Do you have comments ?

diff --git a/bin/ps/extern.h b/bin/ps/extern.h
index faeeb19..eb1ede6 100644
--- a/bin/ps/extern.h
+++ b/bin/ps/extern.h
@@ -81,12 +81,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
diff --git a/bin/ps/keyword.c b/bin/ps/keyword.c
index 3bcc23b..09eb756 100644
--- a/bin/ps/keyword.c
+++ b/bin/ps/keyword.c
@@ -189,6 +189,7 @@ static VAR var[] =3D {
 		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},
@@ -210,6 +211,8 @@ static VAR var[] =3D {
 		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, 6, 0, CHAR, NULL, 0},
diff --git a/bin/ps/print.c b/bin/ps/print.c
index 46b979b..253793a 100644
--- a/bin/ps/print.c
+++ b/bin/ps/print.c
@@ -550,12 +550,11 @@ vsize(KINFO *k, VARENT *ve)
 	(void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
 }
=20
-void
-cputime(KINFO *k, VARENT *ve)
+static void
+printtime(KINFO *k, VARENT *ve, long secs, long psecs)
+/* psecs is "parts" of a second. first micro, then centi */
 {
 	VAR *v;
-	long secs;
-	long psecs;	/* "parts" of a second. first micro, then centi */
 	char obuff[128];
 	static char decimal_point;
=20
@@ -566,20 +565,7 @@ cputime(KINFO *k, VARENT *ve)
 		secs =3D 0;
 		psecs =3D 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 =3D k->ki_p->ki_runtime / 1000000;
-		psecs =3D k->ki_p->ki_runtime % 1000000;
-		if (sumrusage) {
-			secs +=3D k->ki_p->ki_childtime.tv_sec;
-			psecs +=3D k->ki_p->ki_childtime.tv_usec;
-		}
-		/*
-		 * round and scale to 100's
-		 */
+		/* round and scale to 100's */
 		psecs =3D (psecs + 5000) / 10000;
 		secs +=3D psecs / 100;
 		psecs =3D psecs % 100;
@@ -590,6 +576,53 @@ cputime(KINFO *k, VARENT *ve)
 }
=20
 void
+cputime(KINFO *k, VARENT *ve)
+{
+	long secs, psecs;
+
+	/*
+	 * 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 =3D k->ki_p->ki_runtime / 1000000;
+	psecs =3D k->ki_p->ki_runtime % 1000000;
+	if (sumrusage) {
+		secs +=3D k->ki_p->ki_childtime.tv_sec;
+		psecs +=3D k->ki_p->ki_childtime.tv_usec;
+	}
+	printtime(k, ve, secs, psecs);
+}
+
+void
+systime(KINFO *k, VARENT *ve)
+{
+	long secs, psecs;
+
+	secs =3D k->ki_p->ki_rusage.ru_stime.tv_sec;
+	psecs =3D k->ki_p->ki_rusage.ru_stime.tv_usec;
+	if (sumrusage) {
+		secs +=3D k->ki_p->ki_childstime.tv_sec;
+		psecs +=3D k->ki_p->ki_childstime.tv_usec;
+	}
+	printtime(k, ve, secs, psecs);
+}
+
+void
+usertime(KINFO *k, VARENT *ve)
+{
+	long secs, psecs;
+
+	secs =3D k->ki_p->ki_rusage.ru_utime.tv_sec;
+	psecs =3D k->ki_p->ki_rusage.ru_utime.tv_usec;
+	if (sumrusage) {
+		secs +=3D k->ki_p->ki_childutime.tv_sec;
+		psecs +=3D k->ki_p->ki_childutime.tv_usec;
+	}
+	printtime(k, ve, secs, psecs);
+}
+
+void
 elapsed(KINFO *k, VARENT *ve)
 {
 	VAR *v;
diff --git a/bin/ps/ps.1 b/bin/ps/ps.1
index 5ae7c54..6f5511f 100644
--- a/bin/ps/ps.1
+++ b/bin/ps/ps.1
@@ -29,7 +29,7 @@
 .\"     @(#)ps.1	8.3 (Berkeley) 4/18/94
 .\" $FreeBSD$
 .\"
-.Dd March 5, 2011
+.Dd March 16, 2011
 .Dt PS 1
 .Os
 .Sh NAME
@@ -596,6 +596,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
@@ -626,6 +628,8 @@ scheduling priority on return from system call (alias
 .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 )

--wbPyWLaTzb4uthOk
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (FreeBSD)

iEUEARECAAYFAk2BGYMACgkQC3+MBN1Mb4g2sQCdHVCLMAgXCpSVOj9Gc403w75W
jmUAl20kIdqocdv4f+n3KFaVXbLYXDo=
=80yo
-----END PGP SIGNATURE-----

--wbPyWLaTzb4uthOk--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20110316201147.GS78089>