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