From 68d93b68d6fc4da19b193dd72b53620d55070260 Mon Sep 17 00:00:00 2001 From: phk Date: Sun, 20 Aug 2006 07:27:52 +0000 Subject: [PATCH] Add varnishhist(1) a program that shows the responsetime as a curses histogram. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@842 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/Makefile.am | 2 +- varnish-cache/bin/varnishhist/Makefile.am | 17 +++ varnish-cache/bin/varnishhist/varnishhist.c | 144 ++++++++++++++++++++ varnish-cache/configure.ac | 1 + 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 varnish-cache/bin/varnishhist/Makefile.am create mode 100644 varnish-cache/bin/varnishhist/varnishhist.c diff --git a/varnish-cache/bin/Makefile.am b/varnish-cache/bin/Makefile.am index 8dc68b0a..d31c6076 100644 --- a/varnish-cache/bin/Makefile.am +++ b/varnish-cache/bin/Makefile.am @@ -1,3 +1,3 @@ # $Id$ -SUBDIRS = varnishd varnishlog varnishncsa varnishstat varnishtop +SUBDIRS = varnishd varnishhist varnishlog varnishncsa varnishstat varnishtop diff --git a/varnish-cache/bin/varnishhist/Makefile.am b/varnish-cache/bin/varnishhist/Makefile.am new file mode 100644 index 00000000..c624d5ae --- /dev/null +++ b/varnish-cache/bin/varnishhist/Makefile.am @@ -0,0 +1,17 @@ +# $Id: Makefile.am 767 2006-08-08 12:31:19Z des $ + +INCLUDES = -I$(top_srcdir)/include + +bin_PROGRAMS = varnishhist + +# dist_man_MANS = varnishlog.1 + +varnishhist_SOURCES = varnishhist.c + +varnishhist_CFLAGS = -include config.h + +varnishhist_LDADD = \ + $(top_builddir)/lib/libcompat/libcompat.a \ + $(top_builddir)/lib/libvarnish/libvarnish.la \ + $(top_builddir)/lib/libvarnishapi/libvarnishapi.la \ + -lm -lcurses diff --git a/varnish-cache/bin/varnishhist/varnishhist.c b/varnish-cache/bin/varnishhist/varnishhist.c new file mode 100644 index 00000000..f0860ce8 --- /dev/null +++ b/varnish-cache/bin/varnishhist/varnishhist.c @@ -0,0 +1,144 @@ +/* + * $Id: varnishlog.c 833 2006-08-18 20:07:37Z phk $ + * + * Log tailer for Varnish + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "compat/vis.h" + +#include "libvarnish.h" +#include "shmlog.h" +#include "varnishapi.h" + +#define HIST_LOW -55 +#define HIST_HIGH 20 +#define HIST_W (1 + (HIST_HIGH - HIST_LOW)) +#define HIST_N 1000 + +static unsigned char rr_hist[HIST_N]; +static unsigned next_hist; +static unsigned bucket_hist[HIST_W]; +static double c_hist; + +static void +r_hist(void) +{ + int x, y; + double m, r; + + m = 0; + r = 0; + for (x = 0; x < HIST_W; x++) { + if (bucket_hist[x] > m) + m = bucket_hist[x]; + r += bucket_hist[x]; + } + + mvprintw(0, 0, "Max %.0f Scale %u Tot: %.0f", m, HIST_N, r); + m = HIST_N / (LINES - 3); + move(1,0); + for (y = LINES - 3; y > 0; y--) { + if (y == 1) + r = 0; + else + r = y * m; + for (x = 0; x < HIST_W; x++) + addch(bucket_hist[x] > r ? '#' : ' '); + addch('\n'); + } + refresh(); +} + +static int +h_hist(void *priv, unsigned tag, unsigned fd, unsigned len, unsigned spec, const char *ptr) +{ + double b; + int i; + + (void)priv; + (void)fd; + (void)len; + (void)spec; + if (tag != SLT_ReqEnd) + return (0); + i = sscanf(ptr, "%*d %*f %*f %lf", &b); + assert(i == 1); + i = log(b) * c_hist; + if (i < HIST_LOW) + i = HIST_LOW; + if (i > HIST_HIGH) + i = HIST_HIGH; + i -= HIST_LOW; + bucket_hist[rr_hist[next_hist]]--; + rr_hist[next_hist] = i; + bucket_hist[rr_hist[next_hist]]++; + if (++next_hist == HIST_N) { + next_hist = 0; + } + if (!(next_hist % 100)) + r_hist(); + return (0); +} + +/*--------------------------------------------------------------------*/ + +static void +usage(void) +{ + fprintf(stderr, + "usage: varnishhist"); + exit(1); +} + +int +main(int argc, char **argv) +{ + int i, c, x; + struct VSL_data *vd; + + vd = VSL_New(); + + while ((c = getopt(argc, argv, VSL_ARGS)) != -1) { + switch (c) { + default: + if (VSL_Arg(vd, c, optarg) > 0) + break; + usage(); + } + } + + c_hist = 10.0 / log(10.0); + initscr(); + erase(); + + bucket_hist[0] = HIST_N; + move(LINES - 2, 0); + for (x = 0; x < HIST_W; x++) + addch('-'); + + for (x = 0; x < HIST_W; x++) { + if ((x + HIST_LOW) % 10 != 0) + continue; + mvprintw(LINES - 2, x, "+"); + mvprintw(LINES - 1, x, "|1e%d", (x + HIST_LOW) / 10); + } + + while (1) { + i = VSL_Dispatch(vd, h_hist, NULL); + if (i < 0) + break; + if (i == 0) + r_hist(); + } + + return (0); +} diff --git a/varnish-cache/configure.ac b/varnish-cache/configure.ac index 925eb4c7..c3faca72 100644 --- a/varnish-cache/configure.ac +++ b/varnish-cache/configure.ac @@ -98,6 +98,7 @@ AC_CONFIG_FILES([ bin/Makefile bin/varnishd/Makefile bin/varnishlog/Makefile + bin/varnishhist/Makefile bin/varnishncsa/Makefile bin/varnishstat/Makefile bin/varnishtop/Makefile -- 2.39.5