From: Otto Moerbeek Date: Mon, 27 Feb 2023 10:29:46 +0000 (+0100) Subject: Account for size per shard X-Git-Tag: rec-4.9.0-alpha1~7^2~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a793f3990a6ec5a1f2218b8c25f5ccf76a46e08f;p=thirdparty%2Fpdns.git Account for size per shard --- diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index 5512d8ae85..b514e7b2e0 100644 --- a/pdns/recursordist/rec-main.cc +++ b/pdns/recursordist/rec-main.cc @@ -1447,7 +1447,6 @@ static int serviceMain(int argc, char* argv[], Logr::log_t log) g_maxNSEC3Iterations = ::arg().asNum("nsec3-max-iterations"); g_maxCacheEntries = ::arg().asNum("max-cache-entries"); - g_maxPacketCacheEntries = ::arg().asNum("max-packetcache-entries"); luaConfigDelayedThreads delayedLuaThreads; try { @@ -2185,7 +2184,6 @@ static void houseKeeping(void*) if (g_packetCache) { static PeriodicTask packetCacheTask{"packetCacheTask", 5}; packetCacheTask.runIfDue(now, []() { - g_packetCache->setMaxSize(g_maxPacketCacheEntries); // g_maxPacketCacheEntries might have changed by rec_control g_packetCache->doPruneTo(g_maxPacketCacheEntries); }); } @@ -3036,8 +3034,8 @@ int main(int argc, char** argv) g_recCache = std::make_unique(::arg().asNum("record-cache-shards")); g_negCache = std::make_unique(::arg().asNum("record-cache-shards") / 8); if (!::arg().mustDo("disable-packetcache")) { - // Only enable packet cache for thread processing queries from the outside world - g_packetCache = std::make_unique(g_maxPacketCacheEntries /*, shards */); + g_maxPacketCacheEntries = ::arg().asNum("max-packetcache-entries"); + g_packetCache = std::make_unique(g_maxPacketCacheEntries, ::arg().asNum("record-cache-shards")); // XXX } ret = serviceMain(argc, argv, startupLog); diff --git a/pdns/recursordist/rec_channel_rec.cc b/pdns/recursordist/rec_channel_rec.cc index 4e82cf6041..f2a7d628e7 100644 --- a/pdns/recursordist/rec_channel_rec.cc +++ b/pdns/recursordist/rec_channel_rec.cc @@ -844,6 +844,7 @@ static string setMaxPacketCacheEntries(T begin, T end) } try { g_maxPacketCacheEntries = pdns::checked_stoi(*begin); + g_packetCache->setMaxSize(g_maxPacketCacheEntries); return "New max packetcache entries: " + std::to_string(g_maxPacketCacheEntries) + "\n"; } catch (const std::exception& e) { diff --git a/pdns/recursordist/recpacketcache.cc b/pdns/recursordist/recpacketcache.cc index 48fb138fc3..184555ebd8 100644 --- a/pdns/recursordist/recpacketcache.cc +++ b/pdns/recursordist/recpacketcache.cc @@ -12,6 +12,14 @@ unsigned int RecursorPacketCache::s_refresh_ttlperc{0}; +void RecursorPacketCache::setShardSizes(size_t shardSize) +{ + for (auto& shard : d_maps) { + auto lock = shard.lock(); + lock->d_shardSize = shardSize; + } +} + uint64_t RecursorPacketCache::size() const { uint64_t count = 0; @@ -231,7 +239,7 @@ void RecursorPacketCache::insertResponsePacket(unsigned int tag, uint32_t qhash, shard->d_map.insert(entry); map.d_entriesCount++; - if (shard->d_map.size() > d_maxSize / d_maps.size()) { + if (shard->d_map.size() > shard->d_shardSize) { auto& seq_idx = shard->d_map.get(); seq_idx.erase(seq_idx.begin()); map.d_entriesCount--; @@ -263,14 +271,16 @@ uint64_t RecursorPacketCache::doDump(int file) size_t shardNum = 0; size_t min = std::numeric_limits::max(); size_t max = 0; + uint64_t maxSize = 0; for (auto& shard : d_maps) { auto lock = shard.lock(); const auto& sidx = lock->d_map.get(); const auto shardSize = lock->d_map.size(); - fprintf(filePtr.get(), "; packetcache shard %zu; size %zu\n", shardNum, shardSize); + fprintf(filePtr.get(), "; packetcache shard %zu; size %zu/%zu\n", shardNum, shardSize, lock->d_shardSize); min = std::min(min, shardSize); max = std::max(max, shardSize); + maxSize += lock->d_shardSize; shardNum++; for (const auto& entry : sidx) { count++; @@ -282,6 +292,6 @@ uint64_t RecursorPacketCache::doDump(int file) } } } - fprintf(filePtr.get(), "; packetcache size: %" PRIu64 "/%zu shards: %zu min/max shard size: %zu/%zu\n", size(), d_maxSize, d_maps.size(), min, max); + fprintf(filePtr.get(), "; packetcache size: %" PRIu64 "/%" PRIu64 " shards: %zu min/max shard size: %zu/%zu\n", size(), maxSize, d_maps.size(), min, max); return count; } diff --git a/pdns/recursordist/recpacketcache.hh b/pdns/recursordist/recpacketcache.hh index 10a005b897..be7611f93d 100644 --- a/pdns/recursordist/recpacketcache.hh +++ b/pdns/recursordist/recpacketcache.hh @@ -57,12 +57,9 @@ public: using OptPBData = boost::optional; RecursorPacketCache(size_t maxsize, size_t shards = 1024) : - d_maps(shards), - d_maxSize(maxsize) + d_maps(shards) { - if (d_maxSize / d_maps.size() == 0) { - d_maxSize = d_maps.size(); - } + setMaxSize(maxsize); } bool getResponsePacket(unsigned int tag, const std::string& queryPacket, time_t now, @@ -86,13 +83,16 @@ public: bool getResponsePacket(unsigned int tag, const std::string& queryPacket, DNSName& qname, uint16_t* qtype, uint16_t* qclass, time_t now, std::string* responsePacket, uint32_t* age, vState* valState, uint32_t* qhash, OptPBData* pbdata, bool tcp); void insertResponsePacket(unsigned int tag, uint32_t qhash, std::string&& query, const DNSName& qname, uint16_t qtype, uint16_t qclass, std::string&& responsePacket, time_t now, uint32_t ttl, const vState& valState, OptPBData&& pbdata, bool tcp); - void doPruneTo(size_t maxSize = 250000); + void doPruneTo(size_t maxSize); uint64_t doDump(int file); uint64_t doWipePacketCache(const DNSName& name, uint16_t qtype = 0xffff, bool subtree = false); void setMaxSize(size_t size) { - d_maxSize = size; + if (size < d_maps.size()) { + size = d_maps.size(); + } + setShardSizes(size / d_maps.size()); } [[nodiscard]] uint64_t size() const; @@ -162,6 +162,7 @@ private: struct LockedContent { packetCache_t d_map; + size_t d_shardSize{0}; uint64_t d_hits{0}; uint64_t d_misses{0}; uint64_t d_contended_count{0}; @@ -205,11 +206,11 @@ private: // return d_maps.at(combine(hash, hash, tcp) % d_maps.size()); // } - size_t d_maxSize; - static bool qrMatch(const packetCache_t::index::type::iterator& iter, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass); bool checkResponseMatches(MapCombo::LockedContent& shard, std::pair::type::iterator, packetCache_t::index::type::iterator> range, const std::string& queryPacket, const DNSName& qname, uint16_t qtype, uint16_t qclass, time_t now, std::string* responsePacket, uint32_t* age, vState* valState, OptPBData* pbdata); + void setShardSizes(size_t shardSize); + public: void preRemoval(MapCombo::LockedContent& map, const Entry& entry) {