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 "util/config_file.h" 54 #include "util/tube.h" 55 #include "util/timehist.h" 56 #include "util/net_help.h" 57 #include "validator/validator.h" 58 #include "ldns/sbuffer.h" 59 60 /** add timers and the values do not overflow or become negative */ 61 static void 62 timeval_add(struct timeval* d, const struct timeval* add) 63 { 64 #ifndef S_SPLINT_S 65 d->tv_sec += add->tv_sec; 66 d->tv_usec += add->tv_usec; 67 if(d->tv_usec > 1000000) { 68 d->tv_usec -= 1000000; 69 d->tv_sec++; 70 } 71 #endif 72 } 73 74 void server_stats_init(struct server_stats* stats, struct config_file* cfg) 75 { 76 memset(stats, 0, sizeof(*stats)); 77 stats->extended = cfg->stat_extended; 78 } 79 80 void server_stats_querymiss(struct server_stats* stats, struct worker* worker) 81 { 82 stats->num_queries_missed_cache++; 83 stats->sum_query_list_size += worker->env.mesh->all.count; 84 if(worker->env.mesh->all.count > stats->max_query_list_size) 85 stats->max_query_list_size = worker->env.mesh->all.count; 86 } 87 88 void server_stats_prefetch(struct server_stats* stats, struct worker* worker) 89 { 90 stats->num_queries_prefetch++; 91 /* changes the query list size so account that, like a querymiss */ 92 stats->sum_query_list_size += worker->env.mesh->all.count; 93 if(worker->env.mesh->all.count > stats->max_query_list_size) 94 stats->max_query_list_size = worker->env.mesh->all.count; 95 } 96 97 void server_stats_log(struct server_stats* stats, struct worker* worker, 98 int threadnum) 99 { 100 log_info("server stats for thread %d: %u queries, " 101 "%u answers from cache, %u recursions, %u prefetch", 102 threadnum, (unsigned)stats->num_queries, 103 (unsigned)(stats->num_queries - 104 stats->num_queries_missed_cache), 105 (unsigned)stats->num_queries_missed_cache, 106 (unsigned)stats->num_queries_prefetch); 107 log_info("server stats for thread %d: requestlist max %u avg %g " 108 "exceeded %u jostled %u", threadnum, 109 (unsigned)stats->max_query_list_size, 110 (stats->num_queries_missed_cache+stats->num_queries_prefetch)? 111 (double)stats->sum_query_list_size/ 112 (stats->num_queries_missed_cache+ 113 stats->num_queries_prefetch) : 0.0, 114 (unsigned)worker->env.mesh->stats_dropped, 115 (unsigned)worker->env.mesh->stats_jostled); 116 } 117 118 /** get rrsets bogus number from validator */ 119 static size_t 120 get_rrset_bogus(struct worker* worker) 121 { 122 int m = modstack_find(&worker->env.mesh->mods, "validator"); 123 struct val_env* ve; 124 size_t r; 125 if(m == -1) 126 return 0; 127 ve = (struct val_env*)worker->env.modinfo[m]; 128 lock_basic_lock(&ve->bogus_lock); 129 r = ve->num_rrset_bogus; 130 if(!worker->env.cfg->stat_cumulative) 131 ve->num_rrset_bogus = 0; 132 lock_basic_unlock(&ve->bogus_lock); 133 return r; 134 } 135 136 void 137 server_stats_compile(struct worker* worker, struct stats_info* s, int reset) 138 { 139 int i; 140 141 s->svr = worker->stats; 142 s->mesh_num_states = worker->env.mesh->all.count; 143 s->mesh_num_reply_states = worker->env.mesh->num_reply_states; 144 s->mesh_jostled = worker->env.mesh->stats_jostled; 145 s->mesh_dropped = worker->env.mesh->stats_dropped; 146 s->mesh_replies_sent = worker->env.mesh->replies_sent; 147 s->mesh_replies_sum_wait = worker->env.mesh->replies_sum_wait; 148 s->mesh_time_median = timehist_quartile(worker->env.mesh->histogram, 149 0.50); 150 151 /* add in the values from the mesh */ 152 s->svr.ans_secure += worker->env.mesh->ans_secure; 153 s->svr.ans_bogus += worker->env.mesh->ans_bogus; 154 s->svr.ans_rcode_nodata += worker->env.mesh->ans_nodata; 155 for(i=0; i<16; i++) 156 s->svr.ans_rcode[i] += worker->env.mesh->ans_rcode[i]; 157 timehist_export(worker->env.mesh->histogram, s->svr.hist, 158 NUM_BUCKETS_HIST); 159 /* values from outside network */ 160 s->svr.unwanted_replies = worker->back->unwanted_replies; 161 162 /* get and reset validator rrset bogus number */ 163 s->svr.rrset_bogus = get_rrset_bogus(worker); 164 165 if(reset && !worker->env.cfg->stat_cumulative) { 166 worker_stats_clear(worker); 167 } 168 } 169 170 void server_stats_obtain(struct worker* worker, struct worker* who, 171 struct stats_info* s, int reset) 172 { 173 uint8_t *reply = NULL; 174 uint32_t len = 0; 175 if(worker == who) { 176 /* just fill it in */ 177 server_stats_compile(worker, s, reset); 178 return; 179 } 180 /* communicate over tube */ 181 verbose(VERB_ALGO, "write stats cmd"); 182 if(reset) 183 worker_send_cmd(who, worker_cmd_stats); 184 else worker_send_cmd(who, worker_cmd_stats_noreset); 185 verbose(VERB_ALGO, "wait for stats reply"); 186 if(!tube_read_msg(worker->cmd, &reply, &len, 0)) 187 fatal_exit("failed to read stats over cmd channel"); 188 if(len != (uint32_t)sizeof(*s)) 189 fatal_exit("stats on cmd channel wrong length %d %d", 190 (int)len, (int)sizeof(*s)); 191 memcpy(s, reply, (size_t)len); 192 free(reply); 193 } 194 195 void server_stats_reply(struct worker* worker, int reset) 196 { 197 struct stats_info s; 198 server_stats_compile(worker, &s, reset); 199 verbose(VERB_ALGO, "write stats replymsg"); 200 if(!tube_write_msg(worker->daemon->workers[0]->cmd, 201 (uint8_t*)&s, sizeof(s), 0)) 202 fatal_exit("could not write stat values over cmd channel"); 203 } 204 205 void server_stats_add(struct stats_info* total, struct stats_info* a) 206 { 207 total->svr.num_queries += a->svr.num_queries; 208 total->svr.num_queries_missed_cache += a->svr.num_queries_missed_cache; 209 total->svr.num_queries_prefetch += a->svr.num_queries_prefetch; 210 total->svr.sum_query_list_size += a->svr.sum_query_list_size; 211 /* the max size reached is upped to higher of both */ 212 if(a->svr.max_query_list_size > total->svr.max_query_list_size) 213 total->svr.max_query_list_size = a->svr.max_query_list_size; 214 215 if(a->svr.extended) { 216 int i; 217 total->svr.qtype_big += a->svr.qtype_big; 218 total->svr.qclass_big += a->svr.qclass_big; 219 total->svr.qtcp += a->svr.qtcp; 220 total->svr.qipv6 += a->svr.qipv6; 221 total->svr.qbit_QR += a->svr.qbit_QR; 222 total->svr.qbit_AA += a->svr.qbit_AA; 223 total->svr.qbit_TC += a->svr.qbit_TC; 224 total->svr.qbit_RD += a->svr.qbit_RD; 225 total->svr.qbit_RA += a->svr.qbit_RA; 226 total->svr.qbit_Z += a->svr.qbit_Z; 227 total->svr.qbit_AD += a->svr.qbit_AD; 228 total->svr.qbit_CD += a->svr.qbit_CD; 229 total->svr.qEDNS += a->svr.qEDNS; 230 total->svr.qEDNS_DO += a->svr.qEDNS_DO; 231 total->svr.ans_rcode_nodata += a->svr.ans_rcode_nodata; 232 total->svr.ans_secure += a->svr.ans_secure; 233 total->svr.ans_bogus += a->svr.ans_bogus; 234 total->svr.rrset_bogus += a->svr.rrset_bogus; 235 total->svr.unwanted_replies += a->svr.unwanted_replies; 236 total->svr.unwanted_queries += a->svr.unwanted_queries; 237 for(i=0; i<STATS_QTYPE_NUM; i++) 238 total->svr.qtype[i] += a->svr.qtype[i]; 239 for(i=0; i<STATS_QCLASS_NUM; i++) 240 total->svr.qclass[i] += a->svr.qclass[i]; 241 for(i=0; i<STATS_OPCODE_NUM; i++) 242 total->svr.qopcode[i] += a->svr.qopcode[i]; 243 for(i=0; i<STATS_RCODE_NUM; i++) 244 total->svr.ans_rcode[i] += a->svr.ans_rcode[i]; 245 for(i=0; i<NUM_BUCKETS_HIST; i++) 246 total->svr.hist[i] += a->svr.hist[i]; 247 } 248 249 total->mesh_num_states += a->mesh_num_states; 250 total->mesh_num_reply_states += a->mesh_num_reply_states; 251 total->mesh_jostled += a->mesh_jostled; 252 total->mesh_dropped += a->mesh_dropped; 253 total->mesh_replies_sent += a->mesh_replies_sent; 254 timeval_add(&total->mesh_replies_sum_wait, &a->mesh_replies_sum_wait); 255 /* the medians are averaged together, this is not as accurate as 256 * taking the median over all of the data, but is good and fast 257 * added up here, division later*/ 258 total->mesh_time_median += a->mesh_time_median; 259 } 260 261 void server_stats_insquery(struct server_stats* stats, struct comm_point* c, 262 uint16_t qtype, uint16_t qclass, struct edns_data* edns, 263 struct comm_reply* repinfo) 264 { 265 uint16_t flags = sldns_buffer_read_u16_at(c->buffer, 2); 266 if(qtype < STATS_QTYPE_NUM) 267 stats->qtype[qtype]++; 268 else stats->qtype_big++; 269 if(qclass < STATS_QCLASS_NUM) 270 stats->qclass[qclass]++; 271 else stats->qclass_big++; 272 stats->qopcode[ LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) ]++; 273 if(c->type != comm_udp) 274 stats->qtcp++; 275 if(repinfo && addr_is_ip6(&repinfo->addr, repinfo->addrlen)) 276 stats->qipv6++; 277 if( (flags&BIT_QR) ) 278 stats->qbit_QR++; 279 if( (flags&BIT_AA) ) 280 stats->qbit_AA++; 281 if( (flags&BIT_TC) ) 282 stats->qbit_TC++; 283 if( (flags&BIT_RD) ) 284 stats->qbit_RD++; 285 if( (flags&BIT_RA) ) 286 stats->qbit_RA++; 287 if( (flags&BIT_Z) ) 288 stats->qbit_Z++; 289 if( (flags&BIT_AD) ) 290 stats->qbit_AD++; 291 if( (flags&BIT_CD) ) 292 stats->qbit_CD++; 293 if(edns->edns_present) { 294 stats->qEDNS++; 295 if( (edns->bits & EDNS_DO) ) 296 stats->qEDNS_DO++; 297 } 298 } 299 300 void server_stats_insrcode(struct server_stats* stats, sldns_buffer* buf) 301 { 302 if(stats->extended && sldns_buffer_limit(buf) != 0) { 303 int r = (int)LDNS_RCODE_WIRE( sldns_buffer_begin(buf) ); 304 stats->ans_rcode[r] ++; 305 if(r == 0 && LDNS_ANCOUNT( sldns_buffer_begin(buf) ) == 0) 306 stats->ans_rcode_nodata ++; 307 } 308 } 309