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 .Fn getbsize
function returns a preferred block size for reporting by system utilities function returns a preferred block size for reporting by system utilities
.Xr df 1 , .Xr df 1 ,
.Xr du 1 , .Xr du 1
.Xr ls 1
and and
.Xr systat 1 , .Xr ls 1 ,
based on the value of the based on the value of the
.Ev BLOCKSIZE .Ev BLOCKSIZE
environment variable. 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 sysputpage(WINDOW* , int, int, int, uint64_t, int);
void sysputspaces(WINDOW* , int, int, int); void sysputspaces(WINDOW* , int, int, int);
void sysputstrs(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 sysputuint64(WINDOW* , int, int, int, uint64_t, int);
void sysputwuint64(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 * Copyright (c) 1980, 1992, 1993
* The Regents of the University of California. All rights reserved. * 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * 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 "extern.h"
#include "devs.h" #include "devs.h"
static char *header; static int pathlen;
static long blocksize;
static int dlen, odlen;
static int hlen;
static int ulen, oulen;
static int pagesize;
WINDOW * WINDOW *
openswap(void) openswap(void)
{ {
return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
return (subwin(stdscr, LINES - 3 - 1, 0, MAINWIN_ROW, 0));
} }
void void
closeswap(WINDOW *w) closeswap(WINDOW *w)
{ {
if (w == NULL) if (w == NULL)
return; return;
wclear(w); wclear(w);
@ -92,29 +89,6 @@ closeswap(WINDOW *w)
static struct kvm_swap kvmsw[NSWAP]; static struct kvm_swap kvmsw[NSWAP];
static int kvnsw, okvnsw; 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 int
initswap(void) initswap(void)
{ {
@ -123,22 +97,13 @@ initswap(void)
if (once) if (once)
return (1); return (1);
header = getbsize(&hlen, &blocksize);
pagesize = getpagesize();
if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) { if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) {
error("systat: kvm_getswapinfo failed"); error("systat: kvm_getswapinfo failed");
return (0); return (0);
} }
okvnsw = kvnsw; pathlen = 80 - 50 /* % */ - 5 /* Used */ - 5 /* Size */ - 3 /* space */;
calclens();
odlen = dlen;
oulen = ulen;
once = 1;
dsinit(12); dsinit(12);
once = 1;
return (1); return (1);
} }
@ -146,16 +111,13 @@ initswap(void)
void void
fetchswap(void) fetchswap(void)
{ {
okvnsw = kvnsw; okvnsw = kvnsw;
if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) { if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) {
error("systat: kvm_getswapinfo failed"); error("systat: kvm_getswapinfo failed");
return; return;
} }
odlen = dlen;
oulen = ulen;
calclens();
struct devinfo *tmp_dinfo; struct devinfo *tmp_dinfo;
tmp_dinfo = last_dev.dinfo; tmp_dinfo = last_dev.dinfo;
@ -172,24 +134,23 @@ labelswap(void)
const char *name; const char *name;
int i; int i;
fetchswap();
werase(wnd); werase(wnd);
mvwprintw(wnd, 0, 0, "%*s%*s%*s %s", dslabel(12, 0, 18);
-dlen, "Disk", hlen, header, ulen, "Used",
"/0% /10 /20 /30 /40 /50 /60 /70 /80 /90 /100"); 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) { for (i = 0; i <= kvnsw; ++i) {
if (i == kvnsw) { name = i == kvnsw ? "Total" : kvmsw[i].ksw_devname;
if (kvnsw == 1) mvwprintw(wnd, 1 + i, 0, "%-*.*s", pathlen, pathlen - 1, name);
break;
name = "Total";
} else
name = kvmsw[i].ksw_devname;
mvwprintw(wnd, i + 1, 0, "%*s", -dlen, name);
} }
dslabel(12, 0, 18);
} }
void void
@ -198,36 +159,23 @@ showswap(void)
int count; int count;
int i; int i;
if (kvnsw != okvnsw || dlen != odlen || ulen != oulen) if (kvnsw != okvnsw)
labelswap(); labelswap();
dsshow(12, 0, 18, &cur_dev, &last_dev);
if (kvnsw <= 0)
return;
for (i = 0; i <= kvnsw; ++i) { for (i = 0; i <= kvnsw; ++i) {
if (i == kvnsw) { sysputpage(wnd, i + 1, pathlen, 5, kvmsw[i].ksw_total, 0);
if (kvnsw == 1) sysputpage(wnd, i + 1, pathlen + 5 + 1, 5, kvmsw[i].ksw_used,
break; 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); 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 * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -43,17 +43,28 @@ __FBSDID("$FreeBSD$");
void void
sysputspaces(WINDOW *wd, int row, int col, int width) 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 void
sysputstrs(WINDOW *wd, int row, int col, int width) 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 void