[Toybox] [PATCH] top: use human_readable for the header lines too.

enh enh at google.com
Wed Feb 13 15:16:56 PST 2019


Even phones have enough RAM these days that KiB is not a reasonable
unit. Traditional top always uses MiB instead of always using KiB, but
we may as well just let human_readable pick a unit (that way if KiB is
reasonable on your box, that's what you'll get).

Before:

  Tasks: 967 total,   1 running, 581 sleeping,   0 stopped,   0 zombie
  Mem: 196734820k total,183891564k used, 12843256k free,  5805008k buffers
  Swap:199888892k total,   719104k used,199169788k free,130367280k cached

After:

  Tasks: 965 total,   2 running, 577 sleeping,   0 stopped,   0 zombie
    Mem:      188G total,      175G used,       13G free,      5.5G buffers
   Swap:      191G total,      702M used,      190G free,      124G cached
---
 toys/posix/ps.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/toys/posix/ps.c b/toys/posix/ps.c
index ce13d4f6..926132c4 100644
--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -1544,6 +1544,8 @@ static void top_common(
         // Display "top" header.
         if (*toys.which->name == 't') {
           struct ofields field;
+          char *hr0 = toybuf+sizeof(toybuf)-32, *hr1 = hr0-32, *hr2 = hr1-32,
+            *hr3 = hr2-32;
           long long ll, up = 0;
           long run[6];
           int j;
@@ -1564,13 +1566,21 @@ static void top_common(
                     "\nBuffers:","\nCached:","\nSwapTotal:","\nSwapFree:"}[i]);
               run[i] = pos ? atol(pos) : 0;
             }
-            sprintf(toybuf,
-             "Mem:%10ldk total,%9ldk used,%9ldk free,%9ldk buffers",
-              run[0], run[0]-run[1], run[1], run[2]);
+
+            human_readable(hr0, 1024*run[0], 0);
+            human_readable(hr1, 1024*(run[0]-run[1]), 0);
+            human_readable(hr2, 1024*run[1], 0);
+            human_readable(hr3, 1024*run[2], 0);
+            sprintf(toybuf, "  Mem: %9s total, %9s used, %9s free,
%9s buffers",
+              hr0, hr1, hr2, hr3);
             lines = header_line(lines, 0);
-            sprintf(toybuf,
-              "Swap:%9ldk total,%9ldk used,%9ldk free,%9ldk cached",
-              run[4], run[4]-run[5], run[5], run[3]);
+
+            human_readable(hr0, 1024*run[4], 0);
+            human_readable(hr1, 1024*(run[4]-run[5]), 0);
+            human_readable(hr2, 1024*run[5], 0);
+            human_readable(hr3, 1024*run[3], 0);
+            sprintf(toybuf, " Swap: %9s total, %9s used, %9s free, %9s cached",
+              hr0, hr1, hr2, hr3);
             lines = header_line(lines, 0);
           }

-- 
2.20.1.791.gb4d0f1c61a-goog
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-top-use-human_readable-for-the-header-lines-too.patch
Type: text/x-patch
Size: 2784 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20190213/ec2fcb98/attachment-0002.bin>


More information about the Toybox mailing list