Re: human-readable swap partition sizes with pstat -sh

From: Giorgos Keramidas <keramida_at_freebsd.org>
Date: Sun, 20 Mar 2005 10:11:04 +0200
Following up to an old thread, which seem to have stall when I got
busier with other things...  Sorry for the delay, guys :-|

On 2005-01-06 12:07, Brooks Davis <brooks_at_one-eyed-alien.net> wrote:
>>>> 	gothmog:/d/src/usr.sbin/pstat$ ./pstat -sh
>>>> 	Device          1K-blocks     Used    Avail Capacity
>>>> 	/dev/ad1s1b       5120000      12K     4.9G     0%
>>>
>>> Look good in general.  Does -kh make sense?  I think so since it would
>>> force the blocks line, but I'm not 100% sure. [...]
>>> On minor, mostly style nit is that while intmax_t is 64-bits, nothing
>>> requires that so you should probably have conver return an int64_t.
>
> The CONVERT macro used to case to (int).  You removed that cast which
> works because humanize_number takes an int64_t and intmax_t is the same
> as int64_t on all architectures.

I see that humanize_number still expects an int64_t.

On 2005-01-06 23:58, Pawel Jakub Dawidek <pjd_at_freebsd.org> wrote:
> I like intmax_t also much better than int64_t, but I took it from
> NetBSD and they got int64_t there. Anyway, I think we don't have to be
> 100% compatible here and I'll look what can be done.

Bearing this in mind, is it ok then if I commit the pstat changes shown
below, which use intmax_t or is it better to change all the intmax_t
instances in pstat.c to int64_t?

%%%
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.46
diff -u -r1.46 pstat.8
--- pstat.8	18 Jan 2005 20:02:41 -0000	1.46
+++ pstat.8	29 Jan 2005 02:03:05 -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.46 2005/01/18 20:02:41 ru Exp $
 .\"
-.Dd May 23, 2002
+.Dd October 23, 2004
 .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 Sun Mar 20 2005 - 11:23:48 UTC

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