]> err.no Git - varnish/commitdiff
Add statistics counter support.
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Wed, 28 Jun 2006 09:21:15 +0000 (09:21 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Wed, 28 Jun 2006 09:21:15 +0000 (09:21 +0000)
stat_field.h defines the counter fields with name, type, (printf)format
and description.

stats.h defines a structure with these fields.

shmlog.h makes the structure  part of the shared memory logs header.

Implent the "stats" CLI word in the management process.

git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@243 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/cache.h
varnish-cache/bin/varnishd/cache_acceptor.c
varnish-cache/bin/varnishd/cache_shmlog.c
varnish-cache/bin/varnishd/mgt.h
varnish-cache/bin/varnishd/varnishd.c
varnish-cache/include/shmlog.h
varnish-cache/include/stat_field.h [new file with mode: 0644]
varnish-cache/include/stats.h [new file with mode: 0644]

index 14cd224eac8a6bce5a7a558e7027593a25b1eb90..e2bf5fea56e40ba6f8d5b6d38d52b16cd528287a 100644 (file)
@@ -200,6 +200,7 @@ void VSL(enum shmlogtag tag, unsigned id, const char *fmt, ...);
        assert(__LINE__ == 0);                                          \
        } while (0)
 #endif
+extern struct varnish_stats *VSL_stats;
 
 /* cache_vcl.c */
 void RelVCL(struct VCL_conf *vc);
index 62b1e22698ab1cc19544ce414328c9a112032d1a..a3944587d015cc41281708cc157b73f2646dfc5b 100644 (file)
@@ -122,6 +122,8 @@ accept_f(int fd, short event, void *arg)
        char port[10];
        int i;
 
+       VSL_stats->cli_conn++;
+
        (void)arg;
        sm = calloc(sizeof *sm, 1);
        assert(sm != NULL);     /*
index 195571a170972fd860ef3f2dbd01aaf82d8f677c..de42fadfd7890caefa79dd6fbf03b244fa7dfe18 100644 (file)
@@ -3,15 +3,22 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <assert.h>
 #include <string.h>
 #include <stdarg.h>
 #include <sys/mman.h>
 
+#include "libvarnish.h"
 #include "shmlog.h"
 
 #include "heritage.h"
 
+struct varnish_stats *VSL_stats;
+
 static struct shmloghead *loghead;
 static unsigned char *logstart, *logend;
 
@@ -130,4 +137,45 @@ VSL_Init(void)
        /* XXX check sanity of loghead */
        logstart = (unsigned char *)loghead + loghead->start;
        logend = logstart + loghead->size;
+       VSL_stats = &loghead->stats;
+}
+
+/*--------------------------------------------------------------------*/
+
+void
+VSL_MgtInit(const char *fn, unsigned size)
+{
+       struct shmloghead slh;
+       int i;
+
+       heritage.vsl_fd = open(fn, O_RDWR | O_CREAT, 0600);
+       if (heritage.vsl_fd < 0) {
+               fprintf(stderr, "Could not open %s: %s\n",
+                   fn, strerror(errno));
+               exit (1);
+       }
+       i = read(heritage.vsl_fd, &slh, sizeof slh);
+       if (i != sizeof slh || slh.magic != SHMLOGHEAD_MAGIC) {
+               /* XXX more checks */
+
+               slh.magic = SHMLOGHEAD_MAGIC;
+               slh.size = size;
+               slh.ptr = 0;
+               slh.start = sizeof slh;
+               AZ(lseek(heritage.vsl_fd, 0, SEEK_SET));
+               i = write(heritage.vsl_fd, &slh, sizeof slh);
+               assert(i == sizeof slh);
+               AZ(ftruncate(heritage.vsl_fd, sizeof slh + size));
+               heritage.vsl_size = slh.size + slh.start;
+       } else {
+               heritage.vsl_size = slh.size + slh.start;
+       }
+
+       /*
+        * Call VSL_Init so that we get a VSL_stats pointer in the
+        * management process as well.
+        */
+       VSL_Init();
+       memset(VSL_stats, 0, sizeof *VSL_stats);
 }
+
index 711a8360afe610d5c2cbd61e814244cd5b4b58e0..364b4c26823a6aabaee1f5deae7052da41821ff7 100644 (file)
@@ -18,3 +18,6 @@ int open_tcp(const char *port);
 
 extern struct stevedore sma_stevedore;
 extern struct stevedore smf_stevedore;
+
+void VSL_MgtInit(const char *fn, unsigned size);
+extern struct varnish_stats *VSL_stats;
index 7c72d149acf87586dd8bbfc07c19ef9440487a9b..17cf40e02814089ff9cdd282f716223c38b47e85 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "mgt.h"
 #include "heritage.h"
+#include "shmlog.h"
 #include "cli_event.h"
 
 /*--------------------------------------------------------------------*/
