[master] 175cac7 LRU code polishing
Poul-Henning Kamp
phk at FreeBSD.org
Thu Feb 4 12:04:18 CET 2016
commit 175cac7ef3a1e68d8ed0f4bd41cc2177d43233d6
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Thu Feb 4 11:04:06 2016 +0000
LRU code polishing
diff --git a/bin/varnishd/storage/storage.h b/bin/varnishd/storage/storage.h
index d63b82b..5e0d78f 100644
--- a/bin/varnishd/storage/storage.h
+++ b/bin/varnishd/storage/storage.h
@@ -119,14 +119,14 @@ int STV_GetFile(const char *fn, int *fdp, const char **fnp, const char *ctx);
uintmax_t STV_FileSize(int fd, const char *size, unsigned *granularity,
const char *ctx);
+/*--------------------------------------------------------------------*/
struct lru *LRU_Alloc(void);
void LRU_Free(struct lru *);
-void LRU_Add(struct objcore *);
+void LRU_Add(struct objcore *, double now);
void LRU_Remove(struct objcore *);
int LRU_NukeOne(struct worker *, struct lru *);
void LRU_Touch(struct worker *, struct objcore *, double now);
-
/*--------------------------------------------------------------------*/
extern const struct stevedore sma_stevedore;
extern const struct stevedore smf_stevedore;
diff --git a/bin/varnishd/storage/storage_lru.c b/bin/varnishd/storage/storage_lru.c
index 0acb91d..0fbfb36 100644
--- a/bin/varnishd/storage/storage_lru.c
+++ b/bin/varnishd/storage/storage_lru.c
@@ -25,22 +25,17 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * STEVEDORE: one who works at or is responsible for loading and
- * unloading ships in port. Example: "on the wharves, stevedores were
- * unloading cargo from the far corners of the world." Origin: Spanish
- * estibador, from estibar to pack. First Known Use: 1788
+ * Least-Recently-Used logic for freeing space in stevedores.
*/
#include "config.h"
-#include <stdio.h>
#include <stdlib.h>
#include "cache/cache.h"
#include "hash/hash_slinger.h"
#include "storage/storage.h"
-#include "vtim.h"
struct lru {
unsigned magic;
@@ -70,16 +65,17 @@ LRU_Free(struct lru *lru)
}
void
-LRU_Add(struct objcore *oc)
+LRU_Add(struct objcore *oc, double now)
{
struct lru *lru;
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
+ AZ(isnan(now));
lru = ObjGetLRU(oc);
CHECK_OBJ_NOTNULL(lru, LRU_MAGIC);
Lck_Lock(&lru->mtx);
VTAILQ_INSERT_TAIL(&lru->lru_head, oc, lru_list);
- oc->last_lru = VTIM_real();
+ oc->last_lru = now;
Lck_Unlock(&lru->mtx);
}
@@ -139,7 +135,7 @@ LRU_Touch(struct worker *wrk, struct objcore *oc, double now)
/*--------------------------------------------------------------------
* Attempt to make space by nuking the oldest object on the LRU list
* which isn't in use.
- * Returns: 1: did, 0: didn't, -1: can't
+ * Returns: 1: did, 0: didn't;
*/
int
@@ -170,7 +166,7 @@ LRU_NukeOne(struct worker *wrk, struct lru *lru)
if (oc == NULL) {
VSLb(wrk->vsl, SLT_ExpKill, "LRU_Fail");
- return (-1);
+ return (0);
}
/* XXX: We could grab and return one storage segment to our caller */
diff --git a/bin/varnishd/storage/storage_persistent.c b/bin/varnishd/storage/storage_persistent.c
index 735c7c5..f405755 100644
--- a/bin/varnishd/storage/storage_persistent.c
+++ b/bin/varnishd/storage/storage_persistent.c
@@ -531,7 +531,7 @@ smp_allocobj(struct worker *wrk, const struct stevedore *stv,
while (1) {
if (really > 0) {
- if (LRU_NukeOne(wrk, stv->lru) == -1)
+ if (!LRU_NukeOne(wrk, stv->lru))
return (0);
really--;
}
diff --git a/bin/varnishd/storage/storage_simple.c b/bin/varnishd/storage/storage_simple.c
index 95f4c05..b02846e 100644
--- a/bin/varnishd/storage/storage_simple.c
+++ b/bin/varnishd/storage/storage_simple.c
@@ -128,7 +128,7 @@ SML_allocobj(struct worker *wrk, const struct stevedore *stv,
ltot = sizeof(struct object) + PRNDUP(wsl);
while (1) {
if (really > 0) {
- if (LRU_NukeOne(wrk, stv->lru) == -1)
+ if (!LRU_NukeOne(wrk, stv->lru))
return (0);
really--;
}
@@ -339,7 +339,7 @@ objallocwithnuke(struct worker *wrk, const struct stevedore *stv, size_t size)
/* no luck; try to free some space and keep trying */
if (fail < cache_param->nuke_limit &&
- LRU_NukeOne(wrk, stv->lru) == -1)
+ !LRU_NukeOne(wrk, stv->lru))
break;
}
CHECK_OBJ_ORNULL(st, STORAGE_MAGIC);
@@ -505,7 +505,7 @@ sml_stable(struct worker *wrk, struct objcore *oc, struct boc *boc)
sml_stv_free(stv, st);
}
- LRU_Add(oc);
+ LRU_Add(oc, wrk->lastused); // approx timestamp is OK
}
static void * __match_proto__(objgetattr_f)
More information about the varnish-commit
mailing list