r4849 - trunk/varnish-cache/bin/varnishstat

phk at varnish-cache.org phk at varnish-cache.org
Fri May 21 12:53:34 CEST 2010


Author: phk
Date: 2010-05-21 12:53:34 +0200 (Fri, 21 May 2010)
New Revision: 4849

Added:
   trunk/varnish-cache/bin/varnishstat/varnishstat.h
   trunk/varnish-cache/bin/varnishstat/varnishstat_curses.c
Modified:
   trunk/varnish-cache/bin/varnishstat/Makefile.am
   trunk/varnish-cache/bin/varnishstat/varnishstat.c
Log:
Move the curses stuff into its own file



Modified: trunk/varnish-cache/bin/varnishstat/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishstat/Makefile.am	2010-05-21 10:34:52 UTC (rev 4848)
+++ trunk/varnish-cache/bin/varnishstat/Makefile.am	2010-05-21 10:53:34 UTC (rev 4849)
@@ -6,7 +6,11 @@
 
 dist_man_MANS = varnishstat.1
 
-varnishstat_SOURCES = varnishstat.c
+varnishstat_SOURCES = \
+	varnishstat.h \
+	\
+	varnishstat.c \
+	varnishstat_curses.c
 
 varnishstat_LDADD = \
 	$(top_builddir)/lib/libvarnish/libvarnish.la \

Modified: trunk/varnish-cache/bin/varnishstat/varnishstat.c
===================================================================
--- trunk/varnish-cache/bin/varnishstat/varnishstat.c	2010-05-21 10:34:52 UTC (rev 4848)
+++ trunk/varnish-cache/bin/varnishstat/varnishstat.c	2010-05-21 10:53:34 UTC (rev 4849)
@@ -27,7 +27,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * Log tailer for Varnish
+ * Statistics output program
  */
 
 #include "config.h"
@@ -37,9 +37,7 @@
 
 #include <sys/time.h>
 
-#include <curses.h>
 #include <errno.h>
-#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -49,22 +47,11 @@
 #include "libvarnish.h"
 #include "shmlog.h"
 #include "varnishapi.h"
+#include "varnishstat.h"
 
 #define FIELD_EXCLUSION_CHARACTER '^'
 
-#define AC(x) assert((x) != ERR)
-
-
-static void
-myexp(double *acc, double val, unsigned *n, unsigned nmax)
-{
-
-	if (*n < nmax)
-		(*n)++;
-	(*acc) += (val - *acc) / (double)*n;
-}
-
-static int
+int
 show_field(const char* field, const char *fields)
 {
 	const char* field_start;
@@ -95,127 +82,6 @@
 }
 
 static void
