varnish-cache/bin/varnishhist/varnishhist.c
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: Guillaume Quintard <guillaume.quintard@gmail.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
 * Log tailer for Varnish
33
 */
34
35
#include "config.h"
36
37
#include <limits.h>
38
#include <math.h>
39
#include <pthread.h>
40
#include <stdarg.h>
41
#include <stdlib.h>
42
#include <string.h>
43
#include <unistd.h>
44
45
#define VOPT_DEFINITION
46
#define VOPT_INC "varnishhist_options.h"
47
48
#include "vdef.h"
49
#include "vcurses.h"
50
#include "vapi/vsl.h"
51
#include "vapi/vsm.h"
52
#include "vapi/voptget.h"
53
#include "vapi/vsig.h"
54
#include "vas.h"
55
#include "vut.h"
56
#include "vtim.h"
57
58
#if 1
59
#define AC(x) assert((x) != ERR)
60
#else
61
#define AC(x) x
62
#endif
63
64
#define HIST_N 2000             /* how far back we remember */
65
#define HIST_RES 100            /* bucket resolution */
66
67
static struct VUT *vut;
68
69
static int hist_low;
70
static int hist_high;
71
static int hist_range;
72
static unsigned hist_buckets;
73
74
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
75
76
static int end_of_file = 0;
77
static unsigned ms_delay = 1000;
78
static unsigned rr_hist[HIST_N];
79
static unsigned nhist;
80
static unsigned next_hist;
81
static unsigned *bucket_miss;
82
static unsigned *bucket_hit;
83
static char *format;
84
static int match_tag;
85
static double timebend = 0, t0;
86
static double vsl_t0 = 0, vsl_to, vsl_ts = 0;
87
static pthread_cond_t timebend_cv;
88
static double log_ten;
89
static char *ident;
90
91
static const unsigned scales[] = {
92
        1,
93
        2,
94
        3,
95
        4,
96
        5,
97
        10,
98
        15,
99
        20,
100
        25,
101
        50,
102
        100,
103
        250,
104
        500,
105
        1000,
106
        2500,
107
        5000,
108
        10000,
109
        25000,
110
        50000,
111
        100000,
112
        UINT_MAX
113
};
114
115
struct profile {
116
        const char *name;
117
        char VSL_arg;
118
        enum VSL_tag_e tag;
119
        const char *prefix;
120
        int field;
121
        int hist_low;
122
        int hist_high;
123
};
124
125
#define HIS_PROF(name,vsl_arg,tag,prefix,field,hist_low,high_high,doc)  \
126
        {name,vsl_arg,tag,prefix,field,hist_low,high_high},
