[master] 2d1ca36 Add a "vsm" instance for the managers static VSM allocations (-S, -T args) and copy them to the "real vsm" when we create it.

Poul-Henning Kamp phk at varnish-cache.org
Mon Nov 21 00:39:24 CET 2011


commit 2d1ca36652afc1196189f60e7aa11b3c9cb948b7
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sun Nov 20 20:58:29 2011 +0000

    Add a "vsm" instance for the managers static VSM allocations (-S, -T args)
    and copy them to the "real vsm" when we create it.

diff --git a/bin/varnishd/common/common.h b/bin/varnishd/common/common.h
index d6de47d..0f25d31 100644
--- a/bin/varnishd/common/common.h
+++ b/bin/varnishd/common/common.h
@@ -48,9 +48,6 @@ struct cli;
 extern pid_t mgt_pid;
 #define ASSERT_MGT() do { assert(getpid() == mgt_pid);} while (0)
 
-/* mgt_shmem.c */
-#define PAN_CLASS "Panic"
-
 /* varnishd.c */
 extern struct vsb *vident;		// XXX: -> heritage ?
 int Symbol_Lookup(struct vsb *vsb, void *ptr);
@@ -77,6 +74,7 @@ void *VSM_common_alloc(struct vsm_sc *sc, ssize_t size,
     const char *class, const char *type, const char *ident);
 void VSM_common_free(struct vsm_sc *sc, void *ptr);
 void VSM_common_delete(struct vsm_sc **sc);
+void VSM_common_copy(struct vsm_sc *to, const struct vsm_sc *from);
 
 /*---------------------------------------------------------------------
  * Generic power-2 rounding macros
diff --git a/bin/varnishd/common/common_vsm.c b/bin/varnishd/common/common_vsm.c
index a2a4fe4..b67e721 100644
--- a/bin/varnishd/common/common_vsm.c
+++ b/bin/varnishd/common/common_vsm.c
@@ -321,3 +321,23 @@ VSM_common_delete(struct vsm_sc **scp)
 	VWMB();
 	FREE_OBJ(sc);
 }
+
+/*--------------------------------------------------------------------
+ * Copy one VSM to another
+ */
+
+void
+VSM_common_copy(struct vsm_sc *to, const struct vsm_sc *from)
+{
+	struct vsm_range *vr;
+	void *p;
+
+	CHECK_OBJ_NOTNULL(to, VSM_SC_MAGIC);
+	CHECK_OBJ_NOTNULL(from, VSM_SC_MAGIC);
+	VTAILQ_FOREACH(vr, &from->r_used, list) {
+		p = VSM_common_alloc(to, vr->chunk->len,
+		    vr->chunk->class, vr->chunk->type, vr->chunk->ident);
+		AN(p);
+		memcpy(p, vr->chunk + 1, vr->chunk->len);
+	}
+}
diff --git a/bin/varnishd/mgt/mgt.h b/bin/varnishd/mgt/mgt.h
index 775fe2c..1b5a872 100644
--- a/bin/varnishd/mgt/mgt.h
+++ b/bin/varnishd/mgt/mgt.h
@@ -82,6 +82,8 @@ void mgt_sandbox_solaris_privsep(void);
 
 /* mgt_shmem.c */
 void mgt_SHM_Init(void);
+void mgt_SHM_static_alloc(const void *, ssize_t size,
+    const char *class, const char *type, const char *ident);
 
 /* stevedore_mgt.c */
 void STV_Config(const char *spec);
@@ -109,9 +111,6 @@ extern unsigned mgt_vcc_err_unref;
 		syslog(pri, fmt, __VA_ARGS__);		\
 	} while (0)
 
-#define VSM_Alloc(a, b, c, d)	VSM_common_alloc(heritage.vsm, a,b,c,d)
-#define VSM_Free(a)		VSM_common_free(heritage.vsm, a)
-
 #if defined(PTHREAD_CANCELED) || defined(PTHREAD_MUTEX_DEFAULT)
 #error "Keep pthreads out of in manager process"
 #endif
diff --git a/bin/varnishd/mgt/mgt_cli.c b/bin/varnishd/mgt/mgt_cli.c
index d3b7396..b141f5b 100644
--- a/bin/varnishd/mgt/mgt_cli.c
+++ b/bin/varnishd/mgt/mgt_cli.c
@@ -43,7 +43,6 @@
 #include <unistd.h>
 
 #include "mgt/mgt.h"
-#include "common/heritage.h"
 #include "common/params.h"
 
 #include "vcli.h"
@@ -493,13 +492,9 @@ mgt_cli_secret(const char *S_arg)
 {
 	int i, fd;
 	char buf[BUFSIZ];
-	char *p;
 
 	/* Save in shmem */
-	i = strlen(S_arg);
-	p = VSM_Alloc(i + 1L, "Arg", "-S", "");
-	AN(p);
-	memcpy(p, S_arg, i + 1L);
+	mgt_SHM_static_alloc(S_arg, strlen(S_arg) + 1L, "Arg", "-S", "");
 
 	srandomdev();			/* XXX: why here ??? */
 	fd = open(S_arg, O_RDONLY);
@@ -527,7 +522,6 @@ mgt_cli_telnet(const char *T_arg)
 	struct vss_addr **ta;
 	int i, n, sock, good;
 	struct telnet *tn;
-	char *p;
 	struct vsb *vsb;
 	char abuf[VTCP_ADDRBUFSIZE];
 	char pbuf[VTCP_PORTBUFSIZE];
@@ -564,9 +558,7 @@ mgt_cli_telnet(const char *T_arg)
 	}
 	AZ(VSB_finish(vsb));
 	/* Save in shmem */
