From owner-freebsd-hackers@FreeBSD.ORG Sat Oct 23 20:30:36 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 F1CA216A4D1 for ; Sat, 23 Oct 2004 20:30:35 +0000 (GMT) Received: from nic.ach.sch.gr (nic.sch.gr [194.63.238.4]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0535243D5A for ; Sat, 23 Oct 2004 20:30:33 +0000 (GMT) (envelope-from keramida@freebsd.org) Received: (qmail 25165 invoked by uid 207); 23 Oct 2004 20:30:29 -0000 Received: from keramida@freebsd.org by nic by uid 201 with qmail-scanner-1.21 (sophie: 3.04/2.19/3.81. Clear:RC:1(81.186.70.215):. Processed in 4.092427 secs); 23 Oct 2004 20:30:29 -0000 Received: from dialup215.ach.sch.gr (HELO gothmog.gr) ([81.186.70.215]) (envelope-sender ) by nic.sch.gr (qmail-ldap-1.03) with DES-CBC3-SHA encrypted SMTP for ; 23 Oct 2004 20:30:24 -0000 Received: from gothmog.gr (gothmog [127.0.0.1]) by gothmog.gr (8.13.1/8.13.1) with ESMTP id i9NKD1PL091387 for ; Sat, 23 Oct 2004 23:13:01 +0300 (EEST) (envelope-from keramida@freebsd.org) Received: (from giorgos@localhost) by gothmog.gr (8.13.1/8.13.1/Submit) id i9NKD1dk091386 for freebsd-hackers@freebsd.org; Sat, 23 Oct 2004 23:13:01 +0300 (EEST) (envelope-from keramida@freebsd.org) Date: Sat, 23 Oct 2004 23:13:00 +0300 From: Giorgos Keramidas To: freebsd-hackers@freebsd.org Message-ID: <20041023201300.GA88466@gothmog.gr> References: <20041023182958.GA35578@gothmog.gr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20041023182958.GA35578@gothmog.gr> Subject: Re: 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 20:30:36 -0000 On 2004-10-23 21:29, Giorgos Keramidas 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 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 #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; @@ -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 ---