127
#define HIS_NO_PREFIX   NULL
128
#define HIS_CLIENT      'c'
129
#define HIS_BACKEND     'b'
130
static const struct profile profiles[] = {
131
#include "varnishhist_profiles.h"
132
        { NULL }
133
};
134
#undef HIS_NO_PREFIX
135
#undef HIS_BACKEND
136
#undef HIS_CLIENT
137
#undef HIS_PROF
138
139
static const struct profile *active_profile;
140
141
static void
142 46180
update(void)
143
{
144
        char t[VTIM_FORMAT_SIZE];
145 46180
        const unsigned w = COLS / hist_range;
146 46180
        const unsigned n = w * hist_range;
147 46180
        unsigned bm[n], bh[n];
148
        unsigned max;
149
        unsigned scale;
150
        int i, j;
151
        unsigned k, l;
152
153
        /* Draw horizontal axis */
154 3373692
        for (k = 0; k < n; ++k)
155 3327512
                (void)mvaddch(LINES - 2, k, '-');
156 459567
        for (i = 0, j = hist_low; i < hist_range; ++i, ++j) {
157 413387
                (void)mvaddch(LINES - 2, w * i, '+');
158 413387
                mvprintw(LINES - 1, w * i, "|1e%d", j);
159 413387
        }
160
161 46180
        if (end_of_file)
162 0
                mvprintw(0, 0, "%*s", COLS - 1, "EOF");
163
        else
164 46180
                mvprintw(0, 0, "%*s", COLS - 1, ident);
165
166
        /* count our flock */
167 46180
        memset(bm, 0, sizeof bm);
168 46180
        memset(bh, 0, sizeof bh);
169 41384880
        for (k = 0, max = 1; k < hist_buckets; ++k) {
170 41338700
                l = k * n / hist_buckets;
171 41338700
                assert(l < n);
172 41338700
                bm[l] += bucket_miss[k];
173 41338700
                bh[l] += bucket_hit[k];
174 41338700
                max = vmax(max, bm[l] + bh[l]);
175 41338700
        }
176
177
        /* scale,time */
178 46180
        assert(LINES - 3 >= 0);
179 46180
        for (i = 0; max / scales[i] > (unsigned)(LINES - 3); ++i)
180
                /* nothing */ ;
181 46180
        scale = scales[i];
182
183 46180
        if (vsl_t0 > 0) {
184 45225
                VTIM_format(vsl_ts, t);
185
186 45225
                mvprintw(0, 0, "1:%u, n = %u, d = %g @ %s x %g",
187 45225
                    scale, nhist, 1e-3 * ms_delay, t, timebend);
188 45225
        } else
189 955
                mvprintw(0, 0, "1:%u, n = %u, d = %g",
190 955
                    scale, nhist, 1e-3 * ms_delay);
191
192 230900
        for (j = 5; j < LINES - 2; j += 5)
193 184720
                mvprintw((LINES - 2) - j, 0, "%u_", j * scale);
194
195
        /* show them */
196 3373692
        for (k = 0; k < n; ++k) {
197 3373467
                for (l = 0; l < bm[k] / scale; ++l)
198 45955
                        (void)mvaddch((LINES - 3) - l, k, '#');
199 3327953
                for (; l < (bm[k] + bh[k]) / scale; ++l)
200 441
                        (void)mvaddch((LINES - 3) - l, k, '|');
201 3327512
        }
202 46180
}
203
204
inline static void
205 160
upd_vsl_ts(const char *p)
206
{
207
208 160
        if (timebend == 0)
209 120
                return;
210
211 40
        p = strchr(p, ' ');
212
213 40
        if (p == NULL)
214 0
                return;
215
216 40
        vsl_ts = vmax_t(double, vsl_ts, strtod(p + 1, NULL));
217 160
}
218
219
static void
220 160
delorean(void)
221
{
222
        int i;
223 160
        double t = VTIM_mono();
224
225 160
        if (vsl_t0 == 0)
226 160
                vsl_to = vsl_t0 = vsl_ts;
227
228 160
        assert(t > t0);
229 160
        vsl_to = vsl_t0 + (t - t0) * timebend;
230
231 160
        if (vsl_ts > vsl_to) {
232 0
                double when = VTIM_real() + vsl_ts - vsl_to;
233 0
                struct timespec ts = VTIM_timespec(when);
234 0
                i = pthread_cond_timedwait(&timebend_cv, &mtx, &ts);
235 0
                assert(i == 0 || i == ETIMEDOUT);
236 0
        }
237 160
}
238
239
static int v_matchproto_ (VSLQ_dispatch_f)
240 240
accumulate(struct VSL_data *vsl, struct VSL_transaction * const pt[],
241
    void *priv)
