]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/unbound/daemon/stats.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / contrib / unbound / daemon / stats.c
1 /*
2  * daemon/stats.c - collect runtime performance indicators.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  * 
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  * 
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 /**
37  * \file
38  *
39  * This file describes the data structure used to collect runtime performance
40  * numbers. These 'statistics' may be of interest to the operator.
41  */
42 #include "config.h"
43 #include <ldns/wire2host.h>
44 #include "daemon/stats.h"
45 #include "daemon/worker.h"
46 #include "daemon/daemon.h"
47 #include "services/mesh.h"
48 #include "services/outside_network.h"
49 #include "util/config_file.h"
50 #include "util/tube.h"
51 #include "util/timehist.h"
52 #include "util/net_help.h"
53 #include "validator/validator.h"
54
55 /** add timers and the values do not overflow or become negative */
56 static void
57 timeval_add(struct timeval* d, const struct timeval* add)
58 {
59 #ifndef S_SPLINT_S
60         d->tv_sec += add->tv_sec;
61         d->tv_usec += add->tv_usec;
62         if(d->tv_usec > 1000000) {
63                 d->tv_usec -= 1000000;
64                 d->tv_sec++;
65         }
66 #endif
67 }
68
69 void server_stats_init(struct server_stats* stats, struct config_file* cfg)
70 {
71         memset(stats, 0, sizeof(*stats));
72         stats->extended = cfg->stat_extended;
73 }
74
75 void server_stats_querymiss(struct server_stats* stats, struct worker* worker)
76 {
77         stats->num_queries_missed_cache++;
78         stats->sum_query_list_size += worker->env.mesh->all.count;
79         if(worker->env.mesh->all.count > stats->max_query_list_size)
80                 stats->max_query_list_size = worker->env.mesh->all.count;
81 }
82
83 void server_stats_prefetch(struct server_stats* stats, struct worker* worker)
84 {
85         stats->num_queries_prefetch++;
86         /* changes the query list size so account that, like a querymiss */
87         stats->sum_query_list_size += worker->env.mesh->all.count;
88         if(worker->env.mesh->all.count > stats->max_query_list_size)
89                 stats->max_query_list_size = worker->env.mesh->all.count;
90 }
91
92 void server_stats_log(struct server_stats* stats, struct worker* worker,
93         int threadnum)
94 {
95         log_info("server stats for thread %d: %u queries, "
96                 "%u answers from cache, %u recursions, %u prefetch", 
97                 threadnum, (unsigned)stats->num_queries, 
98                 (unsigned)(stats->num_queries - 
99                         stats->num_queries_missed_cache),
100                 (unsigned)stats->num_queries_missed_cache,
101                 (unsigned)stats->num_queries_prefetch);
102         log_info("server stats for thread %d: requestlist max %u avg %g "
103                 "exceeded %u jostled %u", threadnum,
104                 (unsigned)stats->max_query_list_size,
105                 (stats->num_queries_missed_cache+stats->num_queries_prefetch)?
106                         (double)stats->sum_query_list_size/
107                         (stats->num_queries_missed_cache+
108                         stats->num_queries_prefetch) : 0.0,
109                 (unsigned)worker->env.mesh->stats_dropped,
110                 (unsigned)worker->env.mesh->stats_jostled);
111 }
112
113 /** get rrsets bogus number from validator */
114 static size_t
115 get_rrset_bogus(struct worker* worker)
116 {
117         int m = modstack_find(&worker->env.mesh->mods, "validator");
118         struct val_env* ve;
119         size_t r;
120         if(m == -1)
121                 return 0;
122         ve = (struct val_env*)worker->env.modinfo[m];
123         lock_basic_lock(&ve->bogus_lock);
124         r = ve->num_rrset_bogus;
125         if(!worker->env.cfg->stat_cumulative)
126                 ve->num_rrset_bogus = 0;
127         lock_basic_unlock(&ve->bogus_lock);
128         return r;
129 }
130
131 void
132 server_stats_compile(struct worker* worker, struct stats_info* s, int reset)
133 {
134         int i;
135
136         s->svr = worker->stats;
137         s->mesh_num_states = worker->env.mesh->all.count;
138         s->mesh_num_reply_states = worker->env.mesh->num_reply_states;
139         s->mesh_jostled = worker->env.mesh->stats_jostled;
140         s->mesh_dropped = worker->env.mesh->stats_dropped;
141         s->mesh_replies_sent = worker->env.mesh->replies_sent;
142         s->mesh_replies_sum_wait = worker->env.mesh->replies_sum_wait;
143         s->mesh_time_median = timehist_quartile(worker->env.mesh->histogram,
144                 0.50);
145
146         /* add in the values from the mesh */
147         s->svr.ans_secure += worker->env.mesh->ans_secure;
148         s->svr.ans_bogus += worker->env.mesh->ans_bogus;
149         s->svr.ans_rcode_nodata += worker->env.mesh->ans_nodata;
150         for(i=0; i<16; i++)
151                 s->svr.ans_rcode[i] += worker->env.mesh->ans_rcode[i];
152         timehist_export(worker->env.mesh->histogram, s->svr.hist, 
153                 NUM_BUCKETS_HIST);
154         /* values from outside network */
155         s->svr.unwanted_replies = worker->back->unwanted_replies;
156
157         /* get and reset validator rrset bogus number */
158         s->svr.rrset_bogus = get_rrset_bogus(worker);
159
160         if(reset && !worker->env.cfg->stat_cumulative) {
161                 worker_stats_clear(worker);
162         }
163 }
164
165 void server_stats_obtain(struct worker* worker, struct worker* who,
166         struct stats_info* s, int reset)
167 {
168         uint8_t *reply = NULL;
169         uint32_t len = 0;
170         if(worker == who) {
171                 /* just fill it in */
172                 server_stats_compile(worker, s, reset);
173                 return;
174         }
175         /* communicate over tube */
176         verbose(VERB_ALGO, "write stats cmd");
177         if(reset)
178                 worker_send_cmd(who, worker_cmd_stats);
179         else    worker_send_cmd(who, worker_cmd_stats_noreset);
180         verbose(VERB_ALGO, "wait for stats reply");
181         if(!tube_read_msg(worker->cmd, &reply, &len, 0))
182                 fatal_exit("failed to read stats over cmd channel");
183         if(len != (uint32_t)sizeof(*s))
184                 fatal_exit("stats on cmd channel wrong length %d %d",
185                         (int)len, (int)sizeof(*s));
186         memcpy(s, reply, (size_t)len);
187         free(reply);
188 }
189
190 void server_stats_reply(struct worker* worker, int reset)
191 {
192         struct stats_info s;
193         server_stats_compile(worker, &s, reset);
194         verbose(VERB_ALGO, "write stats replymsg");
195         if(!tube_write_msg(worker->daemon->workers[0]->cmd, 
196                 (uint8_t*)&s, sizeof(s), 0))
197                 fatal_exit("could not write stat values over cmd channel");
198 }
199
200 void server_stats_add(struct stats_info* total, struct stats_info* a)
201 {
202         total->svr.num_queries += a->svr.num_queries;
203         total->svr.num_queries_missed_cache += a->svr.num_queries_missed_cache;
204         total->svr.num_queries_prefetch += a->svr.num_queries_prefetch;
205         total->svr.sum_query_list_size += a->svr.sum_query_list_size;
206         /* the max size reached is upped to higher of both */
207         if(a->svr.max_query_list_size > total->svr.max_query_list_size)
208                 total->svr.max_query_list_size = a->svr.max_query_list_size;
209
210         if(a->svr.extended) {
211                 int i;
212                 total->svr.qtype_big += a->svr.qtype_big;
213                 total->svr.qclass_big += a->svr.qclass_big;
214                 total->svr.qtcp += a->svr.qtcp;
215                 total->svr.qipv6 += a->svr.qipv6;
216                 total->svr.qbit_QR += a->svr.qbit_QR;
217                 total->svr.qbit_AA += a->svr.qbit_AA;
218                 total->svr.qbit_TC += a->svr.qbit_TC;
219                 total->svr.qbit_RD += a->svr.qbit_RD;
220                 total->svr.qbit_RA += a->svr.qbit_RA;
221                 total->svr.qbit_Z += a->svr.qbit_Z;
222                 total->svr.qbit_AD += a->svr.qbit_AD;
223                 total->svr.qbit_CD += a->svr.qbit_CD;
224                 total->svr.qEDNS += a->svr.qEDNS;
225                 total->svr.qEDNS_DO += a->svr.qEDNS_DO;
226                 total->svr.ans_rcode_nodata += a->svr.ans_rcode_nodata;
227                 total->svr.ans_secure += a->svr.ans_secure;
228                 total->svr.ans_bogus += a->svr.ans_bogus;
229                 total->svr.rrset_bogus += a->svr.rrset_bogus;
230                 total->svr.unwanted_replies += a->svr.unwanted_replies;
231                 total->svr.unwanted_queries += a->svr.unwanted_queries;
232                 for(i=0; i<STATS_QTYPE_NUM; i++)
233                         total->svr.qtype[i] += a->svr.qtype[i];
234                 for(i=0; i<STATS_QCLASS_NUM; i++)
235                         total->svr.qclass[i] += a->svr.qclass[i];
236                 for(i=0; i<STATS_OPCODE_NUM; i++)
237                         total->svr.qopcode[i] += a->svr.qopcode[i];
238                 for(i=0; i<STATS_RCODE_NUM; i++)
239                         total->svr.ans_rcode[i] += a->svr.ans_rcode[i];
240                 for(i=0; i<NUM_BUCKETS_HIST; i++)
241                         total->svr.hist[i] += a->svr.hist[i];
242         }
243
244         total->mesh_num_states += a->mesh_num_states;
245         total->mesh_num_reply_states += a->mesh_num_reply_states;
246         total->mesh_jostled += a->mesh_jostled;
247         total->mesh_dropped += a->mesh_dropped;
248         total->mesh_replies_sent += a->mesh_replies_sent;
249         timeval_add(&total->mesh_replies_sum_wait, &a->mesh_replies_sum_wait);
250         /* the medians are averaged together, this is not as accurate as
251          * taking the median over all of the data, but is good and fast
252          * added up here, division later*/
253         total->mesh_time_median += a->mesh_time_median;
254 }
255
256 void server_stats_insquery(struct server_stats* stats, struct comm_point* c,
257         uint16_t qtype, uint16_t qclass, struct edns_data* edns,
258         struct comm_reply* repinfo)
259 {
260         uint16_t flags = ldns_buffer_read_u16_at(c->buffer, 2);
261         if(qtype < STATS_QTYPE_NUM)
262                 stats->qtype[qtype]++;
263         else    stats->qtype_big++;
264         if(qclass < STATS_QCLASS_NUM)
265                 stats->qclass[qclass]++;
266         else    stats->qclass_big++;
267         stats->qopcode[ LDNS_OPCODE_WIRE(ldns_buffer_begin(c->buffer)) ]++;
268         if(c->type != comm_udp)
269                 stats->qtcp++;
270         if(repinfo && addr_is_ip6(&repinfo->addr, repinfo->addrlen))
271                 stats->qipv6++;
272         if( (flags&BIT_QR) )
273                 stats->qbit_QR++;
274         if( (flags&BIT_AA) )
275                 stats->qbit_AA++;
276         if( (flags&BIT_TC) )
277                 stats->qbit_TC++;
278         if( (flags&BIT_RD) )
279                 stats->qbit_RD++;
280         if( (flags&BIT_RA) )
281                 stats->qbit_RA++;
282         if( (flags&BIT_Z) )
283                 stats->qbit_Z++;
284         if( (flags&BIT_AD) )
285                 stats->qbit_AD++;
286         if( (flags&BIT_CD) )
287                 stats->qbit_CD++;
288         if(edns->edns_present) {
289                 stats->qEDNS++;
290                 if( (edns->bits & EDNS_DO) )
291                         stats->qEDNS_DO++;
292         }
293 }
294
295 void server_stats_insrcode(struct server_stats* stats, ldns_buffer* buf)
296 {
297         if(stats->extended && ldns_buffer_limit(buf) != 0) {
298                 int r = (int)LDNS_RCODE_WIRE( ldns_buffer_begin(buf) );
299                 stats->ans_rcode[r] ++;
300                 if(r == 0 && LDNS_ANCOUNT( ldns_buffer_begin(buf) ) == 0)
301                         stats->ans_rcode_nodata ++;
302         }
303 }