From owner-freebsd-hackers@FreeBSD.ORG Sat Oct 23 18:30:08 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 686C616A4CE for ; Sat, 23 Oct 2004 18:30:08 +0000 (GMT) Received: from aiolos.otenet.gr (aiolos.otenet.gr [195.170.0.23]) by mx1.FreeBSD.org (Postfix) with ESMTP id A63E243D1F for ; Sat, 23 Oct 2004 18:30:07 +0000 (GMT) (envelope-from keramida@freebsd.org) Received: from gothmog.gr (patr530-b207.otenet.gr [212.205.244.215]) i9NIU47A008377 for ; Sat, 23 Oct 2004 21:30:05 +0300 Received: from gothmog.gr (gothmog [127.0.0.1]) by gothmog.gr (8.13.1/8.13.1) with ESMTP id i9NITwGo046278 for ; Sat, 23 Oct 2004 21:29:58 +0300 (EEST) (envelope-from keramida@freebsd.org) Received: (from giorgos@localhost) by gothmog.gr (8.13.1/8.13.1/Submit) id i9NITwut046277 for freebsd-hackers@freebsd.org; Sat, 23 Oct 2004 21:29:58 +0300 (EEST) (envelope-from keramida@freebsd.org) Date: Sat, 23 Oct 2004 21:29:58 +0300 From: Giorgos Keramidas To: freebsd-hackers@freebsd.org Message-ID: <20041023182958.GA35578@gothmog.gr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Subject: pstat/swapinfo -h flag X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 23 Oct 2004 18:30:08 -0000 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 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 #include #include +#include #include #include #include @@ -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 ---