]> err.no Git - varnish/commitdiff
Add shared memory log setup and stuffer function in the child process.
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 23 Mar 2006 15:31:20 +0000 (15:31 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 23 Mar 2006 15:31:20 +0000 (15:31 +0000)
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
varnish-cache/bin/varnishd/cache.h
varnish-cache/bin/varnishd/cache_main.c
varnish-cache/bin/varnishd/cache_shmlog.c [new file with mode: 0644]
varnish-cache/bin/varnishd/heritage.h
varnish-cache/bin/varnishd/varnishd.c

index 0f49ac31077a978493e078e895e0380a634928dd..4d3fcf14b50a5df8cf6a7a9cca51706b2e1ecba0 100644 (file)
@@ -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 \
index 0f37131553ac5910edeee65dfcb70900b8c1d319..2bd42f61a2cafaf42b93cc05b415c82e64b96553 100644 (file)
@@ -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
+
+
index 266fd1728c5066ea4896c452ebd4b5127c02a02b..59da0e1fda676dd861e194f075bcfdda34d046bf 100644 (file)
@@ -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 (file)
index 0000000..562b437
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <assert.h>
+#include <stdarg.h>
+#include <sys/mman.h>
+
+#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;
+}
index e555b79fb68abd6653638e16772b6379cd574523..fa6a9be2bdf798c3cca83db4916b6d4082ade45b 100644 (file)
@@ -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;
index bc9c6d8cbb0f2f341e9897e24cf5ae3fdcdce157..b04449c3e9de684d53f2619cecaa4e565e66efb6 100644 (file)
@@ -5,7 +5,7 @@
  */
 
 #include <assert.h>
-#include <err.h>
+#include <string.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -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 <printf.h>
 
@@ -208,6 +241,8 @@ main(int argc, char *argv[])
         */
        open_tcp(portnumber);
 
+       init_vsl("/tmp/_vsl", 1000);
+
        testme();