[master] c4eef1e Make the simple stevedores even simpler by making trim a alloc/free sequence.

Poul-Henning Kamp phk at FreeBSD.org
Mon Jan 11 11:57:33 CET 2016


commit c4eef1e216330630a18aac85a7b5d22b22aef692
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Jan 11 10:24:12 2016 +0000

    Make the simple stevedores even simpler by making trim a alloc/free
    sequence.

diff --git a/bin/varnishd/storage/storage.h b/bin/varnishd/storage/storage.h
index 0793b4e..ee10ae2 100644
--- a/bin/varnishd/storage/storage.h
+++ b/bin/varnishd/storage/storage.h
@@ -87,7 +87,6 @@ struct stevedore {
 	storage_init_f		*init;		/* called by mgt process */
 	storage_open_f		*open;		/* called by cache process */
 	storage_alloc_f		*alloc;		/* --//-- only if SML */
-	storage_trim_f		*trim;		/* --//-- only if SML */
 	storage_free_f		*free;		/* --//-- only if SML */
 	storage_close_f		*close;		/* --//-- */
 	storage_allocobj_f	*allocobj;	/* --//-- */
diff --git a/bin/varnishd/storage/storage_file.c b/bin/varnishd/storage/storage_file.c
index 9df6d9f..89258e6 100644
--- a/bin/varnishd/storage/storage_file.c
+++ b/bin/varnishd/storage/storage_file.c
@@ -307,36 +307,6 @@ free_smf(struct smf *sp)
 }
 
 /*--------------------------------------------------------------------
- * Trim the tail of a range.
- */
-
-static void
-trim_smf(struct smf *sp, size_t bytes)
-{
-	struct smf *sp2;
-	struct smf_sc *sc = sp->sc;
-
-	AN(sp->alloc);
-	assert(bytes > 0);
-	assert(bytes < sp->size);
-	AZ(bytes % sc->pagesize);
-	AZ(sp->size % sc->pagesize);
-	CHECK_OBJ_NOTNULL(sp, SMF_MAGIC);
-	sp2 = malloc(sizeof *sp2);
-	XXXAN(sp2);
-	sc->stats->g_smf++;
-	*sp2 = *sp;
-
-	sp2->size -= bytes;
-	sp->size = bytes;
-	sp2->ptr += bytes;
-	sp2->offset += bytes;
-	VTAILQ_INSERT_AFTER(&sc->order, sp, sp2, order);
-	VTAILQ_INSERT_TAIL(&sc->used, sp2, status);
-	free_smf(sp2);
-}
-
-/*--------------------------------------------------------------------
  * Insert a newly created range as busy, then free it to do any collapses
  */
 
@@ -480,39 +450,6 @@ smf_alloc(const struct stevedore *st, size_t size)
 
 /*--------------------------------------------------------------------*/
 
