Add proper width calculation for time fields (time, cputime and usertime).

This fixes the ugly overflow in "ps aux" output for "[idle]".
This commit is contained in:
Edward Tomasz Napierala 2011-03-24 20:15:42 +00:00
parent c6d4ed3a32
commit f9db2550b2
3 changed files with 52 additions and 5 deletions

View File

@ -74,12 +74,15 @@ void rgroupname(KINFO *, VARENT *);
void runame(KINFO *, VARENT *);
void rvar(KINFO *, VARENT *);
int s_comm(KINFO *);
int s_cputime(KINFO *);
int s_label(KINFO *);
int s_loginclass(KINFO *);
int s_logname(KINFO *);
int s_rgroupname(KINFO *);
int s_runame(KINFO *);
int s_systime(KINFO *);
int s_uname(KINFO *);
int s_usertime(KINFO *);
void showkey(void);
void started(KINFO *, VARENT *);
void state(KINFO *, VARENT *);

View File

@ -189,12 +189,14 @@ 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},
{"systime", "SYSTIME", NULL, USER|DSIZ, systime, s_systime, 15, 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},
{"tdnam", "TDNAM", NULL, LJUST, tdnam, NULL, COMMLEN, 0, CHAR, NULL, 0},
{"time", "TIME", NULL, USER, cputime, NULL, 9, 0, CHAR, NULL, 0},
{"time", "TIME", NULL, USER|DSIZ, cputime, s_cputime, 15, 0, CHAR,
NULL, 0},
{"tpgid", "TPGID", NULL, 0, kvar, NULL, 4, KOFF(ki_tpgid), UINT,
PIDFMT, 0},
{"tsid", "TSID", NULL, 0, kvar, NULL, PIDLEN, KOFF(ki_tsid), UINT,
@ -211,8 +213,8 @@ 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},
{"usertime", "USERTIME", NULL, USER|DSIZ, usertime, s_usertime, 15, 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},

View File

@ -570,11 +570,20 @@ printtime(KINFO *k, VARENT *ve, long secs, long psecs)
secs += psecs / 100;
psecs = psecs % 100;
}
(void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
(void)snprintf(obuff, sizeof(obuff), "%ld:%02ld%c%02ld",
secs / 60, secs % 60, decimal_point, psecs);
(void)printf("%*s", v->width, obuff);
}
static int
sizetime(long secs)
{
if (secs < 60)
return (7);
return (log10(secs / 60) + 7);
}
void
cputime(KINFO *k, VARENT *ve)
{
@ -929,6 +938,17 @@ s_comm(KINFO *k)
return (strlen(tmpbuff));
}
int
s_cputime(KINFO *k)
{
long secs;
secs = k->ki_p->ki_runtime / 1000000;
if (sumrusage)
secs += k->ki_p->ki_childtime.tv_sec;
return (sizetime(secs));
}
int
s_label(KINFO *k)
{
@ -975,3 +995,25 @@ s_logname(KINFO *k)
return (strlen(s));
}
int
s_systime(KINFO *k)
{
long secs;
secs = k->ki_p->ki_rusage.ru_stime.tv_sec;
if (sumrusage)
secs += k->ki_p->ki_childstime.tv_sec;
return (sizetime(secs));
}
int
s_usertime(KINFO *k)
{
long secs;
secs = k->ki_p->ki_rusage.ru_utime.tv_sec;
if (sumrusage)
secs += k->ki_p->ki_childutime.tv_sec;
return (sizetime(secs));
}