r835 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Sat Aug 19 22:15:09 CEST 2006


Author: phk
Date: 2006-08-19 22:15:09 +0200 (Sat, 19 Aug 2006)
New Revision: 835

Modified:
   trunk/varnish-cache/bin/varnishd/cache_backend.c
   trunk/varnish-cache/bin/varnishd/cache_pool.c
   trunk/varnish-cache/bin/varnishd/cache_session.c
   trunk/varnish-cache/bin/varnishd/heritage.h
   trunk/varnish-cache/bin/varnishd/rfc2616.c
   trunk/varnish-cache/bin/varnishd/shmlog.c
   trunk/varnish-cache/bin/varnishd/varnishd.c
Log:
We have a number of adjustable parameters, things like "default TTL" which
should be adjustable at runtime.

We need to make adjustments in such a way that a restart of the child also
uses the new paramters.

We can either do this by parsing the CLI in both mgt+child and have both
update their private copy, or we can parse it only in one of them and
update a shared copy.

We opt for the latter method.

Add a "struct params" which holds the adjustable parameters and put on
in the shmlog segment, between struct shmloghead and the round-robin
buffer.

Move parameters from heritage to params.

We put it there without exposing it in struct shmloghead which is
the public view of the shared memory because we do not want to make
it a public API or even to tempt people to think that it is one.

Now I just need to add the CLI functions to actually twiddle the
parameters.



Modified: trunk/varnish-cache/bin/varnishd/cache_backend.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_backend.c	2006-08-19 19:46:32 UTC (rev 834)
+++ trunk/varnish-cache/bin/varnishd/cache_backend.c	2006-08-19 20:15:09 UTC (rev 835)
@@ -39,7 +39,7 @@
 	struct vbe_conn *vbc;
 	unsigned char *p;
 
-	vbc = calloc(sizeof *vbc + heritage.mem_workspace * 2, 1);
+	vbc = calloc(sizeof *vbc + params->mem_workspace * 2, 1);
 	if (vbc == NULL)
 		return (NULL);
 	VSL_stats->n_vbe_conn++;
@@ -48,9 +48,9 @@
 	vbc->http2 = &vbc->http_mem[1];
 	vbc->fd = -1;
 	p = (void *)(vbc + 1);
-	http_Setup(vbc->http, p, heritage.mem_workspace);
-	p += heritage.mem_workspace;
-	http_Setup(vbc->http2, p, heritage.mem_workspace);
+	http_Setup(vbc->http, p, params->mem_workspace);
+	p += params->mem_workspace;
+	http_Setup(vbc->http2, p, params->mem_workspace);
 	return (vbc);
 }
 

