[master] 0125de0 Turn obj.hits and obj.last_use (notice new '_', see below) into VCL only variables, and add a commented example in default.vcl.

Poul-Henning Kamp phk at varnish-cache.org
Tue Sep 17 11:32:31 CEST 2013


commit 0125de0eaa7a88f9170f4358cb5f9e731953b3e7
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Sep 17 09:29:10 2013 +0000

    Turn obj.hits and obj.last_use (notice new '_', see below) into VCL
    only variables, and add a commented example in default.vcl.
    
    The reason for this is to avoid VM write-backs to cached objects by default.
    
    Previously we did this with the obj_readonly parameter, but this puts
    it right where the user can see it.
    
    Notice that the two variables are not locked, so they cannot be
    trusted to be precise.
    
    obj.lastuse changes to obj.last_use because its type changes from
    DURATION to TIME, (and to improve readability in general)
    
    Fix & Polish relevant test-cases

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index f23241c..7647e9c 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -595,7 +595,6 @@ struct object {
 	struct objcore		*objcore;
 
 	uint8_t			*vary;
-	unsigned		hits;
 	uint16_t		response;
 
 	/* XXX: make bitmap */
@@ -609,6 +608,8 @@ struct object {
 
 	struct exp		exp;
 
+	/* VCL only variables */
+	long			hits;
 	double			last_modified;
 
 	struct http		*http;
diff --git a/bin/varnishd/cache/cache_hash.c b/bin/varnishd/cache/cache_hash.c
index e2959fe..90af1fc 100644
--- a/bin/varnishd/cache/cache_hash.c
+++ b/bin/varnishd/cache/cache_hash.c
@@ -435,8 +435,6 @@ HSH_Lookup(struct req *req, struct objcore **ocp, struct objcore **bocp,
 			oc->refcnt++;
 			Lck_Unlock(&oh->mtx);
 			assert(HSH_DerefObjHead(&wrk->stats, &oh));
-			if (!cache_param->obj_readonly && o->hits < INT_MAX)
-				o->hits++;
 			*ocp = oc;
 			return (HSH_HIT);
 		}
diff --git a/bin/varnishd/cache/cache_req_fsm.c b/bin/varnishd/cache/cache_req_fsm.c
index b0e7be7..812759c 100644
--- a/bin/varnishd/cache/cache_req_fsm.c
+++ b/bin/varnishd/cache/cache_req_fsm.c
@@ -108,8 +108,6 @@ cnt_deliver(struct worker *wrk, struct req *req)
 		if ((req->t_resp - req->obj->objcore->last_lru) >
 		    cache_param->lru_timeout && EXP_Touch(req->obj->objcore))
 			req->obj->objcore->last_lru = req->t_resp;
-		if (!cache_param->obj_readonly)
-			req->obj->last_use = req->t_resp; /* XXX: locking ? */
 	}
 
 	HTTP_Setup(req->resp, req->ws, req->vsl, HTTP_Resp);
diff --git a/bin/varnishd/cache/cache_vrt_var.c b/bin/varnishd/cache/cache_vrt_var.c
index a1c35f0..3d691a5 100644
--- a/bin/varnishd/cache/cache_vrt_var.c
+++ b/bin/varnishd/cache/cache_vrt_var.c
@@ -41,7 +41,6 @@
 #include "vrt_obj.h"
 #include "vsa.h"
 #include "vtcp.h"
-#include "vtim.h"
 
 static char vrt_hostname[255] = "";
 
@@ -609,26 +608,31 @@ VRT_r_server_port(const struct vrt_ctx *ctx)
 
 /*--------------------------------------------------------------------*/
 
-long
-VRT_r_obj_hits(const struct vrt_ctx *ctx)
-{
-
-	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
-	CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
-	CHECK_OBJ_NOTNULL(ctx->req->obj, OBJECT_MAGIC);	/* XXX */
-	return (ctx->req->obj->hits);
+#define VOBJ_L(type, field)						\
+void									\
+VRT_l_obj_##field(const struct vrt_ctx *ctx, type a)			\
+{									\
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);				\
+	CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);				\
+	CHECK_OBJ_NOTNULL(ctx->req->obj, OBJECT_MAGIC);			\
+	ctx->req->obj->field = a;					\
 }
 
