Maintained by: NLnet Labs

[Unbound-users] [review patch] msg and rrset cache count

Maciej Soltysiak
Wed Jul 30 21:05:05 CEST 2014


Hi,

I would like to ask for an initial review of a patch to add 2
additional statistic entries to the stats command.

This is not a final patch, just a quick&dirty proof of concept.

Rationale for this is that I want to measure the cache size in a
defined interval for charting. I've been doing dump_cache and working
on that, but that blocks the resolver for quite a while. With my
current cache sizes it's up to 13 seconds. I want something quicker.

I thought I do a patch that does the counting in a way as dump_cache
would, but do not produce output, just count.

Is that the proper way to do it?

Best regards,
Maciej Soltysiak
-------------- next part --------------
diff -Nru unbound-1.4.22/daemon/remote.c ../unbound-1.4.22/daemon/remote.c
--- unbound-1.4.22/daemon/remote.c	2014-02-07 14:28:39.000000000 +0100
+++ ../unbound-1.4.22/daemon/remote.c	2014-07-28 19:23:22.810171402 +0200
@@ -874,6 +874,10 @@
 		(unsigned)s->svr.unwanted_queries)) return 0;
 	if(!ssl_printf(ssl, "unwanted.replies"SQ"%u\n", 
 		(unsigned)s->svr.unwanted_replies)) return 0;
+	if(!ssl_printf(ssl, "msg.cache.count"SQ"%u\n",
+		(unsigned)s->svr.msg_cache_count)) return 0;
+	if(!ssl_printf(ssl, "rrset.cache.count"SQ"%u\n",
+		(unsigned)s->svr.rrset_cache_count)) return 0;
 	return 1;
 }
 
diff -Nru unbound-1.4.22/daemon/stats.c ../unbound-1.4.22/daemon/stats.c
--- unbound-1.4.22/daemon/stats.c	2014-02-07 14:28:39.000000000 +0100
+++ ../unbound-1.4.22/daemon/stats.c	2014-07-28 19:47:45.249959917 +0200
@@ -56,6 +56,7 @@
 #include "util/net_help.h"
 #include "validator/validator.h"
 #include "ldns/sbuffer.h"
+#include "services/cache/rrset.h"
 
 /** add timers and the values do not overflow or become negative */
 static void
@@ -133,9 +134,79 @@
 	return r;
 }
 
+static size_t
+count_rrset_lruhash(struct lruhash* h, time_t now)
+{
+	size_t cnt;
+	struct lruhash_entry* e;
+	/* walk in order of lru; best first */
+	for(cnt=0,e=h->lru_start; e; e = e->lru_next) {
+		lock_rw_rdlock(&e->lock);
+		cnt++;
+		lock_rw_unlock(&e->lock);
+	}
+	return cnt;
+}
+
+/** dump rrset cache */
+static size_t
+count_rrset_cache(struct worker* worker)
+{
+	struct rrset_cache* r = worker->env.rrset_cache;
+	size_t slab, sum, cnt;
+
+	for(slab=0, sum=0, cnt=0; slab<r->table.size; slab++) {
+		lock_quick_lock(&r->table.array[slab]->lock);
+		cnt = count_rrset_lruhash(r->table.array[slab],
+			*worker->env.now);
+		lock_quick_unlock(&r->table.array[slab]->lock);
+		sum += cnt;
+	}
+	return sum;
+}
+
+/** dump lruhash msg cache */
+static size_t
+count_msg_lruhash(struct worker* worker, struct lruhash* h)
+{
+	struct lruhash_entry* e;
+	struct query_info* k;
+	struct reply_info* d;
+	size_t cnt = 0;
+	
+	/* walk in order of lru; best first */
+	for(e=h->lru_start; e; e = e->lru_next) {
+		lock_rw_rdlock(&e->lock);
+		cnt++;
+		lock_rw_unlock(&e->lock);
+	}
+	return cnt;
+}
+
+
+/** dump msg cache */
+static size_t
+count_msg_cache(struct worker* worker)
+{
+	struct slabhash* sh = worker->env.msg_cache;
+	size_t slab;
+	size_t sum, cnt;
+	
+	for(slab=0, cnt=0, sum=0; slab<sh->size; slab++) {
+		lock_quick_lock(&sh->array[slab]->lock);
+		cnt = count_msg_lruhash(worker, sh->array[slab]);
+		lock_quick_unlock(&sh->array[slab]->lock);
+		sum += cnt;
+	}
+	return sum;
+}
+
+
 void
 server_stats_compile(struct worker* worker, struct stats_info* s, int reset)
 {
+	struct slabhash* sh = worker->env.msg_cache;
+
 	int i;
 
 	s->svr = worker->stats;
@@ -162,6 +233,10 @@
 	/* get and reset validator rrset bogus number */
 	s->svr.rrset_bogus = get_rrset_bogus(worker);
 
+	/* get cache size */
+	s->svr.msg_cache_count = count_msg_cache(worker);
+	s->svr.rrset_cache_count = count_rrset_cache(worker);
+
 	if(reset && !worker->env.cfg->stat_cumulative) {
 		worker_stats_clear(worker);
 	}
diff -Nru unbound-1.4.22/daemon/stats.h ../unbound-1.4.22/daemon/stats.h
--- unbound-1.4.22/daemon/stats.h	2014-02-07 14:28:39.000000000 +0100
+++ ../unbound-1.4.22/daemon/stats.h	2014-07-28 19:22:52.086172793 +0200
@@ -61,6 +61,8 @@
 
 /** per worker statistics */
 struct server_stats {
+	size_t msg_cache_count;
+	size_t rrset_cache_count;
 	/** number of queries from clients received. */
 	size_t num_queries;
 	/** number of queries that had a cache-miss. */