From f1450923cbb3c836c833c1d3825ffae968894769 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Mon, 13 Mar 2023 15:28:03 +0000 Subject: [PATCH] Extend CacheBase with dump() method --- src/Common/CacheBase.h | 7 +++++++ src/Common/ICachePolicy.h | 8 ++++++++ src/Common/LRUCachePolicy.h | 9 +++++++++ src/Common/SLRUCachePolicy.h | 9 +++++++++ 4 files changed, 33 insertions(+) diff --git a/src/Common/CacheBase.h b/src/Common/CacheBase.h index 89aa78b390c..f38c3943f09 100644 --- a/src/Common/CacheBase.h +++ b/src/Common/CacheBase.h @@ -37,6 +37,7 @@ public: using Key = typename CachePolicy::Key; using Mapped = typename CachePolicy::Mapped; using MappedPtr = typename CachePolicy::MappedPtr; + using KeyMapped = typename CachePolicy::KeyMapped; /// Use this ctor if you don't care about the internal cache policy. explicit CacheBase(size_t max_size_in_bytes, size_t max_entries = 0, double size_ratio = 0.5) @@ -160,6 +161,12 @@ public: out_misses = misses; } + std::vector dump() const + { + std::lock_guard lock(mutex); + return cache_policy->dump(); + } + void reset() { std::lock_guard lock(mutex); diff --git a/src/Common/ICachePolicy.h b/src/Common/ICachePolicy.h index f14e5b30c4f..c9d5bb282f6 100644 --- a/src/Common/ICachePolicy.h +++ b/src/Common/ICachePolicy.h @@ -25,6 +25,12 @@ public: using MappedPtr = std::shared_ptr; using OnWeightLossFunction = std::function; + struct KeyMapped + { + Key key; + MappedPtr mapped; + }; + virtual size_t weight(std::lock_guard & /* cache_lock */) const = 0; virtual size_t count(std::lock_guard & /* cache_lock */) const = 0; virtual size_t maxSize(std::lock_guard& /* cache_lock */) const = 0; @@ -34,6 +40,8 @@ public: virtual MappedPtr get(const Key & key, std::lock_guard & /* cache_lock */) = 0; virtual void set(const Key & key, const MappedPtr & mapped, std::lock_guard & /* cache_lock */) = 0; + virtual std::vector dump() const = 0; + virtual ~ICachePolicy() = default; }; diff --git a/src/Common/LRUCachePolicy.h b/src/Common/LRUCachePolicy.h index e308d016587..d08434eb1bf 100644 --- a/src/Common/LRUCachePolicy.h +++ b/src/Common/LRUCachePolicy.h @@ -22,6 +22,7 @@ class LRUCachePolicy : public ICachePolicy; using typename Base::MappedPtr; + using typename Base::KeyMapped; using typename Base::OnWeightLossFunction; /** Initialize LRUCachePolicy with max_size_in_bytes and max_entries. @@ -116,6 +117,14 @@ public: removeOverflow(); } + std::vector dump() const override + { + std::vector res; + for (const auto & [key, cell] : cells) + res.push_back({key, cell.value}); + return res; + } + protected: using LRUQueue = std::list; using LRUQueueIterator = typename LRUQueue::iterator; diff --git a/src/Common/SLRUCachePolicy.h b/src/Common/SLRUCachePolicy.h index a568d7f9c7f..42ee9916805 100644 --- a/src/Common/SLRUCachePolicy.h +++ b/src/Common/SLRUCachePolicy.h @@ -24,6 +24,7 @@ class SLRUCachePolicy : public ICachePolicy; using typename Base::MappedPtr; + using typename Base::KeyMapped; using typename Base::OnWeightLossFunction; /** Initialize SLRUCachePolicy with max_size_in_bytes and max_protected_size. @@ -148,6 +149,14 @@ public: removeOverflow(probationary_queue, max_size_in_bytes, current_size_in_bytes, /*is_protected=*/false); } + std::vector dump() const override + { + std::vector res; + for (const auto & [key, cell] : cells) + res.push_back({key, cell.value}); + return res; + } + protected: using SLRUQueue = std::list; using SLRUQueueIterator = typename SLRUQueue::iterator;