Code Coverage: vinyl-cache/bin/vinylstat/vinylstat_curses.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 * Author: Dag-Erling Smørgrav <des@des.no>
7
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
8
 *
9
 * SPDX-License-Identifier: BSD-2-Clause
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 *
32
 * Statistics output program
33
 */
34
35
#include "config.h"
36
37
#include <stdlib.h>
38
#include <unistd.h>
39
#include <string.h>
40
#include <stdint.h>
41
#include <math.h>
42
43
#include "vdef.h"
44
#include "vas.h"
45
#include "miniobj.h"
46
#include "vqueue.h"
47
#include "vtim.h"
48
#include "vapi/vsig.h"
49
50
#include "vinylstat.h"
51
#include "vcurses.h"
52
53 8
#define LINES_STATUS            3
54 8
#define LINES_BAR_T             1
55 8
#define LINES_BAR_B             1
56 8
#define LINES_INFO              3
57 8
#define LINES_POINTS_MIN        3
58
59 9
#define N_COL                   6
60 6245
#define COLW                    14
61 15
#define COLW_NAME_MIN           24
62
63 147
#define VALUE_MAX               999999999999
64
65 5579
#define REBUILD_NEXT            (1u << 0)
66 10
#define REBUILD_FIRST           (1u << 1)
67
68
enum kb_e {
69
#define BINDING(name, desc) KB_ ## name,
70
#define BINDING_SIG
71
#include "vinylstat_bindings.h"
72
};
73
74
struct ma {
75
        unsigned n, nmax;
76
        double acc;
77
};
78
79
struct pt {
80
        unsigned                magic;
81
#define PT_MAGIC                0x41698E4F
82
        VTAILQ_ENTRY(pt)        list;
83
84
        const struct VSC_point  *vpt;
85
86
        char                    seen;
87
88
        uint64_t                cur, last;
89
        double                  t_cur, t_last;
90
        double                  chg, avg;
91
92
        struct ma               ma_10, ma_100, ma_1000;
93
};
94
95
struct hitrate {
96
        uint64_t lhit, lmiss;
97
        struct ma hr_10;
98
        struct ma hr_100;
99
        struct ma hr_1000;
100
};
101
static struct hitrate hitrate;
102
103
static VTAILQ_HEAD(, pt) ptlist = VTAILQ_HEAD_INITIALIZER(ptlist);
104
static int n_ptlist = 0;
105
static int n_ptarray = 0;
106
static struct pt **ptarray = NULL;
107
static const volatile uint64_t *mgt_uptime;
108
static const volatile uint64_t *main_uptime;
109
static const volatile uint64_t *main_cache_hit;
110
static const volatile uint64_t *main_cache_miss;
111
112
static int l_status, l_bar_t, l_points, l_bar_b, l_info;
113
static unsigned colw_name = COLW_NAME_MIN;
114
static WINDOW *w_status = NULL;
115
static WINDOW *w_bar_t = NULL;
116
static WINDOW *w_points = NULL;
117
static WINDOW *w_bar_b = NULL;
118
static WINDOW *w_info = NULL;
119
120
static const struct VSC_level_desc *verbosity;
121
static int show_help = 0;
122
static int help_line = 0;
123
static int keep_running = 1;
124
static int hide_unseen = 1;
125
static int raw_vsc = 0;
126
static int page_start = 0;
127
static int current = 0;
128
static int rebuild = 0;
129
static int redraw = 0;
130
static int sample = 0;
131
static int reset_averages = 0;
132
static int scale = 1;
133
static double t_sample = 0.;
134
static double interval = 1.;
135
static unsigned vsm_status = 0;
136
137 2
#define NOTIF_MAXLEN 256
138
static char notification_message[NOTIF_MAXLEN] = "";
139
static vtim_mono notification_eol = 0.0;
140
141
static void
142
init_hitrate(void)
143 7
{
144 7
        memset(&hitrate, 0, sizeof (struct hitrate));
145 7
        if (main_cache_hit != NULL) {
146 7
                hitrate.lhit = *main_cache_hit;
147 7
                hitrate.lmiss = *main_cache_miss;
148 7
        }
149 7
        hitrate.hr_10.nmax = 10;
150 7
        hitrate.hr_100.nmax = 100;
151 7
        hitrate.hr_1000.nmax = 1000;
152 7
}
153
154
static void
155
update_ma(struct ma *ma, double val)
156 5847
{
157 5847
        AN(ma);
158 5847
        AN(ma->nmax);
159 5847
        if (ma->n < ma->nmax)
160 5847
                ma->n++;
161 5847
        ma->acc += (val - ma->acc) / (double)ma->n;
162 5847
}
163
164
static void
165
update_position(void)
166 76
{
167 76
        int old_current, old_page_start;
168
169 76
        old_current = current;
170 76
        old_page_start = page_start;
171
172 76
        if (n_ptarray == 0) {
173 31
                current = 0;
174 31
                page_start = 0;
175 45
        } else {
176 45
                current = vlimit_t(int, current, 0, n_ptarray - 1);
177 45
                page_start = vmin(page_start, current);
178 45
                if (current > page_start + (l_points - 1))
179 2
                        page_start = current - (l_points - 1);
180 45
                page_start = vlimit_t(int, page_start, 0, n_ptarray - 1);
181 45
        }
182
183 76
        if (current != old_current || page_start != old_page_start)
184 18
                redraw = 1;
185 76
}
186
187
static void
188
delete_pt_array(void)
189 24
{
190 24
        if (ptarray != NULL)
191 24
                free(ptarray);
192 24
        ptarray = NULL;
193 24
        n_ptarray = 0;
194
195 24
        update_position();
196 24
}
197
198
static void
199
build_pt_array(void)
200 31
{
201 31
        int i;
202 31
        struct pt *pt;
203 31
        struct pt *pt_current = NULL;
204 31
        int current_line = 0;
205
206 31
        if (current < n_ptarray) {
207 17
                pt_current = ptarray[current];
208 17
                current_line = current - page_start;
209 17
        }
210
211 31
        if (ptarray != NULL)
212 24
                delete_pt_array();
213 31
        AZ(n_ptarray);
214 31
        ptarray = calloc(n_ptlist, sizeof *ptarray);
215 31
        AN(ptarray);
216
217 11187
        VTAILQ_FOREACH(pt, &ptlist, list) {
218 11187
                CHECK_OBJ_NOTNULL(pt, PT_MAGIC);
219 11187
                if (pt->vpt->level > verbosity) {
220 6234
                        if (has_f && (rebuild & REBUILD_FIRST))
221 3
                                verbosity = VSC_ChangeLevel(verbosity,
222 3
                                    pt->vpt->level - verbosity);
223 6231
                        else
224 6231
                                continue;
225 6234
                }
226 4956
                if (!pt->seen && hide_unseen)
227 3803
                        continue;
228 1153
                assert(n_ptarray < n_ptlist);
229 1153
                ptarray[n_ptarray++] = pt;
230 1153
        }
231 31
        assert(n_ptarray <= n_ptlist);
232
233 763
        for (i = 0; pt_current != NULL && i < n_ptarray; i++)
234 749
                if (ptarray[i] == pt_current)
235 17
                        break;
236 31
        current = i;
237 31
        page_start = current - current_line;
238 31
        update_position();
239
240 31
        rebuild = 0;
241 31
        redraw = 1;
242 31
}
243
244
static void
245
sample_points(void)
246 28
{
247 28
        struct pt *pt;
248 28
        uint64_t v;
249
250 9944
        VTAILQ_FOREACH(pt, &ptlist, list) {
251 9944
                AN(pt->vpt);
252 9944
                AN(pt->vpt->ptr);
253 9944
                v = VSC_Value(pt->vpt);
254 9944
                if (v == 0 && !pt->seen)
255 7537
                        continue;
256 2407
                if (!pt->seen) {
257 632
                        pt->seen = 1;
258 632
                        rebuild = REBUILD_NEXT;
259 632
                }
260 2407
                pt->last = pt->cur;
261 2407
                pt->cur = v;
262 2407
                pt->t_last = pt->t_cur;
263 2407
                pt->t_cur = VTIM_mono();
264
265 2407
                if (reset_averages) {
266 0
                        pt->chg = 0;
267 0
                        pt->ma_10.n = 0;
268 0
                        pt->ma_100.n = 0;
269 0
                        pt->ma_1000.n = 0;
270 0
                }
271 2407
                if (pt->t_last)
272 1775
                        pt->chg = ((int64_t)pt->cur - (int64_t)pt->last) /
273 1775
                            (pt->t_cur - pt->t_last);
274
275 2407
                if (pt->vpt->semantics == 'g') {
276 644
                        pt->avg = 0.;
277 644
                        update_ma(&pt->ma_10, (int64_t)pt->cur);
278 644
                        update_ma(&pt->ma_100, (int64_t)pt->cur);
279 644
                        update_ma(&pt->ma_1000, (int64_t)pt->cur);
280 1763
                } else if (pt->vpt->semantics == 'c') {
281 1741
                        if (main_uptime != NULL && *main_uptime)
282 1423
                                pt->avg = pt->cur / (double)*main_uptime;
283 318
                        else
284 318
                                pt->avg = 0.;
285 1741
                        if (pt->t_last) {
286 1277
                                update_ma(&pt->ma_10, pt->chg);
287 1277
                                update_ma(&pt->ma_100, pt->chg);
288 1277
                                update_ma(&pt->ma_1000, pt->chg);
289 1277
                        }
290 1741
                }
291 2407
        }
292 28
}
293
294
static void
295
sample_hitrate(void)
296 28
{
297 28
        double hr, mr, ratio;
298 28
        uint64_t hit, miss;
299
300 28
        if (main_cache_hit == NULL)
301 0
                return;
302
303 28
        hit = *main_cache_hit;
304 28
        miss = *main_cache_miss;
305 28
        hr = hit - hitrate.lhit;
306 28
        mr = miss - hitrate.lmiss;
307 28
        hitrate.lhit = hit;
308 28
        hitrate.lmiss = miss;
309
310 28
        if (hr + mr != 0)
311 3
                ratio = hr / (hr + mr);
312 25
        else
313 25
                ratio = 0;
314 28
        if (reset_averages) {
315 0
                hitrate.hr_10.n = 0;
316 0
                hitrate.hr_100.n = 0;
317 0
                hitrate.hr_1000.n = 0;
318 0
        }
319 28
        update_ma(&hitrate.hr_10, ratio);
320 28
        update_ma(&hitrate.hr_100, ratio);
321 28
        update_ma(&hitrate.hr_1000, ratio);
322 28
}
323
324
static void
325
sample_data(void)
326 28
{
327 28
        t_sample = VTIM_mono();
328 28
        sample = 0;
329 28
        redraw = 1;
330 28
        sample_points();
331 28
        sample_hitrate();
332 28
        reset_averages = 0;
333 28
}
334
335
static void
336
destroy_window(WINDOW **w)
337 40
{
338
339 40
        AN(w);
340 40
        if (*w == NULL)
341 35
                return;
342 5
        AC(delwin(*w));
343 5
        *w = NULL;
344 5
}
345
346
static void
347
make_windows(void)
348 8
{
349 8
        int Y, X;
350 8
        int y;
351 8
        int y_status, y_bar_t, y_points, y_bar_b, y_info;
352
353 8
        destroy_window(&w_status);
354 8
        destroy_window(&w_bar_t);
355 8
        destroy_window(&w_points);
356 8
        destroy_window(&w_bar_b);
357 8
        destroy_window(&w_info);
358
359 8
        Y = LINES;
360 8
        X = COLS;
361
362 8
        l_status = LINES_STATUS;
363 8
        l_bar_t = LINES_BAR_T;
364 8
        l_bar_b = LINES_BAR_B;
365 8
        l_info = LINES_INFO;
366 8
        l_points = Y - (l_status + l_bar_t + l_bar_b + l_info);
367 8
        if (l_points < LINES_POINTS_MIN) {
368 0
                l_points += l_info;
369 0
                l_info = 0;
370 0
        }
371 8
        l_points = vmax(l_points, LINES_POINTS_MIN);
372
373 8
        y = 0;
374 8
        y_status = y;
375 8
        y += l_status;
376 8
        y_bar_t = y;
377 8
        y += l_bar_t;
378 8
        y_points = y;
379 8
        y += l_points;
380 8
        y_bar_b = y;
381 8
        y += l_bar_b;
382 8
        y_info = y;
383 8
        y += l_info;
384 8
        assert(y >= Y);
385
386 8
        w_status = newwin(l_status, X, y_status, 0);
387 8
        AN(w_status);
388 8
        AC(nodelay(w_status, 1));
389 8
        AC(keypad(w_status, 1));
390 8
        AC(wnoutrefresh(w_status));
391
392 8
        w_bar_t = newwin(l_bar_t, X, y_bar_t, 0);
393 8
        AN(w_bar_t);
394 8
        wbkgd(w_bar_t, A_REVERSE);
395 8
        AC(wnoutrefresh(w_bar_t));
396
397 8
        w_points = newwin(l_points, X, y_points, 0);
398 8
        AN(w_points);
399 8
        AC(wnoutrefresh(w_points));
400
401 8
        w_bar_b = newwin(l_bar_b, X, y_bar_b, 0);
402 8
        AN(w_bar_b);
403 8
        wbkgd(w_bar_b, A_REVERSE);
404 8
        AC(wnoutrefresh(w_bar_b));
405
406 8
        if (l_info) {
407 8
                w_info = newwin(l_info, X, y_info, 0);
408 8
                AN(w_info);
409 8
                AC(wnoutrefresh(w_info));
410 8
        }
411
412 8
        if (X - COLW_NAME_MIN > N_COL * COLW)
413 1
                colw_name = X - (N_COL * COLW);
414 7
        else
415 7
                colw_name = COLW_NAME_MIN;
416
417 8
        redraw = 1;
418 8
}
419
420
static void
421
print_duration(WINDOW *w, uint64_t t)
422 106
{
423
424 106
        IC(wprintw(w, "%4ju+%02ju:%02ju:%02ju",
425 106
            (uintmax_t)t / 86400, (uintmax_t)(t % 86400) / 3600,
426 106
            (uintmax_t)(t % 3600) / 60, (uintmax_t)t % 60));
427 106
}
428
429
static void
430
running(WINDOW *w, uint64_t up, int flg)
431 122
{
432 122
        if (vsm_status & flg) {
433 104
                print_duration(w_status, up);
434 104
        } else {
435 18
                wattron(w, A_STANDOUT);
436 18
                IC(wprintw(w, "  Not Running"));
437 18
                wattroff(w, A_STANDOUT);
438 18
        }
439 122
}
440
441
static void
442
draw_status(void)
443 61
{
444 61
        uint64_t up_mgt = 0;
445 61
        uint64_t up_chld = 0;
446
447 61
        AN(w_status);
448
449 61
        AC(werase(w_status));
450
451 61
        if (mgt_uptime != NULL)
452 61
                up_mgt = *mgt_uptime;
453 61
        if (main_uptime != NULL)
454 61
                up_chld = *main_uptime;
455
456 61
        IC(mvwprintw(w_status, 0, 0, "Uptime mgt:   "));
457 61
        running(w_status, up_mgt, VSM_MGT_RUNNING);
458 61
        IC(mvwprintw(w_status, 1, 0, "Uptime child: "));
459 61
        running(w_status, up_chld, VSM_WRK_RUNNING);
460 61
        IC(mvwprintw(w_status, 2, 0, "Press <h> to toggle help screen"));
461
462 61
        if (VTIM_mono() < notification_eol)
463 61
                mvwaddstr(w_status, 2, 0, notification_message);
464
465 61
        if (COLS > 70) {
466 61
                IC(mvwprintw(w_status, 0, getmaxx(w_status) - 37,
467 61
                    "Hitrate n: %8u %8u %8u", hitrate.hr_10.n, hitrate.hr_100.n,
468 61
                    hitrate.hr_1000.n));
469 61
                IC(mvwprintw(w_status, 1, getmaxx(w_status) - 37,
470 61
                    "   avg(n): %8.4f %8.4f %8.4f", hitrate.hr_10.acc,
471 61
                    hitrate.hr_100.acc, hitrate.hr_1000.acc));
472 61
        }
473
474 61
        AC(wnoutrefresh(w_status));
475 61
}
476
477
static void
478
draw_bar_t(void)
479 53
{
480 53
        int X, x;
481 53
        enum {
482 53
                COL_CUR,
483 53
                COL_CHG,
484 53
                COL_AVG,
485 53
                COL_MA10,
486 53
                COL_MA100,
487 53
                COL_MA1000,
488 53
                COL_LAST
489 53
        } col;
490
491 53
        AN(w_bar_t);
492
493 53
        X = getmaxx(w_bar_t);
494 53
        x = 0;
495 53
        AC(werase(w_bar_t));
496 53
        if (page_start > 0)
497 37
                IC(mvwprintw(w_bar_t, 0, x, "^^^"));
498 53
        x += 4;
499 53
        IC(mvwprintw(w_bar_t, 0, x, "%.*s", colw_name - 4, "NAME"));
500 53
        x += colw_name - 4;
501 53
        col = COL_CUR;
502 279
        while (col < COL_LAST) {
503 272
                if (X - x < COLW)
504 46
                        break;
505 226
                switch (col) {
506 53
                case COL_CUR:
507 53
                        IC(mvwprintw(w_bar_t, 0, x, " %12.12s", "CURRENT"));
508 53
                        break;
509 53
                case COL_CHG:
510 53
                        IC(mvwprintw(w_bar_t, 0, x, " %12.12s", "CHANGE"));
511 53
                        break;
512 53
                case COL_AVG:
513 53
                        IC(mvwprintw(w_bar_t, 0, x, " %12.12s", "AVERAGE"));
514 53
                        break;
515 53
                case COL_MA10:
516 53
                        IC(mvwprintw(w_bar_t, 0, x, " %12.12s", "AVG_10"));
517 53
                        break;
518 7
                case COL_MA100:
519 7
                        IC(mvwprintw(w_bar_t, 0, x, " %12.12s", "AVG_100"));
520 7
                        break;
521 7
                case COL_MA1000:
522 7
                        IC(mvwprintw(w_bar_t, 0, x, " %12.12s", "AVG_1000"));
523 7
                        break;
524 0
                default:
525 0
                        break;
526 226
                }
527 226
                x += COLW;
528 226
                col++;
529 226
        }
530
531 53
        AC(wnoutrefresh(w_bar_t));
532 53
}
533
534
static void
535
draw_line_default(WINDOW *w, int y, int x, int X, const struct pt *pt)
536 426
{
537 426
        enum {
538 426
                COL_CUR,
539 426
                COL_CHG,
540 426
                COL_AVG,
541 426
                COL_MA10,
542 426
                COL_MA100,
543 426
                COL_MA1000,
544 426
                COL_LAST
545 426
        } col;
546
547 426
        AN(w);
548 426
        AN(pt);
549
550 426
        col = COL_CUR;
551 2298
        while (col < COL_LAST) {
552 2214
                if (X - x < COLW)
553 342
                        break;
554 1872
                switch (col) {
555 426
                case COL_CUR:
556 426
                        IC(mvwprintw(w, y, x, " %12ju", (uintmax_t)pt->cur));
557 426
                        break;
558 426
                case COL_CHG:
559 426
                        if (pt->t_last)
560 300
                                IC(mvwprintw(w, y, x, " %12.2f", pt->chg));
561 126
                        else
562 126
                                IC(mvwprintw(w, y, x, " %12s", ".  "));
563 426
                        break;
564 426
                case COL_AVG:
565 426
                        if (pt->avg)
566 140
                                IC(mvwprintw(w, y, x, " %12.2f", pt->avg));
567 286
                        else
568 286
                                IC(mvwprintw(w, y, x, " %12s", ".  "));
569 426
                        break;
570 426
                case COL_MA10:
571 426
                        IC(mvwprintw(w, y, x, " %12.2f", pt->ma_10.acc));
572 426
                        break;
573 84
                case COL_MA100:
574 84
                        IC(mvwprintw(w, y, x, " %12.2f", pt->ma_100.acc));
575 84
                        break;
576 84
                case COL_MA1000:
577 84
                        IC(mvwprintw(w, y, x, " %12.2f", pt->ma_1000.acc));
578 84
                        break;
579 0
                default:
580 0
                        break;
581 1872
                }
582 1872
                x += COLW;
583 1872
                col++;
584 1872
        }
585 426
}
586
587
static double
588
scale_bytes(double val, char *q)
589 265
{
590 265
        const char *p;
591
592 332
        for (p = " KMGTPEZY"; *p; p++) {
593 332
                if (fabs(val) < 1024.)
594 265
                        break;
595 67
                val /= 1024.;
596 67
        }
597 265
        *q = *p;
598 265
        return (val);
599 265
}
600
601
static void
602
print_bytes(WINDOW *w, double val)
603 399
{
604 399
        char q = ' ';
605
606 399
        if (scale)
607 265
                val = scale_bytes(val, &q);
608 399
        IC(wprintw(w, " %12.2f%c", val, q));
609 399
}
610
611
static void
612
print_trunc(WINDOW *w, uintmax_t val)
613 147
{
614 147
        if (val > VALUE_MAX) {
615 0
                while (val > VALUE_MAX)
616 0
                        val /= 1000;
617 0
                IC(wprintw(w, " %9ju...", val));
618 0
        } else
619 147
                IC(wprintw(w, " %12ju", val));
620 147
}
621
622
static void
623
draw_line_bytes(WINDOW *w, int y, int x, int X, const struct pt *pt)
624 166
{
625 166
        enum {
626 166
                COL_CUR,
627 166
                COL_CHG,
628 166
                COL_AVG,
629 166
                COL_MA10,
630 166
                COL_MA100,
631 166
                COL_MA1000,
632 166
                COL_LAST
633 166
        } col;
634
635 166
        AN(w);
636 166
        AN(pt);
637
638 166
        col = COL_CUR;
639 874
        while (col < COL_LAST) {
640 852
                if (X - x < COLW)
641 144
                        break;
642 708
                wmove(w, y, x);
643 708
                switch (col) {
644 166
                case COL_CUR:
645 166
                        if (scale && pt->cur > 1024)
646 19
                                print_bytes(w, (double)pt->cur);
647 147
                        else
648 147
                                print_trunc(w, (uintmax_t)pt->cur);
649 166
                        break;
650 166
                case COL_CHG:
651 166
                        if (pt->t_last)
652 98
                                print_bytes(w, pt->chg);
653 68
                        else
654 68
                                IC(wprintw(w, " %12s", ".  "));
655 166
                        break;
656 166
                case COL_AVG:
657 166
                        if (pt->avg)
658 72
                                print_bytes(w, pt->avg);
659 94
                        else
660 94
                                IC(wprintw(w, " %12s", ".  "));
661 166
                        break;
662 166
                case COL_MA10:
663 166
                        print_bytes(w, pt->ma_10.acc);
664 166
                        break;
665 22
                case COL_MA100:
666 22
                        print_bytes(w, pt->ma_100.acc);
667 22
                        break;
668 22
                case COL_MA1000:
669 22
                        print_bytes(w, pt->ma_1000.acc);
670 22
                        break;
671 0
                default:
672 0
                        break;
673 708
                }
674 708
                x += COLW;
675 708
                col++;
676 708
        }
677 166
}
678
679
static void
680
draw_line_bitmap(WINDOW *w, int y, int x, int X, const struct pt *pt)
681 21
{
682 21
        unsigned ch;
683 21
        enum {
684 21
                COL_VAL,
685 21
                COL_MAP,
686 21
                COL_LAST
687 21
        } col;
688
689 21
        AN(w);
690 21
        AN(pt);
691 21
        assert(pt->vpt->format == 'b');
692
693 21
        col = COL_VAL;
694 63
        while (col < COL_LAST) {
695 42
                switch (col) {
696 21
                case COL_VAL:
697 21
                        if (X - x < COLW)
698 0
                                return;
699 21
                        IC(mvwprintw(w, y, x, "   %10.10jx",
700 21
                            (uintmax_t)((pt->cur >> 24) & 0xffffffffffLL)));
701 21
                        x += COLW;
702 21
                        break;
703 21
                case COL_MAP:
704 21
                        if (X - x < 2 * COLW)
705 0
                                return;
706 21
                        x += (2 * COLW) - 24;
707 525
                        for (ch = 0x800000; ch; ch >>= 1) {
708 504
                                if (pt->cur & ch)
709 504
                                        mvwaddch(w, y, x, 'V');
710 336
                                else
711 504
                                        mvwaddch(w, y, x, '_');
712 504
                                x++;
713 504
                        }
714 21
                        break;
715 0
                default:
716 0
                        break;
717 42
                }
718 42
                col++;
719 42
        }
720 21
}
721
722
static void
723
draw_line_duration(WINDOW *w, int y, int x, int X, const struct pt *pt)
724 4
{
725 4
        enum {
726 4
                COL_DUR,
727 4
                COL_LAST
728 4
        } col;
729
730 4
        AN(w);
731 4
        AN(pt);
732
733 4
        col = COL_DUR;
734 8
        while (col < COL_LAST) {
735 4
                if (X - x < COLW)
736 0
                        break;
737 4
                switch (col) {
738 4
                case COL_DUR:
739 4
                        wmove(w, y, x);
740 4
                        if (scale)
741 2
                                print_duration(w, pt->cur);
742 2
                        else
743 2
                                IC(wprintw(w, " %12ju", (uintmax_t)pt->cur));
744 4
                        break;
745 0
                default:
746 0
                        break;
747 4
                }
748 4
                x += COLW;
749 4
                col++;
750 4
        }
751 4
}
752
753
static void
754
draw_line(WINDOW *w, int y, const struct pt *pt)
755 617
{
756 617
        int x, X;
757
758 617
        assert(colw_name >= COLW_NAME_MIN);
759 617
        X = getmaxx(w);
760 617
        x = 0;
761 617
        if (vstrlen(pt->vpt->name) > colw_name)
762 57
                IC(mvwprintw(w, y, x, "%.*s...", colw_name - 3, pt->vpt->name));
763 560
        else
764 560
                IC(mvwprintw(w, y, x, "%.*s", colw_name, pt->vpt->name));
765 617
        x += colw_name;
766
767 617
        switch (pt->vpt->format) {
768 21
        case 'b':
769 21
                draw_line_bitmap(w, y, x, X, pt);
770 21
                break;
771 166
        case 'B':
772 166
                draw_line_bytes(w, y, x, X, pt);
773 166
                break;
774 4
        case 'd':
775 4
                draw_line_duration(w, y, x, X, pt);
776 4
                break;
777 426
        default:
778 426
                draw_line_default(w, y, x, X, pt);
779 426
                break;
780 617
        }
781 617
}
782
783
static void
784
draw_points(void)
785 53
{
786 53
        int line;
787 53
        int n;
788
789 53
        AN(w_points);
790
791 53
        AC(werase(w_points));
792 53
        if (n_ptarray == 0) {
793 7
                AC(wnoutrefresh(w_points));
794 7
                return;
795 7
        }
796
797 46
        assert(current >= 0);
798 46
        assert(current < n_ptarray);
799 46
        assert(page_start >= 0);
800 46
        assert(page_start < n_ptarray);
801 46
        assert(current >= page_start);
802 46
        assert(current - page_start < l_points);
803
804 663
        for (line = 0; line < l_points; line++) {
805 633
                n = line + page_start;
806 633
                if (n >= n_ptarray)
807 16
                        break;
808 617
                if (n == current)
809 617
                        wattron(w_points, A_BOLD);
810 617
                draw_line(w_points, line, ptarray[n]);
811 617
                if (n == current)
812 617
                        wattroff(w_points, A_BOLD);
813 617
        }
814 46
        AC(wnoutrefresh(w_points));
815 46
}
816
817
static void
818
draw_help(void)
819 8
{
820 8
        const char *const *p;
821 8
        int l, y, X;
822
823 8
        if (l_points >= bindings_help_len) {
824 0
                assert(help_line == 0);
825 0
                l = bindings_help_len;
826 8
        } else {
827 8
                assert(help_line >= 0);
828 8
                assert(help_line <= bindings_help_len - l_points);
829 8
                l = l_points;
830 8
        }
831
832 8
        X = getmaxx(w_points);
833 8
        AC(werase(w_points));
834
835 136
        for (y = 0, p = bindings_help + help_line; y < l; y++, p++) {
836 128
                if (**p == '\t') {
837 75
                        IC(mvwprintw(w_points, y, 0, "    %.*s", X - 4, *p + 1));
838 75
                } else {
839 53
                        wattron(w_points, A_BOLD);
840 53
                        IC(mvwprintw(w_points, y, 0, "%.*s", X, *p));
841 53
                        wattroff(w_points, A_BOLD);
842 53
                }
843 128
        }
844
845 8
        AC(wnoutrefresh(w_points));
846 8
}
847
848
static void
849
draw_bar_b(void)
850 53
{
851 53
        int x, X;
852 53
        char buf[64];
853
854 53
        AN(w_bar_b);
855
856 53
        x = 0;
857 53
        X = getmaxx(w_bar_b);
858 53
        AC(werase(w_bar_b));
859 53
        if (page_start + l_points < n_ptarray)
860 26
                IC(mvwprintw(w_bar_b, 0, x, "vvv"));
861 53
        x += 4;
862 53
        if (current < n_ptarray)
863 46
                IC(mvwprintw(w_bar_b, 0, x, "%s", ptarray[current]->vpt->name));
864
865 53
        bprintf(buf, "%d-%d/%d", page_start + 1,
866 53
            page_start + l_points < n_ptarray ?
867 53
                page_start + l_points : n_ptarray,
868 53
            n_ptarray);
869 53
        IC(mvwprintw(w_bar_b, 0, X - vstrlen(buf), "%s", buf));
870 53
        X -= vstrlen(buf) + 2;
871
872 53
        if (verbosity != NULL) {
873 53
                IC(mvwprintw(w_bar_b, 0, X - vstrlen(verbosity->label), "%s",
874 53
                             verbosity->label));
875 53
                X -= vstrlen(verbosity->label) + 2;
876 53
        }
877 53
        if (!hide_unseen) {
878 15
                IC(mvwprintw(w_bar_b, 0, X - 6, "%s", "UNSEEN"));
879 15
                X -= 8;
880 15
        }
881 53
        if (raw_vsc)
882 0
                IC(mvwprintw(w_bar_b, 0, X - 3, "%s", "RAW"));
883
884 53
        AC(wnoutrefresh(w_bar_b));
885 53
}
886
887
static void
888
draw_info(void)
889 53
{
890
891 53
        if (w_info == NULL)
892 0
                return;
893
894 53
        AC(werase(w_info));
895 53
        if (current < n_ptarray) {
896
                /* XXX: Word wrapping, and overflow handling? */
897 46
                IC(mvwprintw(w_info, 0, 0, "%s:",
898 46
                    ptarray[current]->vpt->sdesc));
899 46
                IC(mvwprintw(w_info, 1, 0, "%s",
900 46
                    ptarray[current]->vpt->ldesc));
901 46
        }
902 53
        AC(wnoutrefresh(w_info));
903 53
}
904
905
static void
906
draw_screen(void)
907 61
{
908 61
        draw_status();
909 61
        if (show_help) {
910 8
                AC(werase(w_bar_t));
911 8
                AC(werase(w_bar_b));
912 8
                AC(werase(w_info));
913 8
                AC(wnoutrefresh(w_bar_t));
914 8
                AC(wnoutrefresh(w_bar_b));
915 8
                AC(wnoutrefresh(w_info));
916 8
                draw_help();
917 53
        } else {
918 53
                draw_bar_t();
919 53
                draw_points();
920 53
                draw_bar_b();
921 53
                draw_info();
922 53
        }
923 61
        AC(doupdate());
924 61
        redraw = 0;
925 61
}
926
927
static void
928
handle_common_keypress(enum kb_e kb)
929 2
{
930
931 2
        switch (kb) {
932 2
        case KB_QUIT:
933 2
                keep_running = 0;
934 2
                return;
935 0
        case KB_SIG_INT:
936 0
                AZ(raise(SIGINT));
937 0
                return;
938 0
        case KB_SIG_TSTP:
939 0
                AZ(raise(SIGTSTP));
940 0
                return;
941 0
        default:
942 0
                WRONG("unexpected key binding");
943 2
        }
944 2
}
945
946
static void
947
handle_points_keypress(struct vsc *vsc, enum kb_e kb)
948 26
{
949
950 26
        switch (kb) {
951 3
        case KB_HELP:
952 3
                show_help = 1;
953 3
                help_line = 0;
954 3
                redraw = 1;
955 3
                return;
956 1
        case KB_UP:
957 1
                if (current == 0)
958 0
                        return;
959 1
                current--;
960 1
                break;
961 0
        case KB_DOWN:
962 0
                if (current == n_ptarray - 1)
963 0
                        return;
964 0
                current++;
965 0
                break;
966 2
        case KB_PAGEUP:
967 2
                current -= l_points;
968 2
                page_start -= l_points;
969 2
                break;
970 7
        case KB_PAGEDOWN:
971 7
                current += l_points;
972 7
                if (page_start + l_points < n_ptarray - 1)
973 5
                        page_start += l_points;
974 7
                break;
975 1
        case KB_TOP:
976 1
                current = 0;
977 1
                break;
978 2
        case KB_BOTTOM:
979 2
                current = n_ptarray - 1;
980 2
                break;
981 2
        case KB_UNSEEN:
982 2
                hide_unseen = 1 - hide_unseen;
983 2
                rebuild = REBUILD_NEXT;
984 2
                break;
985 0
        case KB_RAW:
986 0
                AN(VSC_Arg(vsc, 'r', NULL));
987 0
                raw_vsc = VSC_IsRaw(vsc);
988 0
                rebuild = REBUILD_NEXT;
989 0
                break;
990 1
        case KB_SCALE:
991 1
                scale = 1 - scale;
992 1
                rebuild = REBUILD_NEXT;
993 1
                break;
994 1
        case KB_ACCEL:
995 1
                interval += 0.1;
996 1
                (void)snprintf(notification_message, NOTIF_MAXLEN,
997 1
                    "Refresh interval set to %.1f seconds.", interval);
998
999 1
                notification_eol = VTIM_mono() + 1.25;
1000 1
                break;
1001 1
        case KB_DECEL:
1002 1
                interval -= 0.1;
1003 1
                if (interval < 0.1)
1004 0
                        interval = 0.1;
1005 1
                (void)snprintf(notification_message, NOTIF_MAXLEN,
1006 1
                    "Refresh interval set to %.1f seconds.", interval);
1007
1008 1
                notification_eol = VTIM_mono() + 1.25;
1009 1
                break;
1010 2
        case KB_VERBOSE:
1011 2
                verbosity = VSC_ChangeLevel(verbosity, 1);
1012 2
                rebuild = REBUILD_NEXT;
1013 2
                break;
1014 0
        case KB_QUIET:
1015 0
                verbosity = VSC_ChangeLevel(verbosity, -1);
1016 0
                rebuild = REBUILD_NEXT;
1017 0
                break;
1018 0
        case KB_SAMPLE:
1019 0
                sample = 1;
1020 0
                return;
1021 1
        case KB_RESET_AVERAGES:
1022 1
                reset_averages = 1;
1023 1
                return;
1024 2
        case KB_QUIT:
1025 2
        case KB_SIG_INT:
1026 2
        case KB_SIG_TSTP:
1027 2
                handle_common_keypress(kb);
1028 2
                return;
1029 0
        default:
1030 0
                WRONG("unhandled key binding");
1031 26
        }
1032
1033 20
        update_position();
1034 20
        redraw = 1;
1035 20
}
1036
1037
static void
1038
handle_help_keypress(enum kb_e kb)
1039 8
{
1040 8
        int hl = help_line;
1041
1042 8
        switch (kb) {
1043 3
        case KB_HELP:
1044 3
                show_help = 0;
1045 3
                redraw = 1;
1046 3
                return;
1047 2
        case KB_UP:
1048 2
                help_line--;
1049 2
                break;
1050 0
        case KB_DOWN:
1051 0
                help_line++;
1052 0
                break;
1053 1
        case KB_PAGEUP:
1054 1
                help_line -= l_points;
1055 1
                break;
1056 1
        case KB_PAGEDOWN:
1057 1
                help_line += l_points;
1058 1
                break;
1059 0
        case KB_TOP:
1060 0
                help_line = 0;
1061 0
                break;
1062 1
        case KB_BOTTOM:
1063 1
                help_line = bindings_help_len;
1064 1
                break;
1065 0
        case KB_UNSEEN:
1066 0
        case KB_RAW:
1067 0
        case KB_SCALE:
1068 0
        case KB_ACCEL:
1069 0
        case KB_DECEL:
1070 0
        case KB_VERBOSE:
1071 0
        case KB_QUIET:
1072 0
        case KB_SAMPLE:
1073 0
        case KB_RESET_AVERAGES:
1074 0
                break;
1075 0
        case KB_QUIT:
1076 0
        case KB_SIG_INT:
1077 0
        case KB_SIG_TSTP:
1078 0
                handle_common_keypress(kb);
1079 0
                return;
1080 0
        default:
1081 0
                WRONG("unhandled key binding");
1082 8
        }
1083
1084 5
        help_line = vlimit_t(int, help_line, 0, bindings_help_len - l_points);
1085 5
        redraw = (help_line != hl);
1086 5
}
1087
1088
static void
1089
handle_keypress(struct vsc *vsc, int ch)
1090 34
{
1091 34
        enum kb_e kb;
1092
1093 34
        switch (ch) {
1094 0
#define BINDING_KEY(chr, name, or)      \
1095 51
        case chr:
1096 0
#define BINDING(name, desc)             \
1097 34
                kb = KB_ ## name;       \
1098 34
                break;
1099 0
#define BINDING_SIG
1100 0
#include "vinylstat_bindings.h"
1101 0
        default:
1102 0
                return;
1103 34
        }
1104
1105 34
        if (show_help)
1106 8
                handle_help_keypress(kb);
1107 26
        else
1108 26
                handle_points_keypress(vsc, kb);
1109 34
}
1110
1111
static void * v_matchproto_(VSC_new_f)
1112
newpt(void *priv, const struct VSC_point *const vpt)
1113 2471
{
1114 2471
        struct pt *pt;
1115
1116 2471
        AZ(priv);
1117 2471
        ALLOC_OBJ(pt, PT_MAGIC);
1118 2471
        rebuild |= REBUILD_NEXT;
1119 2471
        AN(pt);
1120 2471
        pt->vpt = vpt;
1121 2471
        pt->last = VSC_Value(vpt);
1122 2471
        pt->ma_10.nmax = 10;
1123 2471
        pt->ma_100.nmax = 100;
1124 2471
        pt->ma_1000.nmax = 1000;
1125
1126 2471
        VTAILQ_INSERT_TAIL(&ptlist, pt, list);
1127 2471
        n_ptlist++;
1128
1129 2471
        AZ(vstrcmp(vpt->ctype, "uint64_t"));
1130
1131 2471
        if (!vstrcmp(vpt->name, "MGT.uptime"))
1132 7
                mgt_uptime = vpt->ptr;
1133 2471
        if (!vstrcmp(vpt->name, "MAIN.uptime"))
1134 7
                main_uptime = vpt->ptr;
1135 2471
        if (!vstrcmp(vpt->name, "MAIN.cache_hit"))
1136 7
                main_cache_hit = vpt->ptr;
1137 2471
        if (!vstrcmp(vpt->name, "MAIN.cache_miss"))
1138 7
                main_cache_miss = vpt->ptr;
1139 2471
        return (pt);
1140 2471
}
1141
1142
static void v_matchproto_(VSC_destroy_f)
1143
delpt(void *priv, const struct VSC_point *const vpt)
1144 2471
{
1145 2471
        struct pt *pt;
1146
1147 2471
        AZ(priv);
1148 2471
        CAST_OBJ_NOTNULL(pt, vpt->priv, PT_MAGIC);
1149 2471
        rebuild |= REBUILD_NEXT;
1150 2471
        VTAILQ_REMOVE(&ptlist, pt, list);
1151 2471
        n_ptlist--;
1152 2471
        FREE_OBJ(pt);
1153 2471
        if (vpt->ptr == mgt_uptime)
1154 7
                mgt_uptime = NULL;
1155 2471
        if (vpt->ptr == main_uptime)
1156 7
                main_uptime = NULL;
1157 2471
        if (vpt->ptr == main_cache_hit)
1158 7
                main_cache_hit = NULL;
1159 2471
        if (vpt->ptr == main_cache_miss)
1160 7
                main_cache_miss = NULL;
1161 2471
}
1162
1163
void
1164
do_curses(struct vsm *vsm, struct vsc *vsc)
1165 7
{
1166 7
        long t;
1167 7
        int ch;
1168 7
        double now;
1169
1170 7
        verbosity = VSC_ChangeLevel(NULL, 0);
1171
1172 7
        (void)initscr();
1173 7
        AC(raw());
1174 7
        AC(noecho());
1175 7
        AC(nonl());
1176 7
        IC(curs_set(0));
1177
1178 7
        make_windows();
1179 7
        AC(doupdate());
1180
1181 7
        VSC_State(vsc, newpt, delpt, NULL);
1182
1183 7
        raw_vsc = VSC_IsRaw(vsc);
1184 7
        rebuild |= REBUILD_FIRST;
1185 7
        (void)VSC_Iter(vsc, vsm, NULL, NULL);
1186 7
        build_pt_array();
1187 7
        init_hitrate();
1188
1189 68
        while (keep_running && !VSIG_int && !VSIG_term && !VSIG_hup) {
1190 61
                (void)VSC_Iter(vsc, vsm, NULL, NULL);
1191 61
                vsm_status = VSM_Status(vsm);
1192 61
                if (vsm_status & (VSM_MGT_RESTARTED|VSM_WRK_RESTARTED))
1193 0
                        init_hitrate();
1194 61
                if (rebuild)
1195 24
                        build_pt_array();
1196
1197 61
                now = VTIM_mono();
1198 61
                if (now - t_sample > interval)
1199 28
                        sample = 1;
1200 61
                if (sample)
1201 28
                        sample_data();
1202 61
                if (redraw)
1203 61
                        draw_screen();
1204
1205 61
                t = (long)((t_sample + interval - now) * 1000);
1206 61
                wtimeout(w_status, t);
1207
1208 61
                ch = wgetch(w_status);
1209 61
                switch (ch) {
1210 26
                case ERR:
1211 26
                        break;
1212 0
#ifdef KEY_RESIZE /* sigh, Solaris lacks this.. */
1213 1
                case KEY_RESIZE:
1214 1
                        make_windows();
1215 1
                        update_position();
1216 1
                        break;
1217 0
#endif
1218 34
                default:
1219 34
                        handle_keypress(vsc, ch);
1220 34
                        break;
1221 61
                }
1222 61
        }
1223 7
        VSC_Destroy(&vsc, vsm);
1224 7
        AN(VTAILQ_EMPTY(&ptlist));
1225 7
        VSM_Destroy(&vsm);
1226 7
        AZ(endwin());
1227 7
}