-double
-VRT_r_obj_lastuse(const struct vrt_ctx *ctx)
-{
-
-	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
-	CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
-	CHECK_OBJ_NOTNULL(ctx->req->obj, OBJECT_MAGIC);	/* XXX */
-	return (VTIM_real() - ctx->req->obj->last_use);
+#define VOBJ_R(type, field)						\
+type									\
+VRT_r_obj_##field(const struct vrt_ctx *ctx)				\
+{									\
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);				\
+	CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);				\
+	CHECK_OBJ_NOTNULL(ctx->req->obj, OBJECT_MAGIC);			\
+	return (ctx->req->obj->field);					\
 }
 
+VOBJ_L(long, hits)
+VOBJ_R(long, hits)
+VOBJ_L(double, last_use)
+VOBJ_R(double, last_use)
+
 unsigned
 VRT_r_obj_uncacheable(const struct vrt_ctx *ctx)
 {
diff --git a/bin/varnishd/default.vcl b/bin/varnishd/default.vcl
index 50e5da2..db64791 100644
--- a/bin/varnishd/default.vcl
+++ b/bin/varnishd/default.vcl
@@ -137,6 +137,13 @@ sub vcl_backend_response {
 }
 
 sub vcl_deliver {
+    /*
+     * These two write to the stored object causing extra page faults
+     * Enable them only if you need them.
+     *
+     * set obj.hits = obj.hits + 1;
+     * set obj.lastuse = now;
+     */
     return (deliver);
 }
 
diff --git a/bin/varnishd/mgt/mgt_param_tbl.c b/bin/varnishd/mgt/mgt_param_tbl.c
index 6a0fc7c..c4a9566 100644
--- a/bin/varnishd/mgt/mgt_param_tbl.c
+++ b/bin/varnishd/mgt/mgt_param_tbl.c
@@ -603,11 +603,5 @@ const struct parspec mgt_parspec[] = {
 		0,
 		"10,100,10", ""},
 
-	{ "obj_readonly", tweak_bool, &mgt_param.obj_readonly, 0, 0,
-		"If set, we do not update obj.hits and obj.lastuse to"
-		" avoid dirtying VM pages associated with cached objects.",
-		0,
-		"false", "bool"},
-
 	{ NULL, NULL, NULL }
 };
diff --git a/bin/varnishd/storage/stevedore.c b/bin/varnishd/storage/stevedore.c
index a7d0b44..a8203c4 100644
--- a/bin/varnishd/storage/stevedore.c
+++ b/bin/varnishd/storage/stevedore.c
@@ -284,6 +284,7 @@ STV_MkObject(struct stevedore *stv, struct busyobj *bo,
 	HTTP_Setup(o->http, bo->ws_o, bo->vsl, HTTP_Obj);
 	o->http->magic = HTTP_MAGIC;
 	o->exp = bo->exp;
+	o->last_use = bo->exp.entered;
 	VTAILQ_INIT(&o->store);
 	bo->stats->n_object++;
 
diff --git a/bin/varnishtest/tests/c00037.vtc b/bin/varnishtest/tests/c00037.vtc
index e854a3b..e26be5b 100644
--- a/bin/varnishtest/tests/c00037.vtc
+++ b/bin/varnishtest/tests/c00037.vtc
@@ -13,13 +13,6 @@ varnish v1 -vcl+backend {
 			set req.hash_always_miss = true;
 		}
 	}
-	sub vcl_deliver {
-		if(obj.hits > 0) {
-			set resp.http.X-Cache = "HIT";
-		} else {
-			set resp.http.X-Cache = "MISS";
-		}
-	}
  } -start 
 
 client c1 {
diff --git a/bin/varnishtest/tests/c00038.vtc b/bin/varnishtest/tests/c00038.vtc
index 07e2657..ee396db 100644
--- a/bin/varnishtest/tests/c00038.vtc
+++ b/bin/varnishtest/tests/c00038.vtc
@@ -24,13 +24,6 @@ varnish v1 -vcl+backend {
 			set req.backend = s2;
 		}
 	}
-	sub vcl_deliver {
-		if(obj.hits > 0) {
-			set resp.http.X-Cache = "HIT";
-		} else {
-			set resp.http.X-Cache = "MISS";
-		}
-	}
 } -start 
 
 client c1 {
diff --git a/bin/varnishtest/tests/r01073.vtc b/bin/varnishtest/tests/r01073.vtc
index 936f8ce..b9f0e87 100644
--- a/bin/varnishtest/tests/r01073.vtc
+++ b/bin/varnishtest/tests/r01073.vtc
@@ -26,13 +26,6 @@ varnish v1 -vcl+backend {
 			set req.backend = s2;
 		}
 	}
