1238106Sdes/*
2238106Sdes * daemon/stats.c - collect runtime performance indicators.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file describes the data structure used to collect runtime performance
40238106Sdes * numbers. These 'statistics' may be of interest to the operator.
41238106Sdes */
42238106Sdes#include "config.h"
43269257Sdes#ifdef HAVE_TIME_H
44269257Sdes#include <time.h>
45269257Sdes#endif
46269257Sdes#include <sys/time.h>
47269257Sdes#include <sys/types.h>
48238106Sdes#include "daemon/stats.h"
49238106Sdes#include "daemon/worker.h"
50238106Sdes#include "daemon/daemon.h"
51238106Sdes#include "services/mesh.h"
52238106Sdes#include "services/outside_network.h"
53291767Sdes#include "services/listen_dnsport.h"
54238106Sdes#include "util/config_file.h"
55238106Sdes#include "util/tube.h"
56238106Sdes#include "util/timehist.h"
57238106Sdes#include "util/net_help.h"
58238106Sdes#include "validator/validator.h"
59291767Sdes#include "sldns/sbuffer.h"
60285206Sdes#include "services/cache/rrset.h"
61285206Sdes#include "services/cache/infra.h"
62285206Sdes#include "validator/val_kcache.h"
63238106Sdes
64238106Sdes/** add timers and the values do not overflow or become negative */
65238106Sdesstatic void
66238106Sdestimeval_add(struct timeval* d, const struct timeval* add)
67238106Sdes{
68238106Sdes#ifndef S_SPLINT_S
69238106Sdes	d->tv_sec += add->tv_sec;
70238106Sdes	d->tv_usec += add->tv_usec;
71238106Sdes	if(d->tv_usec > 1000000) {
72238106Sdes		d->tv_usec -= 1000000;
73238106Sdes		d->tv_sec++;
74238106Sdes	}
75238106Sdes#endif
76238106Sdes}
77238106Sdes
78238106Sdesvoid server_stats_init(struct server_stats* stats, struct config_file* cfg)
79238106Sdes{
80238106Sdes	memset(stats, 0, sizeof(*stats));
81238106Sdes	stats->extended = cfg->stat_extended;
82238106Sdes}
83238106Sdes
84238106Sdesvoid server_stats_querymiss(struct server_stats* stats, struct worker* worker)
85238106Sdes{
86238106Sdes	stats->num_queries_missed_cache++;
87238106Sdes	stats->sum_query_list_size += worker->env.mesh->all.count;
88238106Sdes	if(worker->env.mesh->all.count > stats->max_query_list_size)
89238106Sdes		stats->max_query_list_size = worker->env.mesh->all.count;
90238106Sdes}
91238106Sdes
92238106Sdesvoid server_stats_prefetch(struct server_stats* stats, struct worker* worker)
93238106Sdes{
94238106Sdes	stats->num_queries_prefetch++;
95238106Sdes	/* changes the query list size so account that, like a querymiss */
96238106Sdes	stats->sum_query_list_size += worker->env.mesh->all.count;
97238106Sdes	if(worker->env.mesh->all.count > stats->max_query_list_size)
98238106Sdes		stats->max_query_list_size = worker->env.mesh->all.count;
99238106Sdes}
100238106Sdes
101238106Sdesvoid server_stats_log(struct server_stats* stats, struct worker* worker,
102238106Sdes	int threadnum)
103238106Sdes{
104238106Sdes	log_info("server stats for thread %d: %u queries, "
105238106Sdes		"%u answers from cache, %u recursions, %u prefetch",
106238106Sdes		threadnum, (unsigned)stats->num_queries,
107238106Sdes		(unsigned)(stats->num_queries -
108238106Sdes			stats->num_queries_missed_cache),
109238106Sdes		(unsigned)stats->num_queries_missed_cache,
110238106Sdes		(unsigned)stats->num_queries_prefetch);
111238106Sdes	log_info("server stats for thread %d: requestlist max %u avg %g "
112238106Sdes		"exceeded %u jostled %u", threadnum,
113238106Sdes		(unsigned)stats->max_query_list_size,
114238106Sdes		(stats->num_queries_missed_cache+stats->num_queries_prefetch)?
115238106Sdes			(double)stats->sum_query_list_size/
116238106Sdes			(stats->num_queries_missed_cache+
117238106Sdes			stats->num_queries_prefetch) : 0.0,
118238106Sdes		(unsigned)worker->env.mesh->stats_dropped,
119238106Sdes		(unsigned)worker->env.mesh->stats_jostled);
120238106Sdes}
121238106Sdes
122238106Sdes/** get rrsets bogus number from validator */
123238106Sdesstatic size_t
124238106Sdesget_rrset_bogus(struct worker* worker)
125238106Sdes{
126238106Sdes	int m = modstack_find(&worker->env.mesh->mods, "validator");
127238106Sdes	struct val_env* ve;
128238106Sdes	size_t r;
129238106Sdes	if(m == -1)
130238106Sdes		return 0;
131238106Sdes	ve = (struct val_env*)worker->env.modinfo[m];
132238106Sdes	lock_basic_lock(&ve->bogus_lock);
133238106Sdes	r = ve->num_rrset_bogus;
134238106Sdes	if(!worker->env.cfg->stat_cumulative)
135238106Sdes		ve->num_rrset_bogus = 0;
136238106Sdes	lock_basic_unlock(&ve->bogus_lock);
137238106Sdes	return r;
138238106Sdes}
139238106Sdes
140238106Sdesvoid
141238106Sdesserver_stats_compile(struct worker* worker, struct stats_info* s, int reset)
142238106Sdes{
143238106Sdes	int i;
144291767Sdes	struct listen_list* lp;
145238106Sdes
146238106Sdes	s->svr = worker->stats;
147238106Sdes	s->mesh_num_states = worker->env.mesh->all.count;
148238106Sdes	s->mesh_num_reply_states = worker->env.mesh->num_reply_states;
149238106Sdes	s->mesh_jostled = worker->env.mesh->stats_jostled;
150238106Sdes	s->mesh_dropped = worker->env.mesh->stats_dropped;
151238106Sdes	s->mesh_replies_sent = worker->env.mesh->replies_sent;
152238106Sdes	s->mesh_replies_sum_wait = worker->env.mesh->replies_sum_wait;
153238106Sdes	s->mesh_time_median = timehist_quartile(worker->env.mesh->histogram,
154238106Sdes		0.50);
155238106Sdes
156238106Sdes	/* add in the values from the mesh */
157238106Sdes	s->svr.ans_secure += worker->env.mesh->ans_secure;
158238106Sdes	s->svr.ans_bogus += worker->env.mesh->ans_bogus;
159238106Sdes	s->svr.ans_rcode_nodata += worker->env.mesh->ans_nodata;
160238106Sdes	for(i=0; i<16; i++)
161238106Sdes		s->svr.ans_rcode[i] += worker->env.mesh->ans_rcode[i];
162238106Sdes	timehist_export(worker->env.mesh->histogram, s->svr.hist,
163238106Sdes		NUM_BUCKETS_HIST);
164238106Sdes	/* values from outside network */
165238106Sdes	s->svr.unwanted_replies = worker->back->unwanted_replies;
166285206Sdes	s->svr.qtcp_outgoing = worker->back->num_tcp_outgoing;
167238106Sdes
168238106Sdes	/* get and reset validator rrset bogus number */
169238106Sdes	s->svr.rrset_bogus = get_rrset_bogus(worker);
170238106Sdes
171285206Sdes	/* get cache sizes */
172285206Sdes	s->svr.msg_cache_count = count_slabhash_entries(worker->env.msg_cache);
173285206Sdes	s->svr.rrset_cache_count = count_slabhash_entries(&worker->env.rrset_cache->table);
174285206Sdes	s->svr.infra_cache_count = count_slabhash_entries(worker->env.infra_cache->hosts);
175285206Sdes	if(worker->env.key_cache)
176285206Sdes		s->svr.key_cache_count = count_slabhash_entries(worker->env.key_cache->slab);
177285206Sdes	else	s->svr.key_cache_count = 0;
178285206Sdes
179291767Sdes	/* get tcp accept usage */
180291767Sdes	s->svr.tcp_accept_usage = 0;
181291767Sdes	for(lp = worker->front->cps; lp; lp = lp->next) {
182291767Sdes		if(lp->com->type == comm_tcp_accept)
183291767Sdes			s->svr.tcp_accept_usage += lp->com->cur_tcp_count;
184291767Sdes	}
185291767Sdes
186238106Sdes	if(reset && !worker->env.cfg->stat_cumulative) {
187238106Sdes		worker_stats_clear(worker);
188238106Sdes	}
189238106Sdes}
190238106Sdes
191238106Sdesvoid server_stats_obtain(struct worker* worker, struct worker* who,
192238106Sdes	struct stats_info* s, int reset)
193238106Sdes{
194238106Sdes	uint8_t *reply = NULL;
195238106Sdes	uint32_t len = 0;
196238106Sdes	if(worker == who) {
197238106Sdes		/* just fill it in */
198238106Sdes		server_stats_compile(worker, s, reset);
199238106Sdes		return;
200238106Sdes	}
201238106Sdes	/* communicate over tube */
202238106Sdes	verbose(VERB_ALGO, "write stats cmd");
203238106Sdes	if(reset)
204238106Sdes		worker_send_cmd(who, worker_cmd_stats);
205238106Sdes	else 	worker_send_cmd(who, worker_cmd_stats_noreset);
206238106Sdes	verbose(VERB_ALGO, "wait for stats reply");
207238106Sdes	if(!tube_read_msg(worker->cmd, &reply, &len, 0))
208238106Sdes		fatal_exit("failed to read stats over cmd channel");
209238106Sdes	if(len != (uint32_t)sizeof(*s))
210238106Sdes		fatal_exit("stats on cmd channel wrong length %d %d",
211238106Sdes			(int)len, (int)sizeof(*s));
212238106Sdes	memcpy(s, reply, (size_t)len);
213238106Sdes	free(reply);
214238106Sdes}
215238106Sdes
216238106Sdesvoid server_stats_reply(struct worker* worker, int reset)
217238106Sdes{
218238106Sdes	struct stats_info s;
219238106Sdes	server_stats_compile(worker, &s, reset);
220238106Sdes	verbose(VERB_ALGO, "write stats replymsg");
221238106Sdes	if(!tube_write_msg(worker->daemon->workers[0]->cmd,
222238106Sdes		(uint8_t*)&s, sizeof(s), 0))
223238106Sdes		fatal_exit("could not write stat values over cmd channel");
224238106Sdes}
225238106Sdes
226238106Sdesvoid server_stats_add(struct stats_info* total, struct stats_info* a)
227238106Sdes{
228238106Sdes	total->svr.num_queries += a->svr.num_queries;
229238106Sdes	total->svr.num_queries_missed_cache += a->svr.num_queries_missed_cache;
230238106Sdes	total->svr.num_queries_prefetch += a->svr.num_queries_prefetch;
231238106Sdes	total->svr.sum_query_list_size += a->svr.sum_query_list_size;
232238106Sdes	/* the max size reached is upped to higher of both */
233238106Sdes	if(a->svr.max_query_list_size > total->svr.max_query_list_size)
234238106Sdes		total->svr.max_query_list_size = a->svr.max_query_list_size;
235238106Sdes
236238106Sdes	if(a->svr.extended) {
237238106Sdes		int i;
238238106Sdes		total->svr.qtype_big += a->svr.qtype_big;
239238106Sdes		total->svr.qclass_big += a->svr.qclass_big;
240238106Sdes		total->svr.qtcp += a->svr.qtcp;
241285206Sdes		total->svr.qtcp_outgoing += a->svr.qtcp_outgoing;
242238106Sdes		total->svr.qipv6 += a->svr.qipv6;
243238106Sdes		total->svr.qbit_QR += a->svr.qbit_QR;
244238106Sdes		total->svr.qbit_AA += a->svr.qbit_AA;
245238106Sdes		total->svr.qbit_TC += a->svr.qbit_TC;
246238106Sdes		total->svr.qbit_RD += a->svr.qbit_RD;
247238106Sdes		total->svr.qbit_RA += a->svr.qbit_RA;
248238106Sdes		total->svr.qbit_Z += a->svr.qbit_Z;
249238106Sdes		total->svr.qbit_AD += a->svr.qbit_AD;
250238106Sdes		total->svr.qbit_CD += a->svr.qbit_CD;
251238106Sdes		total->svr.qEDNS += a->svr.qEDNS;
252238106Sdes		total->svr.qEDNS_DO += a->svr.qEDNS_DO;
253238106Sdes		total->svr.ans_rcode_nodata += a->svr.ans_rcode_nodata;
254238106Sdes		total->svr.ans_secure += a->svr.ans_secure;
255238106Sdes		total->svr.ans_bogus += a->svr.ans_bogus;
256238106Sdes		total->svr.rrset_bogus += a->svr.rrset_bogus;
257238106Sdes		total->svr.unwanted_replies += a->svr.unwanted_replies;
258238106Sdes		total->svr.unwanted_queries += a->svr.unwanted_queries;
259291767Sdes		total->svr.tcp_accept_usage += a->svr.tcp_accept_usage;
260238106Sdes		for(i=0; i<STATS_QTYPE_NUM; i++)
261238106Sdes			total->svr.qtype[i] += a->svr.qtype[i];
262238106Sdes		for(i=0; i<STATS_QCLASS_NUM; i++)
263238106Sdes			total->svr.qclass[i] += a->svr.qclass[i];
264238106Sdes		for(i=0; i<STATS_OPCODE_NUM; i++)
265238106Sdes			total->svr.qopcode[i] += a->svr.qopcode[i];
266238106Sdes		for(i=0; i<STATS_RCODE_NUM; i++)
267238106Sdes			total->svr.ans_rcode[i] += a->svr.ans_rcode[i];
268238106Sdes		for(i=0; i<NUM_BUCKETS_HIST; i++)
269238106Sdes			total->svr.hist[i] += a->svr.hist[i];
270238106Sdes	}
271238106Sdes
272238106Sdes	total->mesh_num_states += a->mesh_num_states;
273238106Sdes	total->mesh_num_reply_states += a->mesh_num_reply_states;
274238106Sdes	total->mesh_jostled += a->mesh_jostled;
275238106Sdes	total->mesh_dropped += a->mesh_dropped;
276238106Sdes	total->mesh_replies_sent += a->mesh_replies_sent;
277238106Sdes	timeval_add(&total->mesh_replies_sum_wait, &a->mesh_replies_sum_wait);
278238106Sdes	/* the medians are averaged together, this is not as accurate as
279238106Sdes	 * taking the median over all of the data, but is good and fast
280238106Sdes	 * added up here, division later*/
281238106Sdes	total->mesh_time_median += a->mesh_time_median;
282238106Sdes}
283238106Sdes
284238106Sdesvoid server_stats_insquery(struct server_stats* stats, struct comm_point* c,
285238106Sdes	uint16_t qtype, uint16_t qclass, struct edns_data* edns,
286238106Sdes	struct comm_reply* repinfo)
287238106Sdes{
288269257Sdes	uint16_t flags = sldns_buffer_read_u16_at(c->buffer, 2);
289238106Sdes	if(qtype < STATS_QTYPE_NUM)
290238106Sdes		stats->qtype[qtype]++;
291238106Sdes	else	stats->qtype_big++;
292238106Sdes	if(qclass < STATS_QCLASS_NUM)
293238106Sdes		stats->qclass[qclass]++;
294238106Sdes	else	stats->qclass_big++;
295269257Sdes	stats->qopcode[ LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) ]++;
296238106Sdes	if(c->type != comm_udp)
297238106Sdes		stats->qtcp++;
298238106Sdes	if(repinfo && addr_is_ip6(&repinfo->addr, repinfo->addrlen))
299238106Sdes		stats->qipv6++;
300238106Sdes	if( (flags&BIT_QR) )
301238106Sdes		stats->qbit_QR++;
302238106Sdes	if( (flags&BIT_AA) )
303238106Sdes		stats->qbit_AA++;
304238106Sdes	if( (flags&BIT_TC) )
305238106Sdes		stats->qbit_TC++;
306238106Sdes	if( (flags&BIT_RD) )
307238106Sdes		stats->qbit_RD++;
308238106Sdes	if( (flags&BIT_RA) )
309238106Sdes		stats->qbit_RA++;
310238106Sdes	if( (flags&BIT_Z) )
311238106Sdes		stats->qbit_Z++;
312238106Sdes	if( (flags&BIT_AD) )
313238106Sdes		stats->qbit_AD++;
314238106Sdes	if( (flags&BIT_CD) )
315238106Sdes		stats->qbit_CD++;
316238106Sdes	if(edns->edns_present) {
317238106Sdes		stats->qEDNS++;
318238106Sdes		if( (edns->bits & EDNS_DO) )
319238106Sdes			stats->qEDNS_DO++;
320238106Sdes	}
321238106Sdes}
322238106Sdes
323269257Sdesvoid server_stats_insrcode(struct server_stats* stats, sldns_buffer* buf)
324238106Sdes{
325269257Sdes	if(stats->extended && sldns_buffer_limit(buf) != 0) {
326269257Sdes		int r = (int)LDNS_RCODE_WIRE( sldns_buffer_begin(buf) );
327238106Sdes		stats->ans_rcode[r] ++;
328269257Sdes		if(r == 0 && LDNS_ANCOUNT( sldns_buffer_begin(buf) ) == 0)
329238106Sdes			stats->ans_rcode_nodata ++;
330238106Sdes	}
331238106Sdes}
332