Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/systat/swap.c
/*- | /*- | ||||
* SPDX-License-Identifier: BSD-3-Clause | * SPDX-License-Identifier: BSD-3-Clause | ||||
* | * | ||||
* 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 | ||||
* are met: | * are met: | ||||
* 1. Redistributions of source code must retain the above copyright | * 1. Redistributions of source code must retain the above copyright | ||||
* notice, this list of conditions and the following disclaimer. | * notice, this list of conditions and the following disclaimer. | ||||
* 2. Redistributions in binary form must reproduce the above copyright | * 2. Redistributions in binary form must reproduce the above copyright | ||||
* notice, this list of conditions and the following disclaimer in the | * notice, this list of conditions and the following disclaimer in the | ||||
Show All 38 Lines | |||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include "systat.h" | #include "systat.h" | ||||
#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); | ||||
wrefresh(w); | wrefresh(w); | ||||
delwin(w); | delwin(w); | ||||
} | } | ||||
/* | /* | ||||
* The meat of all the swap stuff is stolen from pstat(8)'s | * The meat of all the swap stuff is stolen from pstat(8)'s | ||||
* swapmode(), which is based on a program called swapinfo written by | * swapmode(), which is based on a program called swapinfo written by | ||||
* Kevin Lahey <kml@rokkaku.atl.ga.us>. | * Kevin Lahey <kml@rokkaku.atl.ga.us>. | ||||
*/ | */ | ||||
#define NSWAP 16 | #define NSWAP 16 | ||||
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) | ||||
{ | { | ||||
static int once = 0; | static int once = 0; | ||||
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 */; | ||||
dsinit(12); | |||||
calclens(); | |||||
odlen = dlen; | |||||
oulen = ulen; | |||||
once = 1; | once = 1; | ||||
dsinit(12); | |||||
return (1); | return (1); | ||||
} | } | ||||
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; | ||||
last_dev.dinfo = cur_dev.dinfo; | last_dev.dinfo = cur_dev.dinfo; | ||||
cur_dev.dinfo = tmp_dinfo; | cur_dev.dinfo = tmp_dinfo; | ||||
last_dev.snap_time = cur_dev.snap_time; | last_dev.snap_time = cur_dev.snap_time; | ||||
dsgetinfo( &cur_dev ); | dsgetinfo( &cur_dev ); | ||||
} | } | ||||
void | void | ||||
labelswap(void) | 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 <= 1) { | |||||
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 | ||||
showswap(void) | showswap(void) | ||||
{ | { | ||||
int count; | int count; | ||||
int i; | int i; | ||||
if (kvnsw != okvnsw || dlen != odlen || ulen != oulen) | if (kvnsw != okvnsw) | ||||
labelswap(); | labelswap(); | ||||
for (i = 0; i <= kvnsw; ++i) { | dsshow(12, 0, 18, &cur_dev, &last_dev); | ||||
if (i == kvnsw) { | |||||
if (kvnsw == 1) | |||||
break; | |||||
} | |||||
if (kvmsw[i].ksw_total == 0) { | if (kvnsw <= 1) | ||||
mvwprintw( | return; | ||||
wnd, | |||||
i + 1, | |||||
dlen + hlen + ulen + 1, | |||||
"(swap not configured)" | |||||
); | |||||
continue; | |||||
} | |||||
wmove(wnd, i + 1, dlen); | for (i = 0; i <= kvnsw; ++i) { | ||||
sysputpage(wnd, i + 1, pathlen, 5, kvmsw[i].ksw_total, 0); | |||||
sysputpage(wnd, i + 1, pathlen + 5 + 1, 5, kvmsw[i].ksw_used, | |||||
0); | |||||
wprintw(wnd, "%*d", hlen, CONVERT(kvmsw[i].ksw_total)); | if (kvmsw[i].ksw_used > 0) { | ||||
wprintw(wnd, "%*d", ulen, CONVERT(kvmsw[i].ksw_used)); | count = 50 * kvmsw[i].ksw_used / kvmsw[i].ksw_total; | ||||
sysputXs(wnd, i + 1, pathlen + 5 + 1 + 5 + 1, count); | |||||
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); | |||||
} | } |