242
{
243
        int i, tag, skip, match, hit;
244
        unsigned u;
245 240
        double value = 0;
246
        struct VSL_transaction *tr;
247
        const char *tsp;
248
        enum vsl_status stat;
249
250 240
        (void)vsl;
251 240
        (void)priv;
252
253 480
        for (tr = pt[0]; tr != NULL; tr = *++pt) {
254 240
                if (VSIG_int || VSIG_term || VSIG_hup)
255 0
                        return (-1);
256
257 240
                if (tr->reason == VSL_r_esi) {
258
                        /* Skip ESI requests */
259 0
                        continue;
260
                }
261
262 240
                hit = 0;
263 240
                skip = 0;
264 240
                match = 0;
265 240
                tsp = NULL;
266 7320
                while (skip == 0) {
267 7320
                        stat = VSL_Next(tr->c);
268 7320
                        if (stat == vsl_e_overrun) {
269
                                /* need to skip forward */
270 0
                                PTOK(pthread_mutex_lock(&mtx));
271 0
                                vsl_to = vsl_t0 = vsl_ts = 0;
272 0
                                t0 = VTIM_mono();
273 0
                                PTOK(pthread_mutex_unlock(&mtx));
274 0
                                break;
275
                        }
276 7320
                        if (stat != vsl_more)
277 240
                                break;
278
279
                        /* get the value we want and register if it's a hit */
280 7080
                        tag = VSL_TAG(tr->c->rec.ptr);
281
282 7080
                        if (VSL_tagflags[tag])
283 720
                                continue;
284
285 6360
                        switch (tag) {
286
                        case SLT_Hit:
287 40
                                hit = 1;
288 40
                                break;
289
                        case SLT_VCL_return:
290 480
                                if (!strcasecmp(VSL_CDATA(tr->c->rec.ptr),
291 480
                                    "restart") ||
292 480
                                    !strcasecmp(VSL_CDATA(tr->c->rec.ptr),
293
                                    "retry"))
294 0
                                        skip = 1;
295 480
                                break;
296
                        case SLT_Timestamp:
297 920
                                tsp = VSL_CDATA(tr->c->rec.ptr);
298
                                /* FALLTHROUGH */
299
                        default:
300 5840
                                if (tag != match_tag)
301 5160
                                        break;
302
303 680
                                if (active_profile->prefix &&
304 1280
                                    strncmp(VSL_CDATA(tr->c->rec.ptr),
305 640
                                    active_profile->prefix,
306 1280
                                    strlen(active_profile->prefix)) != 0)
307 520
                                        break;
308
309 320
                                i = sscanf(VSL_CDATA(tr->c->rec.ptr),
310 160
                                    format, &value);
311 160
                                if (i != 1)
312 0
                                        break;
313 160
                                match = 1;
314 160
                                break;
315
                        }
316
                }
317
318 240
                if (skip || !match || value <= 0)
319 80
                        continue;
320
321
                /* select bucket */
322 160
                i = vlimit_t(int, lround(HIST_RES * log(value) / log_ten),
323 160
                    hist_low * HIST_RES, hist_high * HIST_RES - 1) -
324 160
                        hist_low * HIST_RES;
325 160
                assert(i >= 0);
326 160
                assert((unsigned)i < hist_buckets);
327
328 160
                PTOK(pthread_mutex_lock(&mtx));
329
330
                /*
331
                 * only parse the last tsp seen in this transaction -
332
                 * it should be the latest.
333
                 */
334 160
                if (tsp)
335 160
                        upd_vsl_ts(tsp);
336
337
                /* phase out old data */
338 160
                if (nhist == HIST_N) {
339 0
                        u = rr_hist[next_hist];
340 0
                        if (u >= hist_buckets) {
341 0
                                u -= hist_buckets;
342 0
                                assert(u < hist_buckets);
343 0
                                assert(bucket_hit[u] > 0);
344 0
                                bucket_hit[u]--;
345 0
                        } else {
346 0
                                assert(bucket_miss[u] > 0);
347 0
                                bucket_miss[u]--;
348
                        }
349 0
                } else {
350 160
                        ++nhist;
351
                }
352
353
                /* phase in new data */
354 160
                if (hit) {
355 40
                        bucket_hit[i]++;
356 40
                        rr_hist[next_hist] = i + hist_buckets;
357 40
                } else {
358 120
                        bucket_miss[i]++;
359 120
                        rr_hist[next_hist] = i;
360
                }
361 160
                if (++next_hist == HIST_N) {
362 0
                        next_hist = 0;
363 0
                }
364 160
                if (vsl_ts >= vsl_to)
365 160
                        delorean();
366 160
                PTOK(pthread_mutex_unlock(&mtx));
367 160
        }
368 240
        return (0);
369 240
}
370
371
static void * v_matchproto_(pthread_t)
372 120
do_curses(void *arg)
373
{
374
        int ch;
375 120
        (void)arg;
376
377 120
        initscr();
378 120
        AC(raw());
379 120
        AC(noecho());
380 120
        AC(nonl());
381 120
        AC(intrflush(stdscr, FALSE));
382 120
        AC(curs_set(0));
383 120
        AC(erase());
384 46300
        while (!VSIG_int && !VSIG_term && !VSIG_hup) {
385
386 46180
                AC(erase());
387 46180
                PTOK(pthread_mutex_lock(&mtx));
388 46180
                update();
389 46180
                PTOK(pthread_mutex_unlock(&mtx));
390 46180
                AC(refresh());
391
392 46180
                assert(ms_delay > 0);
393 46180
                timeout(ms_delay);
394 46180
                switch ((ch = getch())) {
395
                case ERR:
396 45220
                        break;
397
#ifdef KEY_RESIZE
398
                case KEY_RESIZE:
399 40
                        AC(erase());
400 40
                        break;
401
#endif
402
                case '\014':    /* Ctrl-L */
403
                case '\024':    /* Ctrl-T */
404 40
                        redrawwin(stdscr);
405 40
                        AC(refresh());
406 40
                        break;
407
                case '\032':    /* Ctrl-Z */
408 0
                        AC(endwin());
409 0
                        AZ(raise(SIGTSTP));
410 0
                        break;
411
                case '\003':    /* Ctrl-C */
412
                case '\021':    /* Ctrl-Q */
413
                case 'Q':
414
                case 'q':
415 40
                        AZ(raise(SIGINT));
416 40
                        break;
417
                case '0':
418
                case '1':
419
                case '2':
420
                case '3':
421
                case '4':
422
                case '5':
423
                case '6':
424
                case '7':
425
                case '8':
426
                case '9':
427 80
                        ms_delay = 1000U << (ch - '0');
428 80
                        break;
429
                case '+':
430 480
                        ms_delay = vmax(ms_delay >> 1, 1U);
431 480
                        break;
432
                case '-':
433 80
                        ms_delay *= 2;
434 80
                        break;
435
                case '>':
436
                case '<':
437
                        /* see below */
438 160
                        break;
439
                default:
440 40
                        AC(beep());
441 40
                        break;
442
                }
443
444 46180
                if (ch == '<' || ch == '>') {
445 160
                        PTOK(pthread_mutex_lock(&mtx));
446 160
                        vsl_to = vsl_t0 = vsl_ts;
447 160
                        t0 = VTIM_mono();
448 160
                        if (timebend == 0)
449 40
                                timebend = 1;
450 120
                        else if (ch == '<')
451 80
                                timebend /= 2;
452
                        else
453 40
                                timebend *= 2;
454 160
                        PTOK(pthread_cond_broadcast(&timebend_cv));
455 160
                        PTOK(pthread_mutex_unlock(&mtx));
456 160
                }
457
        }
458 120
        AC(endwin());
459 120
        return (NULL);
460
}
461
462
/*--------------------------------------------------------------------*/
463
464
static void v_noreturn_
465 200
profile_error(const char *s)
466
{
467 400
        fprintf(stderr, "-P: '%s' is not a valid"
468 200
            " profile name or definition\n", s);
469 200
        exit(1);
470
}
471
472
int
473 760
main(int argc, char **argv)
474
{
475
        int i;
476
        char *colon;
477 760
        const char *ptag, *profile = "responsetime";
478
        pthread_t thr;
479
        int fnum;
480 760
        struct profile cli_p = {0};
481
482 760
        vut = VUT_InitProg(argc, argv, &vopt_spec);
483 760
        AN(vut);
484 760
        PTOK(pthread_cond_init(&timebend_cv, NULL));
485
486 1120
        while ((i = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
487 880
                switch (i) {
488
                case 'h':
489
                        /* Usage help */
490 40
                        VUT_Usage(vut, &vopt_spec, 0);
491
                case 'p':
492 120
                        ms_delay = lround(1e3 * strtod(optarg, NULL));
493 120
                        if (ms_delay == 0)
494 80
                                VUT_Error(vut, 1, "-p: invalid '%s'", optarg);
495 40
                        break;
496
                case 'P':
497 440
                        colon = strchr(optarg, ':');
498
                        /* no colon, take the profile as a name */
499 440
                        if (colon == NULL) {
500 80
                                profile = optarg;
501 80
                                break;
502
                        }
503
                        /* else check if valid definition */
504 360
                        if (colon == optarg + 1 && (*optarg == 'b' ||
505 0
                            *optarg == 'c' || *optarg == 'E')) {
506 280
                                cli_p.VSL_arg = *optarg;
507 280
                                ptag = colon + 1;
508 280
                                colon = strchr(colon + 1, ':');
509 280
                                if (colon == NULL)
510 40
                                        profile_error(optarg);
511 240
                        } else {
512 80
                                ptag = optarg;
513 80
                                cli_p.VSL_arg = 'c';
514
                        }
515
516 320
                        assert(colon);
517
518 320
                        match_tag = VSL_Name2Tag(ptag, colon - ptag);
519 320
                        if (match_tag < 0)
520 80
                                VUT_Error(vut, 1,
521
                                    "-P: '%s' is not a valid tag name",
522 40
                                    optarg);
523
524 280
                        if (VSL_tagflags[match_tag])
525 80
                                VUT_Error(vut, 1,
526
                                    "-P: '%s' is an unsafe or binary record",
527 40
                                    optarg);
528
529 240
                        cli_p.prefix = colon + 1;
530
531 240
                        colon = strchr(colon + 1, ':');
532 240
                        if (colon == NULL)
533 40
                                profile_error(optarg);
534
535 200
                        *colon = '\0';
536 200
                        if (*cli_p.prefix == '\0')
537 160
                                cli_p.prefix = NULL;
538
539 200
                        if (sscanf(colon + 1, "%d",  &cli_p.field) != 1)
540 80
                                profile_error(optarg);
541
542 120
                        cli_p.name = "custom";
543 120
                        cli_p.tag = (enum VSL_tag_e)match_tag;
544 120
                        cli_p.hist_low = -6;
545 120
                        cli_p.hist_high = 3;
546 120
                        profile = NULL;
547 120
                        active_profile = &cli_p;
548
549 120
                        colon = strchr(colon + 1, ':');
550 120
                        if (colon == NULL)
551 40
                                break;
552
553 240
                        if (sscanf(colon + 1, "%d:%d",  &cli_p.hist_low,
554 160
                                   &cli_p.hist_high) != 2)
555 40
                                profile_error(optarg);
556
557 40
                        break;
558
                case 'B':
559 120
                        timebend = strtod(optarg, NULL);
560 120
                        if (timebend == 0)
561 80
                                VUT_Error(vut, 1,
562
                                    "-B: being able to bend time does not"
563
                                    " mean we can stop it"
564 40
                                    " (invalid factor '%s')", optarg);
565 80
                        if (timebend < 0)
566 80
                                VUT_Error(vut, 1,
567
                                    "-B: being able to bend time does not"
568
                                    " mean we can make it go backwards"
569 40
                                    " (invalid factor '%s')", optarg);
570 40
                        break;
571
                default:
572 160
                        if (!VUT_Arg(vut, i, optarg))
573 40
                                VUT_Usage(vut, &vopt_spec, 1);
574 120
                }
575
        }
576
577 240
        if (optind != argc)
578 40
                VUT_Usage(vut, &vopt_spec, 1);
579
580
        /* Check for valid grouping mode */
581 200
        assert(vut->g_arg < VSL_g__MAX);
582 200
        if (vut->g_arg != VSL_g_vxid && vut->g_arg != VSL_g_request)
583 80
                VUT_Error(vut, 1, "Invalid grouping mode: %s"
584
                    " (only vxid and request are supported)",
585 40
                    VSLQ_grouping[vut->g_arg]);
586
587 160
        if (profile) {
588 600
                for (active_profile = profiles; active_profile->name;
589 480
                     active_profile++) {
590 560
                        if (strcmp(active_profile->name, profile) == 0)
591 80
                                break;
592 480
                }
593 120
        }
594 160
        AN(active_profile);
595 160
        if (!active_profile->name)
596 40
                VUT_Error(vut, 1, "-P: No such profile '%s'", profile);
597
598 120
        assert(active_profile->VSL_arg == 'b' ||
599
            active_profile->VSL_arg == 'c' ||
600
            active_profile->VSL_arg == 'E');
601 120
        assert(VUT_Arg(vut, active_profile->VSL_arg, NULL));
602 120
        match_tag = active_profile->tag;
603 120
        fnum = active_profile->field;
604 120
        hist_low = active_profile->hist_low;
605 120
        hist_high = active_profile->hist_high;
606
607 120
        hist_range = hist_high - hist_low;
608 120
        hist_buckets = hist_range * HIST_RES;
609 120
        bucket_hit = calloc(hist_buckets, sizeof *bucket_hit);
610 120
        bucket_miss = calloc(hist_buckets, sizeof *bucket_miss);
611
612 120
        if (timebend > 0)
613 40
                t0 = VTIM_mono();
614
615 120
        format = malloc(4L * fnum);
616 120
        AN(format);
617 440
        for (i = 0; i < fnum - 1; i++)
618 320
                strcpy(format + 4 * i, "%*s ");
619 120
        strcpy(format + 4 * (fnum - 1), "%lf");
620
621 120
        log_ten = log(10.0);
622
623 120
        VUT_Setup(vut);
624 120
        if (vut->vsm)
625 120
                ident = VSM_Dup(vut->vsm, "Arg", "-i");
626
        else
627 0
                ident = strdup("");
628 120
        PTOK(pthread_create(&thr, NULL, do_curses, NULL));
629 120
        vut->dispatch_f = accumulate;
630 120
        vut->dispatch_priv = NULL;
631 120
        (void)VUT_Main(vut);
632 120
        end_of_file = 1;
633 120
        PTOK(pthread_join(thr, NULL));
634 120
        VUT_Fini(&vut);
635 120
        exit(0);
636
}