1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 char copyright[] = 9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10 All rights reserved.\n"; 11 #endif not lint 12 13 #ifndef lint 14 static char sccsid[] = "@(#)main.c 5.3 (Berkeley) 03/26/87"; 15 #endif not lint 16 17 #include "systat.h" 18 19 static struct nlist nlst[] = { 20 #define X_CCPU 0 21 { "_ccpu" }, 22 #define X_AVENRUN 1 23 { "_avenrun" }, 24 #define X_HZ 2 25 { "_hz" }, 26 #define X_PHZ 3 27 { "_phz" }, 28 { "" } 29 }; 30 31 int kmem = -1; 32 int mem = -1; 33 int swap = -1; 34 int naptime = 5; 35 36 int die(); 37 int display(); 38 int suspend(); 39 int (*sigtstpdfl)(); 40 41 double ccpu; 42 int dellave; 43 44 static WINDOW *wload; /* one line window for load average */ 45 46 main(argc, argv) 47 int argc; 48 char **argv; 49 { 50 51 argc--, argv++; 52 while (argc > 0) { 53 if (argv[0][0] == '-') { 54 struct cmdtab *p; 55 56 p = lookup(&argv[0][1]); 57 if (p == (struct cmdtab *)-1) { 58 fprintf(stderr, "%s: unknown request\n", 59 &argv[0][1]); 60 exit(1); 61 } 62 curcmd = p; 63 } else { 64 naptime = atoi(argv[0]); 65 if (naptime <= 0) 66 naptime = 5; 67 } 68 argc--, argv++; 69 } 70 nlist("/vmunix", nlst); 71 if (nlst[X_CCPU].n_type == 0) { 72 fprintf(stderr, "Couldn't namelist /vmunix.\n"); 73 exit(1); 74 } 75 kmemf = "/dev/kmem"; 76 kmem = open(kmemf, O_RDONLY); 77 if (kmem < 0) { 78 perror(kmemf); 79 exit(1); 80 } 81 memf = "/dev/mem"; 82 mem = open(memf, O_RDONLY); 83 if (mem < 0) { 84 perror(memf); 85 exit(1); 86 } 87 swapf = "/dev/drum"; 88 swap = open(swapf, O_RDONLY); 89 if (swap < 0) { 90 perror(swapf); 91 exit(1); 92 } 93 signal(SIGINT, die); 94 signal(SIGQUIT, die); 95 signal(SIGTERM, die); 96 97 /* 98 * Initialize display. Load average appears in a one line 99 * window of its own. Current command's display appears in 100 * an overlapping sub-window of stdscr configured by the display 101 * routines to minimize update work by curses. 102 */ 103 initscr(); 104 CMDLINE = LINES - 1; 105 wnd = (*curcmd->c_open)(); 106 if (wnd == NULL) { 107 fprintf(stderr, "Couldn't initialize display.\n"); 108 die(); 109 } 110 wload = newwin(1, 0, 3, 20); 111 if (wload == NULL) { 112 fprintf(stderr, "Couldn't set up load average window.\n"); 113 die(); 114 } 115 116 gethostname(hostname, sizeof (hostname)); 117 lseek(kmem, nlst[X_CCPU].n_value, L_SET); 118 read(kmem, &ccpu, sizeof (ccpu)); 119 lccpu = log(ccpu); 120 hz = getw(nlst[X_HZ].n_value); 121 phz = getw(nlst[X_PHZ].n_value); 122 (*curcmd->c_init)(); 123 curcmd->c_flags |= CF_INIT; 124 labels(); 125 126 known[0].k_uid = -1; 127 known[0].k_name[0] = '\0'; 128 numknown = 1; 129 procs[0].pid = -1; 130 strcpy(procs[0].cmd, "<idle>"); 131 numprocs = 1; 132 dellave = 0.0; 133 134 signal(SIGALRM, display); 135 sigtstpdfl = signal(SIGTSTP, suspend); 136 display(); 137 noecho(); 138 crmode(); 139 keyboard(); 140 /*NOTREACHED*/ 141 } 142 143 labels() 144 { 145 146 if (curcmd->c_flags & CF_LOADAV) { 147 mvaddstr(2, 20, 148 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); 149 mvaddstr(3, 5, "Load Average"); 150 } 151 (*curcmd->c_label)(); 152 #ifdef notdef 153 mvprintw(21, 25, "CPU usage on %s", hostname); 154 #endif 155 refresh(); 156 } 157 158 display() 159 { 160 register int i, j; 161 162 /* Get the load average over the last minute. */ 163 lseek(kmem, nlst[X_AVENRUN].n_value, L_SET); 164 read(kmem, avenrun, sizeof (avenrun)); 165 (*curcmd->c_fetch)(); 166 if (curcmd->c_flags & CF_LOADAV) { 167 j = 5.0*avenrun[0] + 0.5; 168 dellave -= avenrun[0]; 169 if (dellave >= 0.0) 170 c = '<'; 171 else { 172 c = '>'; 173 dellave = -dellave; 174 } 175 if (dellave < 0.1) 176 c = '|'; 177 dellave = avenrun[0]; 178 wmove(wload, 0, 0); wclrtoeol(wload); 179 for (i = (j > 50) ? 50 : j; i > 0; i--) 180 waddch(wload, c); 181 if (j > 50) 182 wprintw(wload, " %4.1f", avenrun[0]); 183 } 184 (*curcmd->c_refresh)(); 185 if (curcmd->c_flags & CF_LOADAV) 186 wrefresh(wload); 187 wrefresh(wnd); 188 move(CMDLINE, col); 189 refresh(); 190 alarm(naptime); 191 } 192 193 load() 194 { 195 double avenrun[3]; 196 197 lseek(kmem, nlst[X_AVENRUN].n_value, L_SET); 198 read(kmem, avenrun, sizeof (avenrun)); 199 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f", 200 avenrun[0], avenrun[1], avenrun[2]); 201 clrtoeol(); 202 } 203 204 die() 205 { 206 207 endwin(); 208 exit(0); 209 } 210 211 error(fmt, a1, a2, a3) 212 { 213 214 mvprintw(CMDLINE, 0, fmt, a1, a2, a3); 215 clrtoeol(); 216 refresh(); 217 } 218