2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
45 "$Id: main.c,v 1.8 1998/06/09 04:17:19 imp Exp $";
48 #include <sys/param.h>
58 static struct nlist namelist[] = {
73 int verbose = 1; /* to report kvm read errs */
78 char hostname[MAXHOSTNAMELEN];
82 static WINDOW *wload; /* one line window for load average */
91 (void) setlocale(LC_TIME, "");
95 if (argv[0][0] == '-') {
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]);
105 naptime = atoi(argv[0]);
111 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
117 signal(SIGQUIT, die);
118 signal(SIGTERM, die);
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.
128 wnd = (*curcmd->c_open)();
130 warnx("couldn't initialize display");
133 wload = newwin(1, 0, 3, 20);
135 warnx("couldn't set up load average window");
138 if (kvm_nlist(kd, namelist)) {
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;
149 curcmd->c_flags |= CF_INIT;
154 signal(SIGALRM, display);
165 if (curcmd->c_flags & CF_LOADAV) {
167 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
168 mvaddstr(3, 5, "Load Average");
170 (*curcmd->c_label)();
172 mvprintw(21, 25, "CPU usage on %s", hostname);
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];
197 dellave = avenrun[0];
198 wmove(wload, 0, 0); wclrtoeol(wload);
199 for (i = (j > 50) ? 50 : j; i > 0; i--)
202 wprintw(wload, " %4.1f", avenrun[0]);
204 (*curcmd->c_refresh)();
205 if (curcmd->c_flags & CF_LOADAV)
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]);
242 error(const char *fmt, ...)
260 getyx(stdscr, oy, ox);
261 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
264 mvaddstr(CMDLINE, 0, buf);
269 (void) vfprintf(stderr, fmt, ap);
270 fprintf(stderr, "\n");
277 struct nlist namelist[];
283 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
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);