1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-12-12 00:13:37 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#ifdef lint
|
|
|
|
static const char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
2001-12-12 00:13:37 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1980, 1992, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
1997-08-13 06:45:11 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2000-12-17 18:43:28 +00:00
|
|
|
#include <sys/time.h>
|
2001-03-23 03:58:25 +00:00
|
|
|
#include <sys/sysctl.h>
|
2014-05-09 16:20:55 +00:00
|
|
|
#include <sys/queue.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1997-08-13 06:45:11 +00:00
|
|
|
#include <err.h>
|
2001-05-25 23:10:27 +00:00
|
|
|
#include <limits.h>
|
1996-03-31 16:39:26 +00:00
|
|
|
#include <locale.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <nlist.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <paths.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
1998-10-08 09:56:10 +00:00
|
|
|
#include <stdlib.h>
|
2014-05-09 16:20:55 +00:00
|
|
|
#include <string.h>
|
1998-10-08 09:56:10 +00:00
|
|
|
#include <unistd.h>
|
2001-12-12 00:13:37 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "systat.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
static int dellave;
|
|
|
|
|
|
|
|
kvm_t *kd;
|
|
|
|
sig_t sigtstpdfl;
|
|
|
|
double avenrun[3];
|
|
|
|
int col;
|
2012-09-17 13:36:47 +00:00
|
|
|
unsigned int delay = 5000000; /* in microseconds */
|
1994-05-27 12:33:43 +00:00
|
|
|
int verbose = 1; /* to report kvm read errs */
|
2000-12-17 18:43:28 +00:00
|
|
|
struct clockinfo clkinfo;
|
1994-12-03 10:06:40 +00:00
|
|
|
double hertz;
|
1994-05-27 12:33:43 +00:00
|
|
|
char c;
|
|
|
|
char *namp;
|
|
|
|
char hostname[MAXHOSTNAMELEN];
|
|
|
|
WINDOW *wnd;
|
|
|
|
int CMDLINE;
|
2001-03-23 03:58:25 +00:00
|
|
|
int use_kvm = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
static WINDOW *wload; /* one line window for load average */
|
|
|
|
|
2014-05-09 16:20:55 +00:00
|
|
|
struct cmdentry {
|
|
|
|
SLIST_ENTRY(cmdentry) link;
|
|
|
|
char *cmd; /* Command name */
|
|
|
|
char *argv; /* Arguments vector for a command */
|
|
|
|
};
|
|
|
|
SLIST_HEAD(, cmdentry) commands;
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_cmd_args (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int in_command = 0;
|
|
|
|
struct cmdentry *cmd = NULL;
|
|
|
|
double t;
|
|
|
|
|
|
|
|
while (argc) {
|
|
|
|
if (argv[0][0] == '-') {
|
|
|
|
if (in_command)
|
|
|
|
SLIST_INSERT_HEAD(&commands, cmd, link);
|
|
|
|
|
|
|
|
if (memcmp(argv[0], "--", 3) == 0) {
|
|
|
|
in_command = 0; /*-- ends a command explicitly*/
|
|
|
|
argc --, argv ++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cmd = calloc(1, sizeof(struct cmdentry));
|
|
|
|
if (cmd == NULL)
|
|
|
|
errx(1, "memory allocating failure");
|
|
|
|
cmd->cmd = strdup(&argv[0][1]);
|
|
|
|
if (cmd->cmd == NULL)
|
|
|
|
errx(1, "memory allocating failure");
|
|
|
|
in_command = 1;
|
|
|
|
}
|
|
|
|
else if (!in_command) {
|
|
|
|
t = strtod(argv[0], NULL) * 1000000.0;
|
|
|
|
if (t > 0 && t < (double)UINT_MAX)
|
|
|
|
delay = (unsigned int)t;
|
|
|
|
}
|
|
|
|
else if (cmd != NULL) {
|
|
|
|
cmd->argv = strdup(argv[0]);
|
|
|
|
if (cmd->argv == NULL)
|
|
|
|
errx(1, "memory allocating failure");
|
|
|
|
in_command = 0;
|
|
|
|
SLIST_INSERT_HEAD(&commands, cmd, link);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
errx(1, "invalid arguments list");
|
|
|
|
|
|
|
|
argc--, argv++;
|
|
|
|
}
|
|
|
|
if (in_command && cmd != NULL)
|
|
|
|
SLIST_INSERT_HEAD(&commands, cmd, link);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
1997-08-26 11:08:24 +00:00
|
|
|
int
|
2004-03-09 11:57:28 +00:00
|
|
|
main(int argc, char **argv)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-05-25 23:10:27 +00:00
|
|
|
char errbuf[_POSIX2_LINE_MAX], dummy;
|
2000-11-25 03:46:45 +00:00
|
|
|
size_t size;
|
2014-05-09 16:20:55 +00:00
|
|
|
struct cmdentry *cmd = NULL;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2009-11-13 03:59:44 +00:00
|
|
|
(void) setlocale(LC_ALL, "");
|
1996-03-31 16:39:26 +00:00
|
|
|
|
2014-05-09 16:20:55 +00:00
|
|
|
SLIST_INIT(&commands);
|
1994-05-27 12:33:43 +00:00
|
|
|
argc--, argv++;
|
2014-05-09 16:20:55 +00:00
|
|
|
if (argc > 0) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (argv[0][0] == '-') {
|
|
|
|
struct cmdtab *p;
|
|
|
|
|
|
|
|
p = lookup(&argv[0][1]);
|
1997-08-13 06:45:11 +00:00
|
|
|
if (p == (struct cmdtab *)-1)
|
|
|
|
errx(1, "%s: ambiguous request", &argv[0][1]);
|
|
|
|
if (p == (struct cmdtab *)0)
|
|
|
|
errx(1, "%s: unknown request", &argv[0][1]);
|
1994-05-27 12:33:43 +00:00
|
|
|
curcmd = p;
|
2014-05-09 16:20:55 +00:00
|
|
|
argc--, argv++;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2014-05-09 16:20:55 +00:00
|
|
|
parse_cmd_args (argc, argv);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
|
2001-03-23 03:58:25 +00:00
|
|
|
if (kd != NULL) {
|
2006-04-30 04:47:23 +00:00
|
|
|
/*
|
2001-03-23 03:58:25 +00:00
|
|
|
* Try to actually read something, we may be in a jail, and
|
|
|
|
* have /dev/null opened as /dev/mem.
|
|
|
|
*/
|
2006-04-30 04:47:23 +00:00
|
|
|
if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 ||
|
2001-03-23 03:58:25 +00:00
|
|
|
kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) !=
|
|
|
|
sizeof(dummy)) {
|
|
|
|
kvm_close(kd);
|
|
|
|
kd = NULL;
|
|
|
|
}
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
if (kd == NULL) {
|
2006-04-30 04:47:23 +00:00
|
|
|
/*
|
2001-03-23 03:58:25 +00:00
|
|
|
* Maybe we are lacking permissions? Retry, this time with bogus
|
|
|
|
* devices. We can now use sysctl only.
|
|
|
|
*/
|
|
|
|
use_kvm = 0;
|
2015-09-28 18:19:22 +00:00
|
|
|
kd = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, _PATH_DEVNULL,
|
2001-05-25 23:10:27 +00:00
|
|
|
O_RDONLY, errbuf);
|
2001-03-23 03:58:25 +00:00
|
|
|
if (kd == NULL) {
|
|
|
|
error("%s", errbuf);
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2009-10-11 12:32:25 +00:00
|
|
|
signal(SIGHUP, die);
|
1994-05-27 12:33:43 +00:00
|
|
|
signal(SIGINT, die);
|
|
|
|
signal(SIGQUIT, die);
|
|
|
|
signal(SIGTERM, die);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize display. Load average appears in a one line
|
|
|
|
* window of its own. Current command's display appears in
|
|
|
|
* an overlapping sub-window of stdscr configured by the display
|
|
|
|
* routines to minimize update work by curses.
|
|
|
|
*/
|
|
|
|
initscr();
|
|
|
|
CMDLINE = LINES - 1;
|
|
|
|
wnd = (*curcmd->c_open)();
|
|
|
|
if (wnd == NULL) {
|
1997-08-13 06:45:11 +00:00
|
|
|
warnx("couldn't initialize display");
|
1994-05-27 12:33:43 +00:00
|
|
|
die(0);
|
|
|
|
}
|
2006-04-30 04:26:46 +00:00
|
|
|
wload = newwin(1, 0, 1, 20);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (wload == NULL) {
|
1997-08-13 06:45:11 +00:00
|
|
|
warnx("couldn't set up load average window");
|
1994-05-27 12:33:43 +00:00
|
|
|
die(0);
|
|
|
|
}
|
|
|
|
gethostname(hostname, sizeof (hostname));
|
2000-11-25 03:46:45 +00:00
|
|
|
size = sizeof(clkinfo);
|
2001-12-12 00:13:37 +00:00
|
|
|
if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0)
|
|
|
|
|| size != sizeof(clkinfo)) {
|
2000-12-17 18:43:28 +00:00
|
|
|
error("kern.clockrate");
|
2000-11-25 03:46:45 +00:00
|
|
|
die(0);
|
|
|
|
}
|
2000-12-17 18:43:28 +00:00
|
|
|
hertz = clkinfo.stathz;
|
1994-05-27 12:33:43 +00:00
|
|
|
(*curcmd->c_init)();
|
|
|
|
curcmd->c_flags |= CF_INIT;
|
|
|
|
labels();
|
|
|
|
|
2014-05-09 16:20:55 +00:00
|
|
|
if (curcmd->c_cmd != NULL)
|
|
|
|
SLIST_FOREACH (cmd, &commands, link)
|
|
|
|
if (!curcmd->c_cmd(cmd->cmd, cmd->argv))
|
|
|
|
warnx("command is not understood");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2014-05-09 16:20:55 +00:00
|
|
|
dellave = 0.0;
|
2012-09-17 13:36:47 +00:00
|
|
|
display();
|
1994-05-27 12:33:43 +00:00
|
|
|
noecho();
|
|
|
|
crmode();
|
|
|
|
keyboard();
|
|
|
|
/*NOTREACHED*/
|
1998-10-08 09:56:10 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
labels(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
if (curcmd->c_flags & CF_LOADAV) {
|
2006-04-30 04:26:46 +00:00
|
|
|
mvaddstr(0, 20,
|
1994-05-27 12:33:43 +00:00
|
|
|
"/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
|
2006-04-30 04:26:46 +00:00
|
|
|
mvaddstr(1, 5, "Load Average");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2015-09-27 09:15:54 +00:00
|
|
|
if (curcmd->c_flags & CF_ZFSARC) {
|
|
|
|
mvaddstr(0, 20,
|
|
|
|
" Total MFU MRU Anon Hdr L2Hdr Other");
|
|
|
|
mvaddstr(1, 5, "ZFS ARC ");
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
(*curcmd->c_label)();
|
|
|
|
#ifdef notdef
|
|
|
|
mvprintw(21, 25, "CPU usage on %s", hostname);
|
|
|
|
#endif
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-19 12:57:07 +00:00
|
|
|
display(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-12 00:13:37 +00:00
|
|
|
int i, j;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/* Get the load average over the last minute. */
|
2016-08-16 15:52:10 +00:00
|
|
|
(void) getloadavg(avenrun, nitems(avenrun));
|
1994-05-27 12:33:43 +00:00
|
|
|
(*curcmd->c_fetch)();
|
|
|
|
if (curcmd->c_flags & CF_LOADAV) {
|
|
|
|
j = 5.0*avenrun[0] + 0.5;
|
|
|
|
dellave -= avenrun[0];
|
|
|
|
if (dellave >= 0.0)
|
|
|
|
c = '<';
|
|
|
|
else {
|
|
|
|
c = '>';
|
|
|
|
dellave = -dellave;
|
|
|
|
}
|
|
|
|
if (dellave < 0.1)
|
|
|
|
c = '|';
|
|
|
|
dellave = avenrun[0];
|
|
|
|
wmove(wload, 0, 0); wclrtoeol(wload);
|
2016-04-27 02:26:31 +00:00
|
|
|
for (i = MIN(j, 50); i > 0; i--)
|
1994-05-27 12:33:43 +00:00
|
|
|
waddch(wload, c);
|
|
|
|
if (j > 50)
|
|
|
|
wprintw(wload, " %4.1f", avenrun[0]);
|
|
|
|
}
|
2015-09-27 09:15:54 +00:00
|
|
|
if (curcmd->c_flags & CF_ZFSARC) {
|
|
|
|
uint64_t arc[7] = {};
|
|
|
|
size_t size = sizeof(arc[0]);
|
|
|
|
if (sysctlbyname("kstat.zfs.misc.arcstats.size",
|
|
|
|
&arc[0], &size, NULL, 0) == 0 ) {
|
|
|
|
GETSYSCTL("vfs.zfs.mfu_size", arc[1]);
|
|
|
|
GETSYSCTL("vfs.zfs.mru_size", arc[2]);
|
|
|
|
GETSYSCTL("vfs.zfs.anon_size", arc[3]);
|
|
|
|
GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc[4]);
|
|
|
|
GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc[5]);
|
|
|
|
GETSYSCTL("kstat.zfs.misc.arcstats.other_size", arc[6]);
|
|
|
|
wmove(wload, 0, 0); wclrtoeol(wload);
|
2016-08-16 15:52:10 +00:00
|
|
|
for (i = 0 ; i < nitems(arc); i++) {
|
2015-09-27 09:15:54 +00:00
|
|
|
if (arc[i] > 10llu * 1024 * 1024 * 1024 ) {
|
|
|
|
wprintw(wload, "%7lluG", arc[i] >> 30);
|
|
|
|
}
|
|
|
|
else if (arc[i] > 10 * 1024 * 1024 ) {
|
|
|
|
wprintw(wload, "%7lluM", arc[i] >> 20);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
wprintw(wload, "%7lluK", arc[i] >> 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
(*curcmd->c_refresh)();
|
2015-09-27 09:15:54 +00:00
|
|
|
if (curcmd->c_flags & (CF_LOADAV |CF_ZFSARC))
|
1994-05-27 12:33:43 +00:00
|
|
|
wrefresh(wload);
|
|
|
|
wrefresh(wnd);
|
|
|
|
move(CMDLINE, col);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
load(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
2016-08-16 15:52:10 +00:00
|
|
|
(void) getloadavg(avenrun, nitems(avenrun));
|
1994-05-27 12:33:43 +00:00
|
|
|
mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
|
|
|
|
avenrun[0], avenrun[1], avenrun[2]);
|
|
|
|
clrtoeol();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
die(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
move(CMDLINE, 0);
|
|
|
|
clrtoeol();
|
|
|
|
refresh();
|
|
|
|
endwin();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
error(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buf[255];
|
|
|
|
int oy, ox;
|
|
|
|
|
2002-03-23 23:37:07 +00:00
|
|
|
va_start(ap, fmt);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (wnd) {
|
|
|
|
getyx(stdscr, oy, ox);
|
1998-06-09 04:17:29 +00:00
|
|
|
(void) vsnprintf(buf, sizeof(buf), fmt, ap);
|
1994-05-27 12:33:43 +00:00
|
|
|
clrtoeol();
|
|
|
|
standout();
|
|
|
|
mvaddstr(CMDLINE, 0, buf);
|
|
|
|
standend();
|
|
|
|
move(oy, ox);
|
|
|
|
refresh();
|
|
|
|
} else {
|
|
|
|
(void) vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-16 19:27:43 +00:00
|
|
|
nlisterr(struct nlist n_list[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
clear();
|
|
|
|
mvprintw(2, 10, "systat: nlist: can't find following symbols:");
|
|
|
|
for (i = 0;
|
2001-12-12 00:13:37 +00:00
|
|
|
n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++)
|
|
|
|
if (n_list[i].n_value == 0)
|
|
|
|
mvprintw(2 + ++n, 10, "%s", n_list[i].n_name);
|
1994-05-27 12:33:43 +00:00
|
|
|
move(CMDLINE, 0);
|
|
|
|
clrtoeol();
|
|
|
|
refresh();
|
|
|
|
endwin();
|
|
|
|
exit(1);
|
|
|
|
}
|