From ce243d289f022921d21168e3682abe5d6f901340 Mon Sep 17 00:00:00 2001 From: Arjan van de Ven Date: Mon, 9 Jan 2012 16:08:45 -0800 Subject: [PATCH] dnsproxy: Track cache hit statistics For various decisions (which entries to prune, which entries to refresh) and for debugging, it's useful to track how many hits a cache entry gets. This patch adds the basic counting for this; the only tricky aspect is that we serve DNS entries out of the cache, even in case of a miss, so we need to compensate for that by subtracting a hit first. --- src/dnsproxy.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/dnsproxy.c b/src/dnsproxy.c index a30f4c4..d2fa07c 100644 --- a/src/dnsproxy.c +++ b/src/dnsproxy.c @@ -138,6 +138,7 @@ struct cache_data { struct cache_entry { char *key; + int hits; struct cache_data *ipv4; struct cache_data *ipv6; }; @@ -1085,6 +1086,13 @@ static int cache_update(struct server_data *srv, unsigned char *msg, data->cache_until = entry->ipv4->cache_until; memcpy(data->data, msg, msg_len); entry->ipv6 = data; + /* + * we will get a "hit" when we serve the response + * out of the cache + */ + entry->hits--; + if (entry->hits < 0) + entry->hits = 0; return 0; } } @@ -1116,6 +1124,7 @@ static int cache_update(struct server_data *srv, unsigned char *msg, entry->key = g_strdup(question); entry->ipv4 = entry->ipv6 = NULL; + entry->hits = 0; if (type == 1) entry->ipv4 = data; @@ -1137,6 +1146,14 @@ static int cache_update(struct server_data *srv, unsigned char *msg, else entry->ipv6 = data; + /* + * compensate for the hit we'll get for serving + * the response out of the cache + */ + entry->hits--; + if (entry->hits < 0) + entry->hits = 0; + new_entry = FALSE; } @@ -1208,8 +1225,10 @@ static int ns_resolv(struct server_data *server, struct request_data *req, else data = entry->ipv6; - if (data) + if (data) { ttl_left = data->valid_until - time(0); + entry->hits++; + } if (data != NULL && req->protocol == IPPROTO_TCP) { send_cached_response(req->client_sk, data->data, -- 2.7.4