xref: /dragonfly/usr.bin/systat/main.c (revision 2d8a3be7)
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  * @(#) Copyright (c) 1980, 1992, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)main.c	8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/systat/main.c,v 1.11.2.1 2001/06/06 20:26:01 tmm Exp $
36  * $DragonFly: src/usr.bin/systat/main.c,v 1.4 2003/10/04 20:36:51 hmp Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/time.h>
41 
42 #include <err.h>
43 #include <limits.h>
44 #include <locale.h>
45 #include <nlist.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include "systat.h"
51 #include "extern.h"
52 
53 static struct nlist namelist[] = {
54 #define X_FIRST		0
55 #define	X_HZ		0
56 	{ "_hz" },
57 #define	X_STATHZ		1
58 	{ "_stathz" },
59 	{ "" }
60 };
61 static int     dellave;
62 
63 kvm_t *kd;
64 sig_t	sigtstpdfl;
65 double avenrun[3];
66 int     col;
67 double	naptime = 5.0;
68 int     verbose = 1;                    /* to report kvm read errs */
69 int     hz, stathz;
70 double	hertz;
71 char    c;
72 char    *namp;
73 char    hostname[MAXHOSTNAMELEN];
74 WINDOW  *wnd;
75 int     CMDLINE;
76 
77 static	WINDOW *wload;			/* one line window for load average */
78 
79 int
80 main(int argc, char **argv)
81 {
82 	char errbuf[_POSIX2_LINE_MAX];
83 
84 	(void) setlocale(LC_TIME, "");
85 
86 	argc--, argv++;
87 	while (argc > 0) {
88 		if (argv[0][0] == '-') {
89 			struct cmdtab *p;
90 
91 			p = lookup(&argv[0][1]);
92 			if (p == (struct cmdtab *)-1)
93 				errx(1, "%s: ambiguous request", &argv[0][1]);
94 			if (p == (struct cmdtab *)0)
95 				errx(1, "%s: unknown request", &argv[0][1]);
96 			curcmd = p;
97 		} else {
98 			naptime = strtod(argv[0], NULL);
99 			if (naptime <= 0.0)
100 				naptime = 5.0;
101 		}
102 		argc--, argv++;
103 	}
104 	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
105 	if (kd == NULL) {
106 		error("%s", errbuf);
107 		exit(1);
108 	}
109 	signal(SIGINT, die);
110 	signal(SIGQUIT, die);
111 	signal(SIGTERM, die);
112 
113 	/*
114 	 * Initialize display.  Load average appears in a one line
115 	 * window of its own.  Current command's display appears in
116 	 * an overlapping sub-window of stdscr configured by the display
117 	 * routines to minimize update work by curses.
118 	 */
119 	initscr();
120 	CMDLINE = LINES - 1;
121 	wnd = (*curcmd->c_open)();
122 	if (wnd == NULL) {
123 		warnx("couldn't initialize display");
124 		die(0);
125 	}
126 	wload = newwin(1, 0, 3, 20);
127 	if (wload == NULL) {
128 		warnx("couldn't set up load average window");
129 		die(0);
130 	}
131 	if (kvm_nlist(kd, namelist)) {
132 		nlisterr(namelist);
133 		exit(1);
134 	}
135 	if (namelist[X_FIRST].n_type == 0)
136 		errx(1, "couldn't read namelist");
137 	gethostname(hostname, sizeof (hostname));
138 	NREAD(X_HZ, &hz, sizeof(hz));
139 	NREAD(X_STATHZ, &stathz, sizeof(stathz));
140 	hertz = stathz ? stathz : hz;
141 	(*curcmd->c_init)();
142 	curcmd->c_flags |= CF_INIT;
143 	labels();
144 
145 	dellave = 0.0;
146 
147 	signal(SIGALRM, display);
148 	display(0);
149 	noecho();
150 	crmode();
151 	keyboard();
152 	/*NOTREACHED*/
153 
154 	return EXIT_SUCCESS;
155 }
156 
157 void
158 labels(void)
159 {
160 	if (curcmd->c_flags & CF_LOADAV) {
161 		mvaddstr(2, 20,
162 		    "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
163 		mvaddstr(3, 5, "Load Average");
164 	}
165 	(*curcmd->c_label)();
166 #ifdef notdef
167 	mvprintw(21, 25, "CPU usage on %s", hostname);
168 #endif
169 	refresh();
170 }
171 
172 void
173 display(int signo)
174 {
175 	register int i, j;
176 	struct itimerval ctv;
177 
178 	/* Get the load average over the last minute. */
179 	(void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
180 	(*curcmd->c_fetch)();
181 	if (curcmd->c_flags & CF_LOADAV) {
182 		j = 5.0*avenrun[0] + 0.5;
183 		dellave -= avenrun[0];
184 		if (dellave >= 0.0)
185 			c = '<';
186 		else {
187 			c = '>';
188 			dellave = -dellave;
189 		}
190 		if (dellave < 0.1)
191 			c = '|';
192 		dellave = avenrun[0];
193 		wmove(wload, 0, 0); wclrtoeol(wload);
194 		for (i = (j > 50) ? 50 : j; i > 0; i--)
195 			waddch(wload, c);
196 		if (j > 50)
197 			wprintw(wload, " %4.1f", avenrun[0]);
198 	}
199 	(*curcmd->c_refresh)();
200 	if (curcmd->c_flags & CF_LOADAV)
201 		wrefresh(wload);
202 	wrefresh(wnd);
203 	move(CMDLINE, col);
204 	refresh();
205 	ctv.it_interval.tv_sec = 0;
206 	ctv.it_interval.tv_usec = 0;
207 	ctv.it_value.tv_sec = (int)naptime;
208 	ctv.it_value.tv_usec = (naptime - (double)(int)naptime) * 1000000.0;
209 	setitimer(ITIMER_REAL, &ctv, NULL);
210 }
211 
212 void
213 load(void)
214 {
215 
216 	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
217 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
218 	    avenrun[0], avenrun[1], avenrun[2]);
219 	clrtoeol();
220 }
221 
222 void
223 die(int signo)
224 {
225 	move(CMDLINE, 0);
226 	clrtoeol();
227 	refresh();
228 	endwin();
229 	exit(0);
230 }
231 
232 #include <stdarg.h>
233 
234 void
235 error(const char *fmt, ...)
236 {
237 	va_list ap;
238 	char buf[255];
239 	int oy, ox;
240 	va_start(ap, fmt);
241 
242 	if (wnd) {
243 		getyx(stdscr, oy, ox);
244 		(void) vsnprintf(buf, sizeof(buf), fmt, ap);
245 		clrtoeol();
246 		standout();
247 		mvaddstr(CMDLINE, 0, buf);
248 		standend();
249 		move(oy, ox);
250 		refresh();
251 	} else {
252 		(void) vfprintf(stderr, fmt, ap);
253 		fprintf(stderr, "\n");
254 	}
255 	va_end(ap);
256 }
257 
258 void
259 nlisterr(struct nlist *namelist)
260 {
261 	int i, n;
262 
263 	n = 0;
264 	clear();
265 	mvprintw(2, 10, "systat: nlist: can't find following symbols:");
266 	for (i = 0;
267 	    namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
268 		if (namelist[i].n_value == 0)
269 			mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
270 	move(CMDLINE, 0);
271 	clrtoeol();
272 	refresh();
273 	endwin();
274 	exit(1);
275 }
276