]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
layer: implemented statistics layer
authorMarek Vavruša <marek.vavrusa@nic.cz>
Fri, 22 Aug 2014 14:04:56 +0000 (16:04 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Fri, 22 Aug 2014 14:04:56 +0000 (16:04 +0200)
knot-resolver.files
lib/Makefile.am
lib/layer/stats.c [new file with mode: 0644]
lib/layer/stats.h [new file with mode: 0644]
lib/resolve.c

index 2b86a004f6fcdd0602738f5653f0a50cc084178d..d7b8b74a17b60c52b3e17a70080c5b20ffcc6bc1 100644 (file)
@@ -19,3 +19,5 @@ lib/delegpt.h
 lib/delegpt.c
 lib/rplan.h
 lib/rplan.c
+lib/layer/stats.h
+lib/layer/stats.c
index 5dae56a2703f9117985c6e055697dff230f2acfc..d1a3edee32203f009da0529a9604363a013afb8c 100644 (file)
@@ -7,6 +7,8 @@ libknotresolve_la_SOURCES =             \
        layer/iterate.c \
        layer/static.h  \
        layer/static.c  \
+       layer/stats.h   \
+       layer/stats.c   \
        layer.h                 \
        context.h               \
        context.c               \
diff --git a/lib/layer/stats.c b/lib/layer/stats.c
new file mode 100644 (file)
index 0000000..f0f2fcd
--- /dev/null
@@ -0,0 +1,95 @@
+/* Copyright 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+#include <common/print.h>
+
+#include "lib/layer/stats.h"
+#include "lib/rplan.h"
+
+#define DEBUG_MSG(fmt, ...) fprintf(stderr, "[stats] " fmt, ## __VA_ARGS__)
+
+static int begin(knot_layer_t *ctx, void *param)
+{
+       ctx->data = param;
+       return ctx->state;
+}
+
+static int finish(knot_layer_t *ctx)
+{
+       struct kr_layer_param *param = ctx->data;
+       struct kr_result *result = param->result;
+
+#ifndef NDEBUG
+       char *qnamestr = knot_dname_to_str(knot_pkt_qname(result->ans));
+       DEBUG_MSG("resolution of %s\n", qnamestr);
+       free(qnamestr);
+
+       DEBUG_MSG("rcode: %d (%u RRs)\n", knot_wire_get_rcode(result->ans->wire), result->ans->rrset_count);
+       DEBUG_MSG("queries: %u\n", result->nr_queries);
+       DEBUG_MSG("total time: %.02f msecs\n", time_diff(&result->t_start, &result->t_end));
+#endif
+
+       return ctx->state;
+}
+
+static int query(knot_layer_t *ctx, knot_pkt_t *pkt)
+{
+       struct kr_layer_param *param = ctx->data;
+       struct kr_result *result = param->result;
+
+       result->nr_queries += 1;
+
+       return ctx->state;
+}
+
+static int answer(knot_layer_t *ctx, knot_pkt_t *pkt)
+{
+       assert(pkt && ctx);
+       struct kr_layer_param *param = ctx->data;
+       struct kr_context* resolve = param->ctx;
+       struct kr_result *result = param->result;
+
+       /* Store stats. */
+       gettimeofday(&result->t_end, NULL);
+
+#ifndef NDEBUG
+       char *ns_name = knot_dname_to_str(resolve->current_ns->name);
+       char pad[16];
+       memset(pad, '-', sizeof(pad));
+       pad[MIN(sizeof(pad) - 1, list_size(&resolve->rplan.q) * 2)] = '\0';
+       DEBUG_MSG("#%s %s ... RC=%d, AA=%d, cumulative time: %.02f msecs\n",
+                 pad, ns_name, knot_wire_get_rcode(pkt->wire),
+                 knot_wire_get_aa(pkt->wire) != 0,
+                 time_diff(&result->t_start, &result->t_end));
+       free(ns_name);
+#endif
+
+       return ctx->state;
+}
+
+/*! \brief Module implementation. */
+static const knot_layer_api_t LAYER_STATS_MODULE = {
+       &begin,
+       NULL,
+       &finish,
+       &answer,
+       &query,
+       NULL
+};
+
+const knot_layer_api_t *layer_stats_module(void)
+{
+       return &LAYER_STATS_MODULE;
+}
diff --git a/lib/layer/stats.h b/lib/layer/stats.h
new file mode 100644 (file)
index 0000000..f5e0a33
--- /dev/null
@@ -0,0 +1,22 @@
+/* Copyright 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+#pragma once
+
+#include "lib/layer.h"
+
+/* Processing module implementation. */
+const knot_layer_api_t *layer_stats_module(void);
+#define LAYER_STATS layer_stats_module()
index 043c5e895e0bc89055b15acc606ae33f9aa8ed2d..409b16837f16a8baddf477c6e65becab4b73e44e 100644 (file)
@@ -104,6 +104,7 @@ int kr_resolve(struct kr_context* ctx, struct kr_result* result,
        knot_requestor_init(&requestor, ctx->pool);
        knot_requestor_overlay(&requestor, LAYER_STATIC, &param);
        knot_requestor_overlay(&requestor, LAYER_ITERATE, &param);
+       knot_requestor_overlay(&requestor, LAYER_STATS, &param);
        while(ctx->state & (NS_PROC_MORE|NS_PROC_FULL)) {
                iterate(&requestor, ctx);
        }