1207151Smarius/*- 2207151Smarius * Copyright (c) 1980, 1992, 1993 3207151Smarius * The Regents of the University of California. All rights reserved. 4207151Smarius * 5207151Smarius * Redistribution and use in source and binary forms, with or without 6207151Smarius * modification, are permitted provided that the following conditions 7207151Smarius * are met: 8207151Smarius * 1. Redistributions of source code must retain the above copyright 9207151Smarius * notice, this list of conditions and the following disclaimer. 10207151Smarius * 2. Redistributions in binary form must reproduce the above copyright 11207151Smarius * notice, this list of conditions and the following disclaimer in the 12207151Smarius * documentation and/or other materials provided with the distribution. 13207151Smarius * 4. Neither the name of the University nor the names of its contributors 14207151Smarius * may be used to endorse or promote products derived from this software 15207151Smarius * without specific prior written permission. 16207151Smarius * 17207151Smarius * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18207151Smarius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19207151Smarius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20207151Smarius * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21207151Smarius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22207151Smarius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23207151Smarius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24207151Smarius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25207151Smarius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26207151Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27207151Smarius * SUCH DAMAGE. 28207151Smarius */ 29207151Smarius 30207151Smarius#include <sys/cdefs.h> 31207151Smarius 32207151Smarius__FBSDID("$FreeBSD$"); 33207151Smarius 34207151Smarius#ifdef lint 35207151Smariusstatic const char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; 36207151Smarius#endif 37207151Smarius 38207151Smarius#ifndef lint 39207151Smariusstatic const char copyright[] = 40207151Smarius"@(#) Copyright (c) 1980, 1992, 1993\n\ 41207151Smarius The Regents of the University of California. All rights reserved.\n"; 42207151Smarius#endif 43207151Smarius 44207151Smarius#include <sys/param.h> 45207151Smarius#include <sys/time.h> 46207151Smarius#include <sys/sysctl.h> 47207151Smarius 48207151Smarius#include <err.h> 49207151Smarius#include <limits.h> 50207151Smarius#include <locale.h> 51207151Smarius#include <nlist.h> 52207151Smarius#include <paths.h> 53207151Smarius#include <signal.h> 54207151Smarius#include <stdio.h> 55#include <stdlib.h> 56#include <unistd.h> 57 58#include "systat.h" 59#include "extern.h" 60 61static int dellave; 62 63kvm_t *kd; 64sig_t sigtstpdfl; 65double avenrun[3]; 66int col; 67unsigned int delay = 5000000; /* in microseconds */ 68int verbose = 1; /* to report kvm read errs */ 69struct clockinfo clkinfo; 70double hertz; 71char c; 72char *namp; 73char hostname[MAXHOSTNAMELEN]; 74WINDOW *wnd; 75int CMDLINE; 76int use_kvm = 1; 77 78static WINDOW *wload; /* one line window for load average */ 79 80int 81main(int argc, char **argv) 82{ 83 char errbuf[_POSIX2_LINE_MAX], dummy; 84 size_t size; 85 double t; 86 87#ifdef USE_WIDECHAR 88 (void) setlocale(LC_ALL, ""); 89#else 90 (void) setlocale(LC_TIME, ""); 91#endif 92 93 argc--, argv++; 94 while (argc > 0) { 95 if (argv[0][0] == '-') { 96 struct cmdtab *p; 97 98 p = lookup(&argv[0][1]); 99 if (p == (struct cmdtab *)-1) 100 errx(1, "%s: ambiguous request", &argv[0][1]); 101 if (p == (struct cmdtab *)0) 102 errx(1, "%s: unknown request", &argv[0][1]); 103 curcmd = p; 104 } else { 105 t = strtod(argv[0], NULL) * 1000000.0; 106 if (t > 0 && t < (double)UINT_MAX) 107 delay = (unsigned int)t; 108 } 109 argc--, argv++; 110 } 111 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); 112 if (kd != NULL) { 113 /* 114 * Try to actually read something, we may be in a jail, and 115 * have /dev/null opened as /dev/mem. 116 */ 117 if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 || 118 kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) != 119 sizeof(dummy)) { 120 kvm_close(kd); 121 kd = NULL; 122 } 123 } 124 if (kd == NULL) { 125 /* 126 * Maybe we are lacking permissions? Retry, this time with bogus 127 * devices. We can now use sysctl only. 128 */ 129 use_kvm = 0; 130 kd = kvm_openfiles("/dev/null", "/dev/null", "/dev/null", 131 O_RDONLY, errbuf); 132 if (kd == NULL) { 133 error("%s", errbuf); 134 exit(1); 135 } 136 } 137 signal(SIGHUP, die); 138 signal(SIGINT, die); 139 signal(SIGQUIT, die); 140 signal(SIGTERM, die); 141 142 /* 143 * Initialize display. Load average appears in a one line 144 * window of its own. Current command's display appears in 145 * an overlapping sub-window of stdscr configured by the display 146 * routines to minimize update work by curses. 147 */ 148 initscr(); 149 CMDLINE = LINES - 1; 150 wnd = (*curcmd->c_open)(); 151 if (wnd == NULL) { 152 warnx("couldn't initialize display"); 153 die(0); 154 } 155 wload = newwin(1, 0, 1, 20); 156 if (wload == NULL) { 157 warnx("couldn't set up load average window"); 158 die(0); 159 } 160 gethostname(hostname, sizeof (hostname)); 161 size = sizeof(clkinfo); 162 if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0) 163 || size != sizeof(clkinfo)) { 164 error("kern.clockrate"); 165 die(0); 166 } 167 hertz = clkinfo.stathz; 168 (*curcmd->c_init)(); 169 curcmd->c_flags |= CF_INIT; 170 labels(); 171 172 dellave = 0.0; 173 174 display(); 175 noecho(); 176 crmode(); 177 keyboard(); 178 /*NOTREACHED*/ 179 180 return EXIT_SUCCESS; 181} 182 183void 184labels(void) 185{ 186 if (curcmd->c_flags & CF_LOADAV) { 187 mvaddstr(0, 20, 188 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); 189 mvaddstr(1, 5, "Load Average"); 190 } 191 if (curcmd->c_flags & CF_ZFSARC) { 192 mvaddstr(0, 20, 193 " Total MFU MRU Anon Hdr L2Hdr Other"); 194 mvaddstr(1, 5, "ZFS ARC "); 195 } 196 (*curcmd->c_label)(); 197#ifdef notdef 198 mvprintw(21, 25, "CPU usage on %s", hostname); 199#endif 200 refresh(); 201} 202 203void 204display(void) 205{ 206 int i, j; 207 208 /* Get the load average over the last minute. */ 209 (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])); 210 (*curcmd->c_fetch)(); 211 if (curcmd->c_flags & CF_LOADAV) { 212 j = 5.0*avenrun[0] + 0.5; 213 dellave -= avenrun[0]; 214 if (dellave >= 0.0) 215 c = '<'; 216 else { 217 c = '>'; 218 dellave = -dellave; 219 } 220 if (dellave < 0.1) 221 c = '|'; 222 dellave = avenrun[0]; 223 wmove(wload, 0, 0); wclrtoeol(wload); 224 for (i = (j > 50) ? 50 : j; i > 0; i--) 225 waddch(wload, c); 226 if (j > 50) 227 wprintw(wload, " %4.1f", avenrun[0]); 228 } 229 if (curcmd->c_flags & CF_ZFSARC) { 230 uint64_t arc[7] = {}; 231 size_t size = sizeof(arc[0]); 232 if (sysctlbyname("kstat.zfs.misc.arcstats.size", 233 &arc[0], &size, NULL, 0) == 0 ) { 234 GETSYSCTL("vfs.zfs.mfu_size", arc[1]); 235 GETSYSCTL("vfs.zfs.mru_size", arc[2]); 236 GETSYSCTL("vfs.zfs.anon_size", arc[3]); 237 GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc[4]); 238 GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc[5]); 239 GETSYSCTL("kstat.zfs.misc.arcstats.other_size", arc[6]); 240 wmove(wload, 0, 0); wclrtoeol(wload); 241 for (i = 0 ; i < sizeof(arc) / sizeof(arc[0]) ; i++) { 242 if (arc[i] > 10llu * 1024 * 1024 * 1024 ) { 243 wprintw(wload, "%7lluG", arc[i] >> 30); 244 } 245 else if (arc[i] > 10 * 1024 * 1024 ) { 246 wprintw(wload, "%7lluM", arc[i] >> 20); 247 } 248 else { 249 wprintw(wload, "%7lluK", arc[i] >> 10); 250 } 251 } 252 } 253 } 254 (*curcmd->c_refresh)(); 255 if (curcmd->c_flags & (CF_LOADAV |CF_ZFSARC)) 256 wrefresh(wload); 257 wrefresh(wnd); 258 move(CMDLINE, col); 259 refresh(); 260} 261 262void 263load(void) 264{ 265 266 (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 267 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f", 268 avenrun[0], avenrun[1], avenrun[2]); 269 clrtoeol(); 270} 271 272void 273die(int signo __unused) 274{ 275 move(CMDLINE, 0); 276 clrtoeol(); 277 refresh(); 278 endwin(); 279 exit(0); 280} 281 282#include <stdarg.h> 283 284void 285error(const char *fmt, ...) 286{ 287 va_list ap; 288 char buf[255]; 289 int oy, ox; 290 291 va_start(ap, fmt); 292 if (wnd) { 293 getyx(stdscr, oy, ox); 294 (void) vsnprintf(buf, sizeof(buf), fmt, ap); 295 clrtoeol(); 296 standout(); 297 mvaddstr(CMDLINE, 0, buf); 298 standend(); 299 move(oy, ox); 300 refresh(); 301 } else { 302 (void) vfprintf(stderr, fmt, ap); 303 fprintf(stderr, "\n"); 304 } 305 va_end(ap); 306} 307 308void 309nlisterr(struct nlist n_list[]) 310{ 311 int i, n; 312 313 n = 0; 314 clear(); 315 mvprintw(2, 10, "systat: nlist: can't find following symbols:"); 316 for (i = 0; 317 n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++) 318 if (n_list[i].n_value == 0) 319 mvprintw(2 + ++n, 10, "%s", n_list[i].n_name); 320 move(CMDLINE, 0); 321 clrtoeol(); 322 refresh(); 323 endwin(); 324 exit(1); 325} 326