From: phk Date: Wed, 28 Jun 2006 09:21:15 +0000 (+0000) Subject: Add statistics counter support. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=37563472cce2abd75481e3dfe5b70fc0707986f4;p=varnish Add statistics counter support. 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 --- diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 14cd224e..e2bf5fea 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -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); diff --git a/varnish-cache/bin/varnishd/cache_acceptor.c b/varnish-cache/bin/varnishd/cache_acceptor.c index 62b1e226..a3944587 100644 --- a/varnish-cache/bin/varnishd/cache_acceptor.c +++ b/varnish-cache/bin/varnishd/cache_acceptor.c @@ -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); /* diff --git a/varnish-cache/bin/varnishd/cache_shmlog.c b/varnish-cache/bin/varnishd/cache_shmlog.c index 195571a1..de42fadf 100644 --- a/varnish-cache/bin/varnishd/cache_shmlog.c +++ b/varnish-cache/bin/varnishd/cache_shmlog.c @@ -3,15 +3,22 @@ */ #include +#include +#include +#include +#include #include #include #include #include +#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); } + diff --git a/varnish-cache/bin/varnishd/mgt.h b/varnish-cache/bin/varnishd/mgt.h index 711a8360..364b4c26 100644 --- a/varnish-cache/bin/varnishd/mgt.h +++ b/varnish-cache/bin/varnishd/mgt.h @@ -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; diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index 7c72d149..17cf40e0 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -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 #include @@ -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(); diff --git a/varnish-cache/include/shmlog.h b/varnish-cache/include/shmlog.h index 52603afb..2e61101b 100644 --- a/varnish-cache/include/shmlog.h +++ b/varnish-cache/include/shmlog.h @@ -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 index 00000000..97202b5f --- /dev/null +++ b/varnish-cache/include/stat_field.h @@ -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 index 00000000..59f273b2 --- /dev/null +++ b/varnish-cache/include/stats.h @@ -0,0 +1,9 @@ +/* $Id$ */ + +#include + +struct varnish_stats { +#define MAC_STAT(n,t,f,e) t n; +#include "stat_field.h" +#undef MAC_STAT +};