From: Otto Date: Tue, 16 Feb 2021 13:00:29 +0000 (+0100) Subject: Avoid some uint64_t vs double confusion and make sure we write stats X-Git-Tag: dnsdist-1.6.0-alpha2~18^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7ab9f171e827c7bce7faca4cfd5b4d46c6f45009;p=thirdparty%2Fpdns.git Avoid some uint64_t vs double confusion and make sure we write stats data in an atomic way. --- diff --git a/pdns/misc.hh b/pdns/misc.hh index 7998623e24..a6c926da70 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -312,6 +312,10 @@ inline float makeFloat(const struct timeval& tv) { return tv.tv_sec + tv.tv_usec/1000000.0f; } +inline double makeDouble(const struct timeval& tv) +{ + return tv.tv_sec + tv.tv_usec/1000000.0; +} inline bool operator<(const struct timeval& lhs, const struct timeval& rhs) { diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 440e898167..c330997aa5 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -2142,7 +2142,7 @@ static void startDoResolve(void *p) finishTCPReply(dc, hadError, true); } - float spent=makeFloat(sr.getNow()-dc->d_now); + double spent = makeDouble(sr.getNow() - dc->d_now); if (!g_quiet) { g_log<getTid()<<"/"<numProcesses()<<"] answer to "<<(dc->d_mdp.d_header.rd?"":"non-rd ")<<"question '"<d_mdp.d_qname<<"|"<d_mdp.d_qtype); g_log<<"': "<ancount)<<" answers, "<arcount)<<" additional, took "<cacheHits++; } - if(spent < 0.001) + if (spent < 0.001) g_stats.answers0_1++; - else if(spent < 0.010) + else if(spent < 0.01) g_stats.answers1_10++; else if(spent < 0.1) g_stats.answers10_100++; @@ -2173,12 +2173,12 @@ static void startDoResolve(void *p) else g_stats.answersSlow++; - uint64_t newLat=(uint64_t)(spent*static_cast(1000000)); - newLat = min(newLat,(uint64_t)(((uint64_t) g_networkTimeoutMsec)*1000)); // outliers of several minutes exist.. - g_stats.avgLatencyUsec=(1-1.0/g_latencyStatSize)*g_stats.avgLatencyUsec + (float)newLat/g_latencyStatSize; + double newLat = spent * 1000000.0; + newLat = min(newLat, g_networkTimeoutMsec * 1000.0); // outliers of several minutes exist.. + g_stats.avgLatencyUsec = (1.0 - 1.0 / g_latencyStatSize) * g_stats.avgLatencyUsec + newLat / g_latencyStatSize; // no worries, we do this for packet cache hits elsewhere - auto ourtime = 1000.0*spent-sr.d_totUsec/1000.0; // in msec + double ourtime = 1000.0 * spent - sr.d_totUsec / 1000.0; // in msec if(ourtime < 1) g_stats.ourtime0_1++; else if(ourtime < 2) @@ -2192,12 +2192,12 @@ static void startDoResolve(void *p) else if(ourtime < 32) g_stats.ourtime16_32++; else { - // cerr<<"SLOW: "< "<d_mdp.d_qname<<"|"<d_mdp.d_qtype)<= 0.0) { - newLat=ourtime*1000; // usec - g_stats.avgLatencyOursUsec=(1-1.0/g_latencyStatSize)*g_stats.avgLatencyOursUsec + (float)newLat/g_latencyStatSize; + + if (ourtime >= 0) { + newLat = ourtime * 1000.0; // usec + g_stats.avgLatencyOursUsec = (1.0 - 1.0 / g_latencyStatSize) * g_stats.avgLatencyOursUsec + newLat / g_latencyStatSize; } #ifdef NOD_ENABLED diff --git a/pdns/rec_channel_rec.cc b/pdns/rec_channel_rec.cc index 0bcc47238e..78a08b7c5f 100644 --- a/pdns/rec_channel_rec.cc +++ b/pdns/rec_channel_rec.cc @@ -1103,7 +1103,7 @@ static void registerAllStats1() addGetStat("qa-latency", doGetAvgLatencyUsec); - addGetStat("x-our-latency", []() { return g_stats.avgLatencyOursUsec; }); + addGetStat("x-our-latency", []() { return g_stats.avgLatencyOursUsec.load(); }); addGetStat("unexpected-packets", &g_stats.unexpectedCount); addGetStat("case-mismatches", &g_stats.caseMismatchCount); addGetStat("spoof-prevents", &g_stats.spoofCount); diff --git a/pdns/syncres.hh b/pdns/syncres.hh index abf4eaf8bf..7632e322c0 100644 --- a/pdns/syncres.hh +++ b/pdns/syncres.hh @@ -980,8 +980,8 @@ struct RecursorStats std::atomic auth4Answers0_1, auth4Answers1_10, auth4Answers10_100, auth4Answers100_1000, auth4AnswersSlow; std::atomic auth6Answers0_1, auth6Answers1_10, auth6Answers10_100, auth6Answers100_1000, auth6AnswersSlow; std::atomic ourtime0_1, ourtime1_2, ourtime2_4, ourtime4_8, ourtime8_16, ourtime16_32, ourtimeSlow; - double avgLatencyUsec{0}; - double avgLatencyOursUsec{0}; + std::atomic avgLatencyUsec; + std::atomic avgLatencyOursUsec; std::atomic qcounter; // not increased for unauth packets std::atomic ipv6qcounter; std::atomic tcpqcounter;