On 2/28/2020 7:06 AM, Otto Moerbeek wrote:
On Thu, Feb 27, 2020 at 08:09:49PM +0100, Piotr Durlej wrote:
Hello,
the following top(1) patch allows for printing CPU percentages >=100%
without overflowing the CPU column width.
https://www.durlej.net/diff/top.diff
Regards,
Piotr Durlej
Please post diffs inline. Also, your snprintf calls are wrong.
-Otto
Thanks, here is a new diff:
--- src/usr.bin/top/machine.c.old Fri Feb 28 15:17:05 2020
+++ src/usr.bin/top/machine.c Fri Feb 28 15:16:42 2020
@@ -80,7 +80,7 @@ static char header[] =
#define UNAME_START 6
#define Proc_format \
- "%5d %-8.8s %3d %4d %5s %5s %-9s %-7.7s %6s %5.2f%% %s"
+ "%5d %-8.8s %3d %4d %5s %5s %-9s %-7.7s %6s %s%% %s"
/* process state names for the "STATE" column of the display */
/*
@@ -562,6 +562,7 @@ format_next_process(struct handle *hndl, const char *(
struct kinfo_proc *pp;
int cputime;
double pct;
+ char pctbuf[8];
char buf[16];
/* find and remember the next proc structure */
@@ -583,6 +584,13 @@ format_next_process(struct handle *hndl, const char *(
else
snprintf(buf, sizeof(buf), "%s", (*get_userid)(pp->p_ruid, 0));
+ if (pp->p_pctcpu < fscale)
+ snprintf(pctbuf, sizeof(pctbuf), "%5.2f", 100.0 * pct);
+ else if (pp->p_pctcpu < fscale * 10)
+ snprintf(pctbuf, sizeof(pctbuf), "%5.1f", 100.0 * pct);
+ else
+ snprintf(pctbuf, sizeof(pctbuf), "%5.0f", 100.0 * pct);
+
/* format this entry */
snprintf(fmt, sizeof(fmt), Proc_format, pp->p_pid, buf,
pp->p_priority - PZERO, pp->p_nice - NZERO,
@@ -590,7 +598,7 @@ format_next_process(struct handle *hndl, const char *(
format_k(pagetok(pp->p_vm_rssize)),
(pp->p_stat == SSLEEP && pp->p_slptime > maxslp) ?
"idle" : state_abbr(pp),
- p_wait, format_time(cputime), 100.0 * pct,
+ p_wait, format_time(cputime), pctbuf,
printable(format_comm(pp)));
*pid = pp->p_pid;