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
        dnel...@allantgroup.com
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;
_______________________________________________
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"

Reply via email to