Modified: trunk/varnish-cache/bin/varnishd/cache_pool.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_pool.c	2006-08-19 19:46:32 UTC (rev 834)
+++ trunk/varnish-cache/bin/varnishd/cache_pool.c	2006-08-19 20:15:09 UTC (rev 835)
@@ -192,7 +192,7 @@
 		} else {
 			/* If we are a dynamic thread, time out and die */
 			AZ(clock_gettime(CLOCK_REALTIME, &ts));
-			ts.tv_sec += heritage.wthread_timeout;
+			ts.tv_sec += params->wthread_timeout;
 			if (pthread_cond_timedwait(&w->cv, &wrk_mtx, &ts)) {
 				VSL_stats->n_wrk--;
 				TAILQ_REMOVE(&wrk_idle, w, list);
@@ -237,7 +237,7 @@
 	wrk_overflow++;
 
 	/* Can we create more threads ? */
-	if (VSL_stats->n_wrk >= heritage.wthread_max) {
+	if (VSL_stats->n_wrk >= params->wthread_max) {
 		VSL_stats->n_wrk_max++;
 		AZ(pthread_mutex_unlock(&wrk_mtx));
 		return;
@@ -273,8 +273,8 @@
 
 	AZ(pthread_mutex_init(&wrk_mtx, NULL));
 
-	VSL(SLT_Debug, 0, "Starting %u worker threads", heritage.wthread_min);
-	for (i = 0; i < heritage.wthread_min; i++) {
+	VSL(SLT_Debug, 0, "Starting %u worker threads", params->wthread_min);
+	for (i = 0; i < params->wthread_min; i++) {
 		VSL_stats->n_wrk++;
 		AZ(pthread_create(&tp, NULL, wrk_thread, &i));
 		AZ(pthread_detach(tp));

Modified: trunk/varnish-cache/bin/varnishd/cache_session.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_session.c	2006-08-19 19:46:32 UTC (rev 834)
+++ trunk/varnish-cache/bin/varnishd/cache_session.c	2006-08-19 20:15:09 UTC (rev 835)
@@ -186,7 +186,7 @@
 	struct sessmem *sm;
 
 	sm = calloc(
-	    sizeof *sm + heritage.mem_workspace,
+	    sizeof *sm + params->mem_workspace,
 	    1);
 	if (sm == NULL)
 		return (NULL);
@@ -203,7 +203,7 @@
 		sm->sess.sockaddrlen = len;
 	}
 
-	http_Setup(&sm->http, (void *)(sm + 1), heritage.mem_workspace);
+	http_Setup(&sm->http, (void *)(sm + 1), params->mem_workspace);
 
 	sm->sess.acct.first = time(NULL);
 

Modified: trunk/varnish-cache/bin/varnishd/heritage.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/heritage.h	2006-08-19 19:46:32 UTC (rev 834)
+++ trunk/varnish-cache/bin/varnishd/heritage.h	2006-08-19 20:15:09 UTC (rev 835)
@@ -25,6 +25,11 @@
 	/* Hash method */
 	struct hash_slinger	*hash;
 
+};
+
+struct params {
+
+	/* TTL used for lack of anything better */
 	unsigned		default_ttl;
 
 	/* Worker threads */
@@ -36,6 +41,7 @@
 	unsigned		mem_workspace;
 };
 
+extern struct params *params;
 extern struct heritage heritage;
 
 void child_main(void);

Modified: trunk/varnish-cache/bin/varnishd/rfc2616.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/rfc2616.c	2006-08-19 19:46:32 UTC (rev 834)
+++ trunk/varnish-cache/bin/varnishd/rfc2616.c	2006-08-19 20:15:09 UTC (rev 835)
@@ -111,7 +111,7 @@
 				retirement_age = h_expires - h_date;
 		}
 		if (retirement_age == INT_MAX)
-			retirement_age = heritage.default_ttl;
+			retirement_age = params->default_ttl;
 
 		ttd = obj->entered + retirement_age;
 	}

Modified: trunk/varnish-cache/bin/varnishd/shmlog.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/shmlog.c	2006-08-19 19:46:32 UTC (rev 834)
+++ trunk/varnish-cache/bin/varnishd/shmlog.c	2006-08-19 20:15:09 UTC (rev 835)
@@ -135,49 +135,76 @@
 
 /*--------------------------------------------------------------------*/
 
-void
-VSL_MgtInit(const char *fn, unsigned size)
+static int
+vsl_goodold(int fd)
 {
 	struct shmloghead slh;
-	int i = 0;
+	int i;
 
 	memset(&slh, 0, sizeof slh);	/* XXX: for flexelint */
-	heritage.vsl_fd = open(fn, O_RDWR, 0644);
-	if (heritage.vsl_fd >= 0)
-		i = read(heritage.vsl_fd, &slh, sizeof slh);
-	if (heritage.vsl_fd < 0 || i != sizeof slh ||
-	    slh.magic != SHMLOGHEAD_MAGIC ||
-	    slh.hdrsize != sizeof slh) {
-		/* XXX more checks */
+	i = read(fd, &slh, sizeof slh);
+	if (i != sizeof slh)
+		return (0);
+	if (slh.magic != SHMLOGHEAD_MAGIC)
+		return (0);
+	if (slh.hdrsize != sizeof slh)
+		return (0);
+	if (slh.start != sizeof slh + sizeof *params)
+		return (0);
+	/* XXX more checks */
+	heritage.vsl_size = slh.size + slh.start;
+	return (1);
+}
 
-		fprintf(stderr, "Creating new SHMFILE\n");
-		if (heritage.vsl_fd >= 0)
-			close(heritage.vsl_fd);
-		(void)unlink(fn);
-		heritage.vsl_fd = open(fn, O_RDWR | O_CREAT, 0644);
-		if (heritage.vsl_fd < 0) {
-			fprintf(stderr, "Could not open %s: %s\n",
-			    fn, strerror(errno));
-			exit (1);
-		}
+static void
+vsl_buildnew(const char *fn, unsigned size)
+{
+	struct shmloghead slh;
+	int i;
 
-		memset(&slh, 0, sizeof slh);
-		slh.magic = SHMLOGHEAD_MAGIC;
-		slh.hdrsize = sizeof slh;
-		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, (off_t)sizeof slh + (off_t)size));
+	(void)unlink(fn);
+	heritage.vsl_fd = open(fn, O_RDWR | O_CREAT, 0644);
+	if (heritage.vsl_fd < 0) {
+		fprintf(stderr, "Could not open %s: %s\n",
+		    fn, strerror(errno));
+		exit (1);
 	}
-	heritage.vsl_size = slh.size + slh.start;
 
+	memset(&slh, 0, sizeof slh);
+	slh.magic = SHMLOGHEAD_MAGIC;
+	slh.hdrsize = sizeof slh;
+	slh.size = size;
+	slh.ptr = 0;
+	slh.start = sizeof slh + sizeof *params;
+	i = write(heritage.vsl_fd, &slh, sizeof slh);
+	assert(i == sizeof slh);
+	heritage.vsl_size = slh.start + size;
+	AZ(ftruncate(heritage.vsl_fd, (off_t)heritage.vsl_size));
+}
+
+void
+VSL_MgtInit(const char *fn, unsigned size)
+{
+	int i;
+	struct params *pp;
+
+	i = open(fn, O_RDWR, 0644);
+	if (i >= 0 && vsl_goodold(i)) {
+		fprintf(stderr, "Using old SHMFILE\n");
+		heritage.vsl_fd = i;
+	} else {
+		fprintf(stderr, "Creating new SHMFILE\n");
+		(void)close(i);
+		vsl_buildnew(fn, size);
+	}
+
 	loghead = mmap(NULL, heritage.vsl_size,
 	    PROT_READ|PROT_WRITE,
 	    MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED,
 	    heritage.vsl_fd, 0);
 	assert(loghead != MAP_FAILED);
 	VSL_stats = &loghead->stats;
+	pp = (void *)(loghead + 1);
+	memcpy(pp, params, sizeof *pp);
+	params = pp;
 }

Modified: trunk/varnish-cache/bin/varnishd/varnishd.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/varnishd.c	2006-08-19 19:46:32 UTC (rev 834)
+++ trunk/varnish-cache/bin/varnishd/varnishd.c	2006-08-19 20:15:09 UTC (rev 835)
@@ -32,6 +32,7 @@
 #endif
 
 struct heritage heritage;
+struct params *params;
 
 /*--------------------------------------------------------------------*/
 
@@ -188,13 +189,13 @@
 		usage();
 	if (ua < 1)
 		usage();
-	heritage.wthread_min = ua;
-	heritage.wthread_max = ua;
-	heritage.wthread_timeout = 10;
+	params->wthread_min = ua;
+	params->wthread_max = ua;
+	params->wthread_timeout = 10;
 	if (i >= 2)
-		heritage.wthread_max = ub;
+		params->wthread_max = ub;
 	if (i >= 3)
-		heritage.wthread_timeout = uc;
+		params->wthread_timeout = uc;
 }
 
 /*--------------------------------------------------------------------
@@ -323,17 +324,20 @@
 	const char *sflag = "file";
 	const char *hflag = "classic";
 	const char *Tflag = NULL;
+	struct params param;
 
 	setbuf(stdout, NULL);
 	setbuf(stderr, NULL);
 
+	memset(&param, 0, sizeof param);
+	params = &param;
 	mgt_vcc_init(); 
 
-	heritage.default_ttl = 120;
-	heritage.wthread_min = 1;
-	heritage.wthread_max = UINT_MAX;
-	heritage.wthread_timeout = 10;
-	heritage.mem_workspace = 4096;
+	params->default_ttl = 120;
+	params->wthread_min = 1;
+	params->wthread_max = UINT_MAX;
+	params->wthread_timeout = 10;
+	params->mem_workspace = 4096;
 
 	while ((o = getopt(argc, argv, "b:df:h:p:s:t:T:Vw:")) != -1)
 		switch (o) {
@@ -356,7 +360,7 @@
 			sflag = optarg;
 			break;
 		case 't':
-			heritage.default_ttl = strtoul(optarg, NULL, 0);
+			params->default_ttl = strtoul(optarg, NULL, 0);
 			break;
 		case 'T':
 			Tflag = optarg;




More information about the varnish-commit mailing list