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