-	sub vcl_deliver {
-		if(obj.hits > 0) {
-			set resp.http.X-Cache = "HIT";
-		} else {
-			set resp.http.X-Cache = "MISS";
-		}
-	}
 } -start 
 
 client c1 {
diff --git a/bin/varnishtest/tests/v00013.vtc b/bin/varnishtest/tests/v00013.vtc
index 031e484..882342f 100644
--- a/bin/varnishtest/tests/v00013.vtc
+++ b/bin/varnishtest/tests/v00013.vtc
@@ -1,4 +1,4 @@
-varnishtest "Check obj.hits"
+varnishtest "Check obj.hits and obj.last_use"
 
 server s1 {
 	rxreq
@@ -13,6 +13,9 @@ varnish v1 -vcl+backend {
 
 	sub vcl_deliver {
 		set resp.http.foo = obj.hits;
+		set resp.http.bar = now - obj.last_use;
+		set obj.hits = obj.hits + 1;
+		set obj.last_use = now;
 	}
 } -start
 
@@ -31,9 +34,11 @@ client c1 {
 	rxresp
 	expect resp.status == 200
 	expect resp.http.foo == 0
+	delay .1
 
 	txreq
 	rxresp
 	expect resp.status == 200
 	expect resp.http.foo == 2
+	expect resp.http.bar >= 0.100
 } -run 
diff --git a/bin/varnishtest/tests/v00025.vtc b/bin/varnishtest/tests/v00025.vtc
index a191969..23c2530 100644
--- a/bin/varnishtest/tests/v00025.vtc
+++ b/bin/varnishtest/tests/v00025.vtc
@@ -7,46 +7,41 @@ server s1 {
 
 varnish v1 -vcl+backend {
 
-sub vcl_deliver {
-	if (obj.lastuse > 3 s) {
-		set resp.http.lastuse = "then";
-	} else {
-		set resp.http.lastuse = "now";
+	sub vcl_deliver {
+		set resp.http.server_port = server.port;
 	}
-	set resp.http.server_port = server.port;
-}
-
-sub vcl_backend_response {
-	set beresp.http.backend = bereq.backend;
-	if (beresp.ttl > 3 s) {
-		set beresp.http.ttl = "long";
-	} else {
-		set beresp.http.ttl = "short";
-	}
-}
 
-sub vcl_hit {
-	if (obj.grace < 3m) {
-		set obj.grace = 1m;
-	} else {
-		set obj.grace = 2m;
-	}
-	if (obj.ttl < 3m) {
-		set obj.ttl = 2m;
-	} else {
-		set obj.ttl = 3m;
+	sub vcl_backend_response {
+		set beresp.http.backend = bereq.backend;
+		if (beresp.ttl > 3 s) {
+			set beresp.http.ttl = "long";
+		} else {
+			set beresp.http.ttl = "short";
+		}
 	}
-}
 
-sub vcl_backend_fetch {
-	if (bereq.between_bytes_timeout < 10s) {
-		set bereq.http.quick = "please";
+	sub vcl_hit {
+		if (obj.grace < 3m) {
+			set obj.grace = 1m;
+		} else {
+			set obj.grace = 2m;
+		}
+		if (obj.ttl < 3m) {
+			set obj.ttl = 2m;
+		} else {
+			set obj.ttl = 3m;
+		}
 	}
-	if (bereq.connect_timeout < 10s) {
-		set bereq.http.hello = "please";
+
+	sub vcl_backend_fetch {
+		if (bereq.between_bytes_timeout < 10s) {
+			set bereq.http.quick = "please";
+		}
+		if (bereq.connect_timeout < 10s) {
+			set bereq.http.hello = "please";
+		}
+		set bereq.connect_timeout = 10s;
 	}
-	set bereq.connect_timeout = 10s;
-}
 
 } -start
 
diff --git a/lib/libvcl/generate.py b/lib/libvcl/generate.py
index 36be27b..012098a 100755
--- a/lib/libvcl/generate.py
+++ b/lib/libvcl/generate.py
@@ -375,7 +375,7 @@ sp_variables = [
 	('obj.hits',
 		'INT',
 		( 'hit', 'deliver',),
-		( ),
+		( 'hit', 'deliver',),
 	),
 	('obj.http.',
 		'HEADER',
@@ -397,10 +397,10 @@ sp_variables = [
 		( 'hit', 'error',),
 		( 'hit', 'error',),
 	),
-	('obj.lastuse',
-		'DURATION',
-		( 'hit', 'deliver', 'error',),
-		( ),
+	('obj.last_use',
+		'TIME',
+		( 'hit', 'deliver',),
+		( 'hit', 'deliver',),
 	),
 	('obj.uncacheable',
 		'BOOL',



More information about the varnish-commit mailing list