Commit 4330915b authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Clean up the LRU stuff a bit, wrap the senteniel with the list head etc.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@4213 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent e852a475
...@@ -296,6 +296,15 @@ struct objcore { ...@@ -296,6 +296,15 @@ struct objcore {
struct ban *ban; struct ban *ban;
}; };
/*--------------------------------------------------------------------*/
struct lru {
unsigned magic;
#define LRU_MAGIC 0x3fec7bb0
VLIST_HEAD(,objcore) lru_head;
struct objcore senteniel;
};
/* Object structure --------------------------------------------------*/ /* Object structure --------------------------------------------------*/
struct object { struct object {
...@@ -488,11 +497,11 @@ extern pthread_t cli_thread; ...@@ -488,11 +497,11 @@ extern pthread_t cli_thread;
/* cache_expiry.c */ /* cache_expiry.c */
void EXP_Insert(struct object *o); void EXP_Insert(struct object *o);
void EXP_Inject(struct objcore *oc, struct objcore *lrut, double ttl); void EXP_Inject(struct objcore *oc, struct lru *lru, double ttl);
void EXP_Init(void); void EXP_Init(void);
void EXP_Rearm(const struct object *o); void EXP_Rearm(const struct object *o);
int EXP_Touch(const struct object *o); int EXP_Touch(const struct object *o);
int EXP_NukeOne(struct sess *sp, const struct objcore_head *lru); int EXP_NukeOne(struct sess *sp, const struct lru *lru);
/* cache_fetch.c */ /* cache_fetch.c */
int FetchHdr(struct sess *sp); int FetchHdr(struct sess *sp);
......
...@@ -97,18 +97,18 @@ update_object_when(const struct object *o) ...@@ -97,18 +97,18 @@ update_object_when(const struct object *o)
*/ */
void void
EXP_Inject(struct objcore *oc, struct objcore *lrut, double ttl) EXP_Inject(struct objcore *oc, struct lru *lru, double ttl)
{ {
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
CHECK_OBJ_NOTNULL(lrut, OBJCORE_MAGIC); CHECK_OBJ_NOTNULL(lru, LRU_MAGIC);
Lck_Lock(&exp_mtx); Lck_Lock(&exp_mtx);
assert(oc->timer_idx == BINHEAP_NOIDX); assert(oc->timer_idx == BINHEAP_NOIDX);
oc->timer_when = ttl; oc->timer_when = ttl;
binheap_insert(exp_heap, oc); binheap_insert(exp_heap, oc);
assert(oc->timer_idx != BINHEAP_NOIDX); assert(oc->timer_idx != BINHEAP_NOIDX);
VLIST_INSERT_BEFORE(lrut, oc, lru_list); VLIST_INSERT_BEFORE(&lru->senteniel, oc, lru_list);
oc->flags |= OC_F_ONLRU; oc->flags |= OC_F_ONLRU;
Lck_Unlock(&exp_mtx); Lck_Unlock(&exp_mtx);
} }
...@@ -123,7 +123,8 @@ EXP_Inject(struct objcore *oc, struct objcore *lrut, double ttl) ...@@ -123,7 +123,8 @@ EXP_Inject(struct objcore *oc, struct objcore *lrut, double ttl)
void void
EXP_Insert(struct object *o) EXP_Insert(struct object *o)
{ {
struct objcore *oc, *lrut; struct objcore *oc;
struct lru *lru;
CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC); CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
CHECK_OBJ_NOTNULL(o->objcore, OBJCORE_MAGIC); CHECK_OBJ_NOTNULL(o->objcore, OBJCORE_MAGIC);
...@@ -134,15 +135,15 @@ EXP_Insert(struct object *o) ...@@ -134,15 +135,15 @@ EXP_Insert(struct object *o)
assert(o->entered != 0 && !isnan(o->entered)); assert(o->entered != 0 && !isnan(o->entered));
o->last_lru = o->entered; o->last_lru = o->entered;
lrut = STV_lru(o->objstore);
Lck_Lock(&exp_mtx); Lck_Lock(&exp_mtx);
assert(oc->timer_idx == BINHEAP_NOIDX); assert(oc->timer_idx == BINHEAP_NOIDX);
(void)update_object_when(o); (void)update_object_when(o);
binheap_insert(exp_heap, oc); binheap_insert(exp_heap, oc);
assert(oc->timer_idx != BINHEAP_NOIDX); assert(oc->timer_idx != BINHEAP_NOIDX);
if (o->objstore != NULL) { if (o->objstore != NULL) {
CHECK_OBJ_NOTNULL(lrut, OBJCORE_MAGIC); lru = STV_lru(o->objstore);
VLIST_INSERT_BEFORE(lrut, oc, lru_list); CHECK_OBJ_NOTNULL(lru, LRU_MAGIC);
VLIST_INSERT_BEFORE(&lru->senteniel, oc, lru_list);
oc->flags |= OC_F_ONLRU; oc->flags |= OC_F_ONLRU;
} }
Lck_Unlock(&exp_mtx); Lck_Unlock(&exp_mtx);
...@@ -163,7 +164,8 @@ int ...@@ -163,7 +164,8 @@ int
EXP_Touch(const struct object *o) EXP_Touch(const struct object *o)
{ {
int retval = 0; int retval = 0;
struct objcore *oc, *lrut; struct objcore *oc;
struct lru *lru;
CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC); CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
oc = o->objcore; oc = o->objcore;
...@@ -174,13 +176,13 @@ EXP_Touch(const struct object *o) ...@@ -174,13 +176,13 @@ EXP_Touch(const struct object *o)
CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC); CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC);
if (o->objstore == NULL) /* XXX ?? */ if (o->objstore == NULL) /* XXX ?? */
return (retval); return (retval);
lrut = STV_lru(o->objstore); lru = STV_lru(o->objstore);
CHECK_OBJ_NOTNULL(lrut, OBJCORE_MAGIC); CHECK_OBJ_NOTNULL(lru, LRU_MAGIC);
if (Lck_Trylock(&exp_mtx)) if (Lck_Trylock(&exp_mtx))
return (retval); return (retval);
if (oc->flags & OC_F_ONLRU) { /* XXX ?? */ if (oc->flags & OC_F_ONLRU) { /* XXX ?? */
VLIST_REMOVE(oc, lru_list); VLIST_REMOVE(oc, lru_list);
VLIST_INSERT_BEFORE(lrut, oc, lru_list); VLIST_INSERT_BEFORE(&lru->senteniel, oc, lru_list);
VSL_stats->n_lru_moved++; VSL_stats->n_lru_moved++;
retval = 1; retval = 1;
} }
...@@ -303,7 +305,7 @@ exp_timer(struct sess *sp, void *priv) ...@@ -303,7 +305,7 @@ exp_timer(struct sess *sp, void *priv)
*/ */
int int
EXP_NukeOne(struct sess *sp, const struct objcore_head *lru) EXP_NukeOne(struct sess *sp, const struct lru *lru)
{ {
struct objcore *oc; struct objcore *oc;
...@@ -318,7 +320,12 @@ EXP_NukeOne(struct sess *sp, const struct objcore_head *lru) ...@@ -318,7 +320,12 @@ EXP_NukeOne(struct sess *sp, const struct objcore_head *lru)
* *
*/ */
Lck_Lock(&exp_mtx); Lck_Lock(&exp_mtx);
VLIST_FOREACH(oc, lru, lru_list) { VLIST_FOREACH(oc, &lru->lru_head, lru_list) {
if (oc == &lru->senteniel) {
AZ(VLIST_NEXT(oc, lru_list));
oc = NULL;
break;
}
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
if (oc->timer_idx == BINHEAP_NOIDX) /* exp_timer has it */ if (oc->timer_idx == BINHEAP_NOIDX) /* exp_timer has it */
continue; continue;
......
...@@ -64,8 +64,3 @@ struct choice { ...@@ -64,8 +64,3 @@ struct choice {
const char *name; const char *name;
void *ptr; void *ptr;
}; };
/* Sort of hack-ish... */
struct objcore;
VLIST_HEAD(objcore_head, objcore);
...@@ -43,6 +43,18 @@ static VTAILQ_HEAD(, stevedore) stevedores = ...@@ -43,6 +43,18 @@ static VTAILQ_HEAD(, stevedore) stevedores =
static const struct stevedore * volatile stv_next; static const struct stevedore * volatile stv_next;
static struct lru *
LRU_Alloc(void)
{
struct lru *l;
ALLOC_OBJ(l, LRU_MAGIC);
AN(l);
VLIST_INIT(&l->lru_head);
VLIST_INSERT_HEAD(&l->lru_head, &l->senteniel, lru_list);
return (l);
}
struct storage * struct storage *
STV_alloc(struct sess *sp, size_t size) STV_alloc(struct sess *sp, size_t size)
{ {
...@@ -81,7 +93,7 @@ STV_alloc(struct sess *sp, size_t size) ...@@ -81,7 +93,7 @@ STV_alloc(struct sess *sp, size_t size)
break; break;
/* no luck; try to free some space and keep trying */ /* no luck; try to free some space and keep trying */
if (EXP_NukeOne(sp, &stv->lru) == -1) if (EXP_NukeOne(sp, stv->lru) == -1)
break; break;
/* Enough is enough: try another if we have one */ /* Enough is enough: try another if we have one */
...@@ -124,9 +136,7 @@ STV_add(const struct stevedore *stv2, int ac, char * const *av) ...@@ -124,9 +136,7 @@ STV_add(const struct stevedore *stv2, int ac, char * const *av)
*stv = *stv2; *stv = *stv2;
AN(stv->name); AN(stv->name);
AN(stv->alloc); AN(stv->alloc);
ALLOC_OBJ(stv->lru_tail, OBJCORE_MAGIC); stv->lru = LRU_Alloc();
VLIST_INIT(&stv->lru);
VLIST_INSERT_HEAD(&stv->lru, stv->lru_tail, lru_list);
if (stv->init != NULL) if (stv->init != NULL)
stv->init(stv, ac, av); stv->init(stv, ac, av);
...@@ -161,12 +171,12 @@ STV_close(void) ...@@ -161,12 +171,12 @@ STV_close(void)
} }
} }
struct objcore * struct lru *
STV_lru(struct storage *st) STV_lru(const struct storage *st)
{ {
CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC); CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
return (st->stevedore->lru_tail); return (st->stevedore->lru);
} }
const struct choice STV_choice[] = { const struct choice STV_choice[] = {
......
...@@ -42,6 +42,7 @@ typedef void storage_free_f(struct storage *); ...@@ -42,6 +42,7 @@ typedef void storage_free_f(struct storage *);
typedef void storage_object_f(const struct sess *sp); typedef void storage_object_f(const struct sess *sp);
typedef void storage_close_f(const struct stevedore *); typedef void storage_close_f(const struct stevedore *);
struct stevedore { struct stevedore {
unsigned magic; unsigned magic;
#define STEVEDORE_MAGIC 0x4baf43db #define STEVEDORE_MAGIC 0x4baf43db
...@@ -54,8 +55,7 @@ struct stevedore { ...@@ -54,8 +55,7 @@ struct stevedore {
storage_object_f *object; storage_object_f *object;
storage_close_f *close; storage_close_f *close;
struct objcore_head lru; struct lru *lru;
struct objcore *lru_tail;
/* private fields */ /* private fields */
void *priv; void *priv;
...@@ -69,7 +69,7 @@ void STV_free(struct storage *st); ...@@ -69,7 +69,7 @@ void STV_free(struct storage *st);
void STV_add(const struct stevedore *stv, int ac, char * const *av); void STV_add(const struct stevedore *stv, int ac, char * const *av);
void STV_open(void); void STV_open(void);
void STV_close(void); void STV_close(void);
struct objcore *STV_lru(struct storage *st); struct lru *STV_lru(const struct storage *st);
int STV_GetFile(const char *fn, int *fdp, const char **fnp, const char *ctx); int STV_GetFile(const char *fn, int *fdp, const char **fnp, const char *ctx);
......
...@@ -837,7 +837,7 @@ smp_load_seg(struct sess *sp, const struct smp_sc *sc, struct smp_seg *sg) ...@@ -837,7 +837,7 @@ smp_load_seg(struct sess *sp, const struct smp_sc *sc, struct smp_seg *sg)
memcpy(sp->wrk->nobjhead->digest, so->hash, SHA256_LEN); memcpy(sp->wrk->nobjhead->digest, so->hash, SHA256_LEN);
(void)HSH_Insert(sp); (void)HSH_Insert(sp);
AZ(sp->wrk->nobjcore); AZ(sp->wrk->nobjcore);
EXP_Inject(oc, sc->parent->lru_tail, so->ttl); EXP_Inject(oc, sc->parent->lru, so->ttl);
sg->nalloc++; sg->nalloc++;
} }
WRK_SumStat(sp->wrk); WRK_SumStat(sp->wrk);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment