1933707f3Ssthen /* 2933707f3Ssthen * daemon/stats.c - collect runtime performance indicators. 3933707f3Ssthen * 4933707f3Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved. 5933707f3Ssthen * 6933707f3Ssthen * This software is open source. 7933707f3Ssthen * 8933707f3Ssthen * Redistribution and use in source and binary forms, with or without 9933707f3Ssthen * modification, are permitted provided that the following conditions 10933707f3Ssthen * are met: 11933707f3Ssthen * 12933707f3Ssthen * Redistributions of source code must retain the above copyright notice, 13933707f3Ssthen * this list of conditions and the following disclaimer. 14933707f3Ssthen * 15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice, 16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation 17933707f3Ssthen * and/or other materials provided with the distribution. 18933707f3Ssthen * 19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may 20933707f3Ssthen * be used to endorse or promote products derived from this software without 21933707f3Ssthen * specific prior written permission. 22933707f3Ssthen * 23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 245d76a658Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 255d76a658Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 265d76a658Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 275d76a658Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 285d76a658Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 295d76a658Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 305d76a658Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 315d76a658Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 325d76a658Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 335d76a658Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34933707f3Ssthen */ 35933707f3Ssthen 36933707f3Ssthen /** 37933707f3Ssthen * \file 38933707f3Ssthen * 39933707f3Ssthen * This file describes the data structure used to collect runtime performance 40933707f3Ssthen * numbers. These 'statistics' may be of interest to the operator. 41933707f3Ssthen */ 42933707f3Ssthen #include "config.h" 435d76a658Ssthen #ifdef HAVE_TIME_H 445d76a658Ssthen #include <time.h> 455d76a658Ssthen #endif 465d76a658Ssthen #include <sys/time.h> 475d76a658Ssthen #include <sys/types.h> 48933707f3Ssthen #include "daemon/stats.h" 49933707f3Ssthen #include "daemon/worker.h" 50933707f3Ssthen #include "daemon/daemon.h" 51933707f3Ssthen #include "services/mesh.h" 52933707f3Ssthen #include "services/outside_network.h" 53fdfb4ba6Ssthen #include "services/listen_dnsport.h" 54933707f3Ssthen #include "util/config_file.h" 55933707f3Ssthen #include "util/tube.h" 56933707f3Ssthen #include "util/timehist.h" 57933707f3Ssthen #include "util/net_help.h" 58933707f3Ssthen #include "validator/validator.h" 597191de28Ssthen #include "iterator/iterator.h" 60fdfb4ba6Ssthen #include "sldns/sbuffer.h" 6198f3ca02Sbrad #include "services/cache/rrset.h" 6298f3ca02Sbrad #include "services/cache/infra.h" 6320237c55Ssthen #include "services/authzone.h" 6498f3ca02Sbrad #include "validator/val_kcache.h" 6520237c55Ssthen #include "validator/val_neg.h" 662308e98cSsthen #ifdef CLIENT_SUBNET 672308e98cSsthen #include "edns-subnet/subnetmod.h" 682308e98cSsthen #endif 69f6b99bafSsthen #ifdef HAVE_SSL 70f6b99bafSsthen #include <openssl/ssl.h> 71f6b99bafSsthen #endif 72933707f3Ssthen 7345872187Ssthen /** How long to wait for threads to transmit statistics, in msec. */ 7445872187Ssthen #define STATS_THREAD_WAIT 60000 7545872187Ssthen 76933707f3Ssthen /** add timers and the values do not overflow or become negative */ 77933707f3Ssthen static void 782be9e038Ssthen stats_timeval_add(long long* d_sec, long long* d_usec, long long add_sec, long long add_usec) 79933707f3Ssthen { 80933707f3Ssthen #ifndef S_SPLINT_S 812be9e038Ssthen (*d_sec) += add_sec; 822be9e038Ssthen (*d_usec) += add_usec; 838240c1b9Ssthen if((*d_usec) >= 1000000) { 842be9e038Ssthen (*d_usec) -= 1000000; 852be9e038Ssthen (*d_sec)++; 86933707f3Ssthen } 87933707f3Ssthen #endif 88933707f3Ssthen } 89933707f3Ssthen 902be9e038Ssthen void server_stats_init(struct ub_server_stats* stats, struct config_file* cfg) 91933707f3Ssthen { 92933707f3Ssthen memset(stats, 0, sizeof(*stats)); 93933707f3Ssthen stats->extended = cfg->stat_extended; 94933707f3Ssthen } 95933707f3Ssthen 962be9e038Ssthen void server_stats_querymiss(struct ub_server_stats* stats, struct worker* worker) 97933707f3Ssthen { 98933707f3Ssthen stats->num_queries_missed_cache++; 99933707f3Ssthen stats->sum_query_list_size += worker->env.mesh->all.count; 1002be9e038Ssthen if((long long)worker->env.mesh->all.count > stats->max_query_list_size) 1012be9e038Ssthen stats->max_query_list_size = (long long)worker->env.mesh->all.count; 102933707f3Ssthen } 103933707f3Ssthen 1042be9e038Ssthen void server_stats_prefetch(struct ub_server_stats* stats, struct worker* worker) 105933707f3Ssthen { 106933707f3Ssthen stats->num_queries_prefetch++; 107933707f3Ssthen /* changes the query list size so account that, like a querymiss */ 108933707f3Ssthen stats->sum_query_list_size += worker->env.mesh->all.count; 1092be9e038Ssthen if((long long)worker->env.mesh->all.count > stats->max_query_list_size) 1102be9e038Ssthen stats->max_query_list_size = (long long)worker->env.mesh->all.count; 111933707f3Ssthen } 112933707f3Ssthen 1132be9e038Ssthen void server_stats_log(struct ub_server_stats* stats, struct worker* worker, 114933707f3Ssthen int threadnum) 115933707f3Ssthen { 116933707f3Ssthen log_info("server stats for thread %d: %u queries, " 11777079be7Ssthen "%u answers from cache, %u recursions, %u prefetch, %u rejected by " 11877079be7Ssthen "ip ratelimiting", 119933707f3Ssthen threadnum, (unsigned)stats->num_queries, 120933707f3Ssthen (unsigned)(stats->num_queries - 121933707f3Ssthen stats->num_queries_missed_cache), 122933707f3Ssthen (unsigned)stats->num_queries_missed_cache, 12377079be7Ssthen (unsigned)stats->num_queries_prefetch, 12477079be7Ssthen (unsigned)stats->num_queries_ip_ratelimited); 125933707f3Ssthen log_info("server stats for thread %d: requestlist max %u avg %g " 126933707f3Ssthen "exceeded %u jostled %u", threadnum, 127933707f3Ssthen (unsigned)stats->max_query_list_size, 128933707f3Ssthen (stats->num_queries_missed_cache+stats->num_queries_prefetch)? 129933707f3Ssthen (double)stats->sum_query_list_size/ 1302be9e038Ssthen (double)(stats->num_queries_missed_cache+ 131933707f3Ssthen stats->num_queries_prefetch) : 0.0, 132933707f3Ssthen (unsigned)worker->env.mesh->stats_dropped, 133933707f3Ssthen (unsigned)worker->env.mesh->stats_jostled); 134933707f3Ssthen } 135933707f3Ssthen 1362308e98cSsthen 1372308e98cSsthen #ifdef CLIENT_SUBNET 1382308e98cSsthen /** Set the EDNS Subnet stats. */ 1392308e98cSsthen static void 1402308e98cSsthen set_subnet_stats(struct worker* worker, struct ub_server_stats* svr, 1412308e98cSsthen int reset) 1422308e98cSsthen { 143e21c60efSsthen int m = modstack_find(&worker->env.mesh->mods, "subnetcache"); 1442308e98cSsthen struct subnet_env* sne; 1452308e98cSsthen if(m == -1) 1462308e98cSsthen return; 1472308e98cSsthen sne = (struct subnet_env*)worker->env.modinfo[m]; 1482308e98cSsthen if(reset && !worker->env.cfg->stat_cumulative) { 1492308e98cSsthen lock_rw_wrlock(&sne->biglock); 1502308e98cSsthen } else { 1512308e98cSsthen lock_rw_rdlock(&sne->biglock); 1522308e98cSsthen } 1532308e98cSsthen svr->num_query_subnet = (long long)(sne->num_msg_nocache + sne->num_msg_cache); 1542308e98cSsthen svr->num_query_subnet_cache = (long long)sne->num_msg_cache; 1552308e98cSsthen if(reset && !worker->env.cfg->stat_cumulative) { 1562308e98cSsthen sne->num_msg_cache = 0; 1572308e98cSsthen sne->num_msg_nocache = 0; 1582308e98cSsthen } 1592308e98cSsthen lock_rw_unlock(&sne->biglock); 1602308e98cSsthen } 1612308e98cSsthen #endif /* CLIENT_SUBNET */ 1622308e98cSsthen 16320237c55Ssthen /** Set the neg cache stats. */ 16420237c55Ssthen static void 16520237c55Ssthen set_neg_cache_stats(struct worker* worker, struct ub_server_stats* svr, 16620237c55Ssthen int reset) 16720237c55Ssthen { 16820237c55Ssthen int m = modstack_find(&worker->env.mesh->mods, "validator"); 16920237c55Ssthen struct val_env* ve; 17020237c55Ssthen struct val_neg_cache* neg; 17120237c55Ssthen if(m == -1) 17220237c55Ssthen return; 17320237c55Ssthen ve = (struct val_env*)worker->env.modinfo[m]; 17420237c55Ssthen if(!ve->neg_cache) 17520237c55Ssthen return; 17620237c55Ssthen neg = ve->neg_cache; 17720237c55Ssthen lock_basic_lock(&neg->lock); 17820237c55Ssthen svr->num_neg_cache_noerror = (long long)neg->num_neg_cache_noerror; 17920237c55Ssthen svr->num_neg_cache_nxdomain = (long long)neg->num_neg_cache_nxdomain; 18020237c55Ssthen if(reset && !worker->env.cfg->stat_cumulative) { 18120237c55Ssthen neg->num_neg_cache_noerror = 0; 18220237c55Ssthen neg->num_neg_cache_nxdomain = 0; 18320237c55Ssthen } 18420237c55Ssthen lock_basic_unlock(&neg->lock); 18520237c55Ssthen } 18620237c55Ssthen 187933707f3Ssthen /** get rrsets bogus number from validator */ 188933707f3Ssthen static size_t 1897191de28Ssthen get_rrset_bogus(struct worker* worker, int reset) 190933707f3Ssthen { 191933707f3Ssthen int m = modstack_find(&worker->env.mesh->mods, "validator"); 192933707f3Ssthen struct val_env* ve; 193933707f3Ssthen size_t r; 194933707f3Ssthen if(m == -1) 195933707f3Ssthen return 0; 196933707f3Ssthen ve = (struct val_env*)worker->env.modinfo[m]; 197933707f3Ssthen lock_basic_lock(&ve->bogus_lock); 198933707f3Ssthen r = ve->num_rrset_bogus; 1997191de28Ssthen if(reset && !worker->env.cfg->stat_cumulative) 200933707f3Ssthen ve->num_rrset_bogus = 0; 201933707f3Ssthen lock_basic_unlock(&ve->bogus_lock); 202933707f3Ssthen return r; 203933707f3Ssthen } 204933707f3Ssthen 2057191de28Ssthen /** get number of ratelimited queries from iterator */ 2067191de28Ssthen static size_t 2077191de28Ssthen get_queries_ratelimit(struct worker* worker, int reset) 2087191de28Ssthen { 2097191de28Ssthen int m = modstack_find(&worker->env.mesh->mods, "iterator"); 2107191de28Ssthen struct iter_env* ie; 2117191de28Ssthen size_t r; 2127191de28Ssthen if(m == -1) 2137191de28Ssthen return 0; 2147191de28Ssthen ie = (struct iter_env*)worker->env.modinfo[m]; 2157191de28Ssthen lock_basic_lock(&ie->queries_ratelimit_lock); 2167191de28Ssthen r = ie->num_queries_ratelimited; 2177191de28Ssthen if(reset && !worker->env.cfg->stat_cumulative) 2187191de28Ssthen ie->num_queries_ratelimited = 0; 2197191de28Ssthen lock_basic_unlock(&ie->queries_ratelimit_lock); 2207191de28Ssthen return r; 2217191de28Ssthen } 2227191de28Ssthen 2237191de28Ssthen #ifdef USE_DNSCRYPT 2247191de28Ssthen /** get the number of shared secret cache miss */ 2257191de28Ssthen static size_t 2267191de28Ssthen get_dnscrypt_cache_miss(struct worker* worker, int reset) 2277191de28Ssthen { 2287191de28Ssthen size_t r; 2297191de28Ssthen struct dnsc_env* de = worker->daemon->dnscenv; 2307191de28Ssthen if(!de) return 0; 2317191de28Ssthen 2327191de28Ssthen lock_basic_lock(&de->shared_secrets_cache_lock); 2337191de28Ssthen r = de->num_query_dnscrypt_secret_missed_cache; 2347191de28Ssthen if(reset && !worker->env.cfg->stat_cumulative) 2357191de28Ssthen de->num_query_dnscrypt_secret_missed_cache = 0; 2367191de28Ssthen lock_basic_unlock(&de->shared_secrets_cache_lock); 2377191de28Ssthen return r; 2387191de28Ssthen } 239bdfc4d55Sflorian 240bdfc4d55Sflorian /** get the number of replayed queries */ 241bdfc4d55Sflorian static size_t 242bdfc4d55Sflorian get_dnscrypt_replay(struct worker* worker, int reset) 243bdfc4d55Sflorian { 244bdfc4d55Sflorian size_t r; 245bdfc4d55Sflorian struct dnsc_env* de = worker->daemon->dnscenv; 246bdfc4d55Sflorian 247bdfc4d55Sflorian lock_basic_lock(&de->nonces_cache_lock); 248bdfc4d55Sflorian r = de->num_query_dnscrypt_replay; 249bdfc4d55Sflorian if(reset && !worker->env.cfg->stat_cumulative) 250bdfc4d55Sflorian de->num_query_dnscrypt_replay = 0; 251bdfc4d55Sflorian lock_basic_unlock(&de->nonces_cache_lock); 252bdfc4d55Sflorian return r; 253bdfc4d55Sflorian } 2547191de28Ssthen #endif /* USE_DNSCRYPT */ 2557191de28Ssthen 256933707f3Ssthen void 2572be9e038Ssthen server_stats_compile(struct worker* worker, struct ub_stats_info* s, int reset) 258933707f3Ssthen { 259933707f3Ssthen int i; 260fdfb4ba6Ssthen struct listen_list* lp; 261933707f3Ssthen 262933707f3Ssthen s->svr = worker->stats; 2632be9e038Ssthen s->mesh_num_states = (long long)worker->env.mesh->all.count; 2642be9e038Ssthen s->mesh_num_reply_states = (long long)worker->env.mesh->num_reply_states; 2652be9e038Ssthen s->mesh_jostled = (long long)worker->env.mesh->stats_jostled; 2662be9e038Ssthen s->mesh_dropped = (long long)worker->env.mesh->stats_dropped; 2672be9e038Ssthen s->mesh_replies_sent = (long long)worker->env.mesh->replies_sent; 2682be9e038Ssthen s->mesh_replies_sum_wait_sec = (long long)worker->env.mesh->replies_sum_wait.tv_sec; 2692be9e038Ssthen s->mesh_replies_sum_wait_usec = (long long)worker->env.mesh->replies_sum_wait.tv_usec; 270933707f3Ssthen s->mesh_time_median = timehist_quartile(worker->env.mesh->histogram, 271933707f3Ssthen 0.50); 272933707f3Ssthen 273933707f3Ssthen /* add in the values from the mesh */ 2742be9e038Ssthen s->svr.ans_secure += (long long)worker->env.mesh->ans_secure; 2752be9e038Ssthen s->svr.ans_bogus += (long long)worker->env.mesh->ans_bogus; 2762be9e038Ssthen s->svr.ans_rcode_nodata += (long long)worker->env.mesh->ans_nodata; 2772c144df0Ssthen s->svr.ans_expired += (long long)worker->env.mesh->ans_expired; 278eaf2578eSsthen for(i=0; i<UB_STATS_RCODE_NUM; i++) 2792be9e038Ssthen s->svr.ans_rcode[i] += (long long)worker->env.mesh->ans_rcode[i]; 280eaf2578eSsthen for(i=0; i<UB_STATS_RPZ_ACTION_NUM; i++) 281eaf2578eSsthen s->svr.rpz_action[i] += (long long)worker->env.mesh->rpz_action[i]; 282933707f3Ssthen timehist_export(worker->env.mesh->histogram, s->svr.hist, 283933707f3Ssthen NUM_BUCKETS_HIST); 284933707f3Ssthen /* values from outside network */ 2852be9e038Ssthen s->svr.unwanted_replies = (long long)worker->back->unwanted_replies; 2862be9e038Ssthen s->svr.qtcp_outgoing = (long long)worker->back->num_tcp_outgoing; 287d1e2768aSsthen s->svr.qudp_outgoing = (long long)worker->back->num_udp_outgoing; 288933707f3Ssthen 289933707f3Ssthen /* get and reset validator rrset bogus number */ 2907191de28Ssthen s->svr.rrset_bogus = (long long)get_rrset_bogus(worker, reset); 2917191de28Ssthen 2927191de28Ssthen /* get and reset iterator query ratelimit number */ 2937191de28Ssthen s->svr.queries_ratelimited = (long long)get_queries_ratelimit(worker, reset); 294933707f3Ssthen 29598f3ca02Sbrad /* get cache sizes */ 2968b7325afSsthen get_slabhash_stats(worker->env.msg_cache, 2978b7325afSsthen &s->svr.msg_cache_count, &s->svr.msg_cache_max_collisions); 2988b7325afSsthen get_slabhash_stats(&worker->env.rrset_cache->table, 2998b7325afSsthen &s->svr.rrset_cache_count, &s->svr.rrset_cache_max_collisions); 3002be9e038Ssthen s->svr.infra_cache_count = (long long)count_slabhash_entries(worker->env.infra_cache->hosts); 30198f3ca02Sbrad if(worker->env.key_cache) 3022be9e038Ssthen s->svr.key_cache_count = (long long)count_slabhash_entries(worker->env.key_cache->slab); 30398f3ca02Sbrad else s->svr.key_cache_count = 0; 30498f3ca02Sbrad 3057191de28Ssthen #ifdef USE_DNSCRYPT 3067191de28Ssthen if(worker->daemon->dnscenv) { 3077191de28Ssthen s->svr.num_query_dnscrypt_secret_missed_cache = 3087191de28Ssthen (long long)get_dnscrypt_cache_miss(worker, reset); 3097191de28Ssthen s->svr.shared_secret_cache_count = (long long)count_slabhash_entries( 3107191de28Ssthen worker->daemon->dnscenv->shared_secrets_cache); 311bdfc4d55Sflorian s->svr.nonce_cache_count = (long long)count_slabhash_entries( 312bdfc4d55Sflorian worker->daemon->dnscenv->nonces_cache); 313bdfc4d55Sflorian s->svr.num_query_dnscrypt_replay = 314bdfc4d55Sflorian (long long)get_dnscrypt_replay(worker, reset); 3157191de28Ssthen } else { 3167191de28Ssthen s->svr.num_query_dnscrypt_secret_missed_cache = 0; 3177191de28Ssthen s->svr.shared_secret_cache_count = 0; 318bdfc4d55Sflorian s->svr.nonce_cache_count = 0; 319bdfc4d55Sflorian s->svr.num_query_dnscrypt_replay = 0; 3207191de28Ssthen } 3217191de28Ssthen #else 3227191de28Ssthen s->svr.num_query_dnscrypt_secret_missed_cache = 0; 3237191de28Ssthen s->svr.shared_secret_cache_count = 0; 324bdfc4d55Sflorian s->svr.nonce_cache_count = 0; 325bdfc4d55Sflorian s->svr.num_query_dnscrypt_replay = 0; 3267191de28Ssthen #endif /* USE_DNSCRYPT */ 32720237c55Ssthen if(worker->env.auth_zones) { 32820237c55Ssthen if(reset && !worker->env.cfg->stat_cumulative) { 32920237c55Ssthen lock_rw_wrlock(&worker->env.auth_zones->lock); 33020237c55Ssthen } else { 33120237c55Ssthen lock_rw_rdlock(&worker->env.auth_zones->lock); 33220237c55Ssthen } 33320237c55Ssthen s->svr.num_query_authzone_up = (long long)worker->env. 33420237c55Ssthen auth_zones->num_query_up; 33520237c55Ssthen s->svr.num_query_authzone_down = (long long)worker->env. 33620237c55Ssthen auth_zones->num_query_down; 33720237c55Ssthen if(reset && !worker->env.cfg->stat_cumulative) { 33820237c55Ssthen worker->env.auth_zones->num_query_up = 0; 33920237c55Ssthen worker->env.auth_zones->num_query_down = 0; 34020237c55Ssthen } 34120237c55Ssthen lock_rw_unlock(&worker->env.auth_zones->lock); 34220237c55Ssthen } 343f6b99bafSsthen s->svr.mem_stream_wait = 344f6b99bafSsthen (long long)tcp_req_info_get_stream_buffer_size(); 3452c144df0Ssthen s->svr.mem_http2_query_buffer = 3462c144df0Ssthen (long long)http2_get_query_buffer_size(); 3472c144df0Ssthen s->svr.mem_http2_response_buffer = 3482c144df0Ssthen (long long)http2_get_response_buffer_size(); 34920237c55Ssthen 35020237c55Ssthen /* Set neg cache usage numbers */ 35120237c55Ssthen set_neg_cache_stats(worker, &s->svr, reset); 3522308e98cSsthen #ifdef CLIENT_SUBNET 3532308e98cSsthen /* EDNS Subnet usage numbers */ 3542308e98cSsthen set_subnet_stats(worker, &s->svr, reset); 3552308e98cSsthen #else 3562308e98cSsthen s->svr.num_query_subnet = 0; 3572308e98cSsthen s->svr.num_query_subnet_cache = 0; 3582308e98cSsthen #endif 3598b7325afSsthen #ifdef USE_CACHEDB 3608b7325afSsthen s->svr.num_query_cachedb = (long long)worker->env.mesh->ans_cachedb; 3618b7325afSsthen #else 3628b7325afSsthen s->svr.num_query_cachedb = 0; 3638b7325afSsthen #endif 3647191de28Ssthen 365fdfb4ba6Ssthen /* get tcp accept usage */ 366fdfb4ba6Ssthen s->svr.tcp_accept_usage = 0; 367fdfb4ba6Ssthen for(lp = worker->front->cps; lp; lp = lp->next) { 368fdfb4ba6Ssthen if(lp->com->type == comm_tcp_accept) 3692be9e038Ssthen s->svr.tcp_accept_usage += (long long)lp->com->cur_tcp_count; 370fdfb4ba6Ssthen } 371fdfb4ba6Ssthen 372933707f3Ssthen if(reset && !worker->env.cfg->stat_cumulative) { 373933707f3Ssthen worker_stats_clear(worker); 374933707f3Ssthen } 375933707f3Ssthen } 376933707f3Ssthen 377933707f3Ssthen void server_stats_obtain(struct worker* worker, struct worker* who, 3782be9e038Ssthen struct ub_stats_info* s, int reset) 379933707f3Ssthen { 380933707f3Ssthen uint8_t *reply = NULL; 381933707f3Ssthen uint32_t len = 0; 382933707f3Ssthen if(worker == who) { 383933707f3Ssthen /* just fill it in */ 384933707f3Ssthen server_stats_compile(worker, s, reset); 385933707f3Ssthen return; 386933707f3Ssthen } 387933707f3Ssthen /* communicate over tube */ 388933707f3Ssthen verbose(VERB_ALGO, "write stats cmd"); 389933707f3Ssthen if(reset) 390933707f3Ssthen worker_send_cmd(who, worker_cmd_stats); 391933707f3Ssthen else worker_send_cmd(who, worker_cmd_stats_noreset); 392933707f3Ssthen verbose(VERB_ALGO, "wait for stats reply"); 39345872187Ssthen if(tube_wait_timeout(worker->cmd, STATS_THREAD_WAIT) == 0) { 394*98bc733bSsthen #if defined(HAVE_PTHREAD) && defined(SIZEOF_PTHREAD_T) && defined(SIZEOF_UNSIGNED_LONG) 395*98bc733bSsthen # if SIZEOF_PTHREAD_T == SIZEOF_UNSIGNED_LONG 396*98bc733bSsthen unsigned long pthid = 0; 397*98bc733bSsthen if(verbosity >= VERB_OPS) 398*98bc733bSsthen memcpy(&pthid, &who->thr_id, sizeof(unsigned long)); 399*98bc733bSsthen # endif 400*98bc733bSsthen #endif 40145872187Ssthen verbose(VERB_OPS, "no response from thread %d" 40245872187Ssthen #ifdef HAVE_GETTID 40345872187Ssthen " LWP %u" 40445872187Ssthen #endif 40545872187Ssthen #if defined(HAVE_PTHREAD) && defined(SIZEOF_PTHREAD_T) && defined(SIZEOF_UNSIGNED_LONG) 40645872187Ssthen # if SIZEOF_PTHREAD_T == SIZEOF_UNSIGNED_LONG 40745872187Ssthen " pthread 0x%lx" 40845872187Ssthen # endif 40945872187Ssthen #endif 41045872187Ssthen , 41145872187Ssthen who->thread_num 41245872187Ssthen #ifdef HAVE_GETTID 41345872187Ssthen , (unsigned)who->thread_tid 41445872187Ssthen #endif 41545872187Ssthen #if defined(HAVE_PTHREAD) && defined(SIZEOF_PTHREAD_T) && defined(SIZEOF_UNSIGNED_LONG) 41645872187Ssthen # if SIZEOF_PTHREAD_T == SIZEOF_UNSIGNED_LONG 417*98bc733bSsthen , pthid 41845872187Ssthen # endif 41945872187Ssthen #endif 42045872187Ssthen ); 42145872187Ssthen } 422933707f3Ssthen if(!tube_read_msg(worker->cmd, &reply, &len, 0)) 423933707f3Ssthen fatal_exit("failed to read stats over cmd channel"); 424933707f3Ssthen if(len != (uint32_t)sizeof(*s)) 425933707f3Ssthen fatal_exit("stats on cmd channel wrong length %d %d", 426933707f3Ssthen (int)len, (int)sizeof(*s)); 427933707f3Ssthen memcpy(s, reply, (size_t)len); 428933707f3Ssthen free(reply); 429933707f3Ssthen } 430933707f3Ssthen 431933707f3Ssthen void server_stats_reply(struct worker* worker, int reset) 432933707f3Ssthen { 4332be9e038Ssthen struct ub_stats_info s; 434933707f3Ssthen server_stats_compile(worker, &s, reset); 435933707f3Ssthen verbose(VERB_ALGO, "write stats replymsg"); 436933707f3Ssthen if(!tube_write_msg(worker->daemon->workers[0]->cmd, 437933707f3Ssthen (uint8_t*)&s, sizeof(s), 0)) 438933707f3Ssthen fatal_exit("could not write stat values over cmd channel"); 439933707f3Ssthen } 440933707f3Ssthen 4412be9e038Ssthen void server_stats_add(struct ub_stats_info* total, struct ub_stats_info* a) 442933707f3Ssthen { 443933707f3Ssthen total->svr.num_queries += a->svr.num_queries; 44477079be7Ssthen total->svr.num_queries_ip_ratelimited += a->svr.num_queries_ip_ratelimited; 4458b7325afSsthen total->svr.num_queries_cookie_valid += a->svr.num_queries_cookie_valid; 4468b7325afSsthen total->svr.num_queries_cookie_client += a->svr.num_queries_cookie_client; 4478b7325afSsthen total->svr.num_queries_cookie_invalid += a->svr.num_queries_cookie_invalid; 448933707f3Ssthen total->svr.num_queries_missed_cache += a->svr.num_queries_missed_cache; 449933707f3Ssthen total->svr.num_queries_prefetch += a->svr.num_queries_prefetch; 4508b7325afSsthen total->svr.num_queries_timed_out += a->svr.num_queries_timed_out; 4518b7325afSsthen if (total->svr.max_query_time_us < a->svr.max_query_time_us) 4528b7325afSsthen total->svr.max_query_time_us = a->svr.max_query_time_us; 453933707f3Ssthen total->svr.sum_query_list_size += a->svr.sum_query_list_size; 454eaf2578eSsthen total->svr.ans_expired += a->svr.ans_expired; 4552be9e038Ssthen #ifdef USE_DNSCRYPT 4562be9e038Ssthen total->svr.num_query_dnscrypt_crypted += a->svr.num_query_dnscrypt_crypted; 4572be9e038Ssthen total->svr.num_query_dnscrypt_cert += a->svr.num_query_dnscrypt_cert; 4582be9e038Ssthen total->svr.num_query_dnscrypt_cleartext += \ 4592be9e038Ssthen a->svr.num_query_dnscrypt_cleartext; 4602be9e038Ssthen total->svr.num_query_dnscrypt_crypted_malformed += \ 4612be9e038Ssthen a->svr.num_query_dnscrypt_crypted_malformed; 4627191de28Ssthen #endif /* USE_DNSCRYPT */ 463933707f3Ssthen /* the max size reached is upped to higher of both */ 464933707f3Ssthen if(a->svr.max_query_list_size > total->svr.max_query_list_size) 465933707f3Ssthen total->svr.max_query_list_size = a->svr.max_query_list_size; 466933707f3Ssthen 467933707f3Ssthen if(a->svr.extended) { 468933707f3Ssthen int i; 469933707f3Ssthen total->svr.qtype_big += a->svr.qtype_big; 470933707f3Ssthen total->svr.qclass_big += a->svr.qclass_big; 471933707f3Ssthen total->svr.qtcp += a->svr.qtcp; 47298f3ca02Sbrad total->svr.qtcp_outgoing += a->svr.qtcp_outgoing; 473d1e2768aSsthen total->svr.qudp_outgoing += a->svr.qudp_outgoing; 4742308e98cSsthen total->svr.qtls += a->svr.qtls; 475f6b99bafSsthen total->svr.qtls_resume += a->svr.qtls_resume; 4762c144df0Ssthen total->svr.qhttps += a->svr.qhttps; 477933707f3Ssthen total->svr.qipv6 += a->svr.qipv6; 478933707f3Ssthen total->svr.qbit_QR += a->svr.qbit_QR; 479933707f3Ssthen total->svr.qbit_AA += a->svr.qbit_AA; 480933707f3Ssthen total->svr.qbit_TC += a->svr.qbit_TC; 481933707f3Ssthen total->svr.qbit_RD += a->svr.qbit_RD; 482933707f3Ssthen total->svr.qbit_RA += a->svr.qbit_RA; 483933707f3Ssthen total->svr.qbit_Z += a->svr.qbit_Z; 484933707f3Ssthen total->svr.qbit_AD += a->svr.qbit_AD; 485933707f3Ssthen total->svr.qbit_CD += a->svr.qbit_CD; 486933707f3Ssthen total->svr.qEDNS += a->svr.qEDNS; 487933707f3Ssthen total->svr.qEDNS_DO += a->svr.qEDNS_DO; 488933707f3Ssthen total->svr.ans_rcode_nodata += a->svr.ans_rcode_nodata; 489933707f3Ssthen total->svr.ans_secure += a->svr.ans_secure; 490933707f3Ssthen total->svr.ans_bogus += a->svr.ans_bogus; 491933707f3Ssthen total->svr.unwanted_replies += a->svr.unwanted_replies; 492933707f3Ssthen total->svr.unwanted_queries += a->svr.unwanted_queries; 493fdfb4ba6Ssthen total->svr.tcp_accept_usage += a->svr.tcp_accept_usage; 4948b7325afSsthen #ifdef USE_CACHEDB 4958b7325afSsthen total->svr.num_query_cachedb += a->svr.num_query_cachedb; 4968b7325afSsthen #endif 4972be9e038Ssthen for(i=0; i<UB_STATS_QTYPE_NUM; i++) 498933707f3Ssthen total->svr.qtype[i] += a->svr.qtype[i]; 4992be9e038Ssthen for(i=0; i<UB_STATS_QCLASS_NUM; i++) 500933707f3Ssthen total->svr.qclass[i] += a->svr.qclass[i]; 5012be9e038Ssthen for(i=0; i<UB_STATS_OPCODE_NUM; i++) 502933707f3Ssthen total->svr.qopcode[i] += a->svr.qopcode[i]; 5032be9e038Ssthen for(i=0; i<UB_STATS_RCODE_NUM; i++) 504933707f3Ssthen total->svr.ans_rcode[i] += a->svr.ans_rcode[i]; 505933707f3Ssthen for(i=0; i<NUM_BUCKETS_HIST; i++) 506933707f3Ssthen total->svr.hist[i] += a->svr.hist[i]; 507eaf2578eSsthen for(i=0; i<UB_STATS_RPZ_ACTION_NUM; i++) 508eaf2578eSsthen total->svr.rpz_action[i] += a->svr.rpz_action[i]; 509933707f3Ssthen } 510933707f3Ssthen 511933707f3Ssthen total->mesh_num_states += a->mesh_num_states; 512933707f3Ssthen total->mesh_num_reply_states += a->mesh_num_reply_states; 513933707f3Ssthen total->mesh_jostled += a->mesh_jostled; 514933707f3Ssthen total->mesh_dropped += a->mesh_dropped; 515933707f3Ssthen total->mesh_replies_sent += a->mesh_replies_sent; 5162be9e038Ssthen 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); 517933707f3Ssthen /* the medians are averaged together, this is not as accurate as 518933707f3Ssthen * taking the median over all of the data, but is good and fast 519933707f3Ssthen * added up here, division later*/ 520933707f3Ssthen total->mesh_time_median += a->mesh_time_median; 521933707f3Ssthen } 522933707f3Ssthen 5232be9e038Ssthen void server_stats_insquery(struct ub_server_stats* stats, struct comm_point* c, 524933707f3Ssthen uint16_t qtype, uint16_t qclass, struct edns_data* edns, 525933707f3Ssthen struct comm_reply* repinfo) 526933707f3Ssthen { 5275d76a658Ssthen uint16_t flags = sldns_buffer_read_u16_at(c->buffer, 2); 5282be9e038Ssthen if(qtype < UB_STATS_QTYPE_NUM) 529933707f3Ssthen stats->qtype[qtype]++; 530933707f3Ssthen else stats->qtype_big++; 5312be9e038Ssthen if(qclass < UB_STATS_QCLASS_NUM) 532933707f3Ssthen stats->qclass[qclass]++; 533933707f3Ssthen else stats->qclass_big++; 5345d76a658Ssthen stats->qopcode[ LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) ]++; 5352308e98cSsthen if(c->type != comm_udp) { 536933707f3Ssthen stats->qtcp++; 537f6b99bafSsthen if(c->ssl != NULL) { 5382308e98cSsthen stats->qtls++; 539f6b99bafSsthen #ifdef HAVE_SSL 540f6b99bafSsthen if(SSL_session_reused(c->ssl)) 541f6b99bafSsthen stats->qtls_resume++; 542f6b99bafSsthen #endif 5432c144df0Ssthen if(c->type == comm_http) 5442c144df0Ssthen stats->qhttps++; 545f6b99bafSsthen } 5462308e98cSsthen } 54745872187Ssthen if(repinfo && addr_is_ip6(&repinfo->remote_addr, repinfo->remote_addrlen)) 548933707f3Ssthen stats->qipv6++; 549933707f3Ssthen if( (flags&BIT_QR) ) 550933707f3Ssthen stats->qbit_QR++; 551933707f3Ssthen if( (flags&BIT_AA) ) 552933707f3Ssthen stats->qbit_AA++; 553933707f3Ssthen if( (flags&BIT_TC) ) 554933707f3Ssthen stats->qbit_TC++; 555933707f3Ssthen if( (flags&BIT_RD) ) 556933707f3Ssthen stats->qbit_RD++; 557933707f3Ssthen if( (flags&BIT_RA) ) 558933707f3Ssthen stats->qbit_RA++; 559933707f3Ssthen if( (flags&BIT_Z) ) 560933707f3Ssthen stats->qbit_Z++; 561933707f3Ssthen if( (flags&BIT_AD) ) 562933707f3Ssthen stats->qbit_AD++; 563933707f3Ssthen if( (flags&BIT_CD) ) 564933707f3Ssthen stats->qbit_CD++; 565933707f3Ssthen if(edns->edns_present) { 566933707f3Ssthen stats->qEDNS++; 567933707f3Ssthen if( (edns->bits & EDNS_DO) ) 568933707f3Ssthen stats->qEDNS_DO++; 569933707f3Ssthen } 570933707f3Ssthen } 571933707f3Ssthen 5722be9e038Ssthen void server_stats_insrcode(struct ub_server_stats* stats, sldns_buffer* buf) 573933707f3Ssthen { 5745d76a658Ssthen if(stats->extended && sldns_buffer_limit(buf) != 0) { 5755d76a658Ssthen int r = (int)LDNS_RCODE_WIRE( sldns_buffer_begin(buf) ); 576933707f3Ssthen stats->ans_rcode[r] ++; 5775d76a658Ssthen if(r == 0 && LDNS_ANCOUNT( sldns_buffer_begin(buf) ) == 0) 578933707f3Ssthen stats->ans_rcode_nodata ++; 579933707f3Ssthen } 580933707f3Ssthen } 5818b7325afSsthen 5828b7325afSsthen void server_stats_downstream_cookie(struct ub_server_stats* stats, 5838b7325afSsthen struct edns_data* edns) 5848b7325afSsthen { 5858b7325afSsthen if(!(edns->edns_present && edns->cookie_present)) return; 5868b7325afSsthen if(edns->cookie_valid) { 5878b7325afSsthen stats->num_queries_cookie_valid++; 5888b7325afSsthen } else if(edns->cookie_client) { 5898b7325afSsthen stats->num_queries_cookie_client++; 5908b7325afSsthen } else { 5918b7325afSsthen stats->num_queries_cookie_invalid++; 5928b7325afSsthen } 5938b7325afSsthen } 594