[experimental-ims] d68a721 Introduce the new VSM "per-child" lifetime.

Geoff Simmons geoff at varnish-cache.org
Mon Jan 9 21:52:40 CET 2012


commit d68a7210c63e3caefb2f010d48dd4c5d2ce37b90
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sun Nov 20 21:26:25 2011 +0000

    Introduce the new VSM "per-child" lifetime.

diff --git a/bin/varnishd/common/common_vsm.c b/bin/varnishd/common/common_vsm.c
index b67e721..4eb4195 100644
--- a/bin/varnishd/common/common_vsm.c
+++ b/bin/varnishd/common/common_vsm.c
@@ -317,7 +317,10 @@ VSM_common_delete(struct vsm_sc **scp)
 		free(vr->ptr);
 		FREE_OBJ(vr);
 	}
+
+	/* Mark VSM as abandoned */
 	sc->head->alloc_seq = 0;
+
 	VWMB();
 	FREE_OBJ(sc);
 }
diff --git a/bin/varnishd/mgt/mgt.h b/bin/varnishd/mgt/mgt.h
index 1b5a872..52463e6 100644
--- a/bin/varnishd/mgt/mgt.h
+++ b/bin/varnishd/mgt/mgt.h
@@ -84,6 +84,10 @@ void mgt_sandbox_solaris_privsep(void);
 void mgt_SHM_Init(void);
 void mgt_SHM_static_alloc(const void *, ssize_t size,
     const char *class, const char *type, const char *ident);
+void mgt_SHM_Create(void);
+void mgt_SHM_Destroy(int keep);
+void mgt_SHM_Size_Adjust(void);
+
 
 /* stevedore_mgt.c */
 void STV_Config(const char *spec);
diff --git a/bin/varnishd/mgt/mgt_child.c b/bin/varnishd/mgt/mgt_child.c
index 6d42260..0f91d05 100644
--- a/bin/varnishd/mgt/mgt_child.c
+++ b/bin/varnishd/mgt/mgt_child.c
@@ -315,6 +315,9 @@ start_child(struct cli *cli)
 	heritage.std_fd = cp[1];
 	child_output = cp[0];
 
