Commit | Line | Data |
---|---|---|
07ed1e09 KM |
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 | ||
0ca7a29c | 13 | #ifndef lint |
e6200bb7 | 14 | static char sccsid[] = "@(#)main.c 5.3 (Berkeley) %G%"; |
07ed1e09 | 15 | #endif not lint |
0ca7a29c SL |
16 | |
17 | #include "systat.h" | |
18 | ||
d830bff6 SL |
19 | static struct nlist nlst[] = { |
20 | #define X_CCPU 0 | |
0ca7a29c | 21 | { "_ccpu" }, |
d830bff6 | 22 | #define X_AVENRUN 1 |
0ca7a29c | 23 | { "_avenrun" }, |
833d578b | 24 | #define X_HZ 2 |
9c0c7319 | 25 | { "_hz" }, |
833d578b | 26 | #define X_PHZ 3 |
9c0c7319 | 27 | { "_phz" }, |
0ca7a29c SL |
28 | { "" } |
29 | }; | |
30 | ||
31 | int kmem = -1; | |
32 | int mem = -1; | |
33 | int swap = -1; | |
f58d0ba1 | 34 | int naptime = 5; |
0ca7a29c SL |
35 | |
36 | int die(); | |
37 | int display(); | |
38 | int suspend(); | |
88e200ad | 39 | int (*sigtstpdfl)(); |
0ca7a29c | 40 | |
9c0c7319 | 41 | double ccpu; |
855ff304 KM |
42 | int dellave; |
43 | ||
c639d430 SL |
44 | static WINDOW *wload; /* one line window for load average */ |
45 | ||
0ca7a29c SL |
46 | main(argc, argv) |
47 | int argc; | |
48 | char **argv; | |
49 | { | |
0ca7a29c | 50 | |
f58d0ba1 SL |
51 | argc--, argv++; |
52 | while (argc > 0) { | |
53 | if (argv[0][0] == '-') { | |
54 | struct cmdtab *p; | |
55 | ||
4fefc2e5 SL |
56 | p = lookup(&argv[0][1]); |
57 | if (p == (struct cmdtab *)-1) { | |
f58d0ba1 SL |
58 | fprintf(stderr, "%s: unknown request\n", |
59 | &argv[0][1]); | |
60 | exit(1); | |
61 | } | |
62 | curcmd = p; | |
63 | } else { | |
e6200bb7 TF |
64 | naptime = atoi(argv[0]); |
65 | if (naptime <= 0) | |
f58d0ba1 SL |
66 | naptime = 5; |
67 | } | |
68 | argc--, argv++; | |
69 | } | |
0ca7a29c | 70 | nlist("/vmunix", nlst); |
d830bff6 SL |
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 | } | |
0ca7a29c SL |
93 | signal(SIGINT, die); |
94 | signal(SIGQUIT, die); | |
95 | signal(SIGTERM, die); | |
96 | ||
d830bff6 | 97 | /* |
c639d430 SL |
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. | |
d830bff6 | 102 | */ |
0ca7a29c | 103 | initscr(); |
855ff304 | 104 | CMDLINE = LINES - 1; |
d830bff6 SL |
105 | wnd = (*curcmd->c_open)(); |
106 | if (wnd == NULL) { | |
107 | fprintf(stderr, "Couldn't initialize display.\n"); | |
108 | die(); | |
109 | } | |
c639d430 SL |
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 | } | |
0ca7a29c | 115 | |
f58d0ba1 | 116 | gethostname(hostname, sizeof (hostname)); |
9c0c7319 | 117 | lseek(kmem, nlst[X_CCPU].n_value, L_SET); |
0ca7a29c SL |
118 | read(kmem, &ccpu, sizeof (ccpu)); |
119 | lccpu = log(ccpu); | |
833d578b SL |
120 | hz = getw(nlst[X_HZ].n_value); |
121 | phz = getw(nlst[X_PHZ].n_value); | |
d830bff6 | 122 | (*curcmd->c_init)(); |
9c0c7319 | 123 | curcmd->c_flags |= CF_INIT; |
0ca7a29c SL |
124 | labels(); |
125 | ||
126 | known[0].k_uid = -1; | |
c639d430 | 127 | known[0].k_name[0] = '\0'; |
0ca7a29c | 128 | numknown = 1; |
c639d430 SL |
129 | procs[0].pid = -1; |
130 | strcpy(procs[0].cmd, "<idle>"); | |
131 | numprocs = 1; | |
0ca7a29c SL |
132 | dellave = 0.0; |
133 | ||
134 | signal(SIGALRM, display); | |
88e200ad | 135 | sigtstpdfl = signal(SIGTSTP, suspend); |
0ca7a29c SL |
136 | display(); |
137 | noecho(); | |
138 | crmode(); | |
d830bff6 SL |
139 | keyboard(); |
140 | /*NOTREACHED*/ | |
0ca7a29c SL |
141 | } |
142 | ||
143 | labels() | |
144 | { | |
145 | ||
9c0c7319 SL |
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 | } | |
0ca7a29c SL |
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); | |
9c0c7319 | 164 | read(kmem, avenrun, sizeof (avenrun)); |
0ca7a29c | 165 | (*curcmd->c_fetch)(); |
9c0c7319 SL |
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 | } | |
0ca7a29c | 184 | (*curcmd->c_refresh)(); |
9c0c7319 SL |
185 | if (curcmd->c_flags & CF_LOADAV) |
186 | wrefresh(wload); | |
0ca7a29c | 187 | wrefresh(wnd); |
855ff304 | 188 | move(CMDLINE, col); |
0ca7a29c SL |
189 | refresh(); |
190 | alarm(naptime); | |
191 | } | |
192 | ||
d830bff6 SL |
193 | load() |
194 | { | |
855ff304 | 195 | double avenrun[3]; |
d830bff6 SL |
196 | |
197 | lseek(kmem, nlst[X_AVENRUN].n_value, L_SET); | |
855ff304 KM |
198 | read(kmem, avenrun, sizeof (avenrun)); |
199 | mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f", | |
200 | avenrun[0], avenrun[1], avenrun[2]); | |
d830bff6 SL |
201 | clrtoeol(); |
202 | } | |
203 | ||
0ca7a29c SL |
204 | die() |
205 | { | |
206 | ||
207 | endwin(); | |
208 | exit(0); | |
209 | } | |
852c7064 SL |
210 | |
211 | error(fmt, a1, a2, a3) | |
212 | { | |
213 | ||
855ff304 | 214 | mvprintw(CMDLINE, 0, fmt, a1, a2, a3); |
852c7064 SL |
215 | clrtoeol(); |
216 | refresh(); | |
217 | } |