Mailing List Archive

r62 - trunk/varnish-cache/bin/varnishd
Author: phk
Date: 2006-03-23 16:31:20 +0100 (Thu, 23 Mar 2006)
New Revision: 62

Added:
trunk/varnish-cache/bin/varnishd/cache_shmlog.c
Modified:
trunk/varnish-cache/bin/varnishd/Makefile.am
trunk/varnish-cache/bin/varnishd/cache.h
trunk/varnish-cache/bin/varnishd/cache_main.c
trunk/varnish-cache/bin/varnishd/heritage.h
trunk/varnish-cache/bin/varnishd/varnishd.c
Log:
Add shared memory log setup and stuffer function in the child process.

Log whenever we get a CLI ping



Modified: trunk/varnish-cache/bin/varnishd/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishd/Makefile.am 2006-03-23 12:20:30 UTC (rev 61)
+++ trunk/varnish-cache/bin/varnishd/Makefile.am 2006-03-23 15:31:20 UTC (rev 62)
@@ -7,6 +7,7 @@
varnishd_SOURCES = \
cache_acceptor.c \
cache_main.c \
+ cache_shmlog.c \
cli_event.c \
mgt_child.c \
tcp.c \

Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h 2006-03-23 12:20:30 UTC (rev 61)
+++ trunk/varnish-cache/bin/varnishd/cache.h 2006-03-23 15:31:20 UTC (rev 62)
@@ -12,3 +12,11 @@

/* 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
+
+

Modified: trunk/varnish-cache/bin/varnishd/cache_main.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_main.c 2006-03-23 12:20:30 UTC (rev 61)
+++ trunk/varnish-cache/bin/varnishd/cache_main.c 2006-03-23 15:31:20 UTC (rev 62)
@@ -17,6 +17,7 @@

#include "libvarnish.h"
#include "heritage.h"
+#include "shmlog.h"
#include "cache.h"
#include "cli_event.h"

@@ -62,6 +63,7 @@
{
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 @@
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 @@
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);


Added: trunk/varnish-cache/bin/varnishd/cache_shmlog.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_shmlog.c 2006-03-23 12:20:30 UTC (rev 61)
+++ trunk/varnish-cache/bin/varnishd/cache_shmlog.c 2006-03-23 15:31:20 UTC (rev 62)
@@ -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;
+}

Modified: trunk/varnish-cache/bin/varnishd/heritage.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/heritage.h 2006-03-23 12:20:30 UTC (rev 61)
+++ trunk/varnish-cache/bin/varnishd/heritage.h 2006-03-23 15:31:20 UTC (rev 62)
@@ -20,6 +20,10 @@
#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;

Modified: trunk/varnish-cache/bin/varnishd/varnishd.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/varnishd.c 2006-03-23 12:20:30 UTC (rev 61)
+++ trunk/varnish-cache/bin/varnishd/varnishd.c 2006-03-23 15:31:20 UTC (rev 62)
@@ -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 @@

/*--------------------------------------------------------------------*/

+#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 @@
*/
open_tcp(portnumber);

+ init_vsl("/tmp/_vsl", 1000);
+
testme();