Maintained by: NLnet Labs

[Unbound-users] [PATCH] Add msg, rrset, infra and key cache sizes to stats command

Maciej Soltysiak
Fri Aug 1 15:00:29 CEST 2014


Hi list!

This patch adds 4 lines to the output of the unbound-control
[stats|stats_noreset]:
msg.cache.count=78
rrset.cache.count=346
infra.cache.count=14
key.cache.count=3

I added this functionality because I was often doing dump_cache and
then working on the output to find out some statistics on the cache
contents. Unfortunately this means locking the resolver because
dump_cache has to lock the slabhash.

This patch does not use any locks because it uses the counts
maintained within the structures.

Also it utilizes the fact that all these are struct slabhash type,
therefore I created only one generic function for that: size_t
count_slabhash_entries(struct slabhash *sh)

Please review this patch (on top of 1.4.22 tarball) for 1.4.23

Patch attached as well as inlined.

Best regards,
Maciej Soltysiak

diff -Nru /home/solt/orig/unbound-1.4.22.deb/daemon/remote.c
./unbound-1.4.22/daemon/remote.c
--- /home/solt/orig/unbound-1.4.22.deb/daemon/remote.c    2014-02-07
14:28:39.000000000 +0100
+++ ./unbound-1.4.22/daemon/remote.c    2014-08-01 14:06:49.530833070 +0200
@@ -874,6 +874,15 @@
         (unsigned)s->svr.unwanted_queries)) return 0;
     if(!ssl_printf(ssl, "unwanted.replies"SQ"%u\n",
         (unsigned)s->svr.unwanted_replies)) return 0;
+    /* cache counts */
+    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;
+    if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
+        (unsigned)s->svr.infra_cache_count)) return 0;
+    if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
+        (unsigned)s->svr.key_cache_count)) return 0;
     return 1;
 }

diff -Nru /home/solt/orig/unbound-1.4.22.deb/daemon/stats.c
./unbound-1.4.22/daemon/stats.c
--- /home/solt/orig/unbound-1.4.22.deb/daemon/stats.c    2014-02-07
14:28:39.000000000 +0100
+++ ./unbound-1.4.22/daemon/stats.c    2014-08-01 14:34:05.066975149 +0200
@@ -56,6 +56,9 @@
 #include "util/net_help.h"
 #include "validator/validator.h"
 #include "ldns/sbuffer.h"
+#include "services/cache/rrset.h"
+#include "services/cache/infra.h"
+#include "validator/val_kcache.h"

 /** add timers and the values do not overflow or become negative */
 static void
@@ -162,6 +165,12 @@
     /* get and reset validator rrset bogus number */
     s->svr.rrset_bogus = get_rrset_bogus(worker);

+    /* get cache sizes */
+    s->svr.msg_cache_count = count_slabhash_entries(worker->env.msg_cache);
+    s->svr.rrset_cache_count =
count_slabhash_entries(&worker->env.rrset_cache->table);
+    s->svr.infra_cache_count =
count_slabhash_entries(worker->env.infra_cache->hosts);
+    s->svr.key_cache_count =
count_slabhash_entries(worker->env.key_cache->slab);
+
     if(reset && !worker->env.cfg->stat_cumulative) {
         worker_stats_clear(worker);
     }
diff -Nru /home/solt/orig/unbound-1.4.22.deb/daemon/stats.h
./unbound-1.4.22/daemon/stats.h
--- /home/solt/orig/unbound-1.4.22.deb/daemon/stats.h    2014-02-07
14:28:39.000000000 +0100
+++ ./unbound-1.4.22/daemon/stats.h    2014-08-01 14:34:21.934978790 +0200
@@ -133,6 +133,15 @@
      * if all histograms are same size (is so by default) then
      * adding up works well. */
     size_t hist[NUM_BUCKETS_HIST];
+
+    /** number of message cache entries */
+    size_t msg_cache_count;
+    /** number of rrset cache entries */
+    size_t rrset_cache_count;
+    /** number of infra cache entries */
+    size_t infra_cache_count;
+    /** number of key cache entries */
+    size_t key_cache_count;
 };

 /**
diff -Nru /home/solt/orig/unbound-1.4.22.deb/util/storage/slabhash.c
./unbound-1.4.22/util/storage/slabhash.c
--- ./unbound-1.4.22/util/storage/slabhash.c    2014-02-07
14:28:39.000000000 +0100
+++ ../test/unbound-1.4.22/util/storage/slabhash.c    2014-08-01
14:43:52.611119849 +0200
@@ -217,3 +217,13 @@
     for(i=0; i<sh->size; i++)
         lruhash_traverse(sh->array[i], wr, func, arg);
 }
+
+size_t count_slabhash_entries(struct slabhash* sh)
+{
+    size_t slab, cnt;
+
+    for(slab=0, cnt=0; slab<sh->size; slab++) {
+               cnt += sh->array[slab]->num;
+    }
+    return cnt;
+}
diff -Nru /home/solt/orig/unbound-1.4.22.deb/util/storage/slabhash.h
./unbound-1.4.22/util/storage/slabhash.h
--- /home/solt/orig/unbound-1.4.22.deb/util/storage/slabhash.h
2014-02-07 14:28:39.000000000 +0100
+++ ./unbound-1.4.22/util/storage/slabhash.h    2014-08-01
14:06:49.534833070 +0200
@@ -184,6 +184,12 @@
 void slabhash_traverse(struct slabhash* table, int wr,
         void (*func)(struct lruhash_entry*, void*), void* arg);

+/*
+ * Count entries in slabhash.
+ * @param table: slabbed hash table;
+ */
+size_t count_slabhash_entries(struct slabhash* table);
+
 /* --- test representation --- */
 /** test structure contains test key */
 struct slabhash_testkey {
-------------- next part --------------
diff -Nru /home/solt/orig/unbound-1.4.22.deb/daemon/remote.c ./unbound-1.4.22/daemon/remote.c
--- /home/solt/orig/unbound-1.4.22.deb/daemon/remote.c	2014-02-07 14:28:39.000000000 +0100
+++ ./unbound-1.4.22/daemon/remote.c	2014-08-01 14:06:49.530833070 +0200
@@ -874,6 +874,15 @@
 		(unsigned)s->svr.unwanted_queries)) return 0;
 	if(!ssl_printf(ssl, "unwanted.replies"SQ"%u\n", 
 		(unsigned)s->svr.unwanted_replies)) return 0;
