- Add a -h flag to pstat to print swap sizes in "human readable"

format, with humanize_number(3).

- Move the common parts of the code that prints the sizes for a single
  swap device and the total to a single function to avoid repeating
  the humanize_number() stuff all over the place.

- Change the type of CONVERT() from intmax_t to int64_t, since this
  makes calling humanize_number() easier but cast the values to
  intmax_t before printing them, to make use of the %jd format that
  printf() supports.

- Document the new -h flag in the manpage and bump its date.

Approved by:	pjd
Useful tips:	brooks
MFC after:	2 weeks
This commit is contained in:
Giorgos Keramidas 2005-03-21 16:36:05 +00:00
parent 1b55f32716
commit 7257230f7b
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=143929
3 changed files with 50 additions and 24 deletions

View File

@ -8,7 +8,7 @@ MLINKS= pstat.8 swapinfo.8
WARNS?= 2
DPADD= ${LIBKVM}
LDADD= -lkvm
DPADD= ${LIBKVM} ${LIBUTIL}
LDADD= -lkvm -lutil
.include <bsd.prog.mk>

View File

@ -35,7 +35,7 @@
.\" @(#)pstat.8 8.5 (Berkeley) 5/13/94
.\" $FreeBSD$
.\"
.Dd May 23, 2002
.Dd March 21, 2005
.Dt PSTAT 8
.Os
.Sh NAME
@ -44,10 +44,10 @@
.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
@ -77,6 +77,11 @@ The following options are available:
.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

View File

@ -65,6 +65,7 @@ __FBSDID("$FreeBSD$");
#include <errno.h>
#include <fcntl.h>
#include <kvm.h>
#include <libutil.h>
#include <limits.h>
#include <nlist.h>
#include <stdio.h>
@ -87,6 +88,7 @@ static struct nlist nl[] = {
{ "" }
};
static int humanflag;
static int usenumflag;
static int totalflag;
static int swapflag;
@ -120,11 +122,11 @@ main(int argc, char *argv[])
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)
@ -132,6 +134,9 @@ main(int argc, char *argv[])
case 'f':
fileflag = 1;
break;
case 'h':
humanflag = 1;
break;
case 'k':
putenv("BLOCKSIZE=1K");
break;
@ -469,7 +474,7 @@ getfiles(char **abuf, size_t *alen)
* by Kevin Lahey <kml@rokkaku.atl.ga.us>.
*/
#define CONVERT(v) ((int)((intmax_t)(v) * pagesize / blocksize))
#define CONVERT(v) ((int64_t)(v) * pagesize / blocksize)
static struct kvm_swap swtot;
static int nswdev;
@ -488,25 +493,43 @@ print_swap_header(void)
}
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", (intmax_t)CONVERT(bused),
(intmax_t)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
@ -519,13 +542,11 @@ print_swap_total(void)
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);
}
}