From 61de71f0e0e88351c3603dca1b47aa95fd169ddb Mon Sep 17 00:00:00 2001 From: phk Date: Wed, 19 Jul 2006 21:14:41 +0000 Subject: [PATCH] Include a "start_time" timestamp in the stats and teach varnishstats to print it in curses mode. Some polishing and cleanup. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@509 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache_main.c | 1 + varnish-cache/bin/varnishd/shmlog.c | 2 +- varnish-cache/bin/varnishstat/varnishstat.c | 107 ++++++++++++-------- varnish-cache/include/stats.h | 1 + 4 files changed, 69 insertions(+), 42 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache_main.c b/varnish-cache/bin/varnishd/cache_main.c index 4b97c48a..8badc7ab 100644 --- a/varnish-cache/bin/varnishd/cache_main.c +++ b/varnish-cache/bin/varnishd/cache_main.c @@ -130,6 +130,7 @@ child_main(void) arm_keepalive(); printf("Ready\n"); + VSL_stats->start_time = time(NULL); i = event_base_loop(eb, 0); if (i != 0) printf("event_dispatch() = %d\n", i); diff --git a/varnish-cache/bin/varnishd/shmlog.c b/varnish-cache/bin/varnishd/shmlog.c index 2aacd26d..05a18c48 100644 --- a/varnish-cache/bin/varnishd/shmlog.c +++ b/varnish-cache/bin/varnishd/shmlog.c @@ -136,6 +136,7 @@ VSL_Init(void) AZ(pthread_mutex_init(&vsl_mutex, NULL)); loghead->starttime = time(NULL); VSL_stats = &loghead->stats; + memset(VSL_stats, 0, sizeof *VSL_stats); } /*--------------------------------------------------------------------*/ @@ -184,7 +185,6 @@ VSL_MgtInit(const char *fn, unsigned size) * management process as well. */ VSL_Init(); - memset(VSL_stats, 0, sizeof *VSL_stats); } /*--------------------------------------------------------------------*/ diff --git a/varnish-cache/bin/varnishstat/varnishstat.c b/varnish-cache/bin/varnishstat/varnishstat.c index 494323cb..38e678ab 100644 --- a/varnish-cache/bin/varnishstat/varnishstat.c +++ b/varnish-cache/bin/varnishstat/varnishstat.c @@ -24,22 +24,81 @@ myexp(double *acc, double val, unsigned *n, unsigned nmax) (*acc) += (val - *acc) / (double)*n; } -int -main(int argc, char **argv) +static void +do_curses(struct varnish_stats *VSL_stats) { - int c; - struct varnish_stats *VSL_stats, copy; - int c_flag = 0; + struct varnish_stats copy; intmax_t ju; struct timespec ts; double tt, lt, hit, miss, ratio; double a1, a2, a3; unsigned n1, n2, n3; + time_t rt; + int i; + - a1 = a2 = a3 = 0; + memset(©, 0, sizeof copy); + + a1 = a2 = a3 = 0.0; n1 = n2 = n3 = 0; + initscr(); + erase(); + lt = 0; + while (1) { + clock_gettime(CLOCK_REALTIME, &ts); + tt = ts.tv_nsec * 1e-9 + ts.tv_sec; + lt = tt - lt; + + rt = ts.tv_sec - VSL_stats->start_time; + + move(0,0); + i = 0; + if (rt > 86400) { + printw("%dd+", rt / 86400); + rt %= 86400; + i++; + } + printw("%02d:", rt / 3600); + rt %= 3600; + printw("%02d:", rt / 60); + rt %= 60; + printw("%02d\n", rt); + hit = (intmax_t)VSL_stats->cache_hit - + (intmax_t)copy.cache_hit; + miss = (intmax_t)VSL_stats->cache_miss - + (intmax_t)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); + } + printw("Hitrate ratio: %8u %8u %8u\n", n1, n2, n3); + printw("Hitrate avg: %8.4f %8.4f %8.4f\n", a1, a2, a3); + printw("\n"); + +#define MAC_STAT(n,t,f,d) \ + ju = VSL_stats->n; \ + printw("%12ju %10.2f " d "\n", ju, (ju - (intmax_t)copy.n)/lt); \ + copy.n = ju; +#include "stat_field.h" +#undef MAC_STAT + lt = tt; + refresh(); + sleep(1); + } +} + +int +main(int argc, char **argv) +{ + int c; + struct varnish_stats *VSL_stats; + int c_flag = 0; VSL_stats = VSL_OpenStats(); @@ -55,41 +114,7 @@ main(int argc, char **argv) } if (c_flag) { - memset(©, 0, sizeof copy); - initscr(); - erase(); - - while (1) { - clock_gettime(CLOCK_MONOTONIC, &ts); - tt = ts.tv_nsec * 1e-9 + ts.tv_sec; - lt = tt - lt; - move(0,0); - hit = (intmax_t)VSL_stats->cache_hit - - (intmax_t)copy.cache_hit; - miss = (intmax_t)VSL_stats->cache_miss - - (intmax_t)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); - } - printw("Hitrate ratio: %8u %8u %8u\n", n1, n2, n3); - printw("Hitrate avg: %8.4f %8.4f %8.4f\n", a1, a2, a3); - printw("\n"); - -#define MAC_STAT(n,t,f,d) \ - ju = VSL_stats->n; \ - printw("%12ju %10.2f " d "\n", ju, (ju - (intmax_t)copy.n)/lt); \ - copy.n = ju; -#include "stat_field.h" -#undef MAC_STAT - lt = tt; - refresh(); - sleep(1); - } + do_curses(VSL_stats); } else { #define MAC_STAT(n,t,f,d) \ diff --git a/varnish-cache/include/stats.h b/varnish-cache/include/stats.h index 2c91a485..5dacec53 100644 --- a/varnish-cache/include/stats.h +++ b/varnish-cache/include/stats.h @@ -3,6 +3,7 @@ #include struct varnish_stats { + time_t start_time; #define MAC_STAT(n,t,f,e) t n; #include "stat_field.h" #undef MAC_STAT -- 2.39.5