]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/daemon/stats.c
Import CK as of commit 5221ae2f3722a78c7fc41e47069ad94983d3bccb.
[FreeBSD/FreeBSD.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
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE 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 #ifdef HAVE_TIME_H
44 #include <time.h>
45 #endif
46 #include <sys/time.h>
47 #include <sys/types.h>
48 #include "daemon/stats.h"
49 #include "daemon/worker.h"
50 #include "daemon/daemon.h"
51 #include "services/mesh.h"
52 #include "services/outside_network.h"
53 #include "services/listen_dnsport.h"
54 #include "util/config_file.h"
55 #include "util/tube.h"
56 #include "util/timehist.h"
57 #include "util/net_help.h"
58 #include "validator/validator.h"
59 #include "iterator/iterator.h"
60 #include "sldns/sbuffer.h"
61 #include "services/cache/rrset.h"
62 #include "services/cache/infra.h"
63 #include "services/authzone.h"
64 #include "validator/val_kcache.h"
65 #include "validator/val_neg.h"
66 #ifdef CLIENT_SUBNET
67 #include "edns-subnet/subnetmod.h"
68 #endif
69
70 /** add timers and the values do not overflow or become negative */
71 static void
72 stats_timeval_add(long long* d_sec, long long* d_usec, long long add_sec, long long add_usec)
73 {
74 #ifndef S_SPLINT_S
75         (*d_sec) += add_sec;
76         (*d_usec) += add_usec;
77         if((*d_usec) > 1000000) {
78                 (*d_usec) -= 1000000;
79                 (*d_sec)++;
80         }
81 #endif
82 }
83
84 void server_stats_init(struct ub_server_stats* stats, struct config_file* cfg)
85 {
86         memset(stats, 0, sizeof(*stats));
87         stats->extended = cfg->stat_extended;
88 }
89
90 void server_stats_querymiss(struct ub_server_stats* stats, struct worker* worker)
91 {
92         stats->num_queries_missed_cache++;
93         stats->sum_query_list_size += worker->env.mesh->all.count;
94         if((long long)worker->env.mesh->all.count > stats->max_query_list_size)
95                 stats->max_query_list_size = (long long)worker->env.mesh->all.count;
96 }
97
98 void server_stats_prefetch(struct ub_server_stats* stats, struct worker* worker)
99 {
100         stats->num_queries_prefetch++;
101         /* changes the query list size so account that, like a querymiss */
102         stats->sum_query_list_size += worker->env.mesh->all.count;
103         if((long long)worker->env.mesh->all.count > stats->max_query_list_size)
104                 stats->max_query_list_size = (long long)worker->env.mesh->all.count;
105 }
106
107 void server_stats_log(struct ub_server_stats* stats, struct worker* worker,
108         int threadnum)
109 {
110         log_info("server stats for thread %d: %u queries, "
111                 "%u answers from cache, %u recursions, %u prefetch, %u rejected by "
112                 "ip ratelimiting",
113                 threadnum, (unsigned)stats->num_queries, 
114                 (unsigned)(stats->num_queries - 
115                         stats->num_queries_missed_cache),
116                 (unsigned)stats->num_queries_missed_cache,
117                 (unsigned)stats->num_queries_prefetch,
118                 (unsigned)stats->num_queries_ip_ratelimited);
119         log_info("server stats for thread %d: requestlist max %u avg %g "
120                 "exceeded %u jostled %u", threadnum,
121                 (unsigned)stats->max_query_list_size,
122                 (stats->num_queries_missed_cache+stats->num_queries_prefetch)?
123                         (double)stats->sum_query_list_size/
124                         (double)(stats->num_queries_missed_cache+
125                         stats->num_queries_prefetch) : 0.0,
126                 (unsigned)worker->env.mesh->stats_dropped,
127                 (unsigned)worker->env.mesh->stats_jostled);
128 }
129
130
131 #ifdef CLIENT_SUBNET
132 /** Set the EDNS Subnet stats. */
133 static void
134 set_subnet_stats(struct worker* worker, struct ub_server_stats* svr,
135         int reset)
136 {
137         int m = modstack_find(&worker->env.mesh->mods, "subnet");
138         struct subnet_env* sne;
139         if(m == -1)
140                 return;
141         sne = (struct subnet_env*)worker->env.modinfo[m];
142         if(reset && !worker->env.cfg->stat_cumulative) {
143                 lock_rw_wrlock(&sne->biglock);
144         } else {
145                 lock_rw_rdlock(&sne->biglock);
146         }
147         svr->num_query_subnet = (long long)(sne->num_msg_nocache + sne->num_msg_cache);
148         svr->num_query_subnet_cache = (long long)sne->num_msg_cache;
149         if(reset && !worker->env.cfg->stat_cumulative) {
150                 sne->num_msg_cache = 0;
151                 sne->num_msg_nocache = 0;
152         }
153         lock_rw_unlock(&sne->biglock);
154 }
155 #endif /* CLIENT_SUBNET */
156
157 /** Set the neg cache stats. */
158 static void
159 set_neg_cache_stats(struct worker* worker, struct ub_server_stats* svr,
160         int reset)
161 {
162         int m = modstack_find(&worker->env.mesh->mods, "validator");
163         struct val_env* ve;
164         struct val_neg_cache* neg;
165         if(m == -1)
166                 return;
167         ve = (struct val_env*)worker->env.modinfo[m];
168         if(!ve->neg_cache)
169                 return;
170         neg = ve->neg_cache;
171         lock_basic_lock(&neg->lock);
172         svr->num_neg_cache_noerror = (long long)neg->num_neg_cache_noerror;
173         svr->num_neg_cache_nxdomain = (long long)neg->num_neg_cache_nxdomain;
174         if(reset && !worker->env.cfg->stat_cumulative) {
175                 neg->num_neg_cache_noerror = 0;
176                 neg->num_neg_cache_nxdomain = 0;
177         }
178         lock_basic_unlock(&neg->lock);
179 }
180
181 /** get rrsets bogus number from validator */
182 static size_t
183 get_rrset_bogus(struct worker* worker, int reset)
184 {
185         int m = modstack_find(&worker->env.mesh->mods, "validator");
186         struct val_env* ve;
187         size_t r;
188         if(m == -1)
189                 return 0;
190         ve = (struct val_env*)worker->env.modinfo[m];
191         lock_basic_lock(&ve->bogus_lock);
192         r = ve->num_rrset_bogus;
193         if(reset && !worker->env.cfg->stat_cumulative)
194                 ve->num_rrset_bogus = 0;
195         lock_basic_unlock(&ve->bogus_lock);
196         return r;
197 }
198
199 /** get number of ratelimited queries from iterator */
200 static size_t
201 get_queries_ratelimit(struct worker* worker, int reset)
202 {
203         int m = modstack_find(&worker->env.mesh->mods, "iterator");
204         struct iter_env* ie;
205         size_t r;
206         if(m == -1)
207                 return 0;
208         ie = (struct iter_env*)worker->env.modinfo[m];
209         lock_basic_lock(&ie->queries_ratelimit_lock);
210         r = ie->num_queries_ratelimited;
211         if(reset && !worker->env.cfg->stat_cumulative)
212                 ie->num_queries_ratelimited = 0;
213         lock_basic_unlock(&ie->queries_ratelimit_lock);
214         return r;
215 }
216
217 #ifdef USE_DNSCRYPT
218 /** get the number of shared secret cache miss */
219 static size_t
220 get_dnscrypt_cache_miss(struct worker* worker, int reset)
221 {
222         size_t r;
223         struct dnsc_env* de = worker->daemon->dnscenv;
224         if(!de) return 0;
225
226         lock_basic_lock(&de->shared_secrets_cache_lock);
227         r = de->num_query_dnscrypt_secret_missed_cache;
228         if(reset && !worker->env.cfg->stat_cumulative)
229                 de->num_query_dnscrypt_secret_missed_cache = 0;
230         lock_basic_unlock(&de->shared_secrets_cache_lock);
231         return r;
232 }
233
234 /** get the number of replayed queries */
235 static size_t
236 get_dnscrypt_replay(struct worker* worker, int reset)
237 {
238         size_t r;
239         struct dnsc_env* de = worker->daemon->dnscenv;
240
241         lock_basic_lock(&de->nonces_cache_lock);
242         r = de->num_query_dnscrypt_replay;
243         if(reset && !worker->env.cfg->stat_cumulative)
244                 de->num_query_dnscrypt_replay = 0;
245         lock_basic_unlock(&de->nonces_cache_lock);
246         return r;
247 }
248 #endif /* USE_DNSCRYPT */
249
250 void
251 server_stats_compile(struct worker* worker, struct ub_stats_info* s, int reset)
252 {
253         int i;
254         struct listen_list* lp;
255
256         s->svr = worker->stats;
257         s->mesh_num_states = (long long)worker->env.mesh->all.count;
258         s->mesh_num_reply_states = (long long)worker->env.mesh->num_reply_states;
259         s->mesh_jostled = (long long)worker->env.mesh->stats_jostled;
260         s->mesh_dropped = (long long)worker->env.mesh->stats_dropped;
261         s->mesh_replies_sent = (long long)worker->env.mesh->replies_sent;
262         s->mesh_replies_sum_wait_sec = (long long)worker->env.mesh->replies_sum_wait.tv_sec;
263         s->mesh_replies_sum_wait_usec = (long long)worker->env.mesh->replies_sum_wait.tv_usec;
264         s->mesh_time_median = timehist_quartile(worker->env.mesh->histogram,
265                 0.50);
266
267         /* add in the values from the mesh */
268         s->svr.ans_secure += (long long)worker->env.mesh->ans_secure;
269         s->svr.ans_bogus += (long long)worker->env.mesh->ans_bogus;
270         s->svr.ans_rcode_nodata += (long long)worker->env.mesh->ans_nodata;
271         for(i=0; i<16; i++)
272                 s->svr.ans_rcode[i] += (long long)worker->env.mesh->ans_rcode[i];
273         timehist_export(worker->env.mesh->histogram, s->svr.hist, 
274                 NUM_BUCKETS_HIST);
275         /* values from outside network */
276         s->svr.unwanted_replies = (long long)worker->back->unwanted_replies;
277         s->svr.qtcp_outgoing = (long long)worker->back->num_tcp_outgoing;
278
279         /* get and reset validator rrset bogus number */
280         s->svr.rrset_bogus = (long long)get_rrset_bogus(worker, reset);
281
282         /* get and reset iterator query ratelimit number */
283         s->svr.queries_ratelimited = (long long)get_queries_ratelimit(worker, reset);
284
285         /* get cache sizes */
286         s->svr.msg_cache_count = (long long)count_slabhash_entries(worker->env.msg_cache);
287         s->svr.rrset_cache_count = (long long)count_slabhash_entries(&worker->env.rrset_cache->table);
288         s->svr.infra_cache_count = (long long)count_slabhash_entries(worker->env.infra_cache->hosts);
289         if(worker->env.key_cache)
290                 s->svr.key_cache_count = (long long)count_slabhash_entries(worker->env.key_cache->slab);
291         else    s->svr.key_cache_count = 0;
292
293 #ifdef USE_DNSCRYPT
294         if(worker->daemon->dnscenv) {
295                 s->svr.num_query_dnscrypt_secret_missed_cache =
296                         (long long)get_dnscrypt_cache_miss(worker, reset);
297                 s->svr.shared_secret_cache_count = (long long)count_slabhash_entries(
298                         worker->daemon->dnscenv->shared_secrets_cache);
299                 s->svr.nonce_cache_count = (long long)count_slabhash_entries(
300                         worker->daemon->dnscenv->nonces_cache);
301                 s->svr.num_query_dnscrypt_replay =
302                         (long long)get_dnscrypt_replay(worker, reset);
303         } else {
304                 s->svr.num_query_dnscrypt_secret_missed_cache = 0;
305                 s->svr.shared_secret_cache_count = 0;
306                 s->svr.nonce_cache_count = 0;
307                 s->svr.num_query_dnscrypt_replay = 0;
308         }
309 #else
310         s->svr.num_query_dnscrypt_secret_missed_cache = 0;
311         s->svr.shared_secret_cache_count = 0;
312         s->svr.nonce_cache_count = 0;
313         s->svr.num_query_dnscrypt_replay = 0;
314 #endif /* USE_DNSCRYPT */
315         if(worker->env.auth_zones) {
316                 if(reset && !worker->env.cfg->stat_cumulative) {
317                         lock_rw_wrlock(&worker->env.auth_zones->lock);
318                 } else {
319                         lock_rw_rdlock(&worker->env.auth_zones->lock);
320                 }
321                 s->svr.num_query_authzone_up = (long long)worker->env.
322                         auth_zones->num_query_up;
323                 s->svr.num_query_authzone_down = (long long)worker->env.
324                         auth_zones->num_query_down;
325                 if(reset && !worker->env.cfg->stat_cumulative) {
326                         worker->env.auth_zones->num_query_up = 0;
327                         worker->env.auth_zones->num_query_down = 0;
328                 }
329                 lock_rw_unlock(&worker->env.auth_zones->lock);
330         }
331
332         /* Set neg cache usage numbers */
333         set_neg_cache_stats(worker, &s->svr, reset);
334 #ifdef CLIENT_SUBNET
335         /* EDNS Subnet usage numbers */
336         set_subnet_stats(worker, &s->svr, reset);
337 #else
338         s->svr.num_query_subnet = 0;
339         s->svr.num_query_subnet_cache = 0;
340 #endif
341
342         /* get tcp accept usage */
343         s->svr.tcp_accept_usage = 0;
344         for(lp = worker->front->cps; lp; lp = lp->next) {
345                 if(lp->com->type == comm_tcp_accept)
346                         s->svr.tcp_accept_usage += (long long)lp->com->cur_tcp_count;
347         }
348
349         if(reset && !worker->env.cfg->stat_cumulative) {
350                 worker_stats_clear(worker);
351         }
352 }
353
354 void server_stats_obtain(struct worker* worker, struct worker* who,
355         struct ub_stats_info* s, int reset)
356 {
357         uint8_t *reply = NULL;
358         uint32_t len = 0;
359         if(worker == who) {
360                 /* just fill it in */
361                 server_stats_compile(worker, s, reset);
362                 return;
363         }
364         /* communicate over tube */
365         verbose(VERB_ALGO, "write stats cmd");
366         if(reset)
367                 worker_send_cmd(who, worker_cmd_stats);
368         else    worker_send_cmd(who, worker_cmd_stats_noreset);
369         verbose(VERB_ALGO, "wait for stats reply");
370         if(!tube_read_msg(worker->cmd, &reply, &len, 0))
371                 fatal_exit("failed to read stats over cmd channel");
372         if(len != (uint32_t)sizeof(*s))
373                 fatal_exit("stats on cmd channel wrong length %d %d",
374                         (int)len, (int)sizeof(*s));
375         memcpy(s, reply, (size_t)len);
376         free(reply);
377 }
378
379 void server_stats_reply(struct worker* worker, int reset)
380 {
381         struct ub_stats_info s;
382         server_stats_compile(worker, &s, reset);
383         verbose(VERB_ALGO, "write stats replymsg");
384         if(!tube_write_msg(worker->daemon->workers[0]->cmd, 
385                 (uint8_t*)&s, sizeof(s), 0))
386                 fatal_exit("could not write stat values over cmd channel");
387 }
388
389 void server_stats_add(struct ub_stats_info* total, struct ub_stats_info* a)
390 {
391         total->svr.num_queries += a->svr.num_queries;
392         total->svr.num_queries_ip_ratelimited += a->svr.num_queries_ip_ratelimited;
393         total->svr.num_queries_missed_cache += a->svr.num_queries_missed_cache;
394         total->svr.num_queries_prefetch += a->svr.num_queries_prefetch;
395         total->svr.sum_query_list_size += a->svr.sum_query_list_size;
396 #ifdef USE_DNSCRYPT
397         total->svr.num_query_dnscrypt_crypted += a->svr.num_query_dnscrypt_crypted;
398         total->svr.num_query_dnscrypt_cert += a->svr.num_query_dnscrypt_cert;
399         total->svr.num_query_dnscrypt_cleartext += \
400                 a->svr.num_query_dnscrypt_cleartext;
401         total->svr.num_query_dnscrypt_crypted_malformed += \
402                 a->svr.num_query_dnscrypt_crypted_malformed;
403 #endif /* USE_DNSCRYPT */
404         /* the max size reached is upped to higher of both */
405         if(a->svr.max_query_list_size > total->svr.max_query_list_size)
406                 total->svr.max_query_list_size = a->svr.max_query_list_size;
407
408         if(a->svr.extended) {
409                 int i;
410                 total->svr.qtype_big += a->svr.qtype_big;
411                 total->svr.qclass_big += a->svr.qclass_big;
412                 total->svr.qtcp += a->svr.qtcp;
413                 total->svr.qtcp_outgoing += a->svr.qtcp_outgoing;
414                 total->svr.qtls += a->svr.qtls;
415                 total->svr.qipv6 += a->svr.qipv6;
416                 total->svr.qbit_QR += a->svr.qbit_QR;
417                 total->svr.qbit_AA += a->svr.qbit_AA;
418                 total->svr.qbit_TC += a->svr.qbit_TC;
419                 total->svr.qbit_RD += a->svr.qbit_RD;
420                 total->svr.qbit_RA += a->svr.qbit_RA;
421                 total->svr.qbit_Z += a->svr.qbit_Z;
422                 total->svr.qbit_AD += a->svr.qbit_AD;
423                 total->svr.qbit_CD += a->svr.qbit_CD;
424                 total->svr.qEDNS += a->svr.qEDNS;
425                 total->svr.qEDNS_DO += a->svr.qEDNS_DO;
426                 total->svr.ans_rcode_nodata += a->svr.ans_rcode_nodata;
427                 total->svr.zero_ttl_responses += a->svr.zero_ttl_responses;
428                 total->svr.ans_secure += a->svr.ans_secure;
429                 total->svr.ans_bogus += a->svr.ans_bogus;
430                 total->svr.unwanted_replies += a->svr.unwanted_replies;
431                 total->svr.unwanted_queries += a->svr.unwanted_queries;
432                 total->svr.tcp_accept_usage += a->svr.tcp_accept_usage;
433                 for(i=0; i<UB_STATS_QTYPE_NUM; i++)
434                         total->svr.qtype[i] += a->svr.qtype[i];
435                 for(i=0; i<UB_STATS_QCLASS_NUM; i++)
436                         total->svr.qclass[i] += a->svr.qclass[i];
437                 for(i=0; i<UB_STATS_OPCODE_NUM; i++)
438                         total->svr.qopcode[i] += a->svr.qopcode[i];
439                 for(i=0; i<UB_STATS_RCODE_NUM; i++)
440                         total->svr.ans_rcode[i] += a->svr.ans_rcode[i];
441                 for(i=0; i<NUM_BUCKETS_HIST; i++)
442                         total->svr.hist[i] += a->svr.hist[i];
443         }
444
445         total->mesh_num_states += a->mesh_num_states;
446         total->mesh_num_reply_states += a->mesh_num_reply_states;
447         total->mesh_jostled += a->mesh_jostled;
448         total->mesh_dropped += a->mesh_dropped;
449         total->mesh_replies_sent += a->mesh_replies_sent;
450         stats_timeval_add(&total->mesh_replies_sum_wait_sec, &total->mesh_replies_sum_wait_usec, a->mesh_replies_sum_wait_sec, a->mesh_replies_sum_wait_usec);
451         /* the medians are averaged together, this is not as accurate as
452          * taking the median over all of the data, but is good and fast
453          * added up here, division later*/
454         total->mesh_time_median += a->mesh_time_median;
455 }
456
457 void server_stats_insquery(struct ub_server_stats* stats, struct comm_point* c,
458         uint16_t qtype, uint16_t qclass, struct edns_data* edns,
459         struct comm_reply* repinfo)
460 {
461         uint16_t flags = sldns_buffer_read_u16_at(c->buffer, 2);
462         if(qtype < UB_STATS_QTYPE_NUM)
463                 stats->qtype[qtype]++;
464         else    stats->qtype_big++;
465         if(qclass < UB_STATS_QCLASS_NUM)
466                 stats->qclass[qclass]++;
467         else    stats->qclass_big++;
468         stats->qopcode[ LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) ]++;
469         if(c->type != comm_udp) {
470                 stats->qtcp++;
471                 if(c->ssl != NULL)
472                         stats->qtls++;
473         }
474         if(repinfo && addr_is_ip6(&repinfo->addr, repinfo->addrlen))
475                 stats->qipv6++;
476         if( (flags&BIT_QR) )
477                 stats->qbit_QR++;
478         if( (flags&BIT_AA) )
479                 stats->qbit_AA++;
480         if( (flags&BIT_TC) )
481                 stats->qbit_TC++;
482         if( (flags&BIT_RD) )
483                 stats->qbit_RD++;
484         if( (flags&BIT_RA) )
485                 stats->qbit_RA++;
486         if( (flags&BIT_Z) )
487                 stats->qbit_Z++;
488         if( (flags&BIT_AD) )
489                 stats->qbit_AD++;
490         if( (flags&BIT_CD) )
491                 stats->qbit_CD++;
492         if(edns->edns_present) {
493                 stats->qEDNS++;
494                 if( (edns->bits & EDNS_DO) )
495                         stats->qEDNS_DO++;
496         }
497 }
498
499 void server_stats_insrcode(struct ub_server_stats* stats, sldns_buffer* buf)
500 {
501         if(stats->extended && sldns_buffer_limit(buf) != 0) {
502                 int r = (int)LDNS_RCODE_WIRE( sldns_buffer_begin(buf) );
503                 stats->ans_rcode[r] ++;
504                 if(r == 0 && LDNS_ANCOUNT( sldns_buffer_begin(buf) ) == 0)
505                         stats->ans_rcode_nodata ++;
506         }
507 }