-static void
-smf_trim(struct storage *s, size_t size, int move_ok)
-{
-	struct smf *smf;
-	struct smf_sc *sc;
-
-	CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC);
-	assert(size > 0);
-	assert(size <= s->space);
-	xxxassert(size > 0);	/* XXX: seen */
-	CAST_OBJ_NOTNULL(smf, s->priv, SMF_MAGIC);
-	assert(size <= smf->size);
-
-	if (!move_ok)
-		return;		/* XXX: trim_smf needs fixed */
-
-	sc = smf->sc;
-	size += (sc->pagesize - 1);
-	size &= ~(sc->pagesize - 1);
-	if (smf->size > size) {
-		Lck_Lock(&sc->mtx);
-		sc->stats->c_freed += (smf->size - size);
-		sc->stats->g_bytes -= (smf->size - size);
-		sc->stats->g_space += (smf->size - size);
-		trim_smf(smf, size);
-		assert(smf->size == size);
-		Lck_Unlock(&sc->mtx);
-		s->space = size;
-	}
-}
-
-/*--------------------------------------------------------------------*/
-
 static void __match_proto__(storage_free_f)
 smf_free(struct storage *s)
 {
@@ -539,7 +476,6 @@ const struct stevedore smf_stevedore = {
 	.init		=	smf_init,
 	.open		=	smf_open,
 	.alloc		=	smf_alloc,
-	.trim		=	smf_trim,
 	.free		=	smf_free,
 	.allocobj	=	SML_allocobj,
 	.methods	=	&SML_methods,
@@ -597,9 +533,6 @@ main(int argc, char **argv)
 		if (s[i] == NULL) {
 			s[i] = smf_alloc(&smf_stevedore, j);
 			printf("A %10p %12d\n", s[i], j);
-		} else if (j < s[i]->space) {
-			smf_trim(s[i], j);
-			printf("T %10p %12d\n", s[i], j);
 		} else {
 			smf_free(s[i]);
 			printf("D %10p\n", s[i]);
diff --git a/bin/varnishd/storage/storage_malloc.c b/bin/varnishd/storage/storage_malloc.c
index 7060f55..2aacd41 100644
--- a/bin/varnishd/storage/storage_malloc.c
+++ b/bin/varnishd/storage/storage_malloc.c
@@ -147,41 +147,6 @@ sma_free(struct storage *s)
 	free(sma);
 }
 
-static void
-sma_trim(struct storage *s, size_t size, int move_ok)
-{
-	struct sma_sc *sma_sc;
-	struct sma *sma;
-	void *p;
-	size_t delta;
-
-	CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC);
-	CAST_OBJ_NOTNULL(sma, s->priv, SMA_MAGIC);
-	sma_sc = sma->sc;
-
-	assert(sma->sz == sma->s.space);
-	assert(size < sma->sz);
-
-	if (!move_ok)
-		return;
-
-	delta = sma->sz - size;
-	if (delta < 256)
-		return;
-	if ((p = realloc(sma->s.ptr, size)) != NULL) {
-		Lck_Lock(&sma_sc->sma_mtx);
-		sma_sc->sma_alloc -= delta;
-		sma_sc->stats->g_bytes -= delta;
-		sma_sc->stats->c_freed += delta;
-		if (sma_sc->sma_max != SIZE_MAX)
-			sma_sc->stats->g_space += delta;
-		sma->sz = size;
-		Lck_Unlock(&sma_sc->sma_mtx);
-		sma->s.ptr = p;
-		s->space = size;
-	}
-}
-
 static double
 sma_used_space(const struct stevedore *st)
 {
@@ -257,7 +222,6 @@ const struct stevedore sma_stevedore = {
 	.open		=	sma_open,
 	.alloc		=	sma_alloc,
 	.free		=	sma_free,
-	.trim		=	sma_trim,
 	.allocobj	=	SML_allocobj,
 	.methods	=	&SML_methods,
 	.var_free_space =	sma_free_space,
diff --git a/bin/varnishd/storage/storage_simple.c b/bin/varnishd/storage/storage_simple.c
index 82c2925..462a8ac 100644
--- a/bin/varnishd/storage/storage_simple.c
+++ b/bin/varnishd/storage/storage_simple.c
@@ -79,17 +79,6 @@ sml_stv_free(const struct stevedore *stv, struct storage *st)
 	stv->free(st);
 }
 
-static void
-sml_stv_trim(const struct stevedore *stv, struct storage *st, size_t size,
-    int move_ok)
-{
-
-	CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
-	CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
-	if (stv->trim)
-		stv->trim(st, size, move_ok);
-}
-
 /*--------------------------------------------------------------------
  * This function is called by stevedores ->allocobj() method, which
  * very often will be SML_allocobj() below, to convert a slab
@@ -104,6 +93,7 @@ SML_MkObject(const struct stevedore *stv, struct objcore *oc, void *ptr)
 	struct object *o;
 
 	CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
+	AN(stv->methods);
 	CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
 
 	assert(PAOK(ptr));
@@ -113,9 +103,8 @@ SML_MkObject(const struct stevedore *stv, struct objcore *oc, void *ptr)
 
 	VTAILQ_INIT(&o->list);
 
-	oc->stobj->magic = STOREOBJ_MAGIC;
+	INIT_OBJ(oc->stobj, STOREOBJ_MAGIC);
 	oc->stobj->stevedore = stv;
-	AN(stv->methods);
 	oc->stobj->priv = o;
 	return (o);
 }
@@ -434,7 +423,7 @@ static void __match_proto__(objtrimstore_f)
 sml_trimstore(struct worker *wrk, struct objcore *oc)
 {
 	const struct stevedore *stv;
-	struct storage *st;
+	struct storage *st, *st1;
 	struct object *o;
 	struct busyobj *bo;
 
@@ -453,14 +442,33 @@ sml_trimstore(struct worker *wrk, struct objcore *oc)
 	o = sml_getobj(wrk, oc);
 	CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
 	st = VTAILQ_LAST(&o->list, storagehead);
+
 	if (st == NULL)
 		return;
+
 	if (st->len == 0) {
 		VTAILQ_REMOVE(&o->list, st, list);
 		sml_stv_free(stv, st);
-	} else if (st->len < st->space) {
-		sml_stv_trim(stv, st, st->len, 1);
+		return;
+	}
+
+	if (st->space - st->len < 512)
+		return;
+
+	st1 = sml_stv_alloc(stv, st->len);
+	if (st1 == NULL)
+		return;
+
+	if (st1->space < st->len) {
+		sml_stv_free(stv, st1);
+		return;
 	}
+
+	memcpy(st1->ptr, st->ptr, st->len);
+	st1->len = st->len;
+	VTAILQ_REMOVE(&o->list, st, list);
+	VTAILQ_INSERT_TAIL(&o->list, st1, list);
+	sml_stv_free(stv, st);
 }
 
 static void * __match_proto__(objgetattr_f)



More information about the varnish-commit mailing list