2 * daemon/stats.c - collect runtime performance indicators.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file describes the data structure used to collect runtime performance
40 * numbers. These 'statistics' may be of interest to the operator.
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 "sldns/sbuffer.h"
60 #include "services/cache/rrset.h"
61 #include "services/cache/infra.h"
62 #include "validator/val_kcache.h"
64 /** add timers and the values do not overflow or become negative */
66 stats_timeval_add(long long* d_sec, long long* d_usec, long long add_sec, long long add_usec)
70 (*d_usec) += add_usec;
71 if((*d_usec) > 1000000) {
78 void server_stats_init(struct ub_server_stats* stats, struct config_file* cfg)
80 memset(stats, 0, sizeof(*stats));
81 stats->extended = cfg->stat_extended;
84 void server_stats_querymiss(struct ub_server_stats* stats, struct worker* worker)
86 stats->num_queries_missed_cache++;
87 stats->sum_query_list_size += worker->env.mesh->all.count;
88 if((long long)worker->env.mesh->all.count > stats->max_query_list_size)
89 stats->max_query_list_size = (long long)worker->env.mesh->all.count;
92 void server_stats_prefetch(struct ub_server_stats* stats, struct worker* worker)
94 stats->num_queries_prefetch++;
95 /* changes the query list size so account that, like a querymiss */
96 stats->sum_query_list_size += worker->env.mesh->all.count;
97 if((long long)worker->env.mesh->all.count > stats->max_query_list_size)
98 stats->max_query_list_size = (long long)worker->env.mesh->all.count;
101 void server_stats_log(struct ub_server_stats* stats, struct worker* worker,
104 log_info("server stats for thread %d: %u queries, "
105 "%u answers from cache, %u recursions, %u prefetch, %u rejected by "
107 threadnum, (unsigned)stats->num_queries,
108 (unsigned)(stats->num_queries -
109 stats->num_queries_missed_cache),
110 (unsigned)stats->num_queries_missed_cache,
111 (unsigned)stats->num_queries_prefetch,
112 (unsigned)stats->num_queries_ip_ratelimited);
113 log_info("server stats for thread %d: requestlist max %u avg %g "
114 "exceeded %u jostled %u", threadnum,
115 (unsigned)stats->max_query_list_size,
116 (stats->num_queries_missed_cache+stats->num_queries_prefetch)?
117 (double)stats->sum_query_list_size/
118 (double)(stats->num_queries_missed_cache+
119 stats->num_queries_prefetch) : 0.0,
120 (unsigned)worker->env.mesh->stats_dropped,
121 (unsigned)worker->env.mesh->stats_jostled);
124 /** get rrsets bogus number from validator */
126 get_rrset_bogus(struct worker* worker)
128 int m = modstack_find(&worker->env.mesh->mods, "validator");
133 ve = (struct val_env*)worker->env.modinfo[m];
134 lock_basic_lock(&ve->bogus_lock);
135 r = ve->num_rrset_bogus;
136 if(!worker->env.cfg->stat_cumulative)
137 ve->num_rrset_bogus = 0;
138 lock_basic_unlock(&ve->bogus_lock);
143 server_stats_compile(struct worker* worker, struct ub_stats_info* s, int reset)
146 struct listen_list* lp;
148 s->svr = worker->stats;
149 s->mesh_num_states = (long long)worker->env.mesh->all.count;
150 s->mesh_num_reply_states = (long long)worker->env.mesh->num_reply_states;
151 s->mesh_jostled = (long long)worker->env.mesh->stats_jostled;
152 s->mesh_dropped = (long long)worker->env.mesh->stats_dropped;
153 s->mesh_replies_sent = (long long)worker->env.mesh->replies_sent;
154 s->mesh_replies_sum_wait_sec = (long long)worker->env.mesh->replies_sum_wait.tv_sec;
155 s->mesh_replies_sum_wait_usec = (long long)worker->env.mesh->replies_sum_wait.tv_usec;
156 s->mesh_time_median = timehist_quartile(worker->env.mesh->histogram,
159 /* add in the values from the mesh */
160 s->svr.ans_secure += (long long)worker->env.mesh->ans_secure;
161 s->svr.ans_bogus += (long long)worker->env.mesh->ans_bogus;
162 s->svr.ans_rcode_nodata += (long long)worker->env.mesh->ans_nodata;
164 s->svr.ans_rcode[i] += (long long)worker->env.mesh->ans_rcode[i];
165 timehist_export(worker->env.mesh->histogram, s->svr.hist,
167 /* values from outside network */
168 s->svr.unwanted_replies = (long long)worker->back->unwanted_replies;
169 s->svr.qtcp_outgoing = (long long)worker->back->num_tcp_outgoing;
171 /* get and reset validator rrset bogus number */
172 s->svr.rrset_bogus = (long long)get_rrset_bogus(worker);
174 /* get cache sizes */
175 s->svr.msg_cache_count = (long long)count_slabhash_entries(worker->env.msg_cache);
176 s->svr.rrset_cache_count = (long long)count_slabhash_entries(&worker->env.rrset_cache->table);
177 s->svr.infra_cache_count = (long long)count_slabhash_entries(worker->env.infra_cache->hosts);
178 if(worker->env.key_cache)
179 s->svr.key_cache_count = (long long)count_slabhash_entries(worker->env.key_cache->slab);
180 else s->svr.key_cache_count = 0;
182 /* get tcp accept usage */
183 s->svr.tcp_accept_usage = 0;
184 for(lp = worker->front->cps; lp; lp = lp->next) {
185 if(lp->com->type == comm_tcp_accept)
186 s->svr.tcp_accept_usage += (long long)lp->com->cur_tcp_count;
189 if(reset && !worker->env.cfg->stat_cumulative) {
190 worker_stats_clear(worker);
194 void server_stats_obtain(struct worker* worker, struct worker* who,
195 struct ub_stats_info* s, int reset)
197 uint8_t *reply = NULL;
200 /* just fill it in */
201 server_stats_compile(worker, s, reset);
204 /* communicate over tube */
205 verbose(VERB_ALGO, "write stats cmd");
207 worker_send_cmd(who, worker_cmd_stats);
208 else worker_send_cmd(who, worker_cmd_stats_noreset);
209 verbose(VERB_ALGO, "wait for stats reply");
210 if(!tube_read_msg(worker->cmd, &reply, &len, 0))
211 fatal_exit("failed to read stats over cmd channel");
212 if(len != (uint32_t)sizeof(*s))
213 fatal_exit("stats on cmd channel wrong length %d %d",
214 (int)len, (int)sizeof(*s));
215 memcpy(s, reply, (size_t)len);
219 void server_stats_reply(struct worker* worker, int reset)
221 struct ub_stats_info s;
222 server_stats_compile(worker, &s, reset);
223 verbose(VERB_ALGO, "write stats replymsg");
224 if(!tube_write_msg(worker->daemon->workers[0]->cmd,
225 (uint8_t*)&s, sizeof(s), 0))
226 fatal_exit("could not write stat values over cmd channel");
229 void server_stats_add(struct ub_stats_info* total, struct ub_stats_info* a)
231 total->svr.num_queries += a->svr.num_queries;
232 total->svr.num_queries_ip_ratelimited += a->svr.num_queries_ip_ratelimited;
233 total->svr.num_queries_missed_cache += a->svr.num_queries_missed_cache;
234 total->svr.num_queries_prefetch += a->svr.num_queries_prefetch;
235 total->svr.sum_query_list_size += a->svr.sum_query_list_size;
237 total->svr.num_query_dnscrypt_crypted += a->svr.num_query_dnscrypt_crypted;
238 total->svr.num_query_dnscrypt_cert += a->svr.num_query_dnscrypt_cert;
239 total->svr.num_query_dnscrypt_cleartext += \
240 a->svr.num_query_dnscrypt_cleartext;
241 total->svr.num_query_dnscrypt_crypted_malformed += \
242 a->svr.num_query_dnscrypt_crypted_malformed;
244 /* the max size reached is upped to higher of both */
245 if(a->svr.max_query_list_size > total->svr.max_query_list_size)
246 total->svr.max_query_list_size = a->svr.max_query_list_size;
248 if(a->svr.extended) {
250 total->svr.qtype_big += a->svr.qtype_big;
251 total->svr.qclass_big += a->svr.qclass_big;
252 total->svr.qtcp += a->svr.qtcp;
253 total->svr.qtcp_outgoing += a->svr.qtcp_outgoing;
254 total->svr.qipv6 += a->svr.qipv6;
255 total->svr.qbit_QR += a->svr.qbit_QR;
256 total->svr.qbit_AA += a->svr.qbit_AA;
257 total->svr.qbit_TC += a->svr.qbit_TC;
258 total->svr.qbit_RD += a->svr.qbit_RD;
259 total->svr.qbit_RA += a->svr.qbit_RA;
260 total->svr.qbit_Z += a->svr.qbit_Z;
261 total->svr.qbit_AD += a->svr.qbit_AD;
262 total->svr.qbit_CD += a->svr.qbit_CD;
263 total->svr.qEDNS += a->svr.qEDNS;
264 total->svr.qEDNS_DO += a->svr.qEDNS_DO;
265 total->svr.ans_rcode_nodata += a->svr.ans_rcode_nodata;
266 total->svr.zero_ttl_responses += a->svr.zero_ttl_responses;
267 total->svr.ans_secure += a->svr.ans_secure;
268 total->svr.ans_bogus += a->svr.ans_bogus;
269 total->svr.rrset_bogus += a->svr.rrset_bogus;
270 total->svr.unwanted_replies += a->svr.unwanted_replies;
271 total->svr.unwanted_queries += a->svr.unwanted_queries;
272 total->svr.tcp_accept_usage += a->svr.tcp_accept_usage;
273 for(i=0; i<UB_STATS_QTYPE_NUM; i++)
274 total->svr.qtype[i] += a->svr.qtype[i];
275 for(i=0; i<UB_STATS_QCLASS_NUM; i++)
276 total->svr.qclass[i] += a->svr.qclass[i];
277 for(i=0; i<UB_STATS_OPCODE_NUM; i++)
278 total->svr.qopcode[i] += a->svr.qopcode[i];
279 for(i=0; i<UB_STATS_RCODE_NUM; i++)
280 total->svr.ans_rcode[i] += a->svr.ans_rcode[i];
281 for(i=0; i<NUM_BUCKETS_HIST; i++)
282 total->svr.hist[i] += a->svr.hist[i];
285 total->mesh_num_states += a->mesh_num_states;
286 total->mesh_num_reply_states += a->mesh_num_reply_states;
287 total->mesh_jostled += a->mesh_jostled;
288 total->mesh_dropped += a->mesh_dropped;
289 total->mesh_replies_sent += a->mesh_replies_sent;
290 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);
291 /* the medians are averaged together, this is not as accurate as
292 * taking the median over all of the data, but is good and fast
293 * added up here, division later*/
294 total->mesh_time_median += a->mesh_time_median;
297 void server_stats_insquery(struct ub_server_stats* stats, struct comm_point* c,
298 uint16_t qtype, uint16_t qclass, struct edns_data* edns,
299 struct comm_reply* repinfo)
301 uint16_t flags = sldns_buffer_read_u16_at(c->buffer, 2);
302 if(qtype < UB_STATS_QTYPE_NUM)
303 stats->qtype[qtype]++;
304 else stats->qtype_big++;
305 if(qclass < UB_STATS_QCLASS_NUM)
306 stats->qclass[qclass]++;
307 else stats->qclass_big++;
308 stats->qopcode[ LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) ]++;
309 if(c->type != comm_udp)
311 if(repinfo && addr_is_ip6(&repinfo->addr, repinfo->addrlen))
329 if(edns->edns_present) {
331 if( (edns->bits & EDNS_DO) )
336 void server_stats_insrcode(struct ub_server_stats* stats, sldns_buffer* buf)
338 if(stats->extended && sldns_buffer_limit(buf) != 0) {
339 int r = (int)LDNS_RCODE_WIRE( sldns_buffer_begin(buf) );
340 stats->ans_rcode[r] ++;
341 if(r == 0 && LDNS_ANCOUNT( sldns_buffer_begin(buf) ) == 0)
342 stats->ans_rcode_nodata ++;