Commit 0125de0e authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

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
parent 35561bcb
......@@ -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;
......
......@@ -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);
}
......
......@@ -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);
......
......@@ -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)
{
......
......@@ -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);
}
......
......@@ -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 }
};
......@@ -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++;
......
......@@ -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 {
......
......@@ -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 {
......
......@@ -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 {
......
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
......@@ -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
......
......@@ -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',
......
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