support "-g" and "-m" as well as "-k" and "-h". swapctl & swaplist code

from Martin Weber, adapted for pstat by myself.
This commit is contained in:
mrg 2003-12-20 13:31:42 +00:00
parent 7dc2749ff7
commit 9bf440d20b
5 changed files with 103 additions and 38 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: swapctl.8,v 1.27 2003/12/20 11:22:25 mrg Exp $
.\" $NetBSD: swapctl.8,v 1.28 2003/12/20 13:31:43 mrg Exp $
.\"
.\" Copyright (c) 1997 Matthew R. Green
.\" All rights reserved.
@ -56,7 +56,7 @@
.Ar path
.Nm
.Fl l | Fl s
.Op Fl k
.Op Fl k | Fl m | Fl g
.Op Fl h
.Nm
.Fl z
@ -182,6 +182,14 @@ options.
The
.Fl k
option uses 1024 byte blocks instead of the default 512 byte.
.It Fl m
The
.Fl m
option uses (1024 * 1024) byte blocks instead of the default 512 byte.
.It Fl g
The
.Fl g
option uses (1024 * 1024 * 1024) byte blocks instead of the default 512 byte.
.It Fl h
The
.Fl h

View File

@ -1,4 +1,4 @@
/* $NetBSD: swapctl.c,v 1.24 2003/12/20 11:22:25 mrg Exp $ */
/* $NetBSD: swapctl.c,v 1.25 2003/12/20 13:31:43 mrg Exp $ */
/*
* Copyright (c) 1996, 1997, 1999 Matthew R. Green
@ -39,8 +39,10 @@
* or all non-block devices
* -a <dev> add this device
* -d <dev> remove this swap device
* -g use gigabytes
* -h use humanize_number(3) for listing
* -l list swap devices
* -m use megabytes
* -s short listing of swap devices
* -k use kilobytes
* -p <pri> use this priority
@ -56,7 +58,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: swapctl.c,v 1.24 2003/12/20 11:22:25 mrg Exp $");
__RCSID("$NetBSD: swapctl.c,v 1.25 2003/12/20 13:31:43 mrg Exp $");
#endif
@ -106,8 +108,10 @@ do { \
/*
* Option flags, and the commands with which they are valid.
*/
int kflag; /* display in 1K blocks */
int kflag; /* display in 1K^x blocks */
#define KFLAG_CMDS (CMD_l | CMD_s)
#define MFLAG_CMDS (CMD_l | CMD_s)
#define GFLAG_CMDS (CMD_l | CMD_s)
int hflag; /* display with humanize_number */
#define HFLAG_CMDS (CMD_l | CMD_s)
@ -152,7 +156,7 @@ main(argc, argv)
}
#endif
while ((c = getopt(argc, argv, "ADUacdhlkp:st:z")) != -1) {
while ((c = getopt(argc, argv, "ADUacdghklmp:st:z")) != -1) {
switch (c) {
case 'A':
SET_COMMAND(CMD_A);
@ -178,16 +182,24 @@ main(argc, argv)
SET_COMMAND(CMD_d);
break;
case 'g':
kflag = 3; /* 1k ^ 3 */
break;
case 'h':
hflag = 1;
break;
case 'k':
kflag = 1;
break;
case 'l':
SET_COMMAND(CMD_l);
break;
case 'k':
kflag = 1;
case 'm':
kflag = 2; /* 1k ^ 2 */
break;
case 'p':
@ -576,6 +588,6 @@ usage()
fprintf(stderr, " %s -a [-p priority] path\n", progname);
fprintf(stderr, " %s -c -p priority path\n", progname);
fprintf(stderr, " %s -d path\n", progname);
fprintf(stderr, " %s -l | -s [-k|-h]\n", progname);
fprintf(stderr, " %s -l | -s [-k|-m|-g|-h]\n", progname);
exit(1);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: swaplist.c,v 1.11 2003/12/20 11:22:25 mrg Exp $ */
/* $NetBSD: swaplist.c,v 1.12 2003/12/20 13:31:43 mrg Exp $ */
/*
* Copyright (c) 1997 Matthew R. Green
@ -30,7 +30,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: swaplist.c,v 1.11 2003/12/20 11:22:25 mrg Exp $");
__RCSID("$NetBSD: swaplist.c,v 1.12 2003/12/20 13:31:43 mrg Exp $");
#endif
@ -66,7 +66,7 @@ list_swap(pri, kflag, pflag, tflag, dolong, hflag)
{
struct swapent *sep, *fsep;
long blocksize;
char *header;
char *header, *suff;
char szbuf[5], usbuf[5], avbuf[5]; /* size, used, avail */
size_t l;
int hlen, totalsize, size, totalinuse, inuse, ncounted, pathmax;
@ -88,17 +88,36 @@ list_swap(pri, kflag, pflag, tflag, dolong, hflag)
rnswap, nswap);
pathmax = 11;
if (dolong && tflag == 0) {
if (kflag) {
switch(kflag) {
case 1:
header = "1K-blocks";
blocksize = 1024;
hlen = strlen(header);
} else if (hflag) {
header = "Size";
blocksize = 1; /* unused */
hlen = strlen(header);
} else
header = getbsize(&hlen, &blocksize);
suff = "KBytes";
break;
case 2:
suff = "MBytes";
header = "1M-blocks";
blocksize = 1024 * 1024;
break;
case 3:
header = "1G-blocks";
blocksize = 1024 * 1024 * 1024;
suff = "GBytes";
break;
default:
suff = "blocks";
if (!hflag) {
header = getbsize(&hlen, &blocksize);
} else {
header = "Size";
blocksize = 1; suff = ""; /* unused */
}
break;
}
if (hflag || kflag)
hlen = strlen(header);
if (dolong && tflag == 0) {
for (i = rnswap; i-- > 0; sep++)
if (pathmax < (l = strlen(sep->se_path)))
pathmax = l;
@ -146,10 +165,22 @@ list_swap(pri, kflag, pflag, tflag, dolong, hflag)
}
}
}
if (tflag)
(void)printf("%dM/%dM swap space\n",
(int)(dbtoqb(totalinuse) / (1024 * 1024)),
(int)(dbtoqb(totalsize) / (1024 * 1024)));
if (tflag) {
if (hflag) {
if ((humanize_number(usbuf, sizeof(usbuf), (dbtoqb(totalinuse)),
"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
err(1, "humanize_number");
if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)),
"", HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
err(1, "humanize_number");
(void)printf("%s/%s swap space\n", usbuf, szbuf);
} else {
(void)printf("%ld/%ld %s swap space\n",
(long)(dbtoqb(totalinuse) / blocksize),
(long)(dbtoqb(totalsize) / blocksize),
suff);
}
}
else if (dolong == 0) {
if (hflag) {
if ((humanize_number(szbuf, sizeof(szbuf), (dbtoqb(totalsize)),
@ -164,11 +195,11 @@ list_swap(pri, kflag, pflag, tflag, dolong, hflag)
(void)printf("total: %s allocated = %s used, %s available.\n",
szbuf, usbuf, avbuf);
} else {
printf("total: %ldk bytes allocated = %ldk used, "
"%ldk available\n",
(long)(dbtoqb(totalsize) / 1024),
(long)(dbtoqb(totalinuse) / 1024),
(long)(dbtoqb(totalsize - totalinuse) / 1024));
printf("total: %ld %s allocated = %ld %s used, "
"%ld %s available\n",
(long)(dbtoqb(totalsize) / blocksize), suff,
(long)(dbtoqb(totalinuse) / blocksize), suff,
(long)(dbtoqb(totalsize - totalinuse) / blocksize), suff);
}
} else if (ncounted > 1) {
if (hflag) {

View File

@ -1,4 +1,4 @@
.\" $NetBSD: pstat.8,v 1.32 2003/12/20 11:22:25 mrg Exp $
.\" $NetBSD: pstat.8,v 1.33 2003/12/20 13:31:42 mrg Exp $
.\"
.\" Copyright (c) 1980, 1991, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
@ -41,7 +41,7 @@
.Fl T | Fl f | Fl s |
.Fl t | Fl v
.Oc
.Op Fl knh
.Op Fl ghkmn
.Op Fl M Ar core
.Op Fl N Ar system
.Sh DESCRIPTION
@ -99,12 +99,20 @@ The location of the vnode table entry or socket structure for this file.
The file offset (see
.Xr lseek 2 ) .
.El
.It Fl k
Use 1K-byte blocks.
.It Fl g
The
.Fl g
option uses (1024 * 1024 * 1024) byte blocks instead of the default 512 byte.
.It Fl h
Use
.Xr humanize_number 3
to display (swap) sizes.
.It Fl k
Use 1K-byte blocks.
.It Fl m
The
.Fl m
option uses (1024 * 1024) byte blocks instead of the default 512 byte.
.It Fl n
Print devices by major/minor number rather than by name.
.It Fl s

View File

@ -1,4 +1,4 @@
/* $NetBSD: pstat.c,v 1.84 2003/12/20 11:22:25 mrg Exp $ */
/* $NetBSD: pstat.c,v 1.85 2003/12/20 13:31:42 mrg Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993, 1994
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95";
#else
__RCSID("$NetBSD: pstat.c,v 1.84 2003/12/20 11:22:25 mrg Exp $");
__RCSID("$NetBSD: pstat.c,v 1.85 2003/12/20 13:31:42 mrg Exp $");
#endif
#endif /* not lint */
@ -190,7 +190,7 @@ main(argc, argv)
setegid(getgid());
fileflag = swapflag = ttyflag = vnodeflag = 0;
while ((ch = getopt(argc, argv, "TM:N:fhiknstv")) != -1)
while ((ch = getopt(argc, argv, "TM:N:fghikmnstv")) != -1)
switch (ch) {
case 'f':
fileflag = 1;
@ -216,9 +216,15 @@ main(argc, argv)
case 'k':
kflag = 1;
break;
case 'g':
kflag = 3; /* 1k ^ 3 */
break;
case 'h':
hflag = 1;
break;
case 'm':
kflag = 2; /* 1k ^ 2 */
break;
case 'v':
case 'i': /* Backward compatibility. */
vnodeflag = 1;
@ -957,7 +963,7 @@ usage()
{
(void)fprintf(stderr,
"usage: %s [-T|-f|-s|-t|-v] [-knh] [-M core] [-N system]\n",
"usage: %s [-T|-f|-s|-t|-v] [-ghkmn] [-M core] [-N system]\n",
getprogname());
exit(1);
}