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