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