1 | #include <stdlib.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <string.h> |
---|
4 | #include <fcntl.h> |
---|
5 | #include <unistd.h> |
---|
6 | |
---|
7 | #include "../../shared/sockets.h" |
---|
8 | #include "../../shared/debug.h" |
---|
9 | |
---|
10 | #include "main.h" |
---|
11 | #include "mode.h" |
---|
12 | #include "cpu.h" |
---|
13 | |
---|
14 | struct load { unsigned long total, user, system, nice, idle; }; |
---|
15 | |
---|
16 | int load_fd = 0; |
---|
17 | static void get_load(struct load * result); |
---|
18 | |
---|
19 | |
---|
20 | static void get_load(struct load * result) |
---|
21 | { |
---|
22 | static struct load last_load = { 0, 0, 0, 0, 0 }; struct load curr_load; |
---|
23 | |
---|
24 | reread(load_fd, "get_load:"); |
---|
25 | sscanf(buffer, "%*s %lu %lu %lu %lu\n", |
---|
26 | &curr_load.user, |
---|
27 | &curr_load.nice, |
---|
28 | &curr_load.system, |
---|
29 | &curr_load.idle); |
---|
30 | curr_load.total = curr_load.user |
---|
31 | + curr_load.nice |
---|
32 | + curr_load.system |
---|
33 | + curr_load.idle; |
---|
34 | result->total = curr_load.total - last_load.total; |
---|
35 | result->user = curr_load.user - last_load.user; |
---|
36 | result->nice = curr_load.nice - last_load.nice; |
---|
37 | result->system = curr_load.system - last_load.system; |
---|
38 | result->idle = curr_load.idle - last_load.idle; |
---|
39 | last_load.total = curr_load.total; |
---|
40 | last_load.user = curr_load.user; |
---|
41 | last_load.nice = curr_load.nice; |
---|
42 | last_load.system = curr_load.system; |
---|
43 | last_load.idle = curr_load.idle; |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | int cpu_init() |
---|
49 | { |
---|
50 | if(!load_fd) |
---|
51 | { |
---|
52 | load_fd = open("/proc/stat",O_RDONLY); |
---|
53 | } |
---|
54 | |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | int cpu_close() |
---|
59 | { |
---|
60 | if(load_fd) |
---|
61 | close(load_fd); |
---|
62 | |
---|
63 | load_fd = 0; |
---|
64 | |
---|
65 | return 0; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | ////////////////////////////////////////////////////////////////////////// |
---|
70 | // CPU screen shows info about percentage of the CPU being used |
---|
71 | // |
---|
72 | int cpu_screen(int rep, int display) |
---|
73 | { |
---|
74 | #undef CPU_BUF_SIZE |
---|
75 | #define CPU_BUF_SIZE 4 |
---|
76 | int i, j, n; |
---|
77 | static int first = 1; |
---|
78 | float value; |
---|
79 | static float cpu[CPU_BUF_SIZE + 1][5];// last buffer is scratch |
---|
80 | struct load load; |
---|
81 | |
---|
82 | |
---|
83 | if(first) |
---|
84 | { |
---|
85 | first = 0; |
---|
86 | |
---|
87 | sock_send_string(sock, "screen_add C\n"); |
---|
88 | sprintf(buffer, "screen_set C name {CPU Use: %s}\n", host); |
---|
89 | sock_send_string(sock, buffer); |
---|
90 | if(lcd_hgt >= 4) |
---|
91 | { |
---|
92 | sock_send_string(sock, "widget_add C title title\n"); |
---|
93 | sock_send_string(sock, "widget_set C title {CPU LOAD}\n"); |
---|
94 | sock_send_string(sock, "widget_add C one string\n"); |
---|
95 | sock_send_string(sock, |
---|
96 | "widget_set C one 1 2 {0 1 Sys 0.0%}\n"); |
---|
97 | sock_send_string(sock, "widget_add C two string\n"); |
---|
98 | sock_send_string(sock, "widget_add C three string\n"); |
---|
99 | sock_send_string(sock, |
---|
100 | "widget_set C one 1 2 {Usr 0.0% Nice 0.0%}\n"); |
---|
101 | sock_send_string(sock, |
---|
102 | "widget_set C two 1 3 {Sys 0.0% Idle 0.0%}\n"); |
---|
103 | sock_send_string(sock, |
---|
104 | "widget_set C three 1 4 {0% 100%}\n"); |
---|
105 | sock_send_string(sock, "widget_add C usr string\n"); |
---|
106 | sock_send_string(sock, "widget_add C nice string\n"); |
---|
107 | sock_send_string(sock, "widget_add C idle string\n"); |
---|
108 | sock_send_string(sock, "widget_add C sys string\n"); |
---|
109 | sock_send_string(sock, "widget_add C bar hbar\n"); |
---|
110 | sock_send_string(sock, "widget_set C bar 3 4 0\n"); |
---|
111 | } |
---|
112 | else |
---|
113 | { |
---|
114 | sock_send_string(sock, "widget_add C cpu0 string\n"); |
---|
115 | if(lcd_wid >= 20) |
---|
116 | sock_send_string(sock, "widget_set C cpu0 1 1 {CPU0[ ]}\n"); |
---|
117 | else |
---|
118 | sock_send_string(sock, "widget_set C cpu0 1 1 {CPU0[ ]}\n"); |
---|
119 | sock_send_string(sock, "widget_add C cpu0% string\n"); |
---|
120 | sprintf(buffer, "widget_set C cpu0%% 1 %i { 0.0%%}\n", lcd_wid-4); |
---|
121 | sock_send_string(sock, buffer); |
---|
122 | sock_send_string(sock, "widget_add C usni string\n"); |
---|
123 | sock_send_string(sock, |
---|
124 | "widget_set C usni 1 2 { U S N I}\n"); |
---|
125 | sock_send_string(sock, "widget_add C usr hbar\n"); |
---|
126 | sock_send_string(sock, "widget_add C sys hbar\n"); |
---|
127 | sock_send_string(sock, "widget_add C nice hbar\n"); |
---|
128 | sock_send_string(sock, "widget_add C idle hbar\n"); |
---|
129 | sock_send_string(sock, "widget_add C total hbar\n"); |
---|
130 | sock_send_string(sock, "widget_set C total 6 1 0\n"); |
---|
131 | } |
---|
132 | |
---|
133 | return 0; |
---|
134 | } |
---|
135 | //else return 0; |
---|
136 | |
---|
137 | get_load(&load); |
---|
138 | |
---|
139 | // Shift values over by one |
---|
140 | for(i=0; i<(CPU_BUF_SIZE-1); i++) |
---|
141 | for(j=0; j<5; j++) |
---|
142 | cpu[i][j] = cpu[i+1][j]; |
---|
143 | |
---|
144 | // Read new data |
---|
145 | cpu[CPU_BUF_SIZE-1][0] = ((float)load.user / (float)load.total) * 100.0; |
---|
146 | cpu[CPU_BUF_SIZE-1][1] = ((float)load.system / (float)load.total) * 100.0; |
---|
147 | cpu[CPU_BUF_SIZE-1][2] = ((float)load.nice / (float)load.total) * 100.0; |
---|
148 | cpu[CPU_BUF_SIZE-1][3] = ((float)load.idle / (float)load.total) * 100.0; |
---|
149 | cpu[CPU_BUF_SIZE-1][4] = (((float)load.user + (float)load.system + |
---|
150 | (float)load.nice) / (float)load.total) * 100.0; |
---|
151 | |
---|
152 | // Only clear on first display... |
---|
153 | if(!rep) |
---|
154 | { |
---|
155 | /* |
---|
156 | // Make all the same, if this is the first time... |
---|
157 | for(i=0; i<CPU_BUF_SIZE-1; i++) |
---|
158 | for(j=0; j<5; j++) |
---|
159 | cpu[i][j] = cpu[CPU_BUF_SIZE-1][j]; |
---|
160 | */ |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | // Average values for final result |
---|
166 | for(i=0; i<5; i++) |
---|
167 | { |
---|
168 | value = 0; |
---|
169 | for(j=0; j<CPU_BUF_SIZE; j++) |
---|
170 | { |
---|
171 | value += cpu[j][i]; |
---|
172 | } |
---|
173 | value /= CPU_BUF_SIZE; |
---|
174 | cpu[CPU_BUF_SIZE][i] = value; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | if(lcd_hgt >= 4) |
---|
179 | { |
---|
180 | value = cpu[CPU_BUF_SIZE][4]; |
---|
181 | n = (int)(value * 70.0); |
---|
182 | if (value >= 99.9) { sprintf(tmp, "100%%"); } |
---|
183 | else { sprintf(tmp, "%4.1f%%", value); } |
---|
184 | sprintf(buffer, "widget_set C title {CPU %s: %s}\n", tmp, host); |
---|
185 | if(display) sock_send_string(sock, buffer); |
---|
186 | |
---|
187 | value = cpu[CPU_BUF_SIZE][0]; |
---|
188 | if (value >= 99.9) { sprintf(tmp, " 100%%"); } |
---|
189 | else { sprintf(tmp, "%4.1f%%", value); } |
---|
190 | sprintf(buffer, "widget_set C usr 5 2 {%s}\n", tmp); |
---|
191 | if(display) sock_send_string(sock, buffer); |
---|
192 | |
---|
193 | value = cpu[CPU_BUF_SIZE][1]; |
---|
194 | if (value >= 99.9) { sprintf(tmp, " 100%%"); } |
---|
195 | else { sprintf(tmp, "%4.1f%%", value); } |
---|
196 | sprintf(buffer, "widget_set C sys 5 3 {%s}\n", tmp); |
---|
197 | if(display) sock_send_string(sock, buffer); |
---|
198 | |
---|
199 | value = cpu[CPU_BUF_SIZE][2]; |
---|
200 | if (value >= 99.9) { sprintf(tmp, " 100%%"); } |
---|
201 | else { sprintf(tmp, "%4.1f%%", value); } |
---|
202 | sprintf(buffer, "widget_set C nice 16 2 {%s}\n", tmp); |
---|
203 | if(display) sock_send_string(sock, buffer); |
---|
204 | |
---|
205 | value = cpu[CPU_BUF_SIZE][3]; |
---|
206 | if (value >= 99.9) { sprintf(tmp, " 100%%"); } |
---|
207 | else { sprintf(tmp, "%4.1f%%", value); } |
---|
208 | sprintf(buffer, "widget_set C idle 16 3 {%s}\n", tmp); |
---|
209 | if(display) sock_send_string(sock, buffer); |
---|
210 | |
---|
211 | value = cpu[CPU_BUF_SIZE][4]; |
---|
212 | n = (int)(value * (lcd_cellwid * 14) / 100.0); |
---|
213 | sprintf(buffer, "widget_set C bar 3 4 %i\n", n); |
---|
214 | if(display) sock_send_string(sock, buffer); |
---|
215 | } // end if(lcd_hgt >= 4) |
---|
216 | else // 20x2 version |
---|
217 | { |
---|
218 | value = cpu[CPU_BUF_SIZE][4]; |
---|
219 | if (value >= 99.9) { sprintf(tmp, "100%%"); } |
---|
220 | else { sprintf(tmp, "%4.1f%%", value); } |
---|
221 | sprintf(buffer, "widget_set C cpu0%% %i 1 {%s}\n", lcd_wid-4, tmp); |
---|
222 | if(display) sock_send_string(sock, buffer); |
---|
223 | |
---|
224 | n = (float)(value * lcd_cellwid * (float)(lcd_wid-11)) / 100.0; |
---|
225 | sprintf(buffer, "widget_set C total 6 1 %i\n", n); |
---|
226 | if(display) sock_send_string(sock, buffer); |
---|
227 | |
---|
228 | value = cpu[CPU_BUF_SIZE][0]; |
---|
229 | n = (float)(value * lcd_cellwid * 4.0) / 100.0; |
---|
230 | sprintf(buffer, "widget_set C usr 1 2 %i\n", n); |
---|
231 | if(display) sock_send_string(sock, buffer); |
---|
232 | |
---|
233 | value = cpu[CPU_BUF_SIZE][1]; |
---|
234 | n = (float)(value * lcd_cellwid * 3.0) / 100.0; |
---|
235 | sprintf(buffer, "widget_set C sys 7 2 %i\n", n); |
---|
236 | if(display) sock_send_string(sock, buffer); |
---|
237 | |
---|
238 | value = cpu[CPU_BUF_SIZE][2]; |
---|
239 | n = (float)(value * lcd_cellwid * 3.0) / 100.0; |
---|
240 | sprintf(buffer, "widget_set C nice 12 2 %i\n", n); |
---|
241 | if(display) sock_send_string(sock, buffer); |
---|
242 | |
---|
243 | value = cpu[CPU_BUF_SIZE][3]; |
---|
244 | n = (float)(value * lcd_cellwid * 3.0) / 100.0; |
---|
245 | sprintf(buffer, "widget_set C idle 17 2 %i\n", n); |
---|
246 | if(display) sock_send_string(sock, buffer); |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | return 0; |
---|
251 | } // End cpu_screen() |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | ////////////////////////////////////////////////////////////////////////// |
---|
256 | // Cpu Graph Screen shows a quick-moving histogram of CPU use. |
---|
257 | // |
---|
258 | int cpu_graph_screen(int rep, int display) |
---|
259 | { |
---|
260 | int i, j, n=0; |
---|
261 | static int first=1; |
---|
262 | float value, maxload; |
---|
263 | #undef CPU_BUF_SIZE |
---|
264 | #define CPU_BUF_SIZE 2 |
---|
265 | static float cpu[CPU_BUF_SIZE + 1];// last buffer is scratch |
---|
266 | static int cpu_past[LCD_MAX_WIDTH]; |
---|
267 | struct load load; |
---|
268 | int status=0; |
---|
269 | |
---|
270 | |
---|
271 | if(first) |
---|
272 | { |
---|
273 | first = 0; |
---|
274 | sock_send_string(sock, "screen_add G\n"); |
---|
275 | sprintf(buffer, "screen_set G name {CPU Graph: %s}\n", host); |
---|
276 | sock_send_string(sock, buffer); |
---|
277 | if(lcd_hgt >= 4) |
---|
278 | { |
---|
279 | sock_send_string(sock, "widget_add G title title\n"); |
---|
280 | sprintf(buffer, "widget_set G title {CPU: %s}\n", host); |
---|
281 | } |
---|
282 | else |
---|
283 | { |
---|
284 | sock_send_string(sock, "widget_add G title string\n"); |
---|
285 | sprintf(buffer, "widget_set G title 1 1 {CPU: %s}\n", host); |
---|
286 | } |
---|
287 | sock_send_string(sock, buffer); |
---|
288 | |
---|
289 | for(i=1; i<=lcd_wid; i++) |
---|
290 | { |
---|
291 | sprintf(tmp, "widget_add G %i vbar\n", i); |
---|
292 | sock_send_string(sock, tmp); |
---|
293 | sprintf(tmp, "widget_set G %i %i %i 0\n", i, i, lcd_hgt); |
---|
294 | sock_send_string(sock, tmp); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | get_load(&load); |
---|
299 | |
---|
300 | // Shift values over by one |
---|
301 | for(i=0; i<(CPU_BUF_SIZE-1); i++) |
---|
302 | cpu[i] = cpu[i+1]; |
---|
303 | |
---|
304 | // Read new data |
---|
305 | cpu[CPU_BUF_SIZE-1] = ((float)load.user + (float)load.system |
---|
306 | + (float)load.nice) / (float)load.total; |
---|
307 | |
---|
308 | |
---|
309 | // Only clear on first display... |
---|
310 | if(!rep) |
---|
311 | { |
---|
312 | /* |
---|
313 | // Make all the same, if this is the first time... |
---|
314 | for(i=0; i<CPU_BUF_SIZE-1; i++) |
---|
315 | cpu[i] = cpu[CPU_BUF_SIZE-1]; |
---|
316 | */ |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | // Average values for final result |
---|
321 | value = 0; |
---|
322 | for(j=0; j<CPU_BUF_SIZE; j++) |
---|
323 | { |
---|
324 | value += cpu[j]; |
---|
325 | } |
---|
326 | value /= (float)CPU_BUF_SIZE; |
---|
327 | cpu[CPU_BUF_SIZE] = value; |
---|
328 | |
---|
329 | |
---|
330 | maxload=0; |
---|
331 | for(i=0; i<lcd_wid-1; i++) |
---|
332 | { |
---|
333 | cpu_past[i] = cpu_past[i+1]; |
---|
334 | |
---|
335 | j = cpu_past[i]; |
---|
336 | sprintf(tmp, "widget_set G %i %i %i %i\n", |
---|
337 | i+1, i+1, lcd_hgt, j); |
---|
338 | if(display) sock_send_string(sock, tmp); |
---|
339 | |
---|
340 | if(cpu_past[i] > maxload) maxload = cpu_past[i]; |
---|
341 | } |
---|
342 | |
---|
343 | value = cpu[CPU_BUF_SIZE]; |
---|
344 | if(lcd_hgt > 2) |
---|
345 | n = (int)(value * (float)(lcd_cellhgt) * (float)(lcd_hgt-1)); |
---|
346 | else |
---|
347 | n = (int)(value * (float)(lcd_cellhgt) * (float)(lcd_hgt)); |
---|
348 | |
---|
349 | cpu_past[lcd_wid-1] = n; |
---|
350 | sprintf(tmp, "widget_set G %i %i %i %i\n", |
---|
351 | lcd_wid, lcd_wid, lcd_hgt, n); |
---|
352 | if(display) sock_send_string(sock, tmp); |
---|
353 | |
---|
354 | |
---|
355 | |
---|
356 | if(n > maxload) maxload = n; |
---|
357 | |
---|
358 | if(cpu_past[lcd_wid-1] > 0 ) status = BACKLIGHT_ON; |
---|
359 | if(maxload < 1) status = BACKLIGHT_OFF; |
---|
360 | |
---|
361 | // return status; |
---|
362 | return 0; |
---|
363 | } // End cpu_graph_screen() |
---|
364 | |
---|
365 | |
---|