1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 34 __FBSDID("$FreeBSD$"); 35 36 #ifdef lint 37 static const char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/time.h> 42 #include <sys/sysctl.h> 43 #include <sys/queue.h> 44 45 #include <err.h> 46 #include <limits.h> 47 #include <locale.h> 48 #include <nlist.h> 49 #include <paths.h> 50 #include <signal.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 #include "systat.h" 57 #include "extern.h" 58 59 static int dellave; 60 61 kvm_t *kd; 62 double avenrun[3]; 63 int col; 64 unsigned int delay = 5000000; /* in microseconds */ 65 int verbose = 1; /* to report kvm read errs */ 66 static struct clockinfo clkinfo; 67 double hertz; 68 char c; 69 char *namp; 70 char hostname[MAXHOSTNAMELEN]; 71 WINDOW *wnd; 72 int CMDLINE; 73 int use_kvm = 1; 74 75 static WINDOW *wload; /* one line window for load average */ 76 77 struct cmdentry { 78 SLIST_ENTRY(cmdentry) link; 79 char *cmd; /* Command name */ 80 char *argv; /* Arguments vector for a command */ 81 }; 82 static SLIST_HEAD(, cmdentry) commands; 83 84 static void 85 parse_cmd_args (int argc, char **argv) 86 { 87 int in_command = 0; 88 struct cmdentry *cmd = NULL; 89 double t; 90 91 while (argc) { 92 if (argv[0][0] == '-') { 93 if (in_command) 94 SLIST_INSERT_HEAD(&commands, cmd, link); 95 96 if (memcmp(argv[0], "--", 3) == 0) { 97 in_command = 0; /*-- ends a command explicitly*/ 98 argc --, argv ++; 99 continue; 100 } 101 cmd = calloc(1, sizeof(struct cmdentry)); 102 if (cmd == NULL) 103 errx(1, "memory allocating failure"); 104 cmd->cmd = strdup(&argv[0][1]); 105 if (cmd->cmd == NULL) 106 errx(1, "memory allocating failure"); 107 in_command = 1; 108 } 109 else if (!in_command) { 110 t = strtod(argv[0], NULL) * 1000000.0; 111 if (t > 0 && t < (double)UINT_MAX) 112 delay = (unsigned int)t; 113 } 114 else if (cmd != NULL) { 115 cmd->argv = strdup(argv[0]); 116 if (cmd->argv == NULL) 117 errx(1, "memory allocating failure"); 118 in_command = 0; 119 SLIST_INSERT_HEAD(&commands, cmd, link); 120 } 121 else 122 errx(1, "invalid arguments list"); 123 124 argc--, argv++; 125 } 126 if (in_command && cmd != NULL) 127 SLIST_INSERT_HEAD(&commands, cmd, link); 128 129 } 130 131 static void 132 resize(int signo __unused) 133 { 134 135 endwin(); 136 refresh(); 137 clear(); 138 139 CMDLINE = LINES - 1; 140 labels(); 141 display(); 142 status(); 143 } 144 145 146 int 147 main(int argc, char **argv) 148 { 149 char errbuf[_POSIX2_LINE_MAX], dummy; 150 size_t size; 151 struct cmdentry *cmd = NULL; 152 short cf, cb; 153 154 (void) setlocale(LC_ALL, ""); 155 156 SLIST_INIT(&commands); 157 argc--, argv++; 158 if (argc > 0) { 159 if (argv[0][0] == '-') { 160 struct cmdtab *p; 161 162 p = lookup(&argv[0][1]); 163 if (p == (struct cmdtab *)-1) 164 errx(1, "%s: ambiguous request", &argv[0][1]); 165 if (p == (struct cmdtab *)0) 166 errx(1, "%s: unknown request", &argv[0][1]); 167 curcmd = p; 168 argc--, argv++; 169 } 170 parse_cmd_args (argc, argv); 171 172 } 173 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); 174 if (kd != NULL) { 175 /* 176 * Try to actually read something, we may be in a jail, and 177 * have /dev/null opened as /dev/mem. 178 */ 179 if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 || 180 kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) != 181 sizeof(dummy)) { 182 kvm_close(kd); 183 kd = NULL; 184 } 185 } 186 if (kd == NULL) { 187 /* 188 * Maybe we are lacking permissions? Retry, this time with bogus 189 * devices. We can now use sysctl only. 190 */ 191 use_kvm = 0; 192 kd = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, _PATH_DEVNULL, 193 O_RDONLY, errbuf); 194 if (kd == NULL) { 195 error("%s", errbuf); 196 exit(1); 197 } 198 } 199 signal(SIGHUP, die); 200 signal(SIGINT, die); 201 signal(SIGQUIT, die); 202 signal(SIGTERM, die); 203 signal(SIGWINCH, resize); 204 205 /* 206 * Initialize display. Load average appears in a one line 207 * window of its own. Current command's display appears in 208 * an overlapping sub-window of stdscr configured by the display 209 * routines to minimize update work by curses. 210 */ 211 initscr(); 212 start_color(); 213 use_default_colors(); 214 pair_content(0, &cf, &cb); 215 init_pair(1, COLOR_GREEN, cb); 216 init_pair(2, COLOR_MAGENTA, cb); 217 init_pair(3, COLOR_RED, cb); 218 init_pair(4, COLOR_BLUE, cb); 219 CMDLINE = LINES - 1; 220 wnd = (*curcmd->c_open)(); 221 if (wnd == NULL) { 222 warnx("couldn't initialize display"); 223 die(0); 224 } 225 wload = newwin(1, 0, 1, 20); 226 if (wload == NULL) { 227 warnx("couldn't set up load average window"); 228 die(0); 229 } 230 gethostname(hostname, sizeof (hostname)); 231 size = sizeof(clkinfo); 232 if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0) 233 || size != sizeof(clkinfo)) { 234 error("kern.clockrate"); 235 die(0); 236 } 237 hertz = clkinfo.stathz; 238 (*curcmd->c_init)(); 239 curcmd->c_flags |= CF_INIT; 240 labels(); 241 242 if (curcmd->c_cmd != NULL) 243 SLIST_FOREACH (cmd, &commands, link) 244 if (!curcmd->c_cmd(cmd->cmd, cmd->argv)) 245 warnx("command is not understood"); 246 247 dellave = 0.0; 248 display(); 249 noecho(); 250 crmode(); 251 keyboard(); 252 /*NOTREACHED*/ 253 254 return EXIT_SUCCESS; 255 } 256 257 void 258 labels(void) 259 { 260 if (curcmd->c_flags & CF_LOADAV) { 261 mvaddstr(0, 20, 262 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); 263 mvaddstr(1, 5, "Load Average"); 264 } 265 if (curcmd->c_flags & CF_ZFSARC) { 266 mvaddstr(0, 20, 267 " Total MFU MRU Anon Hdr L2Hdr Other"); 268 mvaddstr(1, 5, "ZFS ARC "); 269 } 270 (*curcmd->c_label)(); 271 #ifdef notdef 272 mvprintw(21, 25, "CPU usage on %s", hostname); 273 #endif 274 refresh(); 275 } 276 277 void 278 display(void) 279 { 280 uint64_t arc_stat; 281 unsigned int ui; 282 int i, j; 283 284 /* Get the load average over the last minute. */ 285 (void) getloadavg(avenrun, nitems(avenrun)); 286 (*curcmd->c_fetch)(); 287 if (curcmd->c_flags & CF_LOADAV) { 288 j = 5.0*avenrun[0] + 0.5; 289 dellave -= avenrun[0]; 290 if (dellave >= 0.0) 291 c = '<'; 292 else { 293 c = '>'; 294 dellave = -dellave; 295 } 296 if (dellave < 0.1) 297 c = '|'; 298 dellave = avenrun[0]; 299 wmove(wload, 0, 0); wclrtoeol(wload); 300 for (i = MIN(j, 50); i > 0; i--) 301 waddch(wload, c); 302 if (j > 50) 303 wprintw(wload, " %4.1f", avenrun[0]); 304 } 305 if (curcmd->c_flags & CF_ZFSARC) { 306 uint64_t arc[7] = {}; 307 size_t size = sizeof(arc[0]); 308 if (sysctlbyname("kstat.zfs.misc.arcstats.size", 309 &arc[0], &size, NULL, 0) == 0 ) { 310 GETSYSCTL("vfs.zfs.mfu_size", arc[1]); 311 GETSYSCTL("vfs.zfs.mru_size", arc[2]); 312 GETSYSCTL("vfs.zfs.anon_size", arc[3]); 313 GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc[4]); 314 GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc[5]); 315 GETSYSCTL("kstat.zfs.misc.arcstats.bonus_size", arc[6]); 316 GETSYSCTL("kstat.zfs.misc.arcstats.dnode_size", arc_stat); 317 arc[6] += arc_stat; 318 GETSYSCTL("kstat.zfs.misc.arcstats.dbuf_size", arc_stat); 319 arc[6] += arc_stat; 320 wmove(wload, 0, 0); wclrtoeol(wload); 321 for (ui = 0 ; ui < nitems(arc); ui++) 322 sysputuint64(wload, 0, ui*8+2, 6, arc[ui], 0); 323 } 324 } 325 (*curcmd->c_refresh)(); 326 if (curcmd->c_flags & (CF_LOADAV |CF_ZFSARC)) 327 wrefresh(wload); 328 wrefresh(wnd); 329 move(CMDLINE, col); 330 refresh(); 331 } 332 333 void 334 load(void) 335 { 336 337 (void) getloadavg(avenrun, nitems(avenrun)); 338 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f", 339 avenrun[0], avenrun[1], avenrun[2]); 340 clrtoeol(); 341 } 342 343 void 344 die(int signo __unused) 345 { 346 move(CMDLINE, 0); 347 clrtoeol(); 348 refresh(); 349 endwin(); 350 exit(0); 351 } 352 353 #include <stdarg.h> 354 355 void 356 error(const char *fmt, ...) 357 { 358 va_list ap; 359 char buf[255]; 360 int oy, ox; 361 362 va_start(ap, fmt); 363 if (wnd) { 364 getyx(stdscr, oy, ox); 365 (void) vsnprintf(buf, sizeof(buf), fmt, ap); 366 clrtoeol(); 367 standout(); 368 mvaddstr(CMDLINE, 0, buf); 369 standend(); 370 move(oy, ox); 371 refresh(); 372 } else { 373 (void) vfprintf(stderr, fmt, ap); 374 fprintf(stderr, "\n"); 375 } 376 va_end(ap); 377 } 378 379 void 380 nlisterr(struct nlist n_list[]) 381 { 382 int i, n; 383 384 n = 0; 385 clear(); 386 mvprintw(2, 10, "systat: nlist: can't find following symbols:"); 387 for (i = 0; 388 n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++) 389 if (n_list[i].n_value == 0) 390 mvprintw(2 + ++n, 10, "%s", n_list[i].n_name); 391 move(CMDLINE, 0); 392 clrtoeol(); 393 refresh(); 394 endwin(); 395 exit(1); 396 } 397