From 14c61e9e3d5e7d49a94890ecbec25ac8d46fdaee Mon Sep 17 00:00:00 2001 From: Mykhailo Kuchma Date: Tue, 28 Apr 2020 11:51:19 +0300 Subject: [PATCH] WIP Add cache statistics Signed-off-by: Mykhailo Kuchma --- .../include/olp/core/cache/DefaultCache.h | 3 +++ .../include/olp/core/cache/KeyValueCache.h | 23 +++++++++++++++++++ olp-cpp-sdk-core/src/cache/DefaultCache.cpp | 4 ++++ .../src/cache/DefaultCacheImpl.cpp | 17 ++++++++++++++ olp-cpp-sdk-core/src/cache/DefaultCacheImpl.h | 4 ++++ 5 files changed, 51 insertions(+) diff --git a/olp-cpp-sdk-core/include/olp/core/cache/DefaultCache.h b/olp-cpp-sdk-core/include/olp/core/cache/DefaultCache.h index 93c6d93ff..b55cbd9ef 100644 --- a/olp-cpp-sdk-core/include/olp/core/cache/DefaultCache.h +++ b/olp-cpp-sdk-core/include/olp/core/cache/DefaultCache.h @@ -149,6 +149,9 @@ class CORE_API DefaultCache : public KeyValueCache { */ bool RemoveKeysWithPrefix(const std::string& prefix) override; + /// Implements GetStatistics method of KeyValueCache. + Statistics GetStatistics() const override; + private: std::shared_ptr impl_; }; diff --git a/olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h b/olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h index 4eda83265..8ff088475 100644 --- a/olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h +++ b/olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h @@ -57,6 +57,22 @@ class CORE_API KeyValueCache { */ using ValueTypePtr = std::shared_ptr; + /** + * @brief The accumulated statistics for the cache. + */ + struct Statistics { + /// The number of bytes written to disk cache with Put methods. + uint64_t bytes_written; + /// The number of bytes read from disk cache with Get methods. + uint64_t bytes_read; + /// The number of bytes removed from disk cache with Remove method. + uint64_t bytes_removed; + /// The number of bytes evicted from disk cache. + uint64_t bytes_evicted; + /// The size of content stored in mutable disk cache. + uint64_t data_size; + }; + virtual ~KeyValueCache() = default; /** @@ -121,6 +137,13 @@ class CORE_API KeyValueCache { * @return True if the values are removed; false otherwise. */ virtual bool RemoveKeysWithPrefix(const std::string& prefix) = 0; + + /** + * @brief Retrieve the accumulated statistics of the cache instance. + * + * @return Statistics structure. + */ + virtual Statistics GetStatistics() const { return Statistics{}; } }; } // namespace cache diff --git a/olp-cpp-sdk-core/src/cache/DefaultCache.cpp b/olp-cpp-sdk-core/src/cache/DefaultCache.cpp index 0565197bb..dbf9463f6 100644 --- a/olp-cpp-sdk-core/src/cache/DefaultCache.cpp +++ b/olp-cpp-sdk-core/src/cache/DefaultCache.cpp @@ -61,5 +61,9 @@ bool DefaultCache::RemoveKeysWithPrefix(const std::string& key) { return impl_->RemoveKeysWithPrefix(key); } +DefaultCache::Statistics DefaultCache::GetStatistics() const { + return impl_->GetStatistics(); +} + } // namespace cache } // namespace olp diff --git a/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp b/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp index 8fe60c084..428156e7b 100644 --- a/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp +++ b/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp @@ -264,6 +264,7 @@ bool DefaultCacheImpl::Remove(const std::string& key) { PurgeDiskItem(key, *mutable_cache_, removed_data_size); mutable_cache_data_size_ -= removed_data_size; + statistics_.bytes_removed += removed_data_size; } return true; @@ -284,12 +285,21 @@ bool DefaultCacheImpl::RemoveKeysWithPrefix(const std::string& key) { if (mutable_cache_) { uint64_t removed_data_size = 0; auto result = mutable_cache_->RemoveKeysWithPrefix(key, removed_data_size); + mutable_cache_data_size_ -= removed_data_size; + statistics_.bytes_removed += removed_data_size; + return result; } return true; } +KeyValueCache::Statistics DefaultCacheImpl::GetStatistics() { + std::lock_guard lock(cache_lock_); + statistics_.data_size = mutable_cache_data_size_; + return statistics_; +} + void DefaultCacheImpl::InitializeLru() { if (!mutable_cache_) { return; @@ -438,6 +448,9 @@ bool DefaultCacheImpl::PutMutableCache(const std::string& key, mutable_cache_data_size_ += added_data_size; mutable_cache_data_size_ -= removed_data_size; + statistics_.bytes_written += added_data_size; + statistics_.bytes_evicted += removed_data_size; + if (mutable_cache_lru_) { const auto result = mutable_cache_lru_->InsertOrAssign(key, item_size); if (result.first == mutable_cache_lru_->end() && !result.second) { @@ -522,7 +535,9 @@ DefaultCacheImpl::GetFromDiscCache(const std::string& key) { if (expiry <= 0) { uint64_t removed_data_size = 0u; PurgeDiskItem(key, *mutable_cache_, removed_data_size); + mutable_cache_data_size_ -= removed_data_size; + statistics_.bytes_evicted += removed_data_size; RemoveKeyLru(key); } else { @@ -535,6 +550,8 @@ DefaultCacheImpl::GetFromDiscCache(const std::string& key) { auto result = mutable_cache_->Get(key); if (result) { + statistics_.bytes_read += result->size(); + return std::make_pair(std::move(result.value()), expiry); } } diff --git a/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.h b/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.h index 926a06b96..9eade2869 100644 --- a/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.h +++ b/olp-cpp-sdk-core/src/cache/DefaultCacheImpl.h @@ -49,6 +49,8 @@ class DefaultCacheImpl { bool RemoveKeysWithPrefix(const std::string& key); + KeyValueCache::Statistics GetStatistics(); + protected: /// The LRU cache definition using the leveldb keys as key and the value size /// as value. @@ -108,6 +110,8 @@ class DefaultCacheImpl { std::unique_ptr protected_cache_; uint64_t mutable_cache_data_size_; std::mutex cache_lock_; + + KeyValueCache::Statistics statistics_; }; } // namespace cache