From ec5e6e12ee6d7047ab013f3a2826abdb52666ea0 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 4 Sep 2020 01:04:43 -0500 Subject: Add commas to top display, and adjust memory units to megabytes if >10G RAM. --- lib/lib.c | 29 ++++++++++++++++++++++------- lib/lib.h | 10 ++++++---- toys/example/demo_number.c | 11 +++++++---- toys/posix/ps.c | 20 ++++++++++---------- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 7fe3611e..28c7e9c4 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1148,18 +1148,33 @@ match: } // display first "dgt" many digits of number plus unit (kilo-exabytes) -int human_readable_long(char *buf, unsigned long long num, int dgt, int style) +int human_readable_long(char *buf, unsigned long long num, int dgt, int unit, + int style) { unsigned long long snap = 0; - int len, unit, divisor = (style&HR_1000) ? 1000 : 1024; + int len, commas = 0, off, ii, divisor = (style&HR_1000) ? 1000 : 1024; // Divide rounding up until we have 3 or fewer digits. Since the part we // print is decimal, the test is 999 even when we divide by 1024. - // We can't run out of units because 1<<64 is 18 exabytes. - for (unit = 0; snprintf(0, 0, "%llu", num)>dgt; unit++) + // The largest unit we can detect is 1<<64 = 18 Exabytes, but we added + // Zettabyte and Yottabyte in case "unit" starts above zero. + for (;;unit++) { + len = snprintf(0, 0, "%llu", num); + if (style&HR_COMMAS) commas = (len>4)*((len-1)/3); + if (len<=(dgt-commas)) break; num = ((snap = num)+(divisor/2))/divisor; + } + if (CFG_TOYBOX_DEBUG && unit>8) return sprintf(buf, "%.*s", dgt, "TILT"); + len = sprintf(buf, "%llu", num); - if (unit && len == 1) { + if (commas) { + for (ii = 0; ii -USE_DEMO_NUMBER(NEWTOY(demo_number, "D#=3<3hdbs", TOYFLAG_BIN)) +USE_DEMO_NUMBER(NEWTOY(demo_number, "D#=3<3M#<0hcdbs", TOYFLAG_BIN)) config DEMO_NUMBER bool "demo_number" default n help - usage: demo_number [-hsbi] NUMBER... + usage: demo_number [-hsbi] [-D LEN] NUMBER... + -D output field is LEN chars + -M input units (index into bkmgtpe) + -c Comma comma down do be do down down -b Use "B" for single byte units (HR_B) -d Decimal units -h Human readable @@ -20,7 +23,7 @@ config DEMO_NUMBER #include "toys.h" GLOBALS( - long D; + long M, D; ) void demo_number_main(void) @@ -31,7 +34,7 @@ void demo_number_main(void) long long ll = atolx(*arg); if (toys.optflags) { - human_readable_long(toybuf, ll, TT.D, toys.optflags); + human_readable_long(toybuf, ll, TT.D, TT.M, toys.optflags); xputs(toybuf); } else printf("%lld\n", ll); } diff --git a/toys/posix/ps.c b/toys/posix/ps.c index cd8c73ea..8707ae5a 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -600,7 +600,7 @@ static char *string_field(struct procpid *tb, struct ofields *field) } if (which <= PS_SHR) ll *= sysconf(_SC_PAGESIZE); if (TT.forcek) sprintf(out, "%lldk", ll/1024); - else human_readable_long(s, ll, i-1, 0); + else human_readable_long(s, ll, i-1, 0, 0); // Posix doesn't specify what flags should say. Man page says // 1 for PF_FORKNOEXEC and 4 for PF_SUPERPRIV from linux/sched.h @@ -1568,7 +1568,7 @@ static void top_common( char hr[4][32]; long long ll, up = 0; long run[6]; - int j; + int j, k; // Count running, sleeping, stopped, zombie processes. // The kernel has more states (and different sets in different @@ -1590,19 +1590,19 @@ static void top_common( j = i%3; pos = strafter(toybuf+256, (char *[]){"MemTotal:","\nMemFree:", "\nBuffers:","\nSwapTotal:","\nSwapFree:","\nCached:"}[i]); - human_readable_long(hr[j+!!j], 1024*(run[i] = pos?atol(pos):0), - 8, 0); - if (j==1) human_readable_long(hr[1], 1024*(run[i-1]-run[i]), 8,0); + run[i] = pos ? atol(pos) : 0; + k = (*run>=10000000); + human_readable_long(hr[j+!!j], run[i]>>(10*k), 8, k+1, HR_COMMAS); + if (j==1) human_readable_long(hr[1], (run[i-1]-run[i])>>(10*k), + 8, k+1, HR_COMMAS); else if (j==2) { - sprintf(toybuf, (i<3) - ? " Mem: %9s total, %9s used, %9s free, %9s buffers" - : " Swap: %9s total, %9s used, %9s free, %9s cached", - hr[0], hr[1], hr[2], hr[3]); + sprintf(toybuf, " %s: %9s total, %9s used, %9s free, %9s %s", + (i<3) ? " Mem" : "Swap", hr[0], hr[1], hr[2], hr[3], + (i<3) ? "buffers" : "cached"); lines = header_line(lines, 0); } } } - pos = toybuf; i = sysconf(_SC_NPROCESSORS_CONF); pos += sprintf(pos, "%d%%cpu", i*100); -- cgit v1.2.3