r842 - in trunk/varnish-cache: . bin bin/varnishhist

phk at projects.linpro.no phk at projects.linpro.no
Sun Aug 20 09:27:53 CEST 2006


Author: phk
Date: 2006-08-20 09:27:52 +0200 (Sun, 20 Aug 2006)
New Revision: 842

Added:
   trunk/varnish-cache/bin/varnishhist/
   trunk/varnish-cache/bin/varnishhist/Makefile.am
   trunk/varnish-cache/bin/varnishhist/varnishhist.c
Modified:
   trunk/varnish-cache/bin/Makefile.am
   trunk/varnish-cache/configure.ac
Log:
Add varnishhist(1) a program that shows the responsetime as a curses
histogram.



Modified: trunk/varnish-cache/bin/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/Makefile.am	2006-08-20 07:27:33 UTC (rev 841)
+++ trunk/varnish-cache/bin/Makefile.am	2006-08-20 07:27:52 UTC (rev 842)
@@ -1,3 +1,3 @@
 # $Id$
 
-SUBDIRS = varnishd varnishlog varnishncsa varnishstat varnishtop
+SUBDIRS = varnishd varnishhist varnishlog varnishncsa varnishstat varnishtop

Added: trunk/varnish-cache/bin/varnishhist/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishhist/Makefile.am	2006-08-20 07:27:33 UTC (rev 841)
+++ trunk/varnish-cache/bin/varnishhist/Makefile.am	2006-08-20 07:27:52 UTC (rev 842)
@@ -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

Added: trunk/varnish-cache/bin/varnishhist/varnishhist.c
===================================================================
--- trunk/varnish-cache/bin/varnishhist/varnishhist.c	2006-08-20 07:27:33 UTC (rev 841)
+++ trunk/varnish-cache/bin/varnishhist/varnishhist.c	2006-08-20 07:27:52 UTC (rev 842)
@@ -0,0 +1,144 @@
+/*
+ * $Id: varnishlog.c 833 2006-08-18 20:07:37Z phk $
+ *
+ * Log tailer for Varnish
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <regex.h>
+#include <math.h>
+#include <curses.h>
+
+#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);
+}

Modified: trunk/varnish-cache/configure.ac
===================================================================
--- trunk/varnish-cache/configure.ac	2006-08-20 07:27:33 UTC (rev 841)
+++ trunk/varnish-cache/configure.ac	2006-08-20 07:27:52 UTC (rev 842)
@@ -98,6 +98,7 @@
     bin/Makefile
     bin/varnishd/Makefile
     bin/varnishlog/Makefile
+    bin/varnishhist/Makefile
     bin/varnishncsa/Makefile
     bin/varnishstat/Makefile
     bin/varnishtop/Makefile




More information about the varnish-commit mailing list