]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
rec: Tidy tcounter related code
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Thu, 13 Nov 2025 14:13:40 +0000 (15:13 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Thu, 13 Nov 2025 14:13:40 +0000 (15:13 +0100)
Signed-off-by: Otto Moerbeek <otto.moerbeek@open-xchange.com>
pdns/recursordist/rec-responsestats.cc
pdns/recursordist/rec-responsestats.hh
pdns/recursordist/rec-tcounters.cc
pdns/recursordist/rec-tcounters.hh
pdns/tcounters.hh

index ea283c3601a78110e97b10b93596e3fc7930f787..be468693f848f5a6881fa514c15c6afa9f65bcd5 100644 (file)
@@ -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 <limits>
-
-#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<uint16_t, uint64_t> RecResponseStats::getQTypeResponseCounts() const
+std::map<uint16_t, uint64_t> RecResponseStats::getQTypeResponseCounts() const
 {
-  map<uint16_t, uint64_t> ret;
+  std::map<uint16_t, uint64_t> 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<uint16_t, uint64_t> RecResponseStats::getQTypeResponseCounts() const
   return ret;
 }
 
-map<uint16_t, uint64_t> RecResponseStats::getSizeResponseCounts() const
+std::map<uint16_t, uint64_t> RecResponseStats::getSizeResponseCounts() const
 {
-  map<uint16_t, uint64_t> ret;
+  std::map<uint16_t, uint64_t> 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<uint16_t, uint64_t> RecResponseStats::getSizeResponseCounts() const
   return ret;
 }
 
-map<uint8_t, uint64_t> RecResponseStats::getRCodeResponseCounts() const
+std::map<uint8_t, uint64_t> RecResponseStats::getRCodeResponseCounts() const
 {
-  map<uint8_t, uint64_t> ret;
+  std::map<uint8_t, uint64_t> 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<uint8_t, uint64_t> 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;
   }
index 00046bc52a421ddb21ba8d309e89a8e30cf4b51f..9bc2edb3a674cd99ea05aafc1114ffbfc5ce6083 100644 (file)
 #pragma once
 
 #include <array>
+#include <map>
+#include <string>
 
 #include "histogram.hh"
-#include "dnspacket.hh"
 
 class RecResponseStats
 {
@@ -63,10 +64,10 @@ public:
     }
     d_sizecounters(respsize);
   }
-  map<uint16_t, uint64_t> getQTypeResponseCounts() const;
-  map<uint16_t, uint64_t> getSizeResponseCounts() const;
-  map<uint8_t, uint64_t> getRCodeResponseCounts() const;
-  string getQTypeReport() const;
+  std::map<uint16_t, uint64_t> getQTypeResponseCounts() const;
+  std::map<uint16_t, uint64_t> getSizeResponseCounts() const;
+  std::map<uint8_t, uint64_t> getRCodeResponseCounts() const;
+  std::string getQTypeReport() const;
 
 private:
   std::array<uint64_t, maxQType + 1> d_qtypecounters{};
index 4f91bded80a6d94ec12471f41734bd4c6daa5320..60e171b9cca88f56fea120d79f6556d7abc6975e 100644 (file)
@@ -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<double>(lhs.weight) + rhs.avg * static_cast<double>(rhs.weight);
+    auto avg = (lhs.avg * static_cast<double>(lhs.weight)) + (rhs.avg * static_cast<double>(rhs.weight));
     avg = weight == 0 ? 0 : avg / static_cast<double>(weight);
     lhs.avg = avg;
     lhs.weight = weight;
index 070d5b221b475103d57178c98151692f5e649724..a26a084eb663d60bfe4d87fb93a5ea0d1f73bad0 100644 (file)
@@ -189,7 +189,7 @@ struct Counters
 
     void addToRollingAvg(double value, uint64_t rollsize)
     {
-      add((1.0 - 1.0 / static_cast<double>(rollsize)) * avg + value / static_cast<double>(rollsize));
+      add(((1.0 - 1.0 / static_cast<double>(rollsize)) * avg) + (value / static_cast<double>(rollsize)));
     }
   };
   // And an array of weighted averaged values
@@ -205,10 +205,10 @@ struct Counters
       return *this;
     }
     static const size_t numberOfRCodes = 16;
-    std::array<uint64_t, numberOfRCodes> rcodeCounters;
+    std::array<uint64_t, numberOfRCodes> rcodeCounters{};
   };
   // An RCodes histogram
-  RCodeCounters auth{};
+  RCodeCounters auth;
 
   std::array<pdns::Histogram, static_cast<size_t>(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<size_t>(index));
     }
-    std::array<uint64_t, static_cast<size_t>(vState::BogusInvalidDNSKEYProtocol) + 1> counts;
+    std::array<uint64_t, static_cast<size_t>(vState::BogusInvalidDNSKEYProtocol) + 1> counts{};
   };
   std::array<DNSSECCounters, static_cast<size_t>(DNSSECHistogram::numberOfCounters)> dnssecCounters{};
 
@@ -254,9 +254,9 @@ struct Counters
     {
       return counts.at(static_cast<size_t>(index));
     }
-    std::array<uint64_t, static_cast<size_t>(DNSFilterEngine::PolicyKind::Custom) + 1> counts;
+    std::array<uint64_t, static_cast<size_t>(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;
   }
index 733e1df425e3492af44962898bcf78ed8602cb3b..78afe87ffef1a4a62a2bc772295d83c8df6138dc 100644 (file)
@@ -23,7 +23,6 @@
 #pragma once
 
 #include <sys/time.h>
-#include <array>
 #include <set>
 #include <unistd.h>