On 2004-10-23 21:29, Giorgos Keramidas <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> I've added a -h flag to pstat(8)/swapinfo(8) that works much like the -h
> flag of du(1), ls(1) and other tools.

I only have ony swap device and failed to notice that a total is printed
for ndevs > 1.  This second try should work better in that case.

I've also moved most of the line format stuff in a print_swap_line()
function to avoid having to touch two places to change the way the lines
are printed ;-)

--- pstat-humanize2.patch starts here ---
Index: Makefile
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pstat/Makefile,v
retrieving revision 1.12
diff -u -r1.12 Makefile
--- Makefile    4 Apr 2003 17:49:17 -0000       1.12
+++ Makefile    23 Oct 2004 17:50:21 -0000
@@ -9,6 +9,6 @@
 WARNS?=        2
 
 DPADD= ${LIBKVM}
-LDADD= -lkvm
+LDADD= -lkvm -lutil
 
 .include <bsd.prog.mk>
Index: pstat.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pstat/pstat.c,v
retrieving revision 1.91
diff -u -r1.91 pstat.c
--- pstat.c     7 Aug 2004 04:27:52 -0000       1.91
+++ pstat.c     23 Oct 2004 20:11:33 -0000
@@ -64,6 +64,7 @@
 #include <err.h>
 #include <fcntl.h>
 #include <kvm.h>
+#include <libutil.h>
 #include <limits.h>
 #include <nlist.h>
 #include <stdio.h>
@@ -86,6 +87,7 @@
        { "" }
 };
 
+static int     humanflag;
 static int     usenumflag;
 static int     totalflag;
 static int     swapflag;
@@ -119,11 +121,11 @@
                opts = argv[0];
        if (!strcmp(opts, "swapinfo")) {
                swapflag = 1;
-               opts = "kM:N:";
-               usagestr = "swapinfo [-k] [-M core [-N system]]";
+               opts = "hkM:N:";
+               usagestr = "swapinfo [-h] [-k] [-M core [-N system]]";
        } else {
-               opts = "TM:N:fknst";
-               usagestr = "pstat [-Tfknst] [-M core [-N system]]";
+               opts = "TM:N:hfknst";
+               usagestr = "pstat [-Tfhknst] [-M core [-N system]]";
        }
 
        while ((ch = getopt(argc, argv, opts)) != -1)
@@ -131,6 +133,9 @@
                case 'f':
                        fileflag = 1;
                        break;
+               case 'h':
+                       humanflag = 1;
+                       break;
                case 'k':
                        putenv("BLOCKSIZE=1K");
                        break;
@@ -487,25 +492,42 @@
 }
 
 static void
-print_swap(struct kvm_swap *ksw)
+print_swap_line(const char *devname, long nblks, long bused, long bavail,
+    float bpercent)
 {
+       char usedbuf[5];
+       char availbuf[5];
        int hlen, pagesize;
        long blocksize;
 
        pagesize = getpagesize();
        getbsize(&hlen, &blocksize);
+
+       printf("%-15s %*d ", devname, hlen, CONVERT(nblks));
+       if (humanflag) {
+               humanize_number(usedbuf, sizeof(usedbuf),
+                   CONVERT(blocksize * bused), "",
+                   HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
+               humanize_number(availbuf, sizeof(availbuf),
+                   CONVERT(blocksize * bavail), "",
+                   HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
+               printf("%8s %8s %5.0f%%\n", usedbuf, availbuf, bpercent);
+       } else {
+               printf("%8ld %8ld %5.0f%%\n", bused, bavail, bpercent);
+       }
+}
+
+static void
+print_swap(struct kvm_swap *ksw)
+{
+
        swtot.ksw_total += ksw->ksw_total;
        swtot.ksw_used += ksw->ksw_used;
        ++nswdev;
-       if (totalflag == 0) {
-               (void)printf("%-15s %*d ",
-                   ksw->ksw_devname, hlen,
-                   CONVERT(ksw->ksw_total));
-               (void)printf("%8d %8d %5.0f%%\n",
-                   CONVERT(ksw->ksw_used),
-                   CONVERT(ksw->ksw_total - ksw->ksw_used),
+       if (totalflag == 0)
+               print_swap_line(ksw->ksw_devname, ksw->ksw_total,
+                   ksw->ksw_used, ksw->ksw_total,
                    (ksw->ksw_used * 100.0) / ksw->ksw_total);
-       }
 }
 
 static void
@@ -521,10 +543,8 @@
                (void)printf("%dM/%dM swap space\n",
                    CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total));
        } else if (nswdev > 1) {
-               (void)printf("%-15s %*d %8d %8d %5.0f%%\n",
-                   "Total", hlen, CONVERT(swtot.ksw_total),
-                   CONVERT(swtot.ksw_used),
-                   CONVERT(swtot.ksw_total - swtot.ksw_used),
+               print_swap_line("Total", swtot.ksw_total, swtot.ksw_used,
+                   swtot.ksw_total - swtot.ksw_used,
                    (swtot.ksw_used * 100.0) / swtot.ksw_total);
        }
 }
--- pstat-humanize2.patch ends here ---
_______________________________________________
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to