2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 static const char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
41 static const char copyright[] __unused =
42 "@(#) Copyright (c) 1980, 1992, 1993\n\
43 The Regents of the University of California. All rights reserved.\n";
46 #include <sys/param.h>
48 #include <sys/sysctl.h>
49 #include <sys/queue.h>
70 unsigned int delay = 5000000; /* in microseconds */
71 int verbose = 1; /* to report kvm read errs */
72 static struct clockinfo clkinfo;
76 char hostname[MAXHOSTNAMELEN];
81 static WINDOW *wload; /* one line window for load average */
84 SLIST_ENTRY(cmdentry) link;
85 char *cmd; /* Command name */
86 char *argv; /* Arguments vector for a command */
88 static SLIST_HEAD(, cmdentry) commands;
91 parse_cmd_args (int argc, char **argv)
94 struct cmdentry *cmd = NULL;
98 if (argv[0][0] == '-') {
100 SLIST_INSERT_HEAD(&commands, cmd, link);
102 if (memcmp(argv[0], "--", 3) == 0) {
103 in_command = 0; /*-- ends a command explicitly*/
107 cmd = calloc(1, sizeof(struct cmdentry));
109 errx(1, "memory allocating failure");
110 cmd->cmd = strdup(&argv[0][1]);
111 if (cmd->cmd == NULL)
112 errx(1, "memory allocating failure");
115 else if (!in_command) {
116 t = strtod(argv[0], NULL) * 1000000.0;
117 if (t > 0 && t < (double)UINT_MAX)
118 delay = (unsigned int)t;
120 else if (cmd != NULL) {
121 cmd->argv = strdup(argv[0]);
122 if (cmd->argv == NULL)
123 errx(1, "memory allocating failure");
125 SLIST_INSERT_HEAD(&commands, cmd, link);
128 errx(1, "invalid arguments list");
132 if (in_command && cmd != NULL)
133 SLIST_INSERT_HEAD(&commands, cmd, link);
138 resize(int signo __unused)
153 main(int argc, char **argv)
155 char errbuf[_POSIX2_LINE_MAX], dummy;
157 struct cmdentry *cmd = NULL;
160 (void) setlocale(LC_ALL, "");
162 SLIST_INIT(&commands);
165 if (argv[0][0] == '-') {
168 p = lookup(&argv[0][1]);
169 if (p == (struct cmdtab *)-1)
170 errx(1, "%s: ambiguous request", &argv[0][1]);
171 if (p == (struct cmdtab *)0)
172 errx(1, "%s: unknown request", &argv[0][1]);
176 parse_cmd_args (argc, argv);
179 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
182 * Try to actually read something, we may be in a jail, and
183 * have /dev/null opened as /dev/mem.
185 if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 ||
186 kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) !=
194 * Maybe we are lacking permissions? Retry, this time with bogus
195 * devices. We can now use sysctl only.
198 kd = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, _PATH_DEVNULL,
207 signal(SIGQUIT, die);
208 signal(SIGTERM, die);
209 signal(SIGWINCH, resize);
212 * Initialize display. Load average appears in a one line
213 * window of its own. Current command's display appears in
214 * an overlapping sub-window of stdscr configured by the display
215 * routines to minimize update work by curses.
219 use_default_colors();
220 pair_content(0, &cf, &cb);
221 init_pair(1, COLOR_GREEN, cb);
222 init_pair(2, COLOR_MAGENTA, cb);
223 init_pair(3, COLOR_RED, cb);
224 init_pair(4, COLOR_BLUE, cb);
226 wnd = (*curcmd->c_open)();
228 warnx("couldn't initialize display");
231 wload = newwin(1, 0, 1, 20);
233 warnx("couldn't set up load average window");
236 gethostname(hostname, sizeof (hostname));
237 size = sizeof(clkinfo);
238 if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0)
239 || size != sizeof(clkinfo)) {
240 error("kern.clockrate");
243 hertz = clkinfo.stathz;
245 curcmd->c_flags |= CF_INIT;
248 if (curcmd->c_cmd != NULL)
249 SLIST_FOREACH (cmd, &commands, link)
250 if (!curcmd->c_cmd(cmd->cmd, cmd->argv))
251 warnx("command is not understood");
266 if (curcmd->c_flags & CF_LOADAV) {
268 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
269 mvaddstr(1, 5, "Load Average");
271 if (curcmd->c_flags & CF_ZFSARC) {
273 " Total MFU MRU Anon Hdr L2Hdr Other");
274 mvaddstr(1, 5, "ZFS ARC ");
276 (*curcmd->c_label)();
278 mvprintw(21, 25, "CPU usage on %s", hostname);
290 /* Get the load average over the last minute. */
291 (void) getloadavg(avenrun, nitems(avenrun));
292 (*curcmd->c_fetch)();
293 if (curcmd->c_flags & CF_LOADAV) {
294 j = 5.0*avenrun[0] + 0.5;
295 dellave -= avenrun[0];
304 dellave = avenrun[0];
305 wmove(wload, 0, 0); wclrtoeol(wload);
306 for (i = MIN(j, 50); i > 0; i--)
309 wprintw(wload, " %4.1f", avenrun[0]);
311 if (curcmd->c_flags & CF_ZFSARC) {
312 uint64_t arc[7] = {};
313 size_t size = sizeof(arc[0]);
314 if (sysctlbyname("kstat.zfs.misc.arcstats.size",
315 &arc[0], &size, NULL, 0) == 0 ) {
316 GETSYSCTL("vfs.zfs.mfu_size", arc[1]);
317 GETSYSCTL("vfs.zfs.mru_size", arc[2]);
318 GETSYSCTL("vfs.zfs.anon_size", arc[3]);
319 GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc[4]);
320 GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc[5]);
321 GETSYSCTL("kstat.zfs.misc.arcstats.bonus_size", arc[6]);
322 GETSYSCTL("kstat.zfs.misc.arcstats.dnode_size", arc_stat);
324 GETSYSCTL("kstat.zfs.misc.arcstats.dbuf_size", arc_stat);
326 wmove(wload, 0, 0); wclrtoeol(wload);
327 for (ui = 0 ; ui < nitems(arc); ui++)
328 sysputuint64(wload, 0, ui*8+2, 6, arc[ui], 0);
331 (*curcmd->c_refresh)();
332 if (curcmd->c_flags & (CF_LOADAV |CF_ZFSARC))
343 (void) getloadavg(avenrun, nitems(avenrun));
344 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
345 avenrun[0], avenrun[1], avenrun[2]);
350 die(int signo __unused)
362 error(const char *fmt, ...)
370 getyx(stdscr, oy, ox);
371 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
374 mvaddstr(CMDLINE, 0, buf);
379 (void) vfprintf(stderr, fmt, ap);
380 fprintf(stderr, "\n");
386 nlisterr(struct nlist n_list[])
392 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
394 n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++)
395 if (n_list[i].n_value == 0)
396 mvprintw(2 + ++n, 10, "%s", n_list[i].n_name);