From: Otto Moerbeek Date: Thu, 13 Nov 2025 14:13:40 +0000 (+0100) Subject: rec: Tidy tcounter related code X-Git-Tag: rec-5.4.0-alpha1~60^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b09e15e50984e6ce7da499b388896619c5a03ca9;p=thirdparty%2Fpdns.git rec: Tidy tcounter related code Signed-off-by: Otto Moerbeek --- diff --git a/pdns/recursordist/rec-responsestats.cc b/pdns/recursordist/rec-responsestats.cc index ea283c3601..be468693f8 100644 --- a/pdns/recursordist/rec-responsestats.cc +++ b/pdns/recursordist/rec-responsestats.cc @@ -1,14 +1,27 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +/* + * This file is part of PowerDNS or dnsdist. + * Copyright -- PowerDNS.COM B.V. and its contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * In addition, for the avoidance of any doubt, permission is granted to + * link this program with OpenSSL and to (re)distribute the binaries + * produced as the result of such linking. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ #include "rec-responsestats.hh" -#include - -#include "namespaces.hh" -#include "logger.hh" - #include "dnsparser.hh" static auto sizeBounds() @@ -21,8 +34,8 @@ static auto sizeBounds() bounds.push_back(80); bounds.push_back(100); bounds.push_back(150); - for (uint64_t n = 200; n < 65000; n += 200) { - bounds.push_back(n); + for (uint64_t count = 200; count < 65000; count += 200) { + bounds.push_back(count); } return bounds; } @@ -30,12 +43,6 @@ static auto sizeBounds() RecResponseStats::RecResponseStats() : d_sizecounters("SizeCounters", sizeBounds()) { - for (auto& entry : d_qtypecounters) { - entry = 0; - } - for (auto& entry : d_rcodecounters) { - entry = 0; - } } RecResponseStats& RecResponseStats::operator+=(const RecResponseStats& rhs) @@ -50,9 +57,9 @@ RecResponseStats& RecResponseStats::operator+=(const RecResponseStats& rhs) return *this; } -map RecResponseStats::getQTypeResponseCounts() const +std::map RecResponseStats::getQTypeResponseCounts() const { - map ret; + std::map ret; for (size_t i = 0; i < d_qtypecounters.size(); ++i) { auto count = d_qtypecounters.at(i); if (count != 0) { @@ -62,9 +69,9 @@ map RecResponseStats::getQTypeResponseCounts() const return ret; } -map RecResponseStats::getSizeResponseCounts() const +std::map RecResponseStats::getSizeResponseCounts() const { - map ret; + std::map ret; for (const auto& sizecounter : d_sizecounters.getRawData()) { if (sizecounter.d_count > 0) { ret[sizecounter.d_boundary] = sizecounter.d_count; @@ -73,9 +80,9 @@ map RecResponseStats::getSizeResponseCounts() const return ret; } -map RecResponseStats::getRCodeResponseCounts() const +std::map RecResponseStats::getRCodeResponseCounts() const { - map ret; + std::map ret; for (size_t i = 0; i < d_rcodecounters.size(); ++i) { auto count = d_rcodecounters.at(i); if (count != 0) { @@ -85,10 +92,10 @@ map RecResponseStats::getRCodeResponseCounts() const return ret; } -string RecResponseStats::getQTypeReport() const +std::string RecResponseStats::getQTypeReport() const { auto qtypenums = getQTypeResponseCounts(); - ostringstream ostr; + std::ostringstream ostr; for (const auto& val : qtypenums) { ostr << DNSRecordContent::NumberToType(val.first) << '\t' << std::to_string(val.second) << endl; } diff --git a/pdns/recursordist/rec-responsestats.hh b/pdns/recursordist/rec-responsestats.hh index 00046bc52a..9bc2edb3a6 100644 --- a/pdns/recursordist/rec-responsestats.hh +++ b/pdns/recursordist/rec-responsestats.hh @@ -22,9 +22,10 @@ #pragma once #include +#include +#include #include "histogram.hh" -#include "dnspacket.hh" class RecResponseStats { @@ -63,10 +64,10 @@ public: } d_sizecounters(respsize); } - map getQTypeResponseCounts() const; - map getSizeResponseCounts() const; - map getRCodeResponseCounts() const; - string getQTypeReport() const; + std::map getQTypeResponseCounts() const; + std::map getSizeResponseCounts() const; + std::map getRCodeResponseCounts() const; + std::string getQTypeReport() const; private: std::array d_qtypecounters{}; diff --git a/pdns/recursordist/rec-tcounters.cc b/pdns/recursordist/rec-tcounters.cc index 4f91bded80..60e171b9cc 100644 --- a/pdns/recursordist/rec-tcounters.cc +++ b/pdns/recursordist/rec-tcounters.cc @@ -38,7 +38,7 @@ Counters& Counters::merge(const Counters& data) auto& lhs = doubleWAvg.at(i); const auto& rhs = data.doubleWAvg.at(i); auto weight = lhs.weight + rhs.weight; - auto avg = lhs.avg * static_cast(lhs.weight) + rhs.avg * static_cast(rhs.weight); + auto avg = (lhs.avg * static_cast(lhs.weight)) + (rhs.avg * static_cast(rhs.weight)); avg = weight == 0 ? 0 : avg / static_cast(weight); lhs.avg = avg; lhs.weight = weight; diff --git a/pdns/recursordist/rec-tcounters.hh b/pdns/recursordist/rec-tcounters.hh index 070d5b221b..a26a084eb6 100644 --- a/pdns/recursordist/rec-tcounters.hh +++ b/pdns/recursordist/rec-tcounters.hh @@ -189,7 +189,7 @@ struct Counters void addToRollingAvg(double value, uint64_t rollsize) { - add((1.0 - 1.0 / static_cast(rollsize)) * avg + value / static_cast(rollsize)); + add(((1.0 - 1.0 / static_cast(rollsize)) * avg) + (value / static_cast(rollsize))); } }; // And an array of weighted averaged values @@ -205,10 +205,10 @@ struct Counters return *this; } static const size_t numberOfRCodes = 16; - std::array rcodeCounters; + std::array rcodeCounters{}; }; // An RCodes histogram - RCodeCounters auth{}; + RCodeCounters auth; std::array(Histogram::numberOfCounters)> histograms = { pdns::Histogram{"answers", {1000, 10000, 100000, 1000000}}, @@ -220,7 +220,7 @@ struct Counters pdns::Histogram{"cumul-authanswers-", 1000, 13}}; // Response stats - RecResponseStats responseStats{}; + RecResponseStats responseStats; // DNSSEC stats struct DNSSECCounters @@ -236,7 +236,7 @@ struct Counters { return counts.at(static_cast(index)); } - std::array(vState::BogusInvalidDNSKEYProtocol) + 1> counts; + std::array(vState::BogusInvalidDNSKEYProtocol) + 1> counts{}; }; std::array(DNSSECHistogram::numberOfCounters)> dnssecCounters{}; @@ -254,9 +254,9 @@ struct Counters { return counts.at(static_cast(index)); } - std::array(DNSFilterEngine::PolicyKind::Custom) + 1> counts; + std::array(DNSFilterEngine::PolicyKind::Custom) + 1> counts{}; }; - PolicyCounters policyCounters{}; + PolicyCounters policyCounters; // Policy hits by name struct PolicyNameCounters @@ -272,27 +272,16 @@ struct Counters }; PolicyNameCounters policyNameHits; - Counters() - { - for (auto& elem : uint64Count) { - elem = 0; - } - // doubleWAvg has a default constructor that initializes - for (auto& elem : auth.rcodeCounters) { - elem = 0; - } - // Histogram has a constructor that initializes - // RecResponseStats has a default constructor that initializes - for (auto& histogram : dnssecCounters) { - for (auto& elem : histogram.counts) { - elem = 0; - } - } - for (auto& elem : policyCounters.counts) { - elem = 0; - } - // PolicyNameCounters has a default constuctor that initializes - } + Counters() = default; + // { + // // uint64Count field has initalizer + // // doubleWAvg has a default constructor that initializes + // // auth.RCodeCounters field is initalized + // // Histogram has a constructor that initializes + // // RecResponseStats has a default constructor that initializes + // // dnssecCounters field is initialized + // // PolicyNameCounters has a default constuctor that initializes + // } // Merge a set of counters into an existing set of counters. For simple counters, that will be additions // for averages, we should take the weights into account. Histograms need to sum all individual counts. @@ -332,7 +321,7 @@ struct Counters } // We only have a single PolicyHistogram indexed PolicyCounters, so no need to select a specific one - PolicyCounters& at(PolicyHistogram) + PolicyCounters& at(PolicyHistogram /* unused */) { return policyCounters; } diff --git a/pdns/tcounters.hh b/pdns/tcounters.hh index 733e1df425..78afe87ffe 100644 --- a/pdns/tcounters.hh +++ b/pdns/tcounters.hh @@ -23,7 +23,6 @@ #pragma once #include -#include #include #include