Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 23 Oct 2004 21:29:58 +0300
From:      Giorgos Keramidas <keramida@freebsd.org>
To:        freebsd-hackers@freebsd.org
Subject:   pstat/swapinfo -h flag
Message-ID:  <20041023182958.GA35578@gothmog.gr>

next in thread | raw e-mail | index | archive | help
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.  Does anyone see something that is
obviously wrong with this, or have a better idea about implementing it?

The diff is against pstat in 6.0-CURRENT.

--- pstat-humanize.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 18:06:53 -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;
@@ -489,6 +494,8 @@
 static void
 print_swap(struct kvm_swap *ksw)
 {
+	char usedbuf[5];
+	char totalbuf[5];
 	int hlen, pagesize;
 	long blocksize;
 
@@ -501,10 +508,21 @@
 		(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),
-		    (ksw->ksw_used * 100.0) / ksw->ksw_total);
+		if (humanflag) {
+			humanize_number(usedbuf, sizeof(usedbuf),
+			    CONVERT(blocksize * ksw->ksw_used), "",
+			    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
+			humanize_number(totalbuf, sizeof(totalbuf),
+			    CONVERT(blocksize * ksw->ksw_total), "",
+			    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
+			printf("%8s %8s %5.0f%%\n", usedbuf, totalbuf,
+			    (ksw->ksw_used * 100.0) / ksw->ksw_total);
+		} else {
+			printf("%8d %8d %5.0f%%\n",
+			    CONVERT(ksw->ksw_used),
+			    CONVERT(ksw->ksw_total - ksw->ksw_used),
+			    (ksw->ksw_used * 100.0) / ksw->ksw_total);
+		}
 	}
 }
 
--- pstat-humanize.patch ends here ---



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20041023182958.GA35578>