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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1992, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
44 #include <sys/param.h>
46 #include <sys/sysctl.h>
67 unsigned int delay = 5000000; /* in microseconds */
68 int verbose = 1; /* to report kvm read errs */
69 struct clockinfo clkinfo;
73 char hostname[MAXHOSTNAMELEN];
78 static WINDOW *wload; /* one line window for load average */
81 main(int argc, char **argv)
83 char errbuf[_POSIX2_LINE_MAX], dummy;
87 (void) setlocale(LC_ALL, "");
91 if (argv[0][0] == '-') {
94 p = lookup(&argv[0][1]);
95 if (p == (struct cmdtab *)-1)
96 errx(1, "%s: ambiguous request", &argv[0][1]);
97 if (p == (struct cmdtab *)0)
98 errx(1, "%s: unknown request", &argv[0][1]);
101 t = strtod(argv[0], NULL) * 1000000.0;
102 if (t > 0 && t < (double)UINT_MAX)
103 delay = (unsigned int)t;
107 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
110 * Try to actually read something, we may be in a jail, and
111 * have /dev/null opened as /dev/mem.
113 if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 ||
114 kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) !=
122 * Maybe we are lacking permissions? Retry, this time with bogus
123 * devices. We can now use sysctl only.
126 kd = kvm_openfiles("/dev/null", "/dev/null", "/dev/null",
135 signal(SIGQUIT, die);
136 signal(SIGTERM, die);
139 * Initialize display. Load average appears in a one line
140 * window of its own. Current command's display appears in
141 * an overlapping sub-window of stdscr configured by the display
142 * routines to minimize update work by curses.
146 wnd = (*curcmd->c_open)();
148 warnx("couldn't initialize display");
151 wload = newwin(1, 0, 1, 20);
153 warnx("couldn't set up load average window");
156 gethostname(hostname, sizeof (hostname));
157 size = sizeof(clkinfo);
158 if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0)
159 || size != sizeof(clkinfo)) {
160 error("kern.clockrate");
163 hertz = clkinfo.stathz;
165 curcmd->c_flags |= CF_INIT;
182 if (curcmd->c_flags & CF_LOADAV) {
184 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
185 mvaddstr(1, 5, "Load Average");
187 (*curcmd->c_label)();
189 mvprintw(21, 25, "CPU usage on %s", hostname);
199 /* Get the load average over the last minute. */
200 (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
201 (*curcmd->c_fetch)();
202 if (curcmd->c_flags & CF_LOADAV) {
203 j = 5.0*avenrun[0] + 0.5;
204 dellave -= avenrun[0];
213 dellave = avenrun[0];
214 wmove(wload, 0, 0); wclrtoeol(wload);
215 for (i = (j > 50) ? 50 : j; i > 0; i--)
218 wprintw(wload, " %4.1f", avenrun[0]);
220 (*curcmd->c_refresh)();
221 if (curcmd->c_flags & CF_LOADAV)
232 (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
233 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
234 avenrun[0], avenrun[1], avenrun[2]);
239 die(int signo __unused)
251 error(const char *fmt, ...)
259 getyx(stdscr, oy, ox);
260 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
263 mvaddstr(CMDLINE, 0, buf);
268 (void) vfprintf(stderr, fmt, ap);
269 fprintf(stderr, "\n");
275 nlisterr(struct nlist n_list[])
281 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
283 n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++)
284 if (n_list[i].n_value == 0)
285 mvprintw(2 + ++n, 10, "%s", n_list[i].n_name);