xref: /freebsd/usr.bin/systat/main.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1992, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] =
45 	"$Id: main.c,v 1.8 1998/06/09 04:17:19 imp Exp $";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 
50 #include <err.h>
51 #include <locale.h>
52 #include <nlist.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include "systat.h"
56 #include "extern.h"
57 
58 static struct nlist namelist[] = {
59 #define X_FIRST		0
60 #define	X_HZ		0
61 	{ "_hz" },
62 #define	X_STATHZ		1
63 	{ "_stathz" },
64 	{ "" }
65 };
66 static int     dellave;
67 
68 kvm_t *kd;
69 sig_t	sigtstpdfl;
70 double avenrun[3];
71 int     col;
72 int	naptime = 5;
73 int     verbose = 1;                    /* to report kvm read errs */
74 int     hz, stathz;
75 double	hertz;
76 char    c;
77 char    *namp;
78 char    hostname[MAXHOSTNAMELEN];
79 WINDOW  *wnd;
80 int     CMDLINE;
81 
82 static	WINDOW *wload;			/* one line window for load average */
83 
84 int
85 main(argc, argv)
86 	int argc;
87 	char **argv;
88 {
89 	char errbuf[80];
90 
91 	(void) setlocale(LC_TIME, "");
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 			naptime = atoi(argv[0]);
106 			if (naptime <= 0)
107 				naptime = 5;
108 		}
109 		argc--, argv++;
110 	}
111 	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
112 	if (kd == NULL) {
113 		error("%s", errbuf);
114 		exit(1);
115 	}
116 	signal(SIGINT, die);
117 	signal(SIGQUIT, die);
118 	signal(SIGTERM, die);
119 
120 	/*
121 	 * Initialize display.  Load average appears in a one line
122 	 * window of its own.  Current command's display appears in
123 	 * an overlapping sub-window of stdscr configured by the display
124 	 * routines to minimize update work by curses.
125 	 */
126 	initscr();
127 	CMDLINE = LINES - 1;
128 	wnd = (*curcmd->c_open)();
129 	if (wnd == NULL) {
130 		warnx("couldn't initialize display");
131 		die(0);
132 	}
133 	wload = newwin(1, 0, 3, 20);
134 	if (wload == NULL) {
135 		warnx("couldn't set up load average window");
136 		die(0);
137 	}
138 	if (kvm_nlist(kd, namelist)) {
139 		nlisterr(namelist);
140 		exit(1);
141 	}
142 	if (namelist[X_FIRST].n_type == 0)
143 		errx(1, "couldn't read namelist");
144 	gethostname(hostname, sizeof (hostname));
145 	NREAD(X_HZ, &hz, sizeof(hz));
146 	NREAD(X_STATHZ, &stathz, sizeof(stathz));
147 	hertz = stathz ? stathz : hz;
148 	(*curcmd->c_init)();
149 	curcmd->c_flags |= CF_INIT;
150 	labels();
151 
152 	dellave = 0.0;
153 
154 	signal(SIGALRM, display);
155 	display(0);
156 	noecho();
157 	crmode();
158 	keyboard();
159 	/*NOTREACHED*/
160 }
161 
162 void
163 labels()
164 {
165 	if (curcmd->c_flags & CF_LOADAV) {
166 		mvaddstr(2, 20,
167 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
168 		mvaddstr(3, 5, "Load Average");
169 	}
170 	(*curcmd->c_label)();
171 #ifdef notdef
172 	mvprintw(21, 25, "CPU usage on %s", hostname);
173 #endif
174 	refresh();
175 }
176 
177 void
178 display(signo)
179 	int signo;
180 {
181 	register int i, j;
182 
183 	/* Get the load average over the last minute. */
184 	(void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
185 	(*curcmd->c_fetch)();
186 	if (curcmd->c_flags & CF_LOADAV) {
187 		j = 5.0*avenrun[0] + 0.5;
188 		dellave -= avenrun[0];
189 		if (dellave >= 0.0)
190 			c = '<';
191 		else {
192 			c = '>';
193 			dellave = -dellave;
194 		}
195 		if (dellave < 0.1)
196 			c = '|';
197 		dellave = avenrun[0];
198 		wmove(wload, 0, 0); wclrtoeol(wload);
199 		for (i = (j > 50) ? 50 : j; i > 0; i--)
200 			waddch(wload, c);
201 		if (j > 50)
202 			wprintw(wload, " %4.1f", avenrun[0]);
203 	}
204 	(*curcmd->c_refresh)();
205 	if (curcmd->c_flags & CF_LOADAV)
206 		wrefresh(wload);
207 	wrefresh(wnd);
208 	move(CMDLINE, col);
209 	refresh();
210 	alarm(naptime);
211 }
212 
213 void
214 load()
215 {
216 
217 	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
218 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
219 	    avenrun[0], avenrun[1], avenrun[2]);
220 	clrtoeol();
221 }
222 
223 void
224 die(signo)
225 	int signo;
226 {
227 	move(CMDLINE, 0);
228 	clrtoeol();
229 	refresh();
230 	endwin();
231 	exit(0);
232 }
233 
234 #if __STDC__
235 #include <stdarg.h>
236 #else
237 #include <varargs.h>
238 #endif
239 
240 #if __STDC__
241 void
242 error(const char *fmt, ...)
243 #else
244 void
245 error(fmt, va_alist)
246 	char *fmt;
247 	va_dcl
248 #endif
249 {
250 	va_list ap;
251 	char buf[255];
252 	int oy, ox;
253 #if __STDC__
254 	va_start(ap, fmt);
255 #else
256 	va_start(ap);
257 #endif
258 
259 	if (wnd) {
260 		getyx(stdscr, oy, ox);
261 		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
262 		clrtoeol();
263 		standout();
264 		mvaddstr(CMDLINE, 0, buf);
265 		standend();
266 		move(oy, ox);
267 		refresh();
268 	} else {
269 		(void) vfprintf(stderr, fmt, ap);
270 		fprintf(stderr, "\n");
271 	}
272 	va_end(ap);
273 }
274 
275 void
276 nlisterr(namelist)
277 	struct nlist namelist[];
278 {
279 	int i, n;
280 
281 	n = 0;
282 	clear();
283 	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
284 	for (i = 0;
285 	    namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
286 		if (namelist[i].n_value == 0)
287 			mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
288 	move(CMDLINE, 0);
289 	clrtoeol();
290 	refresh();
291 	endwin();
292 	exit(1);
293 }
294