|
0 |
#define _GNU_SOURCE
|
|
1 |
#include <stdio.h>
|
|
2 |
#include <libintl.h>
|
|
3 |
#include <math.h>
|
|
4 |
#include <poll.h>
|
|
5 |
#include <stdarg.h>
|
|
6 |
#include <signal.h>
|
|
7 |
#include <stdlib.h>
|
|
8 |
#include <string.h>
|
|
9 |
#include <sys/ioctl.h>
|
|
10 |
|
|
11 |
#include <ncurses.h>
|
|
12 |
|
|
13 |
#include "error.h"
|
|
14 |
#include "colors.h"
|
|
15 |
#include "gen.h"
|
|
16 |
#include "main.h"
|
|
17 |
#include "kalman.h"
|
|
18 |
#ifdef FW
|
|
19 |
#include "fft.h"
|
|
20 |
#endif
|
|
21 |
#include "utils.h"
|
|
22 |
|
|
23 |
char win_resize = 0;
|
|
24 |
WINDOW *w_stats = NULL, *w_line1 = NULL, *w_slow = NULL, *w_line2 = NULL, *w_fast = NULL;
|
|
25 |
int stats_h = 10;
|
|
26 |
int logs_n = 0, slow_n = 0, fast_n = 0;
|
|
27 |
char **slow_history = NULL, **fast_history = NULL;
|
|
28 |
int window_history_n = 0;
|
|
29 |
char use_colors = 0;
|
|
30 |
|
|
31 |
double graph_limit = MY_DOUBLE_INF;
|
|
32 |
double hz = 1.0;
|
|
33 |
|
|
34 |
double *history = NULL, *history_temp = NULL, *history_fft_magn = NULL, *history_fft_phase = NULL;
|
|
35 |
char *history_set = NULL;
|
|
36 |
int history_n = 0;
|
|
37 |
|
|
38 |
char draw_phase = 0;
|
|
39 |
|
|
40 |
char pause_graphs = 0;
|
|
41 |
|
|
42 |
typedef enum { C_WHITE = 0, C_GREEN, C_YELLOW, C_BLUE, C_MAGENTA, C_CYAN, C_RED } color_t;
|
|
43 |
|
|
44 |
void update_terminal(void)
|
|
45 |
{
|
|
46 |
wnoutrefresh(w_stats);
|
|
47 |
wnoutrefresh(w_slow);
|
|
48 |
wnoutrefresh(w_fast);
|
|
49 |
|
|
50 |
doupdate();
|
|
51 |
}
|
|
52 |
|
|
53 |
void create_windows(void)
|
|
54 |
{
|
|
55 |
char *r_a = gettext("realloc issue");
|
|
56 |
int nr = 0;
|
|
57 |
|
|
58 |
if (w_stats)
|
|
59 |
{
|
|
60 |
delwin(w_stats);
|
|
61 |
delwin(w_line1);
|
|
62 |
delwin(w_slow);
|
|
63 |
delwin(w_line2);
|
|
64 |
delwin(w_fast);
|
|
65 |
}
|
|
66 |
|
|
67 |
#ifdef FW
|
|
68 |
fft_free();
|
|
69 |
fft_init(max_x);
|
|
70 |
#endif
|
|
71 |
|
|
72 |
if (max_x > history_n)
|
|
73 |
{
|
|
74 |
history = (double *)realloc(history, sizeof(double) * max_x);
|
|
75 |
if (!history)
|
|
76 |
error_exit(r_a);
|
|
77 |
|
|
78 |
history_temp = (double *)realloc(history_temp, sizeof(double) * max_x);
|
|
79 |
if (!history_temp)
|
|
80 |
error_exit(r_a);
|
|
81 |
|
|
82 |
/* halve of it is enough really */
|
|
83 |
history_fft_magn = (double *)realloc(history_fft_magn, sizeof(double) * max_x);
|
|
84 |
if (!history_fft_magn)
|
|
85 |
error_exit(r_a);
|
|
86 |
|
|
87 |
history_fft_phase = (double *)realloc(history_fft_phase, sizeof(double) * max_x);
|
|
88 |
if (!history_fft_phase)
|
|
89 |
error_exit(r_a);
|
|
90 |
|
|
91 |
history_set = (char *)realloc(history_set, sizeof(char) * max_x);
|
|
92 |
if (!history_set)
|
|
93 |
error_exit(r_a);
|
|
94 |
|
|
95 |
memset(&history[history_n], 0x00, (max_x - history_n) * sizeof(double));
|
|
96 |
memset(&history_set[history_n], 0x00, (max_x - history_n) * sizeof(char));
|
|
97 |
|
|
98 |
history_n = max_x;
|
|
99 |
}
|
|
100 |
|
|
101 |
if ((int)max_y > window_history_n)
|
|
102 |
{
|
|
103 |
slow_history = (char **)realloc(slow_history, sizeof(char *) * max_y);
|
|
104 |
if (!slow_history)
|
|
105 |
error_exit(r_a);
|
|
106 |
|
|
107 |
fast_history = (char **)realloc(fast_history, sizeof(char *) * max_y);
|
|
108 |
if (!fast_history)
|
|
109 |
error_exit(r_a);
|
|
110 |
|
|
111 |
memset(&slow_history[window_history_n], 0x00, (max_y - window_history_n) * sizeof(char *));
|
|
112 |
memset(&fast_history[window_history_n], 0x00, (max_y - window_history_n) * sizeof(char *));
|
|
113 |
|
|
114 |
window_history_n = max_y;
|
|
115 |
}
|
|
116 |
|
|
117 |
w_stats = newwin(stats_h, max_x, 0, 0);
|
|
118 |
scrollok(w_stats, false);
|
|
119 |
|
|
120 |
w_line1 = newwin(1, max_x, stats_h, 0);
|
|
121 |
scrollok(w_line1, false);
|
|
122 |
wnoutrefresh(w_line1);
|
|
123 |
|
|
124 |
logs_n = max_y - (stats_h + 1 + 1);
|
|
125 |
fast_n = logs_n * 11 / 20;
|
|
126 |
slow_n = logs_n - fast_n;
|
|
127 |
|
|
128 |
w_slow = newwin(slow_n, max_x, (stats_h + 1), 0);
|
|
129 |
scrollok(w_slow, true);
|
|
130 |
|
|
131 |
w_line2 = newwin(1, max_x, (stats_h + 1) + slow_n, 0);
|
|
132 |
scrollok(w_line2, false);
|
|
133 |
wnoutrefresh(w_line2);
|
|
134 |
|
|
135 |
w_fast = newwin(fast_n, max_x, (stats_h + 1) + slow_n + 1, 0);
|
|
136 |
scrollok(w_fast, true);
|
|
137 |
|
|
138 |
wattron(w_line1, A_REVERSE);
|
|
139 |
wattron(w_line2, A_REVERSE);
|
|
140 |
for(nr=0; nr<max_x; nr++)
|
|
141 |
{
|
|
142 |
wprintw(w_line1, " ");
|
|
143 |
wprintw(w_line2, " ");
|
|
144 |
}
|
|
145 |
wattroff(w_line2, A_REVERSE);
|
|
146 |
wattroff(w_line1, A_REVERSE);
|
|
147 |
|
|
148 |
wnoutrefresh(w_line1);
|
|
149 |
wnoutrefresh(w_line2);
|
|
150 |
|
|
151 |
doupdate();
|
|
152 |
|
|
153 |
signal(SIGWINCH, handler);
|
|
154 |
}
|
|
155 |
|
|
156 |
void myprintloc(WINDOW *w, int y, int x, const char *fmt, ...)
|
|
157 |
{
|
|
158 |
char *line = NULL;
|
|
159 |
int line_len = 0;
|
|
160 |
va_list ap;
|
|
161 |
|
|
162 |
va_start(ap, fmt);
|
|
163 |
line_len = vasprintf(&line, fmt, ap);
|
|
164 |
va_end(ap);
|
|
165 |
|
|
166 |
wmove(w, y, x);
|
|
167 |
|
|
168 |
if (use_colors)
|
|
169 |
{
|
|
170 |
int index = 0;
|
|
171 |
|
|
172 |
for(index=0; index<line_len; index++)
|
|
173 |
{
|
|
174 |
static int clr = C_WHITE, att = A_NORMAL;
|
|
175 |
|
|
176 |
if (line[index] == COLOR_ESCAPE[0])
|
|
177 |
{
|
|
178 |
switch(line[++index])
|
|
179 |
{
|
|
180 |
case '1':
|
|
181 |
clr = C_RED;
|
|
182 |
break;
|
|
183 |
case '2':
|
|
184 |
clr = C_BLUE;
|
|
185 |
break;
|
|
186 |
case '3':
|
|
187 |
clr = C_GREEN;
|
|
188 |
break;
|
|
189 |
case '4':
|
|
190 |
clr = C_YELLOW;
|
|
191 |
break;
|
|
192 |
case '5':
|
|
193 |
clr = C_MAGENTA;
|
|
194 |
break;
|
|
195 |
case '6':
|
|
196 |
clr = C_CYAN;
|
|
197 |
break;
|
|
198 |
case '7':
|
|
199 |
clr = C_WHITE;
|
|
200 |
break;
|
|
201 |
case '8':
|
|
202 |
att = A_BOLD;
|
|
203 |
break;
|
|
204 |
case '9':
|
|
205 |
att = A_NORMAL;
|
|
206 |
break;
|
|
207 |
}
|
|
208 |
|
|
209 |
wattr_set(w, att, clr, NULL);
|
|
210 |
}
|
|
211 |
else
|
|
212 |
{
|
|
213 |
waddch(w, line[index]);
|
|
214 |
}
|
|
215 |
}
|
|
216 |
}
|
|
217 |
else
|
|
218 |
{
|
|
219 |
mvwprintw(w, y, x, "%s", line);
|
|
220 |
}
|
|
221 |
|
|
222 |
free(line);
|
|
223 |
}
|
|
224 |
|
|
225 |
void myprint(WINDOW *w, const char *what)
|
|
226 |
{
|
|
227 |
if (use_colors)
|
|
228 |
{
|
|
229 |
int y = -1, x = -1;
|
|
230 |
|
|
231 |
getyx(w, y, x);
|
|
232 |
|
|
233 |
myprintloc(w, y, x, what);
|
|
234 |
}
|
|
235 |
else
|
|
236 |
{
|
|
237 |
wprintw(w, what);
|
|
238 |
}
|
|
239 |
}
|
|
240 |
|
|
241 |
void recreate_terminal(void)
|
|
242 |
{
|
|
243 |
int index = 0;
|
|
244 |
|
|
245 |
determine_terminal_size(&max_y, &max_x);
|
|
246 |
|
|
247 |
resizeterm(max_y, max_x);
|
|
248 |
|
|
249 |
endwin();
|
|
250 |
refresh();
|
|
251 |
|
|
252 |
create_windows();
|
|
253 |
|
|
254 |
for(index = window_history_n - 1; index >= 0; index--)
|
|
255 |
{
|
|
256 |
if (slow_history[index])
|
|
257 |
myprint(w_slow, slow_history[index]);
|
|
258 |
if (fast_history[index])
|
|
259 |
myprint(w_fast, fast_history[index]);
|
|
260 |
}
|
|
261 |
|
|
262 |
doupdate();
|
|
263 |
|
|
264 |
win_resize = 0;
|
|
265 |
}
|
|
266 |
|
|
267 |
void init_ncurses_ui(double graph_limit_in, double hz_in, char use_colors_in)
|
|
268 |
{
|
|
269 |
graph_limit = graph_limit_in;
|
|
270 |
hz = hz_in;
|
|
271 |
use_colors = use_colors_in;
|
|
272 |
|
|
273 |
initscr();
|
|
274 |
start_color();
|
|
275 |
keypad(stdscr, TRUE);
|
|
276 |
intrflush(stdscr, FALSE);
|
|
277 |
noecho();
|
|
278 |
refresh();
|
|
279 |
nodelay(stdscr, FALSE);
|
|
280 |
meta(stdscr, TRUE); /* enable 8-bit input */
|
|
281 |
idlok(stdscr, TRUE); /* may give a little clunky screenredraw */
|
|
282 |
idcok(stdscr, TRUE); /* may give a little clunky screenredraw */
|
|
283 |
leaveok(stdscr, FALSE);
|
|
284 |
|
|
285 |
init_pair(C_WHITE, COLOR_WHITE, COLOR_BLACK);
|
|
286 |
init_pair(C_CYAN, COLOR_CYAN, COLOR_BLACK);
|
|
287 |
init_pair(C_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
|
|
288 |
init_pair(C_BLUE, COLOR_BLUE, COLOR_BLACK);
|
|
289 |
init_pair(C_YELLOW, COLOR_YELLOW, COLOR_BLACK);
|
|
290 |
init_pair(C_GREEN, COLOR_GREEN, COLOR_BLACK);
|
|
291 |
init_pair(C_RED, COLOR_RED, COLOR_BLACK);
|
|
292 |
|
|
293 |
kalman_init(0.0);
|
|
294 |
|
|
295 |
recreate_terminal();
|
|
296 |
}
|
|
297 |
|
|
298 |
void end_ncurses(void)
|
|
299 |
{
|
|
300 |
int index = 0;
|
|
301 |
|
|
302 |
for(index=0; index<window_history_n; index++)
|
|
303 |
{
|
|
304 |
free(slow_history[index]);
|
|
305 |
free(fast_history[index]);
|
|
306 |
}
|
|
307 |
|
|
308 |
free(slow_history);
|
|
309 |
free(fast_history);
|
|
310 |
|
|
311 |
if (w_stats)
|
|
312 |
{
|
|
313 |
delwin(w_stats);
|
|
314 |
delwin(w_line1);
|
|
315 |
delwin(w_slow);
|
|
316 |
delwin(w_line2);
|
|
317 |
delwin(w_fast);
|
|
318 |
}
|
|
319 |
|
|
320 |
endwin();
|
|
321 |
|
|
322 |
free(history);
|
|
323 |
free(history_set);
|
|
324 |
|
|
325 |
#ifdef FW
|
|
326 |
fft_free();
|
|
327 |
fft_stop();
|
|
328 |
#endif
|
|
329 |
free(history_temp);
|
|
330 |
free(history_fft_phase);
|
|
331 |
free(history_fft_magn);
|
|
332 |
}
|
|
333 |
|
|
334 |
void fast_log(const char *fmt, ...)
|
|
335 |
{
|
|
336 |
char buffer[4096] = { 0 };
|
|
337 |
va_list ap;
|
|
338 |
|
|
339 |
va_start(ap, fmt);
|
|
340 |
vsnprintf(buffer, sizeof buffer, fmt, ap);
|
|
341 |
va_end(ap);
|
|
342 |
|
|
343 |
free(fast_history[window_history_n - 1]);
|
|
344 |
memmove(&fast_history[1], &fast_history[0], (window_history_n - 1) * sizeof(char *));
|
|
345 |
fast_history[0] = strdup(buffer);
|
|
346 |
|
|
347 |
myprint(w_fast, buffer);
|
|
348 |
|
|
349 |
if (win_resize)
|
|
350 |
recreate_terminal();
|
|
351 |
}
|
|
352 |
|
|
353 |
void slow_log(const char *fmt, ...)
|
|
354 |
{
|
|
355 |
char buffer[4096] = { 0 };
|
|
356 |
va_list ap;
|
|
357 |
|
|
358 |
va_start(ap, fmt);
|
|
359 |
vsnprintf(buffer, sizeof buffer, fmt, ap);
|
|
360 |
va_end(ap);
|
|
361 |
|
|
362 |
free(slow_history[window_history_n - 1]);
|
|
363 |
memmove(&slow_history[1], &slow_history[0], (window_history_n - 1) * sizeof(char *));
|
|
364 |
slow_history[0] = strdup(buffer);
|
|
365 |
|
|
366 |
myprint(w_slow, buffer);
|
|
367 |
|
|
368 |
if (win_resize)
|
|
369 |
recreate_terminal();
|
|
370 |
}
|
|
371 |
|
|
372 |
void my_beep(void)
|
|
373 |
{
|
|
374 |
beep();
|
|
375 |
}
|
|
376 |
|
|
377 |
void status_line(char *fmt, ...)
|
|
378 |
{
|
|
379 |
char *line = NULL;
|
|
380 |
va_list ap;
|
|
381 |
|
|
382 |
wattron(w_line2, A_REVERSE);
|
|
383 |
|
|
384 |
wmove(w_line2, 0, 0);
|
|
385 |
|
|
386 |
va_start(ap, fmt);
|
|
387 |
|
|
388 |
vasprintf(&line, fmt, ap);
|
|
389 |
myprint(w_line2, line);
|
|
390 |
free(line);
|
|
391 |
|
|
392 |
va_end(ap);
|
|
393 |
|
|
394 |
wattroff(w_line2, A_REVERSE);
|
|
395 |
|
|
396 |
wnoutrefresh(w_line2);
|
|
397 |
|
|
398 |
if (win_resize)
|
|
399 |
recreate_terminal();
|
|
400 |
}
|
|
401 |
|
|
402 |
void draw_column(WINDOW *win, int x, int height, char overflow, char limitter)
|
|
403 |
{
|
|
404 |
void *dummy = NULL;
|
|
405 |
int y = 0, end_y = 0, win_h = 0, win_w = 0;
|
|
406 |
|
|
407 |
getmaxyx(win, win_h, win_w);
|
|
408 |
(void)win_w; /* silence warnings */
|
|
409 |
|
|
410 |
end_y = max(0, win_h - height);
|
|
411 |
|
|
412 |
for(y=win_h - 1; y >= end_y; y--)
|
|
413 |
mvwchgat(win, y, x, 1, A_REVERSE, C_YELLOW, dummy);
|
|
414 |
|
|
415 |
if (limitter)
|
|
416 |
mvwchgat(win, 0, x, 1, A_REVERSE, C_BLUE, dummy);
|
|
417 |
else if (overflow)
|
|
418 |
mvwchgat(win, 0, x, 1, A_REVERSE, C_RED, dummy);
|
|
419 |
else if (height == 0)
|
|
420 |
mvwchgat(win, win_h - 1, x, 1, A_REVERSE, C_GREEN, dummy);
|
|
421 |
}
|
|
422 |
|
|
423 |
void draw_rad_column(WINDOW *win, int x, double val)
|
|
424 |
{
|
|
425 |
void *dummy = NULL;
|
|
426 |
int y = 0, end_y = 0, win_h = 0, win_w = 0;
|
|
427 |
int center_y = 0;
|
|
428 |
|
|
429 |
getmaxyx(win, win_h, win_w);
|
|
430 |
(void)win_w; /* silence warnings */
|
|
431 |
|
|
432 |
center_y = win_h / 2;
|
|
433 |
end_y = (int)((double)(win_h / 2) * ((val / PI) + 1.0));
|
|
434 |
|
|
435 |
if (end_y > center_y)
|
|
436 |
{
|
|
437 |
for(y=center_y; y<end_y; y++)
|
|
438 |
mvwchgat(win, y, x, 1, A_REVERSE, C_YELLOW, dummy);
|
|
439 |
}
|
|
440 |
else
|
|
441 |
{
|
|
442 |
for(y=end_y; y<center_y; y++)
|
|
443 |
mvwchgat(win, y, x, 1, A_REVERSE, C_YELLOW, dummy);
|
|
444 |
}
|
|
445 |
}
|
|
446 |
|
|
447 |
double get_cur_scc()
|
|
448 |
{
|
|
449 |
double scc_val = 0.0;
|
|
450 |
double prev_val = 0.0, u0 = 0.0;
|
|
451 |
double t[3] = { 0 };
|
|
452 |
int loop = 0, n = 0;
|
|
453 |
char first = 1;
|
|
454 |
|
|
455 |
t[0] = t[1] = t[2] = 0.0;
|
|
456 |
|
|
457 |
for(loop=0; loop<history_n; loop++)
|
|
458 |
{
|
|
459 |
double cur_val = history[loop];
|
|
460 |
|
|
461 |
if (!history_set[loop])
|
|
462 |
continue;
|
|
463 |
|
|
464 |
if (first)
|
|
465 |
{
|
|
466 |
prev_val = 0;
|
|
467 |
u0 = cur_val;
|
|
468 |
first = 0;
|
|
469 |
}
|
|
470 |
else
|
|
471 |
{
|
|
472 |
t[0] += prev_val * cur_val;
|
|
473 |
}
|
|
474 |
|
|
475 |
t[1] = t[1] + cur_val;
|
|
476 |
t[2] = t[2] + (cur_val * cur_val);
|
|
477 |
prev_val = cur_val;
|
|
478 |
|
|
479 |
n++;
|
|
480 |
}
|
|
481 |
|
|
482 |
t[0] = t[0] + prev_val * u0;
|
|
483 |
t[1] = t[1] * t[1];
|
|
484 |
|
|
485 |
scc_val = (double)n * t[2] - t[1];
|
|
486 |
|
|
487 |
if (scc_val == 0.0)
|
|
488 |
return -1.0;
|
|
489 |
|
|
490 |
return ((double)n * t[0] - t[1]) / scc_val;
|
|
491 |
}
|
|
492 |
|
|
493 |
#ifdef FW
|
|
494 |
void draw_fft(void)
|
|
495 |
{
|
|
496 |
double mx_mag = 0.0;
|
|
497 |
int index = 0, highest = 0;
|
|
498 |
int cx = 0, cy = 0;
|
|
499 |
/* double max_freq = hz / 2.0; */
|
|
500 |
double highest_freq = 0, avg_freq_index = 0.0, total_val = 0.0, avg_freq = 0.0;
|
|
501 |
int dummy = 0;
|
|
502 |
|
|
503 |
getyx(w_slow, cy, cx);
|
|
504 |
|
|
505 |
for(index=0; index<max_x; index++)
|
|
506 |
{
|
|
507 |
double val = 0.0;
|
|
508 |
|
|
509 |
if (history_set[index])
|
|
510 |
val = history[index];
|
|
511 |
else
|
|
512 |
val = index > 0 ? history[index - 1] : 0;
|
|
513 |
|
|
514 |
if (val > graph_limit)
|
|
515 |
val = graph_limit;
|
|
516 |
|
|
517 |
history_temp[index] = val;
|
|
518 |
}
|
|
519 |
|
|
520 |
fft_do(history_temp, history_fft_magn, history_fft_phase);
|
|
521 |
|
|
522 |
for(index=1; index<max_x/2; index++)
|
|
523 |
{
|
|
524 |
avg_freq_index += (double)index * history_fft_magn[index];
|
|
525 |
total_val += history_fft_magn[index];
|
|
526 |
|
|
527 |
if (history_fft_magn[index] > mx_mag)
|
|
528 |
{
|
|
529 |
mx_mag = history_fft_magn[index];
|
|
530 |
highest = index;
|
|
531 |
}
|
|
532 |
}
|
|
533 |
|
|
534 |
highest_freq = (hz / (double)max_x) * (double)highest;
|
|
535 |
|
|
536 |
avg_freq_index /= total_val;
|
|
537 |
avg_freq = (hz / (double)max_x) * avg_freq_index;
|
|
538 |
|
|
539 |
wattron(w_line1, A_REVERSE);
|
|
540 |
myprintloc(w_line1, 0, 38, gettext("highest: %6.2fHz, avg: %6.2fHz"), highest_freq, avg_freq);
|
|
541 |
wattroff(w_line1, A_REVERSE);
|
|
542 |
wnoutrefresh(w_line1);
|
|
543 |
|
|
544 |
dummy = max_x / 2 + 1;
|
|
545 |
|
|
546 |
if (draw_phase)
|
|
547 |
{
|
|
548 |
int y = 0;
|
|
549 |
|
|
550 |
for(y=0; y<slow_n; y++)
|
|
551 |
mvwchgat(w_slow, y, dummy, 1, A_REVERSE, C_WHITE, NULL);
|
|
552 |
|
|
553 |
for(index=0; index<slow_n; index++)
|
|
554 |
mvwchgat(w_slow, index, 0, max_x, A_NORMAL, C_WHITE, NULL);
|
|
555 |
|
|
556 |
for(index=1; index<dummy - 1; index++)
|
|
557 |
draw_rad_column(w_slow, index - 1, history_fft_phase[index]);
|
|
558 |
}
|
|
559 |
else
|
|
560 |
{
|
|
561 |
for(index=0; index<slow_n; index++)
|
|
562 |
mvwchgat(w_slow, index, max_x / 2, max_x / 2, A_NORMAL, C_WHITE, NULL);
|
|
563 |
}
|
|
564 |
|
|
565 |
for(index=1; index<dummy; index++)
|
|
566 |
{
|
|
567 |
int height_magn = (int)((double)slow_n * (history_fft_magn[index] / mx_mag));
|
|
568 |
draw_column(w_slow, max_x / 2 + index - 1, height_magn, 0, 0);
|
|
569 |
}
|
|
570 |
|
|
571 |
wmove(w_slow, cy, cx);
|
|
572 |
|
|
573 |
wnoutrefresh(w_slow);
|
|
574 |
}
|
|
575 |
#endif
|
|
576 |
|
|
577 |
double calc_trend()
|
|
578 |
{
|
|
579 |
int half = history_n / 2, index = 0;
|
|
580 |
double v1 = 0.0, v2 = 0.0;
|
|
581 |
int n_v1 = 0, n_v2 = 0;
|
|
582 |
|
|
583 |
for(index=0; index<half; index++)
|
|
584 |
{
|
|
585 |
if (!history_set[index])
|
|
586 |
continue;
|
|
587 |
|
|
588 |
v1 += history[index];
|
|
589 |
n_v1++;
|
|
590 |
}
|
|
591 |
|
|
592 |
for(index=half; index<history_n; index++)
|
|
593 |
{
|
|
594 |
if (!history_set[index])
|
|
595 |
continue;
|
|
596 |
|
|
597 |
v2 += history[index];
|
|
598 |
n_v2++;
|
|
599 |
}
|
|
600 |
|
|
601 |
if (n_v2 == 0 || n_v1 == 0)
|
|
602 |
return 0;
|
|
603 |
|
|
604 |
v1 /= (double)n_v1;
|
|
605 |
v2 /= (double)n_v2;
|
|
606 |
|
|
607 |
return (v1 - v2) / (v2 / 100.0);
|
|
608 |
}
|
|
609 |
|
|
610 |
void draw_graph(double val)
|
|
611 |
{
|
|
612 |
int index = 0, loop_n = min(max_x, history_n), n = 0, n2 = 0;
|
|
613 |
double avg = 0, sd = 0;
|
|
614 |
double avg2 = 0, sd2 = 0;
|
|
615 |
double mi = MY_DOUBLE_INF, ma = -MY_DOUBLE_INF, diff = 0.0;
|
|
616 |
|
|
617 |
for(index=0; index<loop_n; index++)
|
|
618 |
{
|
|
619 |
double val = history[index];
|
|
620 |
|
|
621 |
if (!history_set[index])
|
|
622 |
continue;
|
|
623 |
|
|
624 |
mi = min(val, mi);
|
|
625 |
ma = max(val, ma);
|
|
626 |
|
|
627 |
avg += val;
|
|
628 |
sd += val * val;
|
|
629 |
n++;
|
|
630 |
}
|
|
631 |
|
|
632 |
if (!n)
|
|
633 |
return;
|
|
634 |
|
|
635 |
avg /= (double)n;
|
|
636 |
sd = sqrt((sd / (double)n) - pow(avg, 2.0));
|
|
637 |
|
|
638 |
mi = max(mi, avg - sd);
|
|
639 |
ma = min(ma, avg + sd);
|
|
640 |
|
|
641 |
for(index=0; index<loop_n; index++)
|
|
642 |
{
|
|
643 |
double val = history[index];
|
|
644 |
|
|
645 |
if (!history_set[index])
|
|
646 |
continue;
|
|
647 |
|
|
648 |
if (val < mi || val > ma)
|
|
649 |
continue;
|
|
650 |
|
|
651 |
avg2 += val;
|
|
652 |
sd2 += val * val;
|
|
653 |
n2++;
|
|
654 |
}
|
|
655 |
|
|
656 |
if (n2)
|
|
657 |
{
|
|
658 |
avg2 /= (double)n2;
|
|
659 |
sd2 = sqrt((sd2 / (double)n2) - pow(avg2, 2.0));
|
|
660 |
|
|
661 |
mi = max(mi, avg2 - sd2);
|
|
662 |
ma = min(ma, avg2 + sd2);
|
|
663 |
diff = ma - mi;
|
|
664 |
|
|
665 |
if (diff == 0.0)
|
|
666 |
diff = 1.0;
|
|
667 |
|
|
668 |
wattron(w_line1, A_REVERSE);
|
|
669 |
myprintloc(w_line1, 0, 0, gettext("graph range: %7.2fms - %7.2fms "), mi, ma);
|
|
670 |
wattroff(w_line1, A_REVERSE);
|
|
671 |
wnoutrefresh(w_line1);
|
|
672 |
|
|
673 |
/* fprintf(stderr, "%d| %f %f %f %f\n", h_stats.n, mi, avg, ma, sd); */
|
|
674 |
|
|
675 |
for(index=0; index<loop_n; index++)
|
|
676 |
{
|
|
677 |
char overflow = 0, limitter = 0;
|
|
678 |
double val = 0, height = 0;
|
|
679 |
int i_h = 0, x = max_x - (1 + index);
|
|
680 |
|
|
681 |
if (!history_set[index])
|
|
682 |
{
|
|
683 |
mvwchgat(w_stats, stats_h - 1, x, 1, A_REVERSE, C_CYAN, NULL);
|
|
684 |
continue;
|
|
685 |
}
|
|
686 |
|
|
687 |
if (history[index] < graph_limit)
|
|
688 |
val = history[index];
|
|
689 |
else
|
|
690 |
{
|
|
691 |
val = graph_limit;
|
|
692 |
limitter = 1;
|
|
693 |
}
|
|
694 |
|
|
695 |
height = (val - mi) / diff;
|
|
696 |
|
|
697 |
if (height > 1.0)
|
|
698 |
{
|
|
699 |
height = 1.0;
|
|
700 |
overflow = 1;
|
|
701 |
}
|
|
702 |
|
|
703 |
i_h = (int)(height * stats_h);
|
|
704 |
/* fprintf(stderr, "%d %f %f %d %d\n", index, history[index], height, i_h, overflow); */
|
|
705 |
|
|
706 |
draw_column(w_stats, x, i_h, overflow, limitter);
|
|
707 |
}
|
|
708 |
}
|
|
709 |
}
|
|
710 |
|
|
711 |
void show_stats_t(int y, int x, char *header, stats_t *data, char abbreviate)
|
|
712 |
{
|
|
713 |
if (data -> valid)
|
|
714 |
{
|
|
715 |
char *cur_str = format_value(data -> cur, 6, 2, abbreviate);
|
|
716 |
char *min_str = format_value(data -> min, 6, 2, abbreviate);
|
|
717 |
char *avg_str = format_value(data -> avg / (double)data -> n, 6, 2, abbreviate);
|
|
718 |
char *max_str = format_value(data -> max, 6, 2, abbreviate);
|
|
719 |
char *sd_str = format_value(calc_sd(data), 6, 2, abbreviate);
|
|
720 |
|
|
721 |
myprintloc(w_stats, y, x, "%s: %s %s %s %s %s", header,
|
|
722 |
data -> cur_valid ? cur_str : gettext(" n/a"),
|
|
723 |
min_str, avg_str, max_str, sd_str);
|
|
724 |
|
|
725 |
free(sd_str);
|
|
726 |
free(max_str);
|
|
727 |
free(avg_str);
|
|
728 |
free(min_str);
|
|
729 |
free(cur_str);
|
|
730 |
}
|
|
731 |
else
|
|
732 |
{
|
|
733 |
myprintloc(w_stats, y, x, gettext("%s: n/a"), header);
|
|
734 |
}
|
|
735 |
}
|
|
736 |
|
|
737 |
void update_stats(stats_t *resolve, stats_t *connect, stats_t *request, stats_t *total, stats_t *ssl_setup, int n_ok, int n_fail, const char *last_connect_str, const char *fp, char use_tfo, char dg, stats_t *st_to, stats_t *tcp_rtt_stats, int re_tx, int pmtu, int tos, stats_t *close_st, stats_t *t_write, int n_cookies, char abbreviate, stats_t *stats_header_size)
|
|
738 |
{
|
|
739 |
double k = 0.0;
|
|
740 |
char force_redraw = 0;
|
|
741 |
struct pollfd p = { 0, POLLIN, 0 };
|
|
742 |
|
|
743 |
werase(w_stats);
|
|
744 |
|
|
745 |
if (n_ok)
|
|
746 |
{
|
|
747 |
char buffer[4096] = { 0 }, *scc_str = NULL, *kalman_str = NULL;
|
|
748 |
int buflen = 0;
|
|
749 |
|
|
750 |
myprintloc(w_stats, 0, 0, " %6s %6s %6s %6s %6s", gettext("latest"), gettext("min"), gettext("avg"), gettext("max"), gettext("sd"));
|
|
751 |
show_stats_t(1, 0, gettext("resolve"), resolve, abbreviate);
|
|
752 |
show_stats_t(2, 0, gettext("connect"), connect, abbreviate);
|
|
753 |
show_stats_t(3, 0, gettext("ssl "), ssl_setup, abbreviate);
|
|
754 |
show_stats_t(4, 0, gettext("send "), t_write, abbreviate);
|
|
755 |
show_stats_t(5, 0, gettext("request"), request, abbreviate);
|
|
756 |
show_stats_t(6, 0, gettext("close "), close_st, abbreviate);
|
|
757 |
show_stats_t(7, 0, gettext("total "), total, abbreviate);
|
|
758 |
|
|
759 |
scc_str = format_value(get_cur_scc(), 5, 3, abbreviate);
|
|
760 |
kalman_str = format_value(kalman_do(total -> cur), 5, 3, abbreviate);
|
|
761 |
myprintloc(w_stats, 8, 0, gettext("ok: %3d, fail: %3d%s, scc: %s, kalman: %s"), n_ok, n_fail, use_tfo ? gettext(", with TFO") : "", scc_str, kalman_str);
|
|
762 |
free(kalman_str);
|
|
763 |
free(scc_str);
|
|
764 |
|
|
765 |
if (max_x >= 44 * 2 + 1)
|
|
766 |
{
|
|
767 |
double trend = calc_trend();
|
|
768 |
char trend_dir = ' ';
|
|
769 |
|
|
770 |
myprintloc(w_stats, 0, 45, " %6s %6s %6s %6s %6s", gettext("cur"), gettext("min"), gettext("avg"), gettext("max"), gettext("sd"));
|
|
771 |
show_stats_t(1, 45, gettext("t offst"), st_to, abbreviate);
|
|
772 |
|
|
773 |
#if defined(linux) || defined(__FreeBSD__)
|
|
774 |
show_stats_t(2, 45, gettext("tcp rtt"), tcp_rtt_stats, abbreviate);
|
|
775 |
#endif
|
|
776 |
show_stats_t(3, 45, gettext("headers"), stats_header_size, abbreviate);
|
|
777 |
|
|
778 |
if (trend < 0)
|
|
779 |
trend_dir = '-';
|
|
780 |
else if (trend > 0)
|
|
781 |
trend_dir = '+';
|
|
782 |
|
|
783 |
myprintloc(w_stats, 8, 48, gettext("# cookies: %d"), n_cookies);
|
|
784 |
|
|
785 |
#ifdef linux
|
|
786 |
myprintloc(w_stats, 9, 48, gettext("trend: %c%6.2f%%, re-tx: %2d, pmtu: %5d, TOS: %02x"), trend_dir, fabs(trend), re_tx, pmtu, tos);
|
|
787 |
#else
|
|
788 |
myprintloc(w_stats, 9, 48, gettext("trend: %c%6.2f%%, TOS: %02x"), trend_dir, fabs(trend), tos);
|
|
789 |
#endif
|
|
790 |
}
|
|
791 |
|
|
792 |
buflen = snprintf(buffer, sizeof buffer, gettext("HTTP rc: %s, SSL fp: %s"), last_connect_str, fp ? fp : gettext("n/a"));
|
|
793 |
|
|
794 |
if (buflen <= max_x)
|
|
795 |
myprintloc(w_stats, 9, 0, "%s", buffer);
|
|
796 |
else
|
|
797 |
{
|
|
798 |
static char prev_sf[48] = { 0 };
|
|
799 |
|
|
800 |
myprintloc(w_stats, 9, 0, gettext("http result code: %s"), last_connect_str);
|
|
801 |
|
|
802 |
if (fp && strcmp(prev_sf, fp))
|
|
803 |
{
|
|
804 |
slow_log(gettext("\nSSL fingerprint: %s"), fp);
|
|
805 |
|
|
806 |
memcpy(prev_sf, fp, 47);
|
|
807 |
}
|
|
808 |
}
|
|
809 |
}
|
|
810 |
|
|
811 |
memmove(&history[1], &history[0], (history_n - 1) * sizeof(double));
|
|
812 |
memmove(&history_set[1], &history_set[0], (history_n - 1) * sizeof(char));
|
|
813 |
|
|
814 |
history[0]= total -> cur;
|
|
815 |
history_set[0] = 1;
|
|
816 |
|
|
817 |
if (poll(&p, 1, 0) == 1 && p.revents == POLLIN)
|
|
818 |
{
|
|
819 |
int c = getch();
|
|
820 |
|
|
821 |
if (c == 12) /* ^L */
|
|
822 |
force_redraw = 1;
|
|
823 |
|
|
824 |
if (c == 'H')
|
|
825 |
pause_graphs = !pause_graphs;
|
|
826 |
|
|
827 |
if (c == 'q')
|
|
828 |
stop = 1;
|
|
829 |
}
|
|
830 |
|
|
831 |
if (dg && !pause_graphs)
|
|
832 |
{
|
|
833 |
draw_graph(k);
|
|
834 |
#ifdef FW
|
|
835 |
draw_fft();
|
|
836 |
#endif
|
|
837 |
}
|
|
838 |
|
|
839 |
wnoutrefresh(w_stats);
|
|
840 |
|
|
841 |
if (win_resize || force_redraw)
|
|
842 |
recreate_terminal();
|
|
843 |
}
|