human-readable swap partition sizes with pstat -sh

From: Giorgos Keramidas <keramida_at_freebsd.org>
Date: Thu, 6 Jan 2005 21:12:01 +0200
The following patch adds support for human-readable partition sizes in
pstat -s and swapinfo output, when the -h option is used:

	gothmog:/d/src/usr.sbin/pstat$ ./pstat -s
	Device          1K-blocks     Used    Avail Capacity
	/dev/ad1s1b       5120000       12  5120000     0%

	gothmog:/d/src/usr.sbin/pstat$ ./pstat -sh
	Device          1K-blocks     Used    Avail Capacity
	/dev/ad1s1b       5120000      12K     4.9G     0%

Does anyone have comments or suggestions for further improvement?

%%%
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 20:44:14 -0000
_at__at_ -8,7 +8,7 _at__at_
 
 WARNS?=	2
 
-DPADD=	${LIBKVM}
-LDADD=	-lkvm
+DPADD=	${LIBKVM} ${LIBUTIL}
+LDADD=	-lkvm -lutil
 
 .include <bsd.prog.mk>
Index: pstat.8
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pstat/pstat.8,v
retrieving revision 1.45
diff -u -r1.45 pstat.8
--- pstat.8	11 Nov 2004 17:30:04 -0000	1.45
+++ pstat.8	6 Jan 2005 15:57:16 -0000
_at__at_ -35,7 +35,7 _at__at_
 .\"     _at_(#)pstat.8	8.5 (Berkeley) 5/13/94
 .\" $FreeBSD: src/usr.sbin/pstat/pstat.8,v 1.45 2004/11/11 17:30:04 dds Exp $
 .\"
-.Dd May 23, 2002
+.Dd January 6, 2005
 .Dt PSTAT 8
 .Os
 .Sh NAME
_at__at_ -44,10 +44,10 _at__at_
 .Nd display system data structures
 .Sh SYNOPSIS
 .Nm
-.Op Fl Tfknst
+.Op Fl Tfhknst
 .Op Fl M Ar core Op Fl N Ar system
 .Nm swapinfo
-.Op Fl k
+.Op Fl hk
 .Op Fl M Ar core Op Fl N Ar system
 .Sh DESCRIPTION
 The
_at__at_ -77,6 +77,11 _at__at_
 .Bl -tag -width indent
 .It Fl n
 Print devices out by major/minor instead of name.
+.It Fl h
+.Dq Human-readable
+output.
+Use unit suffixes when printing swap partition sizes:
+Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte.
 .It Fl k
 Print sizes in kilobytes, regardless of the setting of the
 .Ev BLOCKSIZE
Index: pstat.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/pstat/pstat.c,v
retrieving revision 1.92
diff -u -r1.92 pstat.c
--- pstat.c	27 Nov 2004 06:51:39 -0000	1.92
+++ pstat.c	6 Dec 2004 04:02:35 -0000
_at__at_ -65,6 +65,7 _at__at_
 #include <errno.h>
 #include <fcntl.h>
 #include <kvm.h>
+#include <libutil.h>
 #include <limits.h>
 #include <nlist.h>
 #include <stdio.h>
_at__at_ -87,6 +88,7 _at__at_
 	{ "" }
 };
 
+static int	humanflag;
 static int	usenumflag;
 static int	totalflag;
 static int	swapflag;
_at__at_ -120,11 +122,11 _at__at_
 		opts = argv[0];
 	if (!strcmp(opts, "swapinfo")) {
 		swapflag = 1;
-		opts = "kM:N:";
-		usagestr = "swapinfo [-k] [-M core [-N system]]";
+		opts = "hkM:N:";
+		usagestr = "swapinfo [-hk] [-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)
_at__at_ -132,6 +134,9 _at__at_
 		case 'f':
 			fileflag = 1;
 			break;
+		case 'h':
+			humanflag = 1;
+			break;
 		case 'k':
 			putenv("BLOCKSIZE=1K");
 			break;
_at__at_ -469,7 +474,7 _at__at_
  * by Kevin Lahey <kml_at_rokkaku.atl.ga.us>.
  */
 
-#define CONVERT(v)	((int)((intmax_t)(v) * pagesize / blocksize))
+#define CONVERT(v)	((intmax_t)(v) * pagesize / blocksize)
 static struct kvm_swap swtot;
 static int nswdev;
 
_at__at_ -488,25 +493,43 _at__at_
 }
 
 static void
-print_swap(struct kvm_swap *ksw)
+print_swap_line(const char *devname, intmax_t nblks, intmax_t bused,
+    intmax_t bavail, float bpercent)
 {
+	char usedbuf[5];
+	char availbuf[5];
 	int hlen, pagesize;
 	long blocksize;
 
 	pagesize = getpagesize();
 	getbsize(&hlen, &blocksize);
+
+	printf("%-15s %*jd ", 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("%8jd %8jd %5.0f%%\n", CONVERT(bused),
+		    CONVERT(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
_at__at_ -519,13 +542,11 _at__at_
 	getbsize(&hlen, &blocksize);
 	if (totalflag) {
 		blocksize = 1024 * 1024;
-		(void)printf("%dM/%dM swap space\n",
+		(void)printf("%jdM/%jdM 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);
 	}
 }
%%%
Received on Thu Jan 06 2005 - 18:12:06 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:25 UTC