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