Improve size readability.

Preserve more space for swap devise names.
Prevent line overflow with long devise name.
Don't draw a bar when swap is not used at all.
Simplify and optimize code.
Change the label to end at end of 100%.
PR:		251655
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D27496
This commit is contained in:
Michael Reifenberger 2021-02-15 20:23:32 +01:00
parent 41032835dc
commit 8d06c3e7a4
4 changed files with 52 additions and 93 deletions

View File

@ -45,10 +45,9 @@ The
.Fn getbsize
function returns a preferred block size for reporting by system utilities
.Xr df 1 ,
.Xr du 1 ,
.Xr ls 1
.Xr du 1
and
.Xr systat 1 ,
.Xr ls 1 ,
based on the value of the
.Ev BLOCKSIZE
environment variable.

View File

@ -168,6 +168,7 @@ char *sysctl_dynread(const char *, size_t *);
void sysputpage(WINDOW* , int, int, int, uint64_t, int);
void sysputspaces(WINDOW* , int, int, int);
void sysputstrs(WINDOW* , int, int, int);
void sysputXs(WINDOW* , int, int, int);
void sysputuint64(WINDOW* , int, int, int, uint64_t, int);
void sysputwuint64(WINDOW* , int, int, int, uint64_t, int);

View File

@ -3,7 +3,7 @@
*
* Copyright (c) 1980, 1992, 1993
* The Regents of the University of California. All rights reserved.
* Copyright (c) 2017 Yoshihiro Ota
* Copyright (c) 2017, 2020 Yoshihiro Ota
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -58,22 +58,19 @@ static const char sccsid[] = "@(#)swap.c 8.3 (Berkeley) 4/29/95";
#include "extern.h"
#include "devs.h"
static char *header;
static long blocksize;
static int dlen, odlen;
static int hlen;
static int ulen, oulen;
static int pagesize;
static int pathlen;
WINDOW *
openswap(void)
{
return (subwin(stdscr, LINES - 3 - 1, 0, MAINWIN_ROW, 0));
}
void
closeswap(WINDOW *w)
{
if (w == NULL)
return;
wclear(w);
@ -92,29 +89,6 @@ closeswap(WINDOW *w)
static struct kvm_swap kvmsw[NSWAP];
static int kvnsw, okvnsw;
static void calclens(void);
#define CONVERT(v) ((int)((int64_t)(v) * pagesize / blocksize))
static void
calclens(void)
{
int i, n;
int len;
dlen = sizeof("Disk");
for (i = 0; i < kvnsw; ++i) {
len = strlen(kvmsw[i].ksw_devname);
if (dlen < len)
dlen = len;
}
ulen = sizeof("Used");
for (n = CONVERT(kvmsw[kvnsw].ksw_used), len = 2; n /= 10; ++len);
if (ulen < len)
ulen = len;
}
int
initswap(void)
{
@ -123,22 +97,13 @@ initswap(void)
if (once)
return (1);
header = getbsize(&hlen, &blocksize);
pagesize = getpagesize();
if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) {
error("systat: kvm_getswapinfo failed");
return (0);
}
okvnsw = kvnsw;
calclens();
odlen = dlen;
oulen = ulen;
once = 1;
pathlen = 80 - 50 /* % */ - 5 /* Used */ - 5 /* Size */ - 3 /* space */;
dsinit(12);
once = 1;
return (1);
}
@ -146,16 +111,13 @@ initswap(void)
void
fetchswap(void)
{
okvnsw = kvnsw;
if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) {
error("systat: kvm_getswapinfo failed");
return;
}
odlen = dlen;
oulen = ulen;
calclens();
struct devinfo *tmp_dinfo;
tmp_dinfo = last_dev.dinfo;
@ -172,24 +134,23 @@ labelswap(void)
const char *name;
int i;
fetchswap();
werase(wnd);
mvwprintw(wnd, 0, 0, "%*s%*s%*s %s",
-dlen, "Disk", hlen, header, ulen, "Used",
"/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100");
dslabel(12, 0, 18);
if (kvnsw <= 0) {
mvwprintw(wnd, 0, 0, "(swap not configured)");
return;
}
mvwprintw(wnd, 0, 0, "%*s%5s %5s %s",
-pathlen, "Device/Path", "Size", "Used",
"|0% /10 /20 /30 /40 / 60\\ 70\\ 80\\ 90\\ 100|");
for (i = 0; i <= kvnsw; ++i) {
if (i == kvnsw) {
if (kvnsw == 1)
break;
name = "Total";
} else
name = kvmsw[i].ksw_devname;
mvwprintw(wnd, i + 1, 0, "%*s", -dlen, name);
name = i == kvnsw ? "Total" : kvmsw[i].ksw_devname;
mvwprintw(wnd, 1 + i, 0, "%-*.*s", pathlen, pathlen - 1, name);
}
dslabel(12, 0, 18);
}
void
@ -198,36 +159,23 @@ showswap(void)
int count;
int i;
if (kvnsw != okvnsw || dlen != odlen || ulen != oulen)
if (kvnsw != okvnsw)
labelswap();
dsshow(12, 0, 18, &cur_dev, &last_dev);
if (kvnsw <= 0)
return;
for (i = 0; i <= kvnsw; ++i) {
if (i == kvnsw) {
if (kvnsw == 1)
break;
sysputpage(wnd, i + 1, pathlen, 5, kvmsw[i].ksw_total, 0);
sysputpage(wnd, i + 1, pathlen + 5 + 1, 5, kvmsw[i].ksw_used,
0);
if (kvmsw[i].ksw_used > 0) {
count = 50 * kvmsw[i].ksw_used / kvmsw[i].ksw_total;
sysputXs(wnd, i + 1, pathlen + 5 + 1 + 5 + 1, count);
}
if (kvmsw[i].ksw_total == 0) {
mvwprintw(
wnd,
i + 1,
dlen + hlen + ulen + 1,
"(swap not configured)"
);
continue;
}
wmove(wnd, i + 1, dlen);
wprintw(wnd, "%*d", hlen, CONVERT(kvmsw[i].ksw_total));
wprintw(wnd, "%*d", ulen, CONVERT(kvmsw[i].ksw_used));
count = 50.0 * kvmsw[i].ksw_used / kvmsw[i].ksw_total + 1;
waddch(wnd, ' ');
while (count--)
waddch(wnd, 'X');
wclrtoeol(wnd);
}
dsshow(12, 0, 18, &cur_dev, &last_dev);
}

View File

@ -1,5 +1,5 @@
/*-
* Copyright (c) 2019 Yoshihiro Ota
* Copyright (c) 2019, 2020 Yoshihiro Ota
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -43,17 +43,28 @@ __FBSDID("$FreeBSD$");
void
sysputspaces(WINDOW *wd, int row, int col, int width)
{
static char str40[] = " ";
static char str60[] = " "
" ";
mvwaddstr(wd, row, col, str40 + sizeof(str40) - width - 1);
mvwaddstr(wd, row, col, str60 + sizeof(str60) - width - 1);
}
void
sysputstrs(WINDOW *wd, int row, int col, int width)
{
static char str40[] = "****************************************";
static char str60[] = "********************"
"****************************************";
mvwaddstr(wnd, row, col, str40 + sizeof(str40) - width - 1);
mvwaddstr(wnd, row, col, str60 + sizeof(str60) - width - 1);
}
void
sysputXs(WINDOW *wd, int row, int col, int width)
{
static char str60[] = "XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
mvwaddstr(wnd, row, col, str60 + sizeof(str60) - width - 1);
}
void