-	p = VSM_Alloc(VSB_len(vsb) + 1, "Arg", "-T", "");
-	AN(p);
-	memcpy(p, VSB_data(vsb), VSB_len(vsb) + 1);
+	mgt_SHM_static_alloc(VSB_data(vsb), VSB_len(vsb) + 1, "Arg", "-T", "");
 	VSB_delete(vsb);
 }
 
diff --git a/bin/varnishd/mgt/mgt_shmem.c b/bin/varnishd/mgt/mgt_shmem.c
index a9a2a7e..075b838 100644
--- a/bin/varnishd/mgt/mgt_shmem.c
+++ b/bin/varnishd/mgt/mgt_shmem.c
@@ -57,7 +57,31 @@
 #define MAP_NOSYNC 0 /* XXX Linux */
 #endif
 
-static int vsm_fd = -1;
+#define PAN_CLASS "Panic"
+
+/*--------------------------------------------------------------------
+ * Use a bogo-VSM to hold master-copies of the VSM chunks the master
+ * publishes, such as -S & -T arguments.
+ */
+
+static struct vsm_sc *static_vsm;
+static char static_vsm_buf[1024];
+
+void
+mgt_SHM_static_alloc(const void *ptr, ssize_t size,
+    const char *class, const char *type, const char *ident)
+{
+	void *p;
+
+	p = VSM_common_alloc(static_vsm, size, class, type, ident);
+	AN(p);
+	memcpy(p, ptr, size);
+	if (heritage.vsm != NULL) {
+		p = VSM_common_alloc(heritage.vsm, size, class, type, ident);
+		AN(p);
+		memcpy(p, ptr, size);
+	}
+}
 
 /*--------------------------------------------------------------------
  * Check that we are not started with the same -n argument as an already
@@ -150,36 +174,21 @@ vsm_zerofile(const char *fn, ssize_t size)
 }
 
 /*--------------------------------------------------------------------
- * Exit handler that clears the owning pid from the SHMLOG
  */
 
-static
-void
-mgt_shm_atexit(void)
+static void
+mgt_SHM_Setup(void)
 {
-
-	if (heritage.vsm != NULL)
-		VSM_common_delete(&heritage.vsm);
-}
-
-void
-mgt_SHM_Init(void)
-{
-	int i;
 	uintmax_t size, ps;
 	void *p;
 	char fnbuf[64];
+	int vsm_fd;
 
 	size = mgt_param.vsl_space + mgt_param.vsm_space;
 	ps = getpagesize();
 	size += ps - 1;
 	size &= ~(ps - 1);
 
-	/* Collision check with already running varnishd */
-	i = vsm_n_check();
-	if (i)
-		exit(i);
-
 	bprintf(fnbuf, "%s.%jd", VSM_FILENAME, (intmax_t)getpid());
 
 	vsm_fd = vsm_zerofile(fnbuf, size);
@@ -191,6 +200,8 @@ mgt_SHM_Init(void)
 	    MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED,
 	    vsm_fd, 0);
 
+	AZ(close(vsm_fd));
+
 	if (p == MAP_FAILED) {
 		fprintf(stderr, "Mmap error %s: %s\n", fnbuf, strerror(errno));
 		exit (-1);
@@ -207,16 +218,46 @@ mgt_SHM_Init(void)
 		(void)unlink(fnbuf);
 		exit (-1);
 	}
+}
+
+/*--------------------------------------------------------------------
+ * Exit handler that clears the owning pid from the SHMLOG
+ */
+
+static
+void
+mgt_shm_atexit(void)
+{
+
+	if (heritage.vsm != NULL)
+		VSM_common_delete(&heritage.vsm);
+}
+
+void
+mgt_SHM_Init(void)
+{
+	int i;
+
+	/* Collision check with already running varnishd */
+	i = vsm_n_check();
+	if (i)
+		exit(i);
+
+	static_vsm = VSM_common_new(static_vsm_buf, sizeof static_vsm_buf);
+
+	mgt_SHM_Setup();
 
 	AZ(atexit(mgt_shm_atexit));
 
-	heritage.param =
-	    VSM_Alloc(sizeof *heritage.param, VSM_CLASS_PARAM, "", "");
+	VSM_common_copy(heritage.vsm, static_vsm);
+
+	heritage.param = VSM_common_alloc(heritage.vsm,
+	    sizeof *heritage.param, VSM_CLASS_PARAM, "", "");
 	AN(heritage.param);
 	*heritage.param = mgt_param;
 
 	heritage.panic_str_len = 64 * 1024;
-	heritage.panic_str =
-	    VSM_Alloc(heritage.panic_str_len, PAN_CLASS, "", "");
+	heritage.panic_str = VSM_common_alloc(heritage.vsm,
+	    heritage.panic_str_len, PAN_CLASS, "", "");
 	AN(heritage.panic_str);
 }



More information about the varnish-commit mailing list