From f8409089319620930868331f7b7744ab86276746 Mon Sep 17 00:00:00 2001 From: phk Date: Sun, 7 Sep 2008 17:31:13 +0000 Subject: [PATCH] Add obj.hits VRT variable which counts how many *previous* hits this object has seen. Idea for prefetching being used as workaround for #310 git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3166 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 2 + varnish-cache/bin/varnishd/cache_hash.c | 3 ++ varnish-cache/bin/varnishd/cache_vrt.c | 9 ++++ .../bin/varnishtest/tests/c00005.vtc | 2 +- .../bin/varnishtest/tests/v00013.vtc | 42 +++++++++++++++++++ varnish-cache/include/vrt_obj.h | 1 + varnish-cache/lib/libvcl/vcc_gen_obj.tcl | 5 +++ varnish-cache/lib/libvcl/vcc_obj.c | 7 ++++ 8 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 varnish-cache/bin/varnishtest/tests/v00013.vtc diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index fa5ef529..de1e19b2 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -285,6 +285,8 @@ struct object { /* Prefetch */ struct object *parent; struct object *child; + + int hits; }; struct objhead { diff --git a/varnish-cache/bin/varnishd/cache_hash.c b/varnish-cache/bin/varnishd/cache_hash.c index f84cb4cd..9f102024 100644 --- a/varnish-cache/bin/varnishd/cache_hash.c +++ b/varnish-cache/bin/varnishd/cache_hash.c @@ -58,6 +58,7 @@ #include #include #include +#include #include #include @@ -253,6 +254,8 @@ HSH_Lookup(struct sess *sp) if (o != NULL) { /* We found an object we like */ o->refcnt++; + if (o->hits < INT_MAX) + o->hits++; UNLOCK(&oh->mtx); if (params->log_hash) WSP(sp, SLT_Hash, "%s", oh->hash); diff --git a/varnish-cache/bin/varnishd/cache_vrt.c b/varnish-cache/bin/varnishd/cache_vrt.c index e3738863..7c05549f 100644 --- a/varnish-cache/bin/varnishd/cache_vrt.c +++ b/varnish-cache/bin/varnishd/cache_vrt.c @@ -552,6 +552,15 @@ VRT_r_now(const struct sess *sp) return (TIM_real()); } +int +VRT_r_obj_hits(const struct sess *sp) +{ + + CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); + CHECK_OBJ_NOTNULL(sp->obj, OBJECT_MAGIC); /* XXX */ + return (sp->obj->hits); +} + double VRT_r_obj_lastuse(const struct sess *sp) { diff --git a/varnish-cache/bin/varnishtest/tests/c00005.vtc b/varnish-cache/bin/varnishtest/tests/c00005.vtc index 98ed3737..f55d1904 100644 --- a/varnish-cache/bin/varnishtest/tests/c00005.vtc +++ b/varnish-cache/bin/varnishtest/tests/c00005.vtc @@ -11,7 +11,7 @@ server s1 { txresp -body "2222\n" } -start -varnish v1 -vcl+backend { +varnish v1 -arg "-p vcl_trace=on" -vcl+backend { acl acl1 { "localhost"; } diff --git a/varnish-cache/bin/varnishtest/tests/v00013.vtc b/varnish-cache/bin/varnishtest/tests/v00013.vtc new file mode 100644 index 00000000..322df741 --- /dev/null +++ b/varnish-cache/bin/varnishtest/tests/v00013.vtc @@ -0,0 +1,42 @@ +# $Id$ + +test "Check obj.hits" + +server s1 { + rxreq + expect req.url == "/" + txresp -body "slash" + rxreq + expect req.url == "/foo" + txresp -body "foo" +} -start + +varnish v1 -vcl+backend { + + sub vcl_deliver { + set resp.http.foo = obj.hits; + } +} -start + +client c1 { + txreq + rxresp + expect resp.status == 200 + expect resp.http.foo == 0 + + txreq + rxresp + expect resp.status == 200 + expect resp.http.foo == 1 + + txreq -url /foo + rxresp + expect resp.status == 200 + expect resp.http.foo == 0 + + txreq + rxresp + expect resp.status == 200 + expect resp.http.foo == 2 +} -run + diff --git a/varnish-cache/include/vrt_obj.h b/varnish-cache/include/vrt_obj.h index 35f1f804..6d7e691a 100644 --- a/varnish-cache/include/vrt_obj.h +++ b/varnish-cache/include/vrt_obj.h @@ -34,6 +34,7 @@ int VRT_r_obj_status(const struct sess *); void VRT_l_obj_status(const struct sess *, int); const char * VRT_r_obj_response(const struct sess *); void VRT_l_obj_response(const struct sess *, const char *, ...); +int VRT_r_obj_hits(const struct sess *); unsigned VRT_r_obj_cacheable(const struct sess *); void VRT_l_obj_cacheable(const struct sess *, unsigned); double VRT_r_obj_ttl(const struct sess *); diff --git a/varnish-cache/lib/libvcl/vcc_gen_obj.tcl b/varnish-cache/lib/libvcl/vcc_gen_obj.tcl index e26d3fa2..2c9a2a0d 100755 --- a/varnish-cache/lib/libvcl/vcc_gen_obj.tcl +++ b/varnish-cache/lib/libvcl/vcc_gen_obj.tcl @@ -144,6 +144,11 @@ set spobj { { fetch error} "const struct sess *" } + { obj.hits + RO INT + { hit fetch deliver } + "const struct sess *" + } { obj.http. RW HDR_OBJ { hit fetch error} diff --git a/varnish-cache/lib/libvcl/vcc_obj.c b/varnish-cache/lib/libvcl/vcc_obj.c index 2aa6a80b..eeff69f6 100644 --- a/varnish-cache/lib/libvcl/vcc_obj.c +++ b/varnish-cache/lib/libvcl/vcc_obj.c @@ -144,6 +144,13 @@ struct var vcc_vars[] = { 0, VCL_MET_FETCH | VCL_MET_ERROR }, + { "obj.hits", INT, 8, + "VRT_r_obj_hits(sp)", + NULL, + V_RO, + 0, + VCL_MET_HIT | VCL_MET_FETCH | VCL_MET_DELIVER + }, { "obj.http.", HEADER, 9, "VRT_r_obj_http_(sp)", "VRT_l_obj_http_(sp, ", -- 2.39.5