@@ -49,7 +50,7 @@ cli_passthrough_cb(unsigned u, const char *r, void *priv)
 }
 
 static void
-cli_func_passthrough(struct cli *cli, char **av __unused, void *priv)
+m_cli_func_passthrough(struct cli *cli, char **av __unused, void *priv)
 {
 
        cli_suspend(cli);
@@ -128,7 +129,7 @@ vcl_default(const char *bflag)
 }
 
 static void
-cli_func_config_inline(struct cli *cli, char **av, void *priv __unused)
+m_cli_func_config_inline(struct cli *cli, char **av, void *priv __unused)
 {
        char *vf;
        struct sbuf *sb;
@@ -193,7 +194,7 @@ vcl_file(const char *fflag)
 /*--------------------------------------------------------------------*/
 
 static void
-cli_func_server_start(struct cli *cli, char **av __unused, void *priv __unused)
+m_cli_func_server_start(struct cli *cli, char **av __unused, void *priv __unused)
 {
 
        mgt_child_start();
@@ -202,7 +203,7 @@ cli_func_server_start(struct cli *cli, char **av __unused, void *priv __unused)
 /*--------------------------------------------------------------------*/
 
 static void
-cli_func_server_stop(struct cli *cli, char **av __unused, void *priv __unused)
+m_cli_func_server_stop(struct cli *cli, char **av __unused, void *priv __unused)
 {
 
        mgt_child_stop();
@@ -211,7 +212,7 @@ cli_func_server_stop(struct cli *cli, char **av __unused, void *priv __unused)
 /*--------------------------------------------------------------------*/
 
 static void
-cli_func_verbose(struct cli *cli, char **av __unused, void *priv)
+m_cli_func_verbose(struct cli *cli, char **av __unused, void *priv)
 {
 
        cli->verbose = !cli->verbose;
@@ -219,7 +220,7 @@ cli_func_verbose(struct cli *cli, char **av __unused, void *priv)
 
 
 static void
-cli_func_ping(struct cli *cli, char **av, void *priv __unused)
+m_cli_func_ping(struct cli *cli, char **av, void *priv __unused)
 {
        time_t t;
 
@@ -232,28 +233,41 @@ cli_func_ping(struct cli *cli, char **av, void *priv __unused)
 
 /*--------------------------------------------------------------------*/
 
+static void
+m_cli_func_stats(struct cli *cli, char **av, void *priv __unused)
+{
+
+       assert (VSL_stats != NULL);
+#define MAC_STAT(n,t,f,d) \
+    cli_out(cli, "%12ju  " d "\n", (VSL_stats->n));
+#include "stat_field.h"
+#undef MAC_STAT
+}
+
+/*--------------------------------------------------------------------*/
+
 static struct cli_proto cli_proto[] = {
        /* URL manipulation */
-       { CLI_URL_QUERY,        cli_func_passthrough, NULL },
-       { CLI_URL_PURGE,        cli_func_passthrough, NULL },
-       { CLI_URL_STATUS,       cli_func_passthrough, NULL },
+       { CLI_URL_QUERY,        m_cli_func_passthrough, NULL },
+       { CLI_URL_PURGE,        m_cli_func_passthrough, NULL },
+       { CLI_URL_STATUS,       m_cli_func_passthrough, NULL },
        { CLI_CONFIG_LOAD,      m_cli_func_config_load, NULL },
-       { CLI_CONFIG_INLINE,    cli_func_config_inline, NULL },
-       { CLI_CONFIG_UNLOAD,    cli_func_passthrough, NULL },
-       { CLI_CONFIG_LIST,      cli_func_passthrough, NULL },
-       { CLI_CONFIG_USE,       cli_func_passthrough, NULL },
-       { CLI_SERVER_FREEZE,    cli_func_passthrough, NULL },
-       { CLI_SERVER_THAW,      cli_func_passthrough, NULL },
-       { CLI_SERVER_SUSPEND,   cli_func_passthrough, NULL },
-       { CLI_SERVER_RESUME,    cli_func_passthrough, NULL },
-       { CLI_SERVER_STOP,      cli_func_server_stop, NULL },
-       { CLI_SERVER_START,     cli_func_server_start, NULL },
+       { CLI_CONFIG_INLINE,    m_cli_func_config_inline, NULL },
+       { CLI_CONFIG_UNLOAD,    m_cli_func_passthrough, NULL },
+       { CLI_CONFIG_LIST,      m_cli_func_passthrough, NULL },
+       { CLI_CONFIG_USE,       m_cli_func_passthrough, NULL },
+       { CLI_SERVER_FREEZE,    m_cli_func_passthrough, NULL },
+       { CLI_SERVER_THAW,      m_cli_func_passthrough, NULL },
+       { CLI_SERVER_SUSPEND,   m_cli_func_passthrough, NULL },
+       { CLI_SERVER_RESUME,    m_cli_func_passthrough, NULL },
+       { CLI_SERVER_STOP,      m_cli_func_server_stop, NULL },
+       { CLI_SERVER_START,     m_cli_func_server_start, NULL },
        { CLI_SERVER_RESTART },
-       { CLI_PING,             cli_func_ping, NULL },
-       { CLI_STATS },
+       { CLI_PING,             m_cli_func_ping, NULL },
+       { CLI_STATS,            m_cli_func_stats, NULL },
        { CLI_ZERO },
        { CLI_HELP,             cli_func_help, cli_proto },
-       { CLI_VERBOSE,          cli_func_verbose, NULL },
+       { CLI_VERBOSE,          m_cli_func_verbose, NULL },
        { CLI_EXIT },
        { CLI_QUIT },
        { CLI_BYE },
@@ -347,39 +361,6 @@ setup_storage(const char *sflag)
 
 /*--------------------------------------------------------------------*/
 
-#include "shmlog.h"
-
-static void
-init_vsl(const char *fn, unsigned size)
-{
-       struct shmloghead slh;
-       int i;
-
-       heritage.vsl_fd = open(fn, O_RDWR | O_CREAT, 0600);
-       if (heritage.vsl_fd < 0) {
-               fprintf(stderr, "Could not open %s: %s\n",
-                   fn, strerror(errno));
-               exit (1);
-       }
-       i = read(heritage.vsl_fd, &slh, sizeof slh);
-       if (i == sizeof slh && slh.magic == SHMLOGHEAD_MAGIC) {
-               /* XXX more checks */
-               heritage.vsl_size = slh.size + slh.start;
-               return;
-       }
-       slh.magic = SHMLOGHEAD_MAGIC;
-       slh.size = size;
-       slh.ptr = 0;
-       slh.start = sizeof slh;
-       AZ(lseek(heritage.vsl_fd, 0, SEEK_SET));
-       i = write(heritage.vsl_fd, &slh, sizeof slh);
-       assert(i == sizeof slh);
-       AZ(ftruncate(heritage.vsl_fd, sizeof slh + size));
-       heritage.vsl_size = slh.size + slh.start;
-}
-
-/*--------------------------------------------------------------------*/
-
 /* for development purposes */
 #include <printf.h>
 #include <err.h>
@@ -459,7 +440,7 @@ main(int argc, char *argv[])
         */
        open_tcp(portnumber);
 
-       init_vsl(SHMLOG_FILENAME, 1024*1024);
+       VSL_MgtInit(SHMLOG_FILENAME, 1024*1024);
 
        testme();
 
index 52603afbbd182f15dba9b5eee231d15e00d7e65c..2e61101b04100ca096305e3bcc678ad55b026e59 100644 (file)
@@ -9,21 +9,25 @@
 
 #define SHMLOG_FILENAME                "/tmp/_.vsl"
 
+#include "stats.h"
+
 struct shmloghead {
 #define SHMLOGHEAD_MAGIC       4185512498U     /* From /dev/random */
-       unsigned        magic;
+       unsigned                magic;
 
        /*
         * Byte offset into the file where the fifolog starts
         * This allows the header to expand later.
         */
-       unsigned        start;
+       unsigned                start;
 
        /* Length of the fifolog area in bytes */
-       unsigned        size;
+       unsigned                size;
 
        /* Current write position relative to the beginning of start */
-       unsigned        ptr;
+       unsigned                ptr;
+
+       struct varnish_stats    stats;
 };
 
 /*
diff --git a/varnish-cache/include/stat_field.h b/varnish-cache/include/stat_field.h
new file mode 100644 (file)
index 0000000..97202b5
--- /dev/null
@@ -0,0 +1,11 @@
+/* $Id$ */
+
+MAC_STAT(cli_conn,     uint64_t, "u", "Client connections accepted")
+MAC_STAT(cli_req,      uint64_t, "u", "Client requests received")
+
+MAC_STAT(cache_hit,    uint64_t, "u", "Cache hits")
+MAC_STAT(cache_miss,   uint64_t, "u", "Cache misses")
+
+MAC_STAT(backend_conn, uint64_t, "u", "Backend connections initiated")
+MAC_STAT(backend_req,  uint64_t, "u", "Backend requests sent")
+
diff --git a/varnish-cache/include/stats.h b/varnish-cache/include/stats.h
new file mode 100644 (file)
index 0000000..59f273b
--- /dev/null
@@ -0,0 +1,9 @@
+/* $Id$ */
+
+#include <sys/types.h>
+
+struct varnish_stats {
+#define MAC_STAT(n,t,f,e)      t n;
+#include "stat_field.h"
+#undef MAC_STAT
+};