-do_curses(struct VSL_data *vd, const struct varnish_stats *VSL_stats, int delay, const char *fields)
-{
-	struct varnish_stats copy;
-	struct varnish_stats seen;
-	intmax_t ju;
-	struct timeval tv;
-	double tt, lt, hit, miss, ratio, up;
-	double a1, a2, a3;
-	unsigned n1, n2, n3;
-	time_t rt;
-	int ch, line;
-
-	memset(&copy, 0, sizeof copy);
-	memset(&seen, 0, sizeof seen);
-
-	a1 = a2 = a3 = 0.0;
-	n1 = n2 = n3 = 0;
-
-	(void)initscr();
-	AC(raw());
-	AC(noecho());
-	AC(nonl());
-	AC(intrflush(stdscr, FALSE));
-	AC(curs_set(0));
-	AC(erase());
-
-	lt = 0;
-	while (1) {
-		AZ(gettimeofday(&tv, NULL));
-		tt = tv.tv_usec * 1e-6 + tv.tv_sec;
-		lt = tt - lt;
-
-		rt = VSL_stats->uptime;
-		up = rt;
-
-		AC(mvprintw(0, 0, "%*s", COLS - 1, VSL_Name(vd)));
-		AC(mvprintw(0, 0, "%d+%02d:%02d:%02d", rt / 86400,
-		    (rt % 86400) / 3600, (rt % 3600) / 60, rt % 60));
-
-		hit = VSL_stats->cache_hit - copy.cache_hit;
-		miss = VSL_stats->cache_miss - copy.cache_miss;
-		hit /= lt;
-		miss /= lt;
-		if (hit + miss != 0) {
-			ratio = hit / (hit + miss);
-			myexp(&a1, ratio, &n1, 10);
-			myexp(&a2, ratio, &n2, 100);
-			myexp(&a3, ratio, &n3, 1000);
-		}
-		AC(mvprintw(1, 0, "Hitrate ratio: %8u %8u %8u", n1, n2, n3));
-		AC(mvprintw(2, 0, "Hitrate avg:   %8.4f %8.4f %8.4f", a1, a2, a3));
-
-		line = 3;
-#define MAC_STAT(n, t, l, ff, d)					\
-	if ((fields == NULL || show_field( #n, fields )) && line < LINES) { \
-		ju = VSL_stats->n;					\
-		if (ju == 0 && !seen.n) {				\
-		} else if (ff == 'a') {					\
-			seen.n = 1;					\
-			line++;						\
-			AC(mvprintw(line, 0,				\
-			    "%12ju %12.2f %12.2f %s\n",			\
-			    ju, (ju - (intmax_t)copy.n)/lt,		\
-			    ju / up, d));				\
-			copy.n = ju;					\
-		} else {						\
-			seen.n = 1;					\
-			line++;						\
-			AC(mvprintw(line, 0, "%12ju %12s %12s %s\n",	\
-			    ju, ".  ", ".  ", d));			\
-		}							\
-	}
-#include "stat_field.h"
-#undef MAC_STAT
-		lt = tt;
-		AC(refresh());
-		timeout(delay * 1000);
-		switch ((ch = getch())) {
-		case ERR:
-			break;
-#ifdef KEY_RESIZE
-		case KEY_RESIZE:
-			AC(erase());
-			break;
-#endif
-		case '\014': /* Ctrl-L */
-		case '\024': /* Ctrl-T */
-			AC(redrawwin(stdscr));
-			AC(refresh());
-			break;
-		case '\003': /* Ctrl-C */
-			AZ(raise(SIGINT));
-			break;
-		case '\032': /* Ctrl-Z */
-			AZ(raise(SIGTSTP));
-			break;
-		case '\021': /* Ctrl-Q */
-		case 'Q':
-		case 'q':
-			AC(endwin());
-			exit(0);
-		case '0':
-		case '1':
-		case '2':
-		case '3':
-		case '4':
-		case '5':
-		case '6':
-		case '7':
-		case '8':
-		case '9':
-			delay = 1U << (ch - '0');
-			break;
-		default:
-			AC(beep());
-			break;
-		}
-	}
-}
-
-static void
 do_xml(const struct varnish_stats *VSL_stats, const char* fields)
 {
 	char time_stamp[20];
@@ -388,6 +254,9 @@
 		}
 	}
 
+	if (VSL_Open(vd))
+		exit(1);
+
 	if ((VSL_stats = VSL_OpenStats(vd)) == NULL)
 		exit(1);
 

Added: trunk/varnish-cache/bin/varnishstat/varnishstat.h
===================================================================
--- trunk/varnish-cache/bin/varnishstat/varnishstat.h	                        (rev 0)
+++ trunk/varnish-cache/bin/varnishstat/varnishstat.h	2010-05-21 10:53:34 UTC (rev 4849)
@@ -0,0 +1,31 @@
+/*-
+ * Copyright (c) 2010 Linpro AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+int show_field(const char* field, const char *fields);
+void do_curses(struct VSL_data *vd, const struct varnish_stats *VSL_stats, int delay, const char *fields);

Copied: trunk/varnish-cache/bin/varnishstat/varnishstat_curses.c (from rev 4847, trunk/varnish-cache/bin/varnishstat/varnishstat.c)
===================================================================
--- trunk/varnish-cache/bin/varnishstat/varnishstat_curses.c	                        (rev 0)
+++ trunk/varnish-cache/bin/varnishstat/varnishstat_curses.c	2010-05-21 10:53:34 UTC (rev 4849)
@@ -0,0 +1,184 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2010 Linpro AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ * Author: Dag-Erling Smørgrav <des at des.no>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Statistics output program
+ */
+
+#include "config.h"
+
+#include "svnid.h"
+SVNID("$Id$")
+
+#include <sys/time.h>
+
+#include <curses.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "libvarnish.h"
+#include "shmlog.h"
+#include "varnishapi.h"
+#include "varnishstat.h"
+
+#define AC(x) assert((x) != ERR)
+
+static void
+myexp(double *acc, double val, unsigned *n, unsigned nmax)
+{
+
+	if (*n < nmax)
+		(*n)++;
+	(*acc) += (val - *acc) / (double)*n;
+}
+
+void
+do_curses(struct VSL_data *vd, const struct varnish_stats *VSL_stats, int delay, const char *fields)
+{
+	struct varnish_stats copy;
+	struct varnish_stats seen;
+	intmax_t ju;
+	struct timeval tv;
+	double tt, lt, hit, miss, ratio, up;
+	double a1, a2, a3;
+	unsigned n1, n2, n3;
+	time_t rt;
+	int ch, line;
+
+	memset(&copy, 0, sizeof copy);
+	memset(&seen, 0, sizeof seen);
+
+	a1 = a2 = a3 = 0.0;
+	n1 = n2 = n3 = 0;
+
+	(void)initscr();
+	AC(raw());
+	AC(noecho());
+	AC(nonl());
+	AC(intrflush(stdscr, FALSE));
+	AC(curs_set(0));
+	AC(erase());
+
+	lt = 0;
+	while (1) {
+		AZ(gettimeofday(&tv, NULL));
+		tt = tv.tv_usec * 1e-6 + tv.tv_sec;
+		lt = tt - lt;
+
+		rt = VSL_stats->uptime;
+		up = rt;
+
+		AC(mvprintw(0, 0, "%*s", COLS - 1, VSL_Name(vd)));
+		AC(mvprintw(0, 0, "%d+%02d:%02d:%02d", rt / 86400,
+		    (rt % 86400) / 3600, (rt % 3600) / 60, rt % 60));
+
+		hit = VSL_stats->cache_hit - copy.cache_hit;
+		miss = VSL_stats->cache_miss - copy.cache_miss;
+		hit /= lt;
+		miss /= lt;
+		if (hit + miss != 0) {
+			ratio = hit / (hit + miss);
+			myexp(&a1, ratio, &n1, 10);
+			myexp(&a2, ratio, &n2, 100);
+			myexp(&a3, ratio, &n3, 1000);
+		}
+		AC(mvprintw(1, 0, "Hitrate ratio: %8u %8u %8u", n1, n2, n3));
+		AC(mvprintw(2, 0, "Hitrate avg:   %8.4f %8.4f %8.4f", a1, a2, a3));
+
+		line = 3;
+#define MAC_STAT(n, t, l, ff, d)					\
+	if ((fields == NULL || show_field( #n, fields )) && line < LINES) { \
+		ju = VSL_stats->n;					\
+		if (ju == 0 && !seen.n) {				\
+		} else if (ff == 'a') {					\
+			seen.n = 1;					\
+			line++;						\
+			AC(mvprintw(line, 0,				\
+			    "%12ju %12.2f %12.2f %s\n",			\
+			    ju, (ju - (intmax_t)copy.n)/lt,		\
+			    ju / up, d));				\
+			copy.n = ju;					\
+		} else {						\
+			seen.n = 1;					\
+			line++;						\
+			AC(mvprintw(line, 0, "%12ju %12s %12s %s\n",	\
+			    ju, ".  ", ".  ", d));			\
+		}							\
+	}
+#include "stat_field.h"
+#undef MAC_STAT
+		lt = tt;
+		AC(refresh());
+		timeout(delay * 1000);
+		switch ((ch = getch())) {
+		case ERR:
+			break;
+#ifdef KEY_RESIZE
+		case KEY_RESIZE:
+			AC(erase());
+			break;
+#endif
+		case '\014': /* Ctrl-L */
+		case '\024': /* Ctrl-T */
+			AC(redrawwin(stdscr));
+			AC(refresh());
+			break;
+		case '\003': /* Ctrl-C */
+			AZ(raise(SIGINT));
+			break;
+		case '\032': /* Ctrl-Z */
+			AZ(raise(SIGTSTP));
+			break;
+		case '\021': /* Ctrl-Q */
+		case 'Q':
+		case 'q':
+			AC(endwin());
+			exit(0);
+		case '0':
+		case '1':
+		case '2':
+		case '3':
+		case '4':
+		case '5':
+		case '6':
+		case '7':
+		case '8':
+		case '9':
+			delay = 1U << (ch - '0');
+			break;
+		default:
+			AC(beep());
+			break;
+		}
+	}
+}




More information about the varnish-commit mailing list