[master] 0b5aeb3 More polishing and cleanup of LRU
Poul-Henning Kamp
phk at FreeBSD.org
Fri Feb 5 00:52:57 CET 2016
commit 0b5aeb39b08b60d39df1cca9d496be2c5ddf5970
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Thu Feb 4 23:08:19 2016 +0000
More polishing and cleanup of LRU
diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index fde3164..f7dd1ce 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -855,7 +855,6 @@ uint64_t ObjGetLen(struct worker *, struct objcore *);
void ObjUpdateMeta(struct worker *, struct objcore *);
void ObjFreeObj(struct worker *, struct objcore *);
void ObjSlim(struct worker *, struct objcore *);
-struct lru *ObjGetLRU(const struct objcore *);
int ObjHasAttr(struct worker *, struct objcore *, enum obj_attr);
void *ObjGetAttr(struct worker *, struct objcore *, enum obj_attr,
ssize_t *len);
diff --git a/bin/varnishd/cache/cache_obj.c b/bin/varnishd/cache/cache_obj.c
index cd0c85c..4968ee4 100644
--- a/bin/varnishd/cache/cache_obj.c
+++ b/bin/varnishd/cache/cache_obj.c
@@ -312,17 +312,6 @@ ObjFreeObj(struct worker *wrk, struct objcore *oc)
}
/*====================================================================
- */
-struct lru *
-ObjGetLRU(const struct objcore *oc)
-{
- const struct obj_methods *m = obj_getmethods(oc);
-
- AN(m->objgetlru);
- return (m->objgetlru(oc));
-}
-
-/*====================================================================
* ObjHasAttr()
*
* Check if object has this attribute
diff --git a/bin/varnishd/cache/cache_req_fsm.c b/bin/varnishd/cache/cache_req_fsm.c
index a24ca3a..feb41d6 100644
--- a/bin/varnishd/cache/cache_req_fsm.c
+++ b/bin/varnishd/cache/cache_req_fsm.c
@@ -63,8 +63,7 @@ cnt_deliver(struct worker *wrk, struct req *req)
assert(req->objcore->refcnt > 0);
- if (req->objcore->exp_flags & OC_EF_EXP)
- ObjTouch(req->wrk, req->objcore, req->t_prev);
+ ObjTouch(req->wrk, req->objcore, req->t_prev);
HTTP_Setup(req->resp, req->ws, req->vsl, SLT_RespMethod);
if (HTTP_Decode(req->resp,
diff --git a/bin/varnishd/storage/stevedore.c b/bin/varnishd/storage/stevedore.c
index 5c20011..fcb8461 100644
--- a/bin/varnishd/storage/stevedore.c
+++ b/bin/varnishd/storage/stevedore.c
@@ -133,10 +133,9 @@ STV_open(void)
stv->open(stv);
}
stv = stv_transient;
- if (stv->open != NULL) {
- stv->lru = LRU_Alloc();
+ stv->lru = LRU_Alloc();
+ if (stv->open != NULL)
stv->open(stv);
- }
stv_next = VTAILQ_FIRST(&stv_stevedores);
}
diff --git a/bin/varnishd/storage/storage.h b/bin/varnishd/storage/storage.h
index 5e0d78f..d00db91 100644
--- a/bin/varnishd/storage/storage.h
+++ b/bin/varnishd/storage/storage.h
@@ -96,13 +96,13 @@ struct stevedore {
const struct obj_methods
*methods;
- struct lru *lru;
+ struct lru *lru; /* For storage_lru.c */
#define VRTSTVVAR(nm, vtype, ctype, dval) storage_var_##ctype *var_##nm;
#include "tbl/vrt_stv_var.h"
#undef VRTSTVVAR
- /* private fields */
+ /* private fields for the stevedore */
void *priv;
VTAILQ_ENTRY(stevedore) list;
diff --git a/bin/varnishd/storage/storage_lru.c b/bin/varnishd/storage/storage_lru.c
index 0fbfb36..4e7ae2e 100644
--- a/bin/varnishd/storage/storage_lru.c
+++ b/bin/varnishd/storage/storage_lru.c
@@ -33,6 +33,7 @@
#include <stdlib.h>
#include "cache/cache.h"
+#include "cache/cache_obj.h"
#include "hash/hash_slinger.h"
#include "storage/storage.h"
@@ -44,22 +45,36 @@ struct lru {
struct lock mtx;
};
+static struct lru *
+lru_get(const struct objcore *oc)
+{
+ CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
+ CHECK_OBJ_NOTNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
+ AN(oc->stobj->stevedore->methods);
+ const struct obj_methods *m = oc->stobj->stevedore->methods;
+
+ if(m->objgetlru != NULL)
+ return (m->objgetlru(oc));
+ return (oc->stobj->stevedore->lru);
+}
+
struct lru *
LRU_Alloc(void)
{
- struct lru *l;
+ struct lru *lru;
- ALLOC_OBJ(l, LRU_MAGIC);
- AN(l);
- VTAILQ_INIT(&l->lru_head);
- Lck_New(&l->mtx, lck_lru);
- return (l);
+ ALLOC_OBJ(lru, LRU_MAGIC);
+ AN(lru);
+ VTAILQ_INIT(&lru->lru_head);
+ Lck_New(&lru->mtx, lck_lru);
+ return (lru);
}
void
LRU_Free(struct lru *lru)
{
CHECK_OBJ_NOTNULL(lru, LRU_MAGIC);
+ AN(VTAILQ_EMPTY(&lru->lru_head));
Lck_Delete(&lru->mtx);
FREE_OBJ(lru);
}
@@ -71,7 +86,7 @@ LRU_Add(struct objcore *oc, double now)
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
AZ(isnan(now));
- lru = ObjGetLRU(oc);
+ lru = lru_get(oc);
CHECK_OBJ_NOTNULL(lru, LRU_MAGIC);
Lck_Lock(&lru->mtx);
VTAILQ_INSERT_TAIL(&lru->lru_head, oc, lru_list);
@@ -85,7 +100,7 @@ LRU_Remove(struct objcore *oc)
struct lru *lru;
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
- lru = ObjGetLRU(oc);
+ lru = lru_get(oc);
CHECK_OBJ_NOTNULL(lru, LRU_MAGIC);
Lck_Lock(&lru->mtx);
if (!isnan(oc->last_lru)) {
@@ -103,6 +118,9 @@ LRU_Touch(struct worker *wrk, struct objcore *oc, double now)
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
+ if (isnan(oc->last_lru))
+ return;
+
/*
* To avoid the exphdl->mtx becoming a hotspot, we only
* attempt to move objects if they have not been moved
@@ -110,20 +128,16 @@ LRU_Touch(struct worker *wrk, struct objcore *oc, double now)
* obviously leaves the LRU list imperfectly sorted.
*/
- if (oc->flags & OC_F_INCOMPLETE)
- return;
-
if (now - oc->last_lru < cache_param->lru_interval)
return;
- lru = ObjGetLRU(oc);
+ lru = lru_get(oc);
CHECK_OBJ_NOTNULL(lru, LRU_MAGIC);
if (Lck_Trylock(&lru->mtx))
return;
if (!isnan(oc->last_lru)) {
- /* Can only touch it while it's actually on the LRU list */
VTAILQ_REMOVE(&lru->lru_head, oc, lru_list);
VTAILQ_INSERT_TAIL(&lru->lru_head, oc, lru_list);
VSC_C_main->n_lru_moved++;
@@ -145,16 +159,16 @@ LRU_NukeOne(struct worker *wrk, struct lru *lru)
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
CHECK_OBJ_NOTNULL(lru, LRU_MAGIC);
+
/* Find the first currently unused object on the LRU. */
Lck_Lock(&lru->mtx);
VTAILQ_FOREACH_SAFE(oc, &lru->lru_head, lru_list, oc2) {
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
+ AZ(isnan(oc->last_lru));
VSLb(wrk->vsl, SLT_ExpKill, "LRU_Cand p=%p f=0x%x r=%d",
oc, oc->flags, oc->refcnt);
- AZ(isnan(oc->last_lru));
-
if (ObjSnipe(wrk, oc)) {
VSC_C_main->n_lru_nuked++; // XXX per lru ?
VTAILQ_REMOVE(&lru->lru_head, oc, lru_list);
@@ -175,6 +189,6 @@ LRU_NukeOne(struct worker *wrk, struct lru *lru)
EXP_Poke(oc);
VSLb(wrk->vsl, SLT_ExpKill, "LRU x=%u", ObjGetXID(wrk, oc));
- (void)HSH_DerefObjCore(wrk, &oc);
+ (void)HSH_DerefObjCore(wrk, &oc); // Ref from ObjSnipe
return (1);
}
diff --git a/bin/varnishd/storage/storage_simple.c b/bin/varnishd/storage/storage_simple.c
index b02846e..1280537 100644
--- a/bin/varnishd/storage/storage_simple.c
+++ b/bin/varnishd/storage/storage_simple.c
@@ -215,16 +215,6 @@ sml_objfree(struct worker *wrk, struct objcore *oc)
wrk->stats->n_object--;
}
-static struct lru * __match_proto__(objgetlru_f)
-sml_objgetlru(const struct objcore *oc)
-{
- const struct stevedore *stv;
-
- stv = oc->stobj->stevedore;
- CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
- return (stv->lru);
-}
-
static int __match_proto__(objiterate_f)
sml_iterator(struct worker *wrk, struct objcore *oc,
void *priv, objiterate_f *func)
@@ -613,7 +603,6 @@ sml_setattr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
const struct obj_methods SML_methods = {
.objfree = sml_objfree,
- .objgetlru = sml_objgetlru,
.objiterator = sml_iterator,
.objgetspace = sml_getspace,
.objextend = sml_extend,
More information about the varnish-commit
mailing list