From e33e0f0958c8a5ea7935e5a706b9207819804f71 Mon Sep 17 00:00:00 2001 From: phk Date: Thu, 23 Mar 2006 15:31:20 +0000 Subject: [PATCH] Add shared memory log setup and stuffer function in the child process. Log whenever we get a CLI ping git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@62 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/Makefile.am | 1 + varnish-cache/bin/varnishd/cache.h | 8 +++ varnish-cache/bin/varnishd/cache_main.c | 7 +- varnish-cache/bin/varnishd/cache_shmlog.c | 87 +++++++++++++++++++++++ varnish-cache/bin/varnishd/heritage.h | 4 ++ varnish-cache/bin/varnishd/varnishd.c | 37 +++++++++- 6 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 varnish-cache/bin/varnishd/cache_shmlog.c diff --git a/varnish-cache/bin/varnishd/Makefile.am b/varnish-cache/bin/varnishd/Makefile.am index 0f49ac31..4d3fcf14 100644 --- a/varnish-cache/bin/varnishd/Makefile.am +++ b/varnish-cache/bin/varnishd/Makefile.am @@ -7,6 +7,7 @@ bin_PROGRAMS = varnishd varnishd_SOURCES = \ cache_acceptor.c \ cache_main.c \ + cache_shmlog.c \ cli_event.c \ mgt_child.c \ tcp.c \ diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 0f371315..2bd42f61 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -12,3 +12,11 @@ struct sess { /* cache_acceptor.c */ void *vca_main(void *arg); + +/* cache_shmlog.c */ +void VSL_Init(void); +#ifdef SHMLOGHEAD_MAGIC +void VSL(enum shmlogtag tag, unsigned id, const char *fmt, ...); +#endif + + diff --git a/varnish-cache/bin/varnishd/cache_main.c b/varnish-cache/bin/varnishd/cache_main.c index 266fd172..59da0e1f 100644 --- a/varnish-cache/bin/varnishd/cache_main.c +++ b/varnish-cache/bin/varnishd/cache_main.c @@ -17,6 +17,7 @@ #include "libvarnish.h" #include "heritage.h" +#include "shmlog.h" #include "cache.h" #include "cli_event.h" @@ -62,6 +63,7 @@ cli_func_ping(struct cli *cli, char **av, void *priv __unused) { time_t t; + VSL(SLT_CLI, 0, av[1]); arm_keepalive(); if (av[2] != NULL) { /* XXX: check clock skew is pointless here */ @@ -92,6 +94,8 @@ child_main(void) setbuf(stderr, NULL); printf("Child starts\n"); + VSL_Init(); + AZ(pthread_create(&vca_thread, NULL, vca_main, NULL)); eb = event_init(); @@ -100,9 +104,10 @@ child_main(void) cli = cli_setup(heritage.fds[2], heritage.fds[1], 0, cli_proto); evtimer_set(&ev_keepalive, timer_keepalive, NULL); + event_base_set(eb, &ev_keepalive); arm_keepalive(); - i = event_dispatch(); + i = event_base_loop(eb, 0); if (i != 0) printf("event_dispatch() = %d\n", i); diff --git a/varnish-cache/bin/varnishd/cache_shmlog.c b/varnish-cache/bin/varnishd/cache_shmlog.c new file mode 100644 index 00000000..562b437d --- /dev/null +++ b/varnish-cache/bin/varnishd/cache_shmlog.c @@ -0,0 +1,87 @@ +/* + * $Id$ + */ + +#include +#include +#include +#include + +#include "shmlog.h" + +#include "heritage.h" + +static struct shmloghead *loghead; +static unsigned char *logstart, *logend; + +void +VSL(enum shmlogtag tag, unsigned id, const char *fmt, ...) +{ + va_list ap; + unsigned char *p, *q; + unsigned m, n; + + va_start(ap, fmt); + + /* XXX: Lock */ + q = NULL; + p = logstart + loghead->ptr; + assert(p < logend); + + /* + * Wrap early if we approach the end + * 32 is arbitraryly larger than minimum of 4. + */ + if (p + 32 > logend) { + q = p; + p = logstart; + *p = SLT_ENDMARKER; + } + n = 0; + if (fmt != NULL) { + while (1) { + m = logend - (p + 4); + if (m > 256) + m = 256; + n = vsnprintf((char *)p + 4, m, fmt, ap); + if (n >= 255) + n = 255; /* we truncate long fields */ + if (n < m) + break; + /* wraparound */ + assert(q == NULL); + q = p; + p = logstart; + *p = SLT_ENDMARKER; + continue; /* Try again */ + } + } + p[1] = n; + p[2] = id >> 8; + p[3] = id & 0xff; + p[0] = tag; + + if (q != NULL) + *q = SLT_WRAPMARKER; + + loghead->ptr = (p + 4 + n) - logstart; + + /* XXX: Unlock */ + + va_end(ap); +} + +void +VSL_Init(void) +{ + + loghead = mmap(NULL, heritage.vsl_size, + PROT_READ|PROT_WRITE, + MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED, + heritage.vsl_fd, 0); + assert(loghead != MAP_FAILED); + + /* XXX check sanity of loghead */ + logstart = (unsigned char *)loghead + loghead->start; + logend = logstart + loghead->size; +} diff --git a/varnish-cache/bin/varnishd/heritage.h b/varnish-cache/bin/varnishd/heritage.h index e555b79f..fa6a9be2 100644 --- a/varnish-cache/bin/varnishd/heritage.h +++ b/varnish-cache/bin/varnishd/heritage.h @@ -20,6 +20,10 @@ struct heritage { #define HERITAGE_NSOCKS 2 /* IPv4 + IPv6 */ int sock_local[HERITAGE_NSOCKS]; int sock_remote[HERITAGE_NSOCKS]; + + /* Share memory log fd and size (incl header) */ + int vsl_fd; + unsigned vsl_size; }; extern struct heritage heritage; diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index bc9c6d8c..b04449c3 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -5,7 +5,7 @@ */ #include -#include +#include #include #include #include @@ -169,6 +169,39 @@ usage(void) /*--------------------------------------------------------------------*/ +#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 @@ -208,6 +241,8 @@ main(int argc, char *argv[]) */ open_tcp(portnumber); + init_vsl("/tmp/_vsl", 1000); + testme(); -- 2.39.5