I have a small, quality of life patch you may find useful.
It changes memory units used on the default page of systat running
with -h flag.

example:
            memory totals (in MB)
           real   virtual     free
Active       36        36     1443
All         523       523     3516

Units should automatically change to GB once the machine has over 10GB of
memory, etc for other units.

I've tested it only on -stable because I couldn't get unmodified systat
from -current to compile. (I'm a newbie, don't shout)
patch applies on fresh repo cloned from github without issues
and probably works.

Copy of diff file in attachment

Index: usr.bin/systat/vmstat.c
===================================================================
RCS file: /cvs/src/usr.bin/systat/vmstat.c,v
retrieving revision 1.91
diff -u -p -u -p -r1.91 vmstat.c
--- usr.bin/systat/vmstat.c    28 Jun 2019 13:35:04 -0000    1.91
+++ usr.bin/systat/vmstat.c    15 Aug 2020 06:07:37 -0000
@@ -103,6 +103,9 @@ static    long *intrloc;
 static    char **intrname;
 static    int ipktsrow;

+int memUnitPrefix = 0; /* 1<<0 == 1KB */
+bool prefixConfigured = false;
+
 WINDOW *
 openkre(void)
 {
@@ -228,12 +231,45 @@ fetchkre(void)
     getinfo(&s3);
 }

+char
+getunitprefix(int bitshift)
+{
+    switch (bitshift) {
+        case(0):
+            return('K');
+        case(10):
+            return('M');
+        case(20):
+            return('G');
+        case(30):
+            return('T');
+        case(40):
+            return('P');
+        default:
+            return('X');
+    }
+    return('X');
+}
+
+#define pgtokb(pg)    ((pg) * (s.uvmexp.pagesize / 1024))
+void
+configPrefix(){
+    while( pgtokb( s.uvmexp.npages ) > ( (1 << (memUnitPrefix+10) ) *10) ){
+        memUnitPrefix+=10;
+    }
+}
+
 void
 labelkre(void)
 {
     int i, j, l;
+    char labelwunit[] = "            memory totals (in KB)";
+
+    if (humanreadable && (! prefixConfigured))
+        configPrefix();
+    labelwunit[30] = getunitprefix(memUnitPrefix);

-    mvprintw(MEMROW, MEMCOL,     "            memory totals (in KB)");
+    mvprintw(MEMROW, MEMCOL,labelwunit);
     mvprintw(MEMROW + 1, MEMCOL, "           real   virtual     free");
     mvprintw(MEMROW + 2, MEMCOL, "Active");
     mvprintw(MEMROW + 3, MEMCOL, "All");
@@ -394,16 +430,16 @@ showkre(void)
             addch(cpuchar[c]);
     }

-#define pgtokb(pg)    ((pg) * (s.uvmexp.pagesize / 1024))
+#define pgtounit(pg)    ((pg) * (s.uvmexp.pagesize / 1024 ) / ( 1 <<
memUnitPrefix ) )

-    putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 7, 8);
-    putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),    /* XXX */
+    putint(pgtounit(s.uvmexp.active), MEMROW + 2, MEMCOL + 7, 8);
+    putint(pgtounit(s.uvmexp.active + s.uvmexp.swpginuse),    /* XXX */
         MEMROW + 2, MEMCOL + 17, 8);
-    putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 7, 8);
-    putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
+    putint(pgtounit(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3,
MEMCOL + 7, 8);
+    putint(pgtounit(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
         MEMROW + 3, MEMCOL + 17, 8);
-    putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 26, 8);
-    putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
+    putint(pgtounit(s.uvmexp.free), MEMROW + 2, MEMCOL + 26, 8);
+    putint(pgtounit(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
         MEMROW + 3, MEMCOL + 26, 8);
     putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
Index: usr.bin/systat/vmstat.c
===================================================================
RCS file: /cvs/src/usr.bin/systat/vmstat.c,v
retrieving revision 1.91
diff -u -p -u -p -r1.91 vmstat.c
--- usr.bin/systat/vmstat.c	28 Jun 2019 13:35:04 -0000	1.91
+++ usr.bin/systat/vmstat.c	15 Aug 2020 06:07:37 -0000
@@ -103,6 +103,9 @@ static	long *intrloc;
 static	char **intrname;
 static	int ipktsrow;
 
+int memUnitPrefix = 0; /* 1<<0 == 1KB */
+bool prefixConfigured = false;
+
 WINDOW *
 openkre(void)
 {
@@ -228,12 +231,45 @@ fetchkre(void)
 	getinfo(&s3);
 }
 
+char
+getunitprefix(int bitshift)
+{
+	switch (bitshift) {
+		case(0):
+			return('K');
+		case(10):
+			return('M');
+		case(20):
+			return('G');
+		case(30):
+			return('T');
+		case(40):
+			return('P');
+		default:
+			return('X');
+	}
+	return('X');
+}
+
+#define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
+void
+configPrefix(){
+	while( pgtokb( s.uvmexp.npages ) > ( (1 << (memUnitPrefix+10) ) *10) ){
+		memUnitPrefix+=10;
+	}
+}
+
 void
 labelkre(void)
 {
 	int i, j, l;
+	char labelwunit[] = "            memory totals (in KB)";
+
+	if (humanreadable && (! prefixConfigured)) 
+		configPrefix();
+	labelwunit[30] = getunitprefix(memUnitPrefix); 
 
-	mvprintw(MEMROW, MEMCOL,     "            memory totals (in KB)");
+	mvprintw(MEMROW, MEMCOL,labelwunit);
 	mvprintw(MEMROW + 1, MEMCOL, "           real   virtual     free");
 	mvprintw(MEMROW + 2, MEMCOL, "Active");
 	mvprintw(MEMROW + 3, MEMCOL, "All");
@@ -394,16 +430,16 @@ showkre(void)
 			addch(cpuchar[c]);
 	}
 
-#define pgtokb(pg)	((pg) * (s.uvmexp.pagesize / 1024))
+#define pgtounit(pg)	((pg) * (s.uvmexp.pagesize / 1024 ) / ( 1 << memUnitPrefix ) )
 
-	putint(pgtokb(s.uvmexp.active), MEMROW + 2, MEMCOL + 7, 8);
-	putint(pgtokb(s.uvmexp.active + s.uvmexp.swpginuse),    /* XXX */
+	putint(pgtounit(s.uvmexp.active), MEMROW + 2, MEMCOL + 7, 8);
+	putint(pgtounit(s.uvmexp.active + s.uvmexp.swpginuse),    /* XXX */
 	    MEMROW + 2, MEMCOL + 17, 8);
-	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 7, 8);
-	putint(pgtokb(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
+	putint(pgtounit(s.uvmexp.npages - s.uvmexp.free), MEMROW + 3, MEMCOL + 7, 8);
+	putint(pgtounit(s.uvmexp.npages - s.uvmexp.free + s.uvmexp.swpginuse),
 	    MEMROW + 3, MEMCOL + 17, 8);
-	putint(pgtokb(s.uvmexp.free), MEMROW + 2, MEMCOL + 26, 8);
-	putint(pgtokb(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
+	putint(pgtounit(s.uvmexp.free), MEMROW + 2, MEMCOL + 26, 8);
+	putint(pgtounit(s.uvmexp.free + s.uvmexp.swpages - s.uvmexp.swpginuse),
 	    MEMROW + 3, MEMCOL + 26, 8);
 	putint(total.t_rq - 1, PROCSROW + 1, PROCSCOL + 3, 3);
 

Reply via email to