Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Add cache statistics #812

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions olp-cpp-sdk-core/include/olp/core/cache/DefaultCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class CORE_API DefaultCache : public KeyValueCache {
*/
bool RemoveKeysWithPrefix(const std::string& prefix) override;

/// Implements GetStatistics method of KeyValueCache.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Implements GetStatistics method of KeyValueCache.
/// Implements the `GetStatistics` method of `KeyValueCache`.

Statistics GetStatistics() const override;

private:
std::shared_ptr<DefaultCacheImpl> impl_;
};
Expand Down
23 changes: 23 additions & 0 deletions olp-cpp-sdk-core/include/olp/core/cache/KeyValueCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ class CORE_API KeyValueCache {
*/
using ValueTypePtr = std::shared_ptr<ValueType>;

/**
* @brief The accumulated statistics for the cache.
*/
struct Statistics {
/// The number of bytes written to disk cache with Put methods.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The number of bytes written to disk cache with Put methods.
/// The number of bytes written to the disk cache with the `Put` methods.

uint64_t bytes_written;
/// The number of bytes read from disk cache with Get methods.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The number of bytes read from disk cache with Get methods.
/// The number of bytes read from the disk cache with the `Get` methods.

uint64_t bytes_read;
/// The number of bytes removed from disk cache with Remove method.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The number of bytes removed from disk cache with Remove method.
/// The number of bytes removed from the disk cache with the `Remove` methods.

uint64_t bytes_removed;
/// The number of bytes evicted from disk cache.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The number of bytes evicted from disk cache.
/// The number of bytes evicted from the disk cache.

uint64_t bytes_evicted;
/// The size of content stored in mutable disk cache.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The size of content stored in mutable disk cache.
/// The size of content stored in the mutable disk cache.

uint64_t data_size;
};

virtual ~KeyValueCache() = default;

/**
Expand Down Expand Up @@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief Retrieve the accumulated statistics of the cache instance.
* @brief Gets the accumulated statistics of the cache instance.

*
* @return Statistics structure.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return Statistics structure.
* @return The statistics structure.

*/
virtual Statistics GetStatistics() const { return Statistics{}; }
Comment on lines +141 to +146
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to DefaultCache only!

};

} // namespace cache
Expand Down
4 changes: 4 additions & 0 deletions olp-cpp-sdk-core/src/cache/DefaultCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<std::mutex> lock(cache_lock_);
statistics_.data_size = mutable_cache_data_size_;
return statistics_;
}

void DefaultCacheImpl::InitializeLru() {
if (!mutable_cache_) {
return;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand Down
4 changes: 4 additions & 0 deletions olp-cpp-sdk-core/src/cache/DefaultCacheImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -108,6 +110,8 @@ class DefaultCacheImpl {
std::unique_ptr<DiskCache> protected_cache_;
uint64_t mutable_cache_data_size_;
std::mutex cache_lock_;

KeyValueCache::Statistics statistics_;
};

} // namespace cache
Expand Down