+	/* cache counts */
+	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;
+	if(!ssl_printf(ssl, "infra.cache.count"SQ"%u\n",
+		(unsigned)s->svr.infra_cache_count)) return 0;
+	if(!ssl_printf(ssl, "key.cache.count"SQ"%u\n",
+		(unsigned)s->svr.key_cache_count)) return 0;
 	return 1;
 }
 
diff -Nru /home/solt/orig/unbound-1.4.22.deb/daemon/stats.c ./unbound-1.4.22/daemon/stats.c
--- /home/solt/orig/unbound-1.4.22.deb/daemon/stats.c	2014-02-07 14:28:39.000000000 +0100
+++ ./unbound-1.4.22/daemon/stats.c	2014-08-01 14:34:05.066975149 +0200
@@ -56,6 +56,9 @@
 #include "util/net_help.h"
 #include "validator/validator.h"
 #include "ldns/sbuffer.h"
+#include "services/cache/rrset.h"
+#include "services/cache/infra.h"
+#include "validator/val_kcache.h"
 
 /** add timers and the values do not overflow or become negative */
 static void
@@ -162,6 +165,12 @@
 	/* get and reset validator rrset bogus number */
 	s->svr.rrset_bogus = get_rrset_bogus(worker);
 
+	/* get cache sizes */
+	s->svr.msg_cache_count = count_slabhash_entries(worker->env.msg_cache);
+	s->svr.rrset_cache_count = count_slabhash_entries(&worker->env.rrset_cache->table);
+	s->svr.infra_cache_count = count_slabhash_entries(worker->env.infra_cache->hosts);
+	s->svr.key_cache_count = count_slabhash_entries(worker->env.key_cache->slab);
+
 	if(reset && !worker->env.cfg->stat_cumulative) {
 		worker_stats_clear(worker);
 	}
diff -Nru /home/solt/orig/unbound-1.4.22.deb/daemon/stats.h ./unbound-1.4.22/daemon/stats.h
--- /home/solt/orig/unbound-1.4.22.deb/daemon/stats.h	2014-02-07 14:28:39.000000000 +0100
+++ ./unbound-1.4.22/daemon/stats.h	2014-08-01 14:34:21.934978790 +0200
@@ -133,6 +133,15 @@
 	 * if all histograms are same size (is so by default) then
 	 * adding up works well. */
 	size_t hist[NUM_BUCKETS_HIST];
+	
+	/** number of message cache entries */
+	size_t msg_cache_count;
+	/** number of rrset cache entries */
+	size_t rrset_cache_count;
+	/** number of infra cache entries */
+	size_t infra_cache_count;
+	/** number of key cache entries */
+	size_t key_cache_count;
 };
 
 /** 
diff -Nru /home/solt/orig/unbound-1.4.22.deb/util/storage/slabhash.c ./unbound-1.4.22/util/storage/slabhash.c
--- ./unbound-1.4.22/util/storage/slabhash.c	2014-02-07 14:28:39.000000000 +0100
+++ ../test/unbound-1.4.22/util/storage/slabhash.c	2014-08-01 14:43:52.611119849 +0200
@@ -217,3 +217,13 @@
 	for(i=0; i<sh->size; i++)
 		lruhash_traverse(sh->array[i], wr, func, arg);
 }
+
+size_t count_slabhash_entries(struct slabhash* sh)
+{
+	size_t slab, cnt;
+
+	for(slab=0, cnt=0; slab<sh->size; slab++) {
+       		cnt += sh->array[slab]->num;
+	}
+	return cnt;
+}
diff -Nru /home/solt/orig/unbound-1.4.22.deb/util/storage/slabhash.h ./unbound-1.4.22/util/storage/slabhash.h
--- /home/solt/orig/unbound-1.4.22.deb/util/storage/slabhash.h	2014-02-07 14:28:39.000000000 +0100
+++ ./unbound-1.4.22/util/storage/slabhash.h	2014-08-01 14:06:49.534833070 +0200
@@ -184,6 +184,12 @@
 void slabhash_traverse(struct slabhash* table, int wr,
         void (*func)(struct lruhash_entry*, void*), void* arg);
 
+/*
+ * Count entries in slabhash.
+ * @param table: slabbed hash table;
+ */
+size_t count_slabhash_entries(struct slabhash* table);
+
 /* --- test representation --- */
 /** test structure contains test key */
 struct slabhash_testkey {