]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Skip some memory allocations in client mode
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 25 May 2021 14:35:13 +0000 (16:35 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 25 May 2021 14:35:13 +0000 (16:35 +0200)
dnsdist does not use most of the configuration in client mode, mostly
caring about the settings needed to access the console, but
we still need to parse all directives and create objects due to the
way our Lua parsing works.
We don't, however, need to allocate the whole storage needed for
packet caches, for backend states or for the in-memory ring buffers.
This reduces the memory consumption of a console client to roughly 1 MB
from hundreds of them in some cases.

pdns/dnsdist-lua.cc
pdns/dnsdist-lua.hh
pdns/dnsdistdist/dnsdist-backend.cc
pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc

index 010478e9cf4ed7fed7f5d06cbbf00bc3561df8c4..d15de8aad0cd3f432f41751d69e1ad36ee649710 100644 (file)
@@ -253,7 +253,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
                       [client, configCheck](boost::variant<string,newserver_t> pvars, boost::optional<int> qps) {
       setLuaSideEffect();
 
-      std::shared_ptr<DownstreamState> ret = std::make_shared<DownstreamState>(ComboAddress());
+      std::shared_ptr<DownstreamState> ret = std::make_shared<DownstreamState>(ComboAddress(), ComboAddress(), 0, std::string(), 1, !(client || configCheck));
       newserver_t vars;
 
       ComboAddress serverAddr;
@@ -827,7 +827,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
 
   luaCtx.writeFunction("getServer", [client](boost::variant<unsigned int, std::string> i) {
       if (client) {
-        return std::make_shared<DownstreamState>(ComboAddress());
+        return std::make_shared<DownstreamState>(ComboAddress(), ComboAddress(), 0, std::string(), 1, false);
       }
       auto states = g_dstates.getCopy();
       if (auto str = boost::get<std::string>(&i)) {
@@ -1819,14 +1819,19 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
       }
     });
 
-  luaCtx.writeFunction("setRingBuffersSize", [](size_t capacity, boost::optional<size_t> numberOfShards) {
+  luaCtx.writeFunction("setRingBuffersSize", [client](size_t capacity, boost::optional<size_t> numberOfShards) {
       setLuaSideEffect();
       if (g_configurationDone) {
         errlog("setRingBuffersSize() cannot be used at runtime!");
         g_outputBuffer="setRingBuffersSize() cannot be used at runtime!\n";
         return;
       }
-      g_rings.setCapacity(capacity, numberOfShards ? *numberOfShards : 10);
+      if (!client) {
+        g_rings.setCapacity(capacity, numberOfShards ? *numberOfShards : 10);
+      }
+      else {
+        g_rings.setCapacity(0, 1);
+      }
     });
 
   luaCtx.writeFunction("setRingBuffersLockRetries", [](size_t retries) {
@@ -2579,7 +2584,7 @@ vector<std::function<void(void)>> setupLua(LuaContext& luaCtx, bool client, bool
   setupLuaBindingsDNSCrypt(luaCtx);
   setupLuaBindingsDNSQuestion(luaCtx);
   setupLuaBindingsKVS(luaCtx, client);
-  setupLuaBindingsPacketCache(luaCtx);
+  setupLuaBindingsPacketCache(luaCtx, client);
   setupLuaBindingsProtoBuf(luaCtx, client, configCheck);
   setupLuaInspection(luaCtx);
   setupLuaRules(luaCtx);
index b40073613ab8dc99890f3b480419922e87369060..28cd00da73969290a3724db26408ccb1b7a9a30f 100644 (file)
@@ -100,7 +100,7 @@ void setupLuaBindings(LuaContext& luaCtx, bool client);
 void setupLuaBindingsDNSCrypt(LuaContext& luaCtx);
 void setupLuaBindingsDNSQuestion(LuaContext& luaCtx);
 void setupLuaBindingsKVS(LuaContext& luaCtx, bool client);
-void setupLuaBindingsPacketCache(LuaContext& luaCtx);
+void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client);
 void setupLuaBindingsProtoBuf(LuaContext& luaCtx, bool client, bool configCheck);
 void setupLuaRules(LuaContext& luaCtx);
 void setupLuaInspection(LuaContext& luaCtx);
index 091839b414ce146cf499164c104f7484259b9679..9425f350c823ffd9449e3e055fe1264868791438 100644 (file)
@@ -153,7 +153,7 @@ void DownstreamState::setWeight(int newWeight)
   }
 }
 
-DownstreamState::DownstreamState(const ComboAddress& remote_, const ComboAddress& sourceAddr_, unsigned int sourceItf_, const std::string& sourceItfName_, size_t numberOfSockets, bool connect=true): sourceItfName(sourceItfName_), remote(remote_), idStates(g_maxOutstanding), sourceAddr(sourceAddr_), sourceItf(sourceItf_), name(remote_.toStringWithPort()), nameWithAddr(remote_.toStringWithPort())
+DownstreamState::DownstreamState(const ComboAddress& remote_, const ComboAddress& sourceAddr_, unsigned int sourceItf_, const std::string& sourceItfName_, size_t numberOfSockets, bool connect=true): sourceItfName(sourceItfName_), remote(remote_), idStates(connect ? g_maxOutstanding : 0), sourceAddr(sourceAddr_), sourceItf(sourceItf_), name(remote_.toStringWithPort()), nameWithAddr(remote_.toStringWithPort())
 {
   id = getUniqueID();
   threadStarted.clear();
index 79c58d558c5ab980c4e36f97c2992bb726bbd0a7..7cadee528bf2b3d2380d8ea349fdc20b96693911 100644 (file)
 #include "dnsdist.hh"
 #include "dnsdist-lua.hh"
 
-void setupLuaBindingsPacketCache(LuaContext& luaCtx)
+void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client)
 {
   /* PacketCache */
-  luaCtx.writeFunction("newPacketCache", [](size_t maxEntries, boost::optional<std::unordered_map<std::string, boost::variant<bool, size_t>>> vars) {
+  luaCtx.writeFunction("newPacketCache", [client](size_t maxEntries, boost::optional<std::unordered_map<std::string, boost::variant<bool, size_t>>> vars) {
 
       bool keepStaleData = false;
       size_t maxTTL = 86400;
@@ -98,6 +98,11 @@ void setupLuaBindingsPacketCache(LuaContext& luaCtx)
         numberOfShards = maxEntries;
       }
 
+      if (client) {
+        maxEntries = 1;
+        numberOfShards = 1;
+      }
+
       auto res = std::make_shared<DNSDistPacketCache>(maxEntries, maxTTL, minTTL, tempFailTTL, maxNegativeTTL, staleTTL, dontAge, numberOfShards, deferrableInsertLock, ecsParsing);
 
       res->setKeepStaleData(keepStaleData);