r3944 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Tue Mar 17 11:20:28 CET 2009


Author: phk
Date: 2009-03-17 11:20:28 +0100 (Tue, 17 Mar 2009)
New Revision: 3944

Modified:
   trunk/varnish-cache/bin/varnishd/common.h
   trunk/varnish-cache/bin/varnishd/mgt.h
   trunk/varnish-cache/bin/varnishd/storage_persistent.c
   trunk/varnish-cache/bin/varnishd/varnishd.c
Log:
Comments, asserts and some locking



Modified: trunk/varnish-cache/bin/varnishd/common.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/common.h	2009-03-17 09:34:23 UTC (rev 3943)
+++ trunk/varnish-cache/bin/varnishd/common.h	2009-03-17 10:20:28 UTC (rev 3944)
@@ -31,6 +31,8 @@
 
 struct cli;
 struct sockaddr;
+extern pid_t mgt_pid;
+#define ASSERT_MGT() do { assert(getpid() == mgt_pid);} while (0)
 
 /* cache_acceptor.c */
 void VCA_tweak_waiter(struct cli *cli, const char *arg);

Modified: trunk/varnish-cache/bin/varnishd/mgt.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt.h	2009-03-17 09:34:23 UTC (rev 3943)
+++ trunk/varnish-cache/bin/varnishd/mgt.h	2009-03-17 10:20:28 UTC (rev 3944)
@@ -38,7 +38,6 @@
 
 extern struct vev_base	*mgt_evb;
 extern unsigned		d_flag;
-extern pid_t		mgt_pid;
 extern int		exit_status;
 
 /* mgt_child.c */

Modified: trunk/varnish-cache/bin/varnishd/storage_persistent.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/storage_persistent.c	2009-03-17 09:34:23 UTC (rev 3943)
+++ trunk/varnish-cache/bin/varnishd/storage_persistent.c	2009-03-17 10:20:28 UTC (rev 3944)
@@ -59,6 +59,19 @@
 #define MAP_NOSYNC 0 /* XXX Linux */
 #endif
 
+#define ASSERT_SILO_THREAD(sc) \
+    do {assert(pthread_self() == (sc)->thread);} while (0)
+
+/*
+ * Context for a signature.
+ *
+ * A signature is a sequence of bytes in the silo, signed by a SHA256 hash
+ * which follows the bytes.
+ *
+ * The context structure allows us to append to a signature without
+ * recalculating the entire SHA256 hash.
+ */
+
 struct smp_signctx {
 	struct smp_sign		*ss;
 	struct SHA256Context	ctx;
@@ -125,6 +138,8 @@
 	struct smp_signctx	seg2;
 
 	struct ban		*tailban;
+
+	struct lock		mtx;
 };
 
 /*
@@ -287,6 +302,7 @@
 {
 	struct smp_ident	*si;
 
+	ASSERT_MGT();
 	assert(strlen(SMP_IDENT_STRING) < sizeof si->ident);
 
 	/* Choose a new random number */
@@ -394,7 +410,7 @@
 	struct smp_sc		*sc;
 	int i;
 	
-	(void)parent;
+	ASSERT_MGT();
 
 	AZ(av[ac]);
 #define SIZOF(foo)       fprintf(stderr, \
@@ -586,6 +602,7 @@
 	uint32_t flags, length;
 	int i, retval = 0;
 
+	ASSERT_CLI();
 	(void)sc;
 	i = smp_chk_sign(ctx);	
 	if (i)
@@ -710,6 +727,7 @@
 	double t_now = TIM_real();
 	struct smp_signctx ctx[1];
 
+	ASSERT_SILO_THREAD(sc);
 	(void)sp;
 	AN(sg->offset);
 	smp_def_sign(sc, ctx, sg->offset, "SEGMENT");
@@ -754,6 +772,7 @@
 	struct smp_seg *sg;
 	int i;
 
+	ASSERT_CLI();
 	i = smp_chk_sign(ctx);	
 	if (i)
 		return (i);
