xref: /freebsd-13-stable/usr.bin/systat/main.c (revision 0d349c19beda4185496f7a81330e0961a462512d)
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 sig_t	sigtstpdfl;
60 double avenrun[3];
61 int     col;
62 unsigned int	delay = 5000000;	/* in microseconds */
63 int     verbose = 1;                    /* to report kvm read errs */
64 struct	clockinfo clkinfo;
65 double	hertz;
66 char    c;
67 char    *namp;
68 char    hostname[MAXHOSTNAMELEN];
69 WINDOW  *wnd;
70 int     CMDLINE;
71 int     use_kvm = 1;
72 
73 static	WINDOW *wload;			/* one line window for load average */
74 
75 struct cmdentry {
76 	SLIST_ENTRY(cmdentry) link;
77 	char		*cmd;		/* Command name	*/
78 	char		*argv;		/* Arguments vector for a command */
79 };
80 SLIST_HEAD(, cmdentry) commands;
81 
82 static void
parse_cmd_args(int argc,char ** argv)83 parse_cmd_args (int argc, char **argv)
84 {
85 	int in_command = 0;
86 	struct cmdentry *cmd = NULL;
87 	double t;
88 
89 	while (argc) {
90 		if (argv[0][0] == '-') {
91 			if (in_command)
92 					SLIST_INSERT_HEAD(&commands, cmd, link);
93 
94 			if (memcmp(argv[0], "--", 3) == 0) {
95 				in_command = 0; /*-- ends a command explicitly*/
96 				argc --, argv ++;
97 				continue;
98 			}
99 			cmd = calloc(1, sizeof(struct cmdentry));
100 			if (cmd == NULL)
101 				errx(1, "memory allocating failure");
102 			cmd->cmd = strdup(&argv[0][1]);
103 			if (cmd->cmd == NULL)
104 				errx(1, "memory allocating failure");
105 			in_command = 1;
106 		}
107 		else if (!in_command) {
108 			t = strtod(argv[0], NULL) * 1000000.0;
109 			if (t > 0 && t < (double)UINT_MAX)
110 				delay = (unsigned int)t;
111 		}
112 		else if (cmd != NULL) {
113 			cmd->argv = strdup(argv[0]);
114 			if (cmd->argv == NULL)
115 				errx(1, "memory allocating failure");
116 			in_command = 0;
117 			SLIST_INSERT_HEAD(&commands, cmd, link);
118 		}
119 		else
120 			errx(1, "invalid arguments list");
121 
122 		argc--, argv++;
123 	}
124 	if (in_command && cmd != NULL)
125 		SLIST_INSERT_HEAD(&commands, cmd, link);
126 
127 }
128 
129 static void
resize(int signo __unused)130 resize(int signo __unused)
131 {
132 
133 	endwin();
134 	refresh();
135 	clear();
136 
137 	CMDLINE = LINES - 1;
138 	labels();
139 	display();
140 	status();
141 }
142 
143 
144 int
main(int argc,char ** argv)145 main(int argc, char **argv)
146 {
147 	char errbuf[_POSIX2_LINE_MAX], dummy;
148 	size_t	size;
149 	struct cmdentry *cmd = NULL;
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 	CMDLINE = LINES - 1;
210 	wnd = (*curcmd->c_open)();
211 	if (wnd == NULL) {
212 		warnx("couldn't initialize display");
213 		die(0);
214 	}
215 	wload = newwin(1, 0, 1, 20);
216 	if (wload == NULL) {
217 		warnx("couldn't set up load average window");
218 		die(0);
219 	}
220 	gethostname(hostname, sizeof (hostname));
221 	size = sizeof(clkinfo);
222 	if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0)
223 	    || size != sizeof(clkinfo)) {
224 		error("kern.clockrate");
225 		die(0);
226 	}
227 	hertz = clkinfo.stathz;
228 	(*curcmd->c_init)();
229 	curcmd->c_flags |= CF_INIT;
230 	labels();
231 
232 	if (curcmd->c_cmd != NULL)
233 		SLIST_FOREACH (cmd, &commands, link)
234 			if (!curcmd->c_cmd(cmd->cmd, cmd->argv))
235 				warnx("command is not understood");
236 
237 	dellave = 0.0;
238 	display();
239 	noecho();
240 	crmode();
241 	keyboard();
242 	/*NOTREACHED*/
243 
244 	return EXIT_SUCCESS;
245 }
246 
247 void
labels(void)248 labels(void)
249 {
250 	if (curcmd->c_flags & CF_LOADAV) {
251 		mvaddstr(0, 20,
252 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
253 		mvaddstr(1, 5, "Load Average");
254 	}
255 	if (curcmd->c_flags & CF_ZFSARC) {
256 		mvaddstr(0, 20,
257 		    "   Total     MFU     MRU    Anon     Hdr   L2Hdr   Other");
258 		mvaddstr(1, 5, "ZFS ARC     ");
259 	}
260 	(*curcmd->c_label)();
261 #ifdef notdef
262 	mvprintw(21, 25, "CPU usage on %s", hostname);
263 #endif
264 	refresh();
265 }
266 
267 void
display(void)268 display(void)
269 {
270 	uint64_t arc_stat;
271 	int i, j;
272 
273 	/* Get the load average over the last minute. */
274 	(void) getloadavg(avenrun, nitems(avenrun));
275 	(*curcmd->c_fetch)();
276 	if (curcmd->c_flags & CF_LOADAV) {
277 		j = 5.0*avenrun[0] + 0.5;
278 		dellave -= avenrun[0];
279 		if (dellave >= 0.0)
280 			c = '<';
281 		else {
282 			c = '>';
283 			dellave = -dellave;
284 		}
285 		if (dellave < 0.1)
286 			c = '|';
287 		dellave = avenrun[0];
288 		wmove(wload, 0, 0); wclrtoeol(wload);
289 		for (i = MIN(j, 50); i > 0; i--)
290 			waddch(wload, c);
291 		if (j > 50)
292 			wprintw(wload, " %4.1f", avenrun[0]);
293 	}
294 	if (curcmd->c_flags & CF_ZFSARC) {
295 	    uint64_t arc[7] = {};
296 	    size_t size = sizeof(arc[0]);
297 	    if (sysctlbyname("kstat.zfs.misc.arcstats.size",
298 		&arc[0], &size, NULL, 0) == 0 ) {
299 		    GETSYSCTL("vfs.zfs.mfu_size", arc[1]);
300 		    GETSYSCTL("vfs.zfs.mru_size", arc[2]);
301 		    GETSYSCTL("vfs.zfs.anon_size", arc[3]);
302 		    GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc[4]);
303 		    GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc[5]);
304 		    GETSYSCTL("kstat.zfs.misc.arcstats.bonus_size", arc[6]);
305 		    GETSYSCTL("kstat.zfs.misc.arcstats.dnode_size", arc_stat);
306 		    arc[6] += arc_stat;
307 		    GETSYSCTL("kstat.zfs.misc.arcstats.dbuf_size", arc_stat);
308 		    arc[6] += arc_stat;
309 		    wmove(wload, 0, 0); wclrtoeol(wload);
310 		    for (i = 0 ; i < nitems(arc); i++)
311 			sysputuint64(wload, 0, i*8+2, 6, arc[i], 0);
312 	    }
313 	}
314 	(*curcmd->c_refresh)();
315 	if (curcmd->c_flags & (CF_LOADAV |CF_ZFSARC))
316 		wrefresh(wload);
317 	wrefresh(wnd);
318 	move(CMDLINE, col);
319 	refresh();
320 }
321 
322 void
load(void)323 load(void)
324 {
325 
326 	(void) getloadavg(avenrun, nitems(avenrun));
327 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
328 	    avenrun[0], avenrun[1], avenrun[2]);
329 	clrtoeol();
330 }
331 
332 void
die(int signo __unused)333 die(int signo __unused)
334 {
335 	move(CMDLINE, 0);
336 	clrtoeol();
337 	refresh();
338 	endwin();
339 	exit(0);
340 }
341 
342 #include <stdarg.h>
343 
344 void
error(const char * fmt,...)345 error(const char *fmt, ...)
346 {
347 	va_list ap;
348 	char buf[255];
349 	int oy, ox;
350 
351 	va_start(ap, fmt);
352 	if (wnd) {
353 		getyx(stdscr, oy, ox);
354 		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
355 		clrtoeol();
356 		standout();
357 		mvaddstr(CMDLINE, 0, buf);
358 		standend();
359 		move(oy, ox);
360 		refresh();
361 	} else {
362 		(void) vfprintf(stderr, fmt, ap);
363 		fprintf(stderr, "\n");
364 	}
365 	va_end(ap);
366 }
367 
368 void
nlisterr(struct nlist n_list[])369 nlisterr(struct nlist n_list[])
370 {
371 	int i, n;
372 
373 	n = 0;
374 	clear();
375 	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
376 	for (i = 0;
377 	    n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++)
378 		if (n_list[i].n_value == 0)
379 			mvprintw(2 + ++n, 10, "%s", n_list[i].n_name);
380 	move(CMDLINE, 0);
381 	clrtoeol();
382 	refresh();
383 	endwin();
384 	exit(1);
385 }
386