+	AN(heritage.vsm);
+	mgt_SHM_Size_Adjust();
+	AN(heritage.vsm);
 	AN(heritage.param);
 	if ((pid = fork()) < 0) {
 		perror("Could not fork child");
@@ -427,8 +430,7 @@ mgt_handle_panicstr(pid_t r)
 {
 	char time_str[30];
 
-	if (heritage.panic_str[0] == '\0')
-		return;
+	AN(heritage.panic_str[0]);
 	REPORT(LOG_ERR, "Child (%jd) Panic message: %s",
 	    (intmax_t)r, heritage.panic_str);
 
@@ -492,7 +494,13 @@ mgt_sigchld(const struct vev *e, int what)
 	REPORT(LOG_INFO, "%s", VSB_data(vsb));
 	VSB_delete(vsb);
 
-	mgt_handle_panicstr(r);
+	if (heritage.panic_str[0] != '\0') {
+		mgt_handle_panicstr(r);
+		mgt_SHM_Destroy(1);
+	} else {
+		mgt_SHM_Destroy(0);
+	}
+	mgt_SHM_Create();
 
 	child_pid = -1;
 
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index f782d03..cc918c1 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -652,6 +652,9 @@ main(int argc, char * const *argv)
 	if (T_arg != NULL)
 		mgt_cli_telnet(T_arg);
 
+	/* Instantiate VSM */
+	mgt_SHM_Create();
+
 	MGT_Run();
 
 	if (pfh != NULL)
diff --git a/bin/varnishd/mgt/mgt_shmem.c b/bin/varnishd/mgt/mgt_shmem.c
index 075b838..9f88f58 100644
--- a/bin/varnishd/mgt/mgt_shmem.c
+++ b/bin/varnishd/mgt/mgt_shmem.c
@@ -59,6 +59,9 @@
 
 #define PAN_CLASS "Panic"
 
+static void *mgt_vsm_p;
+static ssize_t mgt_vsm_l;
+
 /*--------------------------------------------------------------------
  * Use a bogo-VSM to hold master-copies of the VSM chunks the master
  * publishes, such as -S & -T arguments.
@@ -174,20 +177,31 @@ vsm_zerofile(const char *fn, ssize_t size)
 }
 
 /*--------------------------------------------------------------------
+ * Create a VSM instance
  */
 
-static void
-mgt_SHM_Setup(void)
+static size_t
+mgt_shm_size(void)
 {
-	uintmax_t size, ps;
-	void *p;
-	char fnbuf[64];
-	int vsm_fd;
+	size_t size, ps;
 
 	size = mgt_param.vsl_space + mgt_param.vsm_space;
 	ps = getpagesize();
 	size += ps - 1;
-	size &= ~(ps - 1);
+	size &= ~(ps - 1U);
+	return (size);
+}
+
+void
+mgt_SHM_Create(void)
+{
+	size_t size;
+	void *p;
+	char fnbuf[64];
+	int vsm_fd;
+
+	AZ(heritage.vsm);
+	size = mgt_shm_size();
 
 	bprintf(fnbuf, "%s.%jd", VSM_FILENAME, (intmax_t)getpid());
 
@@ -207,11 +221,26 @@ mgt_SHM_Setup(void)
 		exit (-1);
 	}
 
+	mgt_vsm_p = p;
+	mgt_vsm_l = size;
+
 	/* This may or may not work */
 	(void)mlock(p, size);
 
 	heritage.vsm = VSM_common_new(p, size);
 
+	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_common_alloc(heritage.vsm,
+	    heritage.panic_str_len, PAN_CLASS, "", "");
+	AN(heritage.panic_str);
+
 	if (rename(fnbuf, VSM_FILENAME)) {
 		fprintf(stderr, "Rename failed %s -> %s: %s\n",
 		    fnbuf, VSM_FILENAME, strerror(errno));
@@ -221,6 +250,41 @@ mgt_SHM_Setup(void)
 }
 
 /*--------------------------------------------------------------------
+ * Destroy a VSM instance
+ */
+
+void
+mgt_SHM_Destroy(int keep)
+{
+
+	AN(heritage.vsm);
+	if (keep)
+		(void)rename(VSM_FILENAME, VSM_FILENAME ".keep");
+	heritage.panic_str = NULL;
+	heritage.panic_str_len = 0;
+	heritage.param = NULL;
+	VSM_common_delete(&heritage.vsm);
+	AZ(munmap(mgt_vsm_p, mgt_vsm_l));
+	mgt_vsm_p = NULL;
+	mgt_vsm_l = 0;
+}
+
+/*--------------------------------------------------------------------
+ * Destroy and recreate VSM if its size should change
+ */
+
+void
+mgt_SHM_Size_Adjust(void)
+{
+
+	AN(heritage.vsm);
+	if (mgt_vsm_l == mgt_shm_size())
+		return;
+	mgt_SHM_Destroy(0);
+	mgt_SHM_Create();
+}
+
+/*--------------------------------------------------------------------
  * Exit handler that clears the owning pid from the SHMLOG
  */
 
@@ -233,6 +297,10 @@ mgt_shm_atexit(void)
 		VSM_common_delete(&heritage.vsm);
 }
 
+/*--------------------------------------------------------------------
+ * Initialize VSM subsystem
+ */
+
 void
 mgt_SHM_Init(void)
 {
@@ -243,21 +311,9 @@ mgt_SHM_Init(void)
 	if (i)
 		exit(i);
 
+	/* Create our static VSM instance */
 	static_vsm = VSM_common_new(static_vsm_buf, sizeof static_vsm_buf);
 
-	mgt_SHM_Setup();
-
+	/* Setup atexit handler */
 	AZ(atexit(mgt_shm_atexit));
-
-	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_common_alloc(heritage.vsm,
-	    heritage.panic_str_len, PAN_CLASS, "", "");
-	AN(heritage.panic_str);
 }



More information about the varnish-commit mailing list