[master] 7050ccee9 Repurpose OC_EF_REFD flag slightly

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Fri Jun 12 13:25:08 UTC 2020


commit 7050ccee978b77d94a622406dc3a181d876fbc2d
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Thu Mar 19 15:17:45 2020 +0100

    Repurpose OC_EF_REFD flag slightly
    
    The OC_EF_REFD flag indicates whether expiry has a ref on the
    OC. Previously, the flag was only gained during the call to
    EXP_Insert. With this patch, and the helper function EXP_RefNewObjcore(),
    the flag is gained while holding the objhead mutex during
    HSH_Unbusy(). This enables the expiry functions to test on missing
    OC_EF_REFD and quickly return without having to take the main expiry
    mutex.

diff --git a/bin/varnishd/cache/cache_expire.c b/bin/varnishd/cache/cache_expire.c
index 67d5b85ba..12feff3da 100644
--- a/bin/varnishd/cache/cache_expire.c
+++ b/bin/varnishd/cache/cache_expire.c
@@ -105,10 +105,11 @@ exp_mail_it(struct objcore *oc, uint8_t cmds)
 {
 	CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
 	assert(oc->refcnt > 0);
+	AZ(cmds & OC_EF_REFD);
 
 	Lck_AssertHeld(&exphdl->mtx);
 
-	if ((cmds | oc->exp_flags) & OC_EF_REFD) {
+	if (oc->exp_flags & OC_EF_REFD) {
 		if (!(oc->exp_flags & OC_EF_POSTED)) {
 			if (cmds & OC_EF_REMOVE)
 				VSTAILQ_INSERT_HEAD(&exphdl->inbox,
@@ -119,11 +120,30 @@ exp_mail_it(struct objcore *oc, uint8_t cmds)
 			VSC_C_main->exp_mailed++;
 		}
 		oc->exp_flags |= cmds | OC_EF_POSTED;
-		AN(oc->exp_flags & OC_EF_REFD);
 		AZ(pthread_cond_signal(&exphdl->condvar));
 	}
 }
 
+/*--------------------------------------------------------------------
+ * Setup a new ObjCore for control by expire. Should be called with the
+ * ObjHead locked by HSH_Unbusy(/HSH_Insert) (in private access).
+ */
+
+void
+EXP_RefNewObjcore(struct objcore *oc)
+{
+	CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
+
+	Lck_AssertHeld(&oc->objhead->mtx);
+
+	AZ(oc->exp_flags);
+	assert(oc->refcnt >= 1);
+	oc->refcnt++;
+	oc->exp_flags |= OC_EF_REFD;
+}
+
+
+
 /*--------------------------------------------------------------------
  * Call EXP's attention to a an oc
  */
@@ -152,6 +172,10 @@ EXP_Insert(struct worker *wrk, struct objcore *oc)
 
 	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
 	CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
+
+	if (!(oc->exp_flags & OC_EF_REFD))
+		return;
+
 	assert(oc->refcnt >= 2);
 
 	AZ(oc->flags & OC_F_DYING);
@@ -159,7 +183,7 @@ EXP_Insert(struct worker *wrk, struct objcore *oc)
 	ObjSendEvent(wrk, oc, OEV_INSERT);
 	Lck_Lock(&exphdl->mtx);
 	AZ(oc->exp_flags & (OC_EF_INSERT | OC_EF_MOVE));
-	exp_mail_it(oc, OC_EF_INSERT | OC_EF_REFD | OC_EF_MOVE);
+	exp_mail_it(oc, OC_EF_INSERT | OC_EF_MOVE);
 	Lck_Unlock(&exphdl->mtx);
 }
 
diff --git a/bin/varnishd/cache/cache_hash.c b/bin/varnishd/cache/cache_hash.c
index 831572956..9fd1b5091 100644
--- a/bin/varnishd/cache/cache_hash.c
+++ b/bin/varnishd/cache/cache_hash.c
@@ -305,7 +305,7 @@ HSH_Insert(struct worker *wrk, const void *digest, struct objcore *oc,
 	   objecthead. The new object inherits our objhead reference. */
 	oc->objhead = oh;
 	VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list);
-	oc->refcnt++;				// For EXP_Insert
+	EXP_RefNewObjcore(oc);
 	Lck_Unlock(&oh->mtx);
 
 	BAN_RefBan(oc, ban);
@@ -836,7 +836,7 @@ HSH_Unbusy(struct worker *wrk, struct objcore *oc)
 	assert(oh->refcnt > 0);
 	assert(oc->refcnt > 0);
 	if (!(oc->flags & OC_F_PRIVATE))
-		oc->refcnt++;			// For EXP_Insert
+		EXP_RefNewObjcore(oc); /* Takes a ref for expiry */
 	/* XXX: strictly speaking, we should sort in Date: order. */
 	VTAILQ_REMOVE(&oh->objcs, oc, hsh_list);
 	VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list);
@@ -846,8 +846,8 @@ HSH_Unbusy(struct worker *wrk, struct objcore *oc)
 		hsh_rush1(wrk, oh, &rush, HSH_RUSH_POLICY);
 	}
 	Lck_Unlock(&oh->mtx);
-	if (!(oc->flags & OC_F_PRIVATE))
-		EXP_Insert(wrk, oc);
+	EXP_Insert(wrk, oc); /* Does nothing unless EXP_RefNewObjcore was
+			      * called */
 	hsh_rush2(wrk, &rush);
 }
 
diff --git a/bin/varnishd/cache/cache_varnishd.h b/bin/varnishd/cache/cache_varnishd.h
index b0a6a1930..339a89ddd 100644
--- a/bin/varnishd/cache/cache_varnishd.h
+++ b/bin/varnishd/cache/cache_varnishd.h
@@ -165,6 +165,7 @@ void VDI_Init(void);
 /* cache_exp.c */
 vtim_real EXP_Ttl(const struct req *, const struct objcore *);
 vtim_real EXP_Ttl_grace(const struct req *, const struct objcore *oc);
+void EXP_RefNewObjcore(struct objcore *);
 void EXP_Insert(struct worker *wrk, struct objcore *oc);
 void EXP_Remove(struct objcore *);
 


More information about the varnish-commit mailing list