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" 53238106Sdes#include "util/config_file.h" 54238106Sdes#include "util/tube.h" 55238106Sdes#include "util/timehist.h" 56238106Sdes#include "util/net_help.h" 57238106Sdes#include "validator/validator.h" 58269257Sdes#include "ldns/sbuffer.h" 59238106Sdes 60238106Sdes/** add timers and the values do not overflow or become negative */ 61238106Sdesstatic void 62238106Sdestimeval_add(struct timeval* d, const struct timeval* add) 63238106Sdes{ 64238106Sdes#ifndef S_SPLINT_S 65238106Sdes d->tv_sec += add->tv_sec; 66238106Sdes d->tv_usec += add->tv_usec; 67238106Sdes if(d->tv_usec > 1000000) { 68238106Sdes d->tv_usec -= 1000000; 69238106Sdes d->tv_sec++; 70238106Sdes } 71238106Sdes#endif 72238106Sdes} 73238106Sdes 74238106Sdesvoid server_stats_init(struct server_stats* stats, struct config_file* cfg) 75238106Sdes{ 76238106Sdes memset(stats, 0, sizeof(*stats)); 77238106Sdes stats->extended = cfg->stat_extended; 78238106Sdes} 79238106Sdes 80238106Sdesvoid server_stats_querymiss(struct server_stats* stats, struct worker* worker) 81238106Sdes{ 82238106Sdes stats->num_queries_missed_cache++; 83238106Sdes stats->sum_query_list_size += worker->env.mesh->all.count; 84238106Sdes if(worker->env.mesh->all.count > stats->max_query_list_size) 85238106Sdes stats->max_query_list_size = worker->env.mesh->all.count; 86238106Sdes} 87238106Sdes 88238106Sdesvoid server_stats_prefetch(struct server_stats* stats, struct worker* worker) 89238106Sdes{ 90238106Sdes stats->num_queries_prefetch++; 91238106Sdes /* changes the query list size so account that, like a querymiss */ 92238106Sdes stats->sum_query_list_size += worker->env.mesh->all.count; 93238106Sdes if(worker->env.mesh->all.count > stats->max_query_list_size) 94238106Sdes stats->max_query_list_size = worker->env.mesh->all.count; 95238106Sdes} 96238106Sdes 97238106Sdesvoid server_stats_log(struct server_stats* stats, struct worker* worker, 98238106Sdes int threadnum) 99238106Sdes{ 100238106Sdes log_info("server stats for thread %d: %u queries, " 101238106Sdes "%u answers from cache, %u recursions, %u prefetch", 102238106Sdes threadnum, (unsigned)stats->num_queries, 103238106Sdes (unsigned)(stats->num_queries - 104238106Sdes stats->num_queries_missed_cache), 105238106Sdes (unsigned)stats->num_queries_missed_cache, 106238106Sdes (unsigned)stats->num_queries_prefetch); 107238106Sdes log_info("server stats for thread %d: requestlist max %u avg %g " 108238106Sdes "exceeded %u jostled %u", threadnum, 109238106Sdes (unsigned)stats->max_query_list_size, 110238106Sdes (stats->num_queries_missed_cache+stats->num_queries_prefetch)? 111238106Sdes (double)stats->sum_query_list_size/ 112238106Sdes (stats->num_queries_missed_cache+ 113238106Sdes stats->num_queries_prefetch) : 0.0, 114238106Sdes (unsigned)worker->env.mesh->stats_dropped, 115238106Sdes (unsigned)worker->env.mesh->stats_jostled); 116238106Sdes} 117238106Sdes 118238106Sdes/** get rrsets bogus number from validator */ 119238106Sdesstatic size_t 120238106Sdesget_rrset_bogus(struct worker* worker) 121238106Sdes{ 122238106Sdes int m = modstack_find(&worker->env.mesh->mods, "validator"); 123238106Sdes struct val_env* ve; 124238106Sdes size_t r; 125238106Sdes if(m == -1) 126238106Sdes return 0; 127238106Sdes ve = (struct val_env*)worker->env.modinfo[m]; 128238106Sdes lock_basic_lock(&ve->bogus_lock); 129238106Sdes r = ve->num_rrset_bogus; 130238106Sdes if(!worker->env.cfg->stat_cumulative) 131238106Sdes ve->num_rrset_bogus = 0; 132238106Sdes lock_basic_unlock(&ve->bogus_lock); 133238106Sdes return r; 134238106Sdes} 135238106Sdes 136238106Sdesvoid 137238106Sdesserver_stats_compile(struct worker* worker, struct stats_info* s, int reset) 138238106Sdes{ 139238106Sdes int i; 140238106Sdes 141238106Sdes s->svr = worker->stats; 142238106Sdes s->mesh_num_states = worker->env.mesh->all.count; 143238106Sdes s->mesh_num_reply_states = worker->env.mesh->num_reply_states; 144238106Sdes s->mesh_jostled = worker->env.mesh->stats_jostled; 145238106Sdes s->mesh_dropped = worker->env.mesh->stats_dropped; 146238106Sdes s->mesh_replies_sent = worker->env.mesh->replies_sent; 147238106Sdes s->mesh_replies_sum_wait = worker->env.mesh->replies_sum_wait; 148238106Sdes s->mesh_time_median = timehist_quartile(worker->env.mesh->histogram, 149238106Sdes 0.50); 150238106Sdes 151238106Sdes /* add in the values from the mesh */ 152238106Sdes s->svr.ans_secure += worker->env.mesh->ans_secure; 153238106Sdes s->svr.ans_bogus += worker->env.mesh->ans_bogus; 154238106Sdes s->svr.ans_rcode_nodata += worker->env.mesh->ans_nodata; 155238106Sdes for(i=0; i<16; i++) 156238106Sdes s->svr.ans_rcode[i] += worker->env.mesh->ans_rcode[i]; 157238106Sdes timehist_export(worker->env.mesh->histogram, s->svr.hist, 158238106Sdes NUM_BUCKETS_HIST); 159238106Sdes /* values from outside network */ 160238106Sdes s->svr.unwanted_replies = worker->back->unwanted_replies; 161238106Sdes 162238106Sdes /* get and reset validator rrset bogus number */ 163238106Sdes s->svr.rrset_bogus = get_rrset_bogus(worker); 164238106Sdes 165238106Sdes if(reset && !worker->env.cfg->stat_cumulative) { 166238106Sdes worker_stats_clear(worker); 167238106Sdes } 168238106Sdes} 169238106Sdes 170238106Sdesvoid server_stats_obtain(struct worker* worker, struct worker* who, 171238106Sdes struct stats_info* s, int reset) 172238106Sdes{ 173238106Sdes uint8_t *reply = NULL; 174238106Sdes uint32_t len = 0; 175238106Sdes if(worker == who) { 176238106Sdes /* just fill it in */ 177238106Sdes server_stats_compile(worker, s, reset); 178238106Sdes return; 179238106Sdes } 180238106Sdes /* communicate over tube */ 181238106Sdes verbose(VERB_ALGO, "write stats cmd"); 182238106Sdes if(reset) 183238106Sdes worker_send_cmd(who, worker_cmd_stats); 184238106Sdes else worker_send_cmd(who, worker_cmd_stats_noreset); 185238106Sdes verbose(VERB_ALGO, "wait for stats reply"); 186238106Sdes if(!tube_read_msg(worker->cmd, &reply, &len, 0)) 187238106Sdes fatal_exit("failed to read stats over cmd channel"); 188238106Sdes if(len != (uint32_t)sizeof(*s)) 189238106Sdes fatal_exit("stats on cmd channel wrong length %d %d", 190238106Sdes (int)len, (int)sizeof(*s)); 191238106Sdes memcpy(s, reply, (size_t)len); 192238106Sdes free(reply); 193238106Sdes} 194238106Sdes 195238106Sdesvoid server_stats_reply(struct worker* worker, int reset) 196238106Sdes{ 197238106Sdes struct stats_info s; 198238106Sdes server_stats_compile(worker, &s, reset); 199238106Sdes verbose(VERB_ALGO, "write stats replymsg"); 200238106Sdes if(!tube_write_msg(worker->daemon->workers[0]->cmd, 201238106Sdes (uint8_t*)&s, sizeof(s), 0)) 202238106Sdes fatal_exit("could not write stat values over cmd channel"); 203238106Sdes} 204238106Sdes 205238106Sdesvoid server_stats_add(struct stats_info* total, struct stats_info* a) 206238106Sdes{ 207238106Sdes total->svr.num_queries += a->svr.num_queries; 208238106Sdes total->svr.num_queries_missed_cache += a->svr.num_queries_missed_cache; 209238106Sdes total->svr.num_queries_prefetch += a->svr.num_queries_prefetch; 210238106Sdes total->svr.sum_query_list_size += a->svr.sum_query_list_size; 211238106Sdes /* the max size reached is upped to higher of both */ 212238106Sdes if(a->svr.max_query_list_size > total->svr.max_query_list_size) 213238106Sdes total->svr.max_query_list_size = a->svr.max_query_list_size; 214238106Sdes 215238106Sdes if(a->svr.extended) { 216238106Sdes int i; 217238106Sdes total->svr.qtype_big += a->svr.qtype_big; 218238106Sdes total->svr.qclass_big += a->svr.qclass_big; 219238106Sdes total->svr.qtcp += a->svr.qtcp; 220238106Sdes total->svr.qipv6 += a->svr.qipv6; 221238106Sdes total->svr.qbit_QR += a->svr.qbit_QR; 222238106Sdes total->svr.qbit_AA += a->svr.qbit_AA; 223238106Sdes total->svr.qbit_TC += a->svr.qbit_TC; 224238106Sdes total->svr.qbit_RD += a->svr.qbit_RD; 225238106Sdes total->svr.qbit_RA += a->svr.qbit_RA; 226238106Sdes total->svr.qbit_Z += a->svr.qbit_Z; 227238106Sdes total->svr.qbit_AD += a->svr.qbit_AD; 228238106Sdes total->svr.qbit_CD += a->svr.qbit_CD; 229238106Sdes total->svr.qEDNS += a->svr.qEDNS; 230238106Sdes total->svr.qEDNS_DO += a->svr.qEDNS_DO; 231238106Sdes total->svr.ans_rcode_nodata += a->svr.ans_rcode_nodata; 232238106Sdes total->svr.ans_secure += a->svr.ans_secure; 233238106Sdes total->svr.ans_bogus += a->svr.ans_bogus; 234238106Sdes total->svr.rrset_bogus += a->svr.rrset_bogus; 235238106Sdes total->svr.unwanted_replies += a->svr.unwanted_replies; 236238106Sdes total->svr.unwanted_queries += a->svr.unwanted_queries; 237238106Sdes for(i=0; i<STATS_QTYPE_NUM; i++) 238238106Sdes total->svr.qtype[i] += a->svr.qtype[i]; 239238106Sdes for(i=0; i<STATS_QCLASS_NUM; i++) 240238106Sdes total->svr.qclass[i] += a->svr.qclass[i]; 241238106Sdes for(i=0; i<STATS_OPCODE_NUM; i++) 242238106Sdes total->svr.qopcode[i] += a->svr.qopcode[i]; 243238106Sdes for(i=0; i<STATS_RCODE_NUM; i++) 244238106Sdes total->svr.ans_rcode[i] += a->svr.ans_rcode[i]; 245238106Sdes for(i=0; i<NUM_BUCKETS_HIST; i++) 246238106Sdes total->svr.hist[i] += a->svr.hist[i]; 247238106Sdes } 248238106Sdes 249238106Sdes total->mesh_num_states += a->mesh_num_states; 250238106Sdes total->mesh_num_reply_states += a->mesh_num_reply_states; 251238106Sdes total->mesh_jostled += a->mesh_jostled; 252238106Sdes total->mesh_dropped += a->mesh_dropped; 253238106Sdes total->mesh_replies_sent += a->mesh_replies_sent; 254238106Sdes timeval_add(&total->mesh_replies_sum_wait, &a->mesh_replies_sum_wait); 255238106Sdes /* the medians are averaged together, this is not as accurate as 256238106Sdes * taking the median over all of the data, but is good and fast 257238106Sdes * added up here, division later*/ 258238106Sdes total->mesh_time_median += a->mesh_time_median; 259238106Sdes} 260238106Sdes 261238106Sdesvoid server_stats_insquery(struct server_stats* stats, struct comm_point* c, 262238106Sdes uint16_t qtype, uint16_t qclass, struct edns_data* edns, 263238106Sdes struct comm_reply* repinfo) 264238106Sdes{ 265269257Sdes uint16_t flags = sldns_buffer_read_u16_at(c->buffer, 2); 266238106Sdes if(qtype < STATS_QTYPE_NUM) 267238106Sdes stats->qtype[qtype]++; 268238106Sdes else stats->qtype_big++; 269238106Sdes if(qclass < STATS_QCLASS_NUM) 270238106Sdes stats->qclass[qclass]++; 271238106Sdes else stats->qclass_big++; 272269257Sdes stats->qopcode[ LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) ]++; 273238106Sdes if(c->type != comm_udp) 274238106Sdes stats->qtcp++; 275238106Sdes if(repinfo && addr_is_ip6(&repinfo->addr, repinfo->addrlen)) 276238106Sdes stats->qipv6++; 277238106Sdes if( (flags&BIT_QR) ) 278238106Sdes stats->qbit_QR++; 279238106Sdes if( (flags&BIT_AA) ) 280238106Sdes stats->qbit_AA++; 281238106Sdes if( (flags&BIT_TC) ) 282238106Sdes stats->qbit_TC++; 283238106Sdes if( (flags&BIT_RD) ) 284238106Sdes stats->qbit_RD++; 285238106Sdes if( (flags&BIT_RA) ) 286238106Sdes stats->qbit_RA++; 287238106Sdes if( (flags&BIT_Z) ) 288238106Sdes stats->qbit_Z++; 289238106Sdes if( (flags&BIT_AD) ) 290238106Sdes stats->qbit_AD++; 291238106Sdes if( (flags&BIT_CD) ) 292238106Sdes stats->qbit_CD++; 293238106Sdes if(edns->edns_present) { 294238106Sdes stats->qEDNS++; 295238106Sdes if( (edns->bits & EDNS_DO) ) 296238106Sdes stats->qEDNS_DO++; 297238106Sdes } 298238106Sdes} 299238106Sdes 300269257Sdesvoid server_stats_insrcode(struct server_stats* stats, sldns_buffer* buf) 301238106Sdes{ 302269257Sdes if(stats->extended && sldns_buffer_limit(buf) != 0) { 303269257Sdes int r = (int)LDNS_RCODE_WIRE( sldns_buffer_begin(buf) ); 304238106Sdes stats->ans_rcode[r] ++; 305269257Sdes if(r == 0 && LDNS_ANCOUNT( sldns_buffer_begin(buf) ) == 0) 306238106Sdes stats->ans_rcode_nodata ++; 307238106Sdes } 308238106Sdes} 309