@@ -896,9 +915,13 @@
 {
 	struct smp_sc	*sc;
 
+	ASSERT_CLI();
+
 	CAST_OBJ_NOTNULL(sc, st->priv, SMP_SC_MAGIC);
 fprintf(stderr, "Open Silo(%p)\n", st);
 
+	Lck_New(&sc->mtx);
+
 	/* We trust the parent to give us a valid silo, for good measure: */
 	AZ(smp_valid_silo(sc));
 
@@ -934,9 +957,13 @@
 {
 	struct smp_sc	*sc;
 
+	ASSERT_CLI();
+
 	CAST_OBJ_NOTNULL(sc, st->priv, SMP_SC_MAGIC);
 fprintf(stderr, "Close Silo(%p)\n", st);
 	smp_close_seg(sc, sc->cur_seg);
+
+	/* XXX: reap thread */
 }
 
 /*--------------------------------------------------------------------
@@ -955,12 +982,14 @@
 	CHECK_OBJ_NOTNULL(sp->obj->objstore->stevedore, STEVEDORE_MAGIC);
 	CAST_OBJ_NOTNULL(sc, sp->obj->objstore->priv, SMP_SC_MAGIC);
 
+	Lck_Lock(&sc->mtx);
 	sg = sc->cur_seg;
 	so = &sg->objs[sg->nalloc++];
 	memcpy(so->hash, sp->obj->objhead->digest, DIGEST_LEN);
 	so->ttl = sp->obj->ttl;
 	so->ptr = sp->obj;
 	so->ban = sp->obj->ban_t;
+	Lck_Unlock(&sc->mtx);
 
 fprintf(stderr, "Object(%p %p)\n", sp, sp->obj);
 }
@@ -977,13 +1006,17 @@
 	struct smp_seg *sg;
 
 	CAST_OBJ_NOTNULL(sc, st->priv, SMP_SC_MAGIC);
+
+	Lck_Lock(&sc->mtx);
 	sg = sc->cur_seg;
 
 	/* XXX: size fit check */
 	AN(sg->next_addr);
+	ss = (void *)(sc->ptr + sg->next_addr);
+	sg->next_addr += size + sizeof *ss;
+	Lck_Unlock(&sc->mtx);
 
 	/* Grab and fill a storage structure */
-	ss = (void *)(sc->ptr + sg->next_addr);
 	memset(ss, 0, sizeof *ss);
 	ss->magic = STORAGE_MAGIC;
 	ss->space = size;
@@ -992,8 +1025,6 @@
 	ss->stevedore = st;
 	ss->fd = sc->fd;
 	ss->where = sg->next_addr + sizeof *ss;
-
-	sg->next_addr += size + sizeof *ss;
 	memcpy(sc->ptr + sg->next_addr, "HERE", 4);
 	return (ss);
 }
@@ -1007,20 +1038,32 @@
 
 fprintf(stderr, "Trim(%p %zu)\n", ss, size);
 	CAST_OBJ_NOTNULL(sc, ss->priv, SMP_SC_MAGIC);
-	sg = sc->cur_seg;
 
 	/* We want 16 bytes alignment */
 	size |= 0xf;
 	size += 1;
 
+	sg = sc->cur_seg;
+	if (ss->ptr + ss->space != sg->next_addr + sc->ptr) 
+		return;
+
+	Lck_Lock(&sc->mtx);
+	sg = sc->cur_seg;
 	if (ss->ptr + ss->space == sg->next_addr + sc->ptr) {
 		memcpy(sc->ptr + sg->next_addr, z, 4);
 		sg->next_addr -= ss->space - size;
 		ss->space = size;
 		memcpy(sc->ptr + sg->next_addr, "HERE", 4);
 	}
+	Lck_Unlock(&sc->mtx);
 }
 
+/*--------------------------------------------------------------------
+ * We don't track frees of storage, we track the objects which own them
+ * instead, when there are no more objects in in the first segment, it
+ * can be reclaimed.
+ */
+
 static void
 smp_free(struct storage *st)
 {
@@ -1029,20 +1072,22 @@
 	(void)st;
 }
 
+/*--------------------------------------------------------------------
+ * Pause until all silos have loaded.
+ */
 
-/*--------------------------------------------------------------------*/
-
 void
 SMP_Ready(void)
 {
 	struct smp_sc *sc;
 
+	ASSERT_CLI();
 	while (1) {
 		VTAILQ_FOREACH(sc, &silos, list) {
-			if (!(sc->flags & SMP_F_LOADED)) {
-				sleep(1);
-				break;
-			}
+			if (sc->flags & SMP_F_LOADED)
+				continue;
+			(void)sleep(1);
+			break;
 		}
 		if (sc == NULL)
 			break;

Modified: trunk/varnish-cache/bin/varnishd/varnishd.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/varnishd.c	2009-03-17 09:34:23 UTC (rev 3943)
+++ trunk/varnish-cache/bin/varnishd/varnishd.c	2009-03-17 10:20:28 UTC (rev 3944)
@@ -417,6 +417,9 @@
 	setbuf(stdout, NULL);
 	setbuf(stderr, NULL);
 
+	/* for ASSERT_MGT() */
+	mgt_pid = getpid();
+
 	/*
 	 * Run in UTC timezone, on the off-chance that this operating
 	 * system does not have a timegm() function, and translates
@@ -615,6 +618,7 @@
 	if (pfh != NULL && vpf_write(pfh))
 		fprintf(stderr, "NOTE: Could not write PID file\n");
 
+	/* Do this again after debugstunt and daemon has run */
 	mgt_pid = getpid();
 
 	mgt_evb = vev_new_base();



More information about the varnish-commit mailing list