[6.0] 0988d5f3c Repurpose OC_EF_REFD flag slightly
Reza Naghibi
reza at naghibi.com
Wed Apr 21 18:26:05 UTC 2021
commit 0988d5f3cfe37f4e5d022338dda1ea90e1b78c73
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.
Conflicts:
bin/varnishd/cache/cache_varnishd.h
diff --git a/bin/varnishd/cache/cache_expire.c b/bin/varnishd/cache/cache_expire.c
index 25d0c6e0e..3f81e2981 100644
--- a/bin/varnishd/cache/cache_expire.c
+++ b/bin/varnishd/cache/cache_expire.c
@@ -101,10 +101,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,
@@ -115,11 +116,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
*/
@@ -148,6 +168,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);
@@ -155,7 +179,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 9f1558aa0..38ef503ba 100644
--- a/bin/varnishd/cache/cache_hash.c
+++ b/bin/varnishd/cache/cache_hash.c
@@ -294,7 +294,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);
@@ -772,7 +772,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);
@@ -782,8 +782,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 cd97d824a..0141cf2ff 100644
--- a/bin/varnishd/cache/cache_varnishd.h
+++ b/bin/varnishd/cache/cache_varnishd.h
@@ -128,6 +128,7 @@ void VDI_Init(void);
/* cache_exp.c */
double EXP_Ttl(const struct req *, const struct objcore *);
double 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