From 464553521781f0683ac0384334c8cc25cee72f0d Mon Sep 17 00:00:00 2001 From: starlord Date: Fri, 27 Sep 2019 10:09:09 +0800 Subject: [PATCH] format cache code Former-commit-id: 99ff6affb78c09941a59c53183427a14b8ff00d1 --- cpp/src/cache/Cache.h | 40 +++++++++++++++--------- cpp/src/cache/Cache.inl | 53 +++++++++++++++++++------------- cpp/src/cache/CacheMgr.h | 26 ++++++++-------- cpp/src/cache/CacheMgr.inl | 58 ++++++++++++++++++++--------------- cpp/src/cache/CpuCacheMgr.cpp | 24 +++++++++------ cpp/src/cache/CpuCacheMgr.h | 17 +++++----- cpp/src/cache/DataObj.h | 37 ++++++++++++---------- cpp/src/cache/GpuCacheMgr.cpp | 26 +++++++++------- cpp/src/cache/GpuCacheMgr.h | 15 ++++----- cpp/src/cache/LRU.h | 22 +++++++------ 10 files changed, 184 insertions(+), 134 deletions(-) diff --git a/cpp/src/cache/Cache.h b/cpp/src/cache/Cache.h index b2a57ebd..b526e933 100644 --- a/cpp/src/cache/Cache.h +++ b/cpp/src/cache/Cache.h @@ -32,30 +32,40 @@ namespace cache { template class Cache { -public: + public: //mem_capacity, units:GB Cache(int64_t capacity_gb, uint64_t cache_max_count); ~Cache() = default; - int64_t usage() const { return usage_; } - int64_t capacity() const { return capacity_; } //unit: BYTE + int64_t usage() const { + return usage_; + } + + int64_t capacity() const { + return capacity_; + } //unit: BYTE void set_capacity(int64_t capacity); //unit: BYTE - double freemem_percent() const { return freemem_percent_; }; - void set_freemem_percent(double percent) { freemem_percent_ = percent; } + double freemem_percent() const { + return freemem_percent_; + } + + void set_freemem_percent(double percent) { + freemem_percent_ = percent; + } size_t size() const; - bool exists(const std::string& key); - ItemObj get(const std::string& key); - void insert(const std::string& key, const ItemObj& item); - void erase(const std::string& key); + bool exists(const std::string &key); + ItemObj get(const std::string &key); + void insert(const std::string &key, const ItemObj &item); + void erase(const std::string &key); void print(); void clear(); -private: + private: void free_memory(); -private: + private: int64_t usage_; int64_t capacity_; double freemem_percent_; @@ -64,8 +74,8 @@ private: mutable std::mutex mutex_; }; -} // cache -} // milvus -} // zilliz +} // namespace cache +} // namespace milvus +} // namespace zilliz -#include "cache/Cache.inl" \ No newline at end of file +#include "cache/Cache.inl" diff --git a/cpp/src/cache/Cache.inl b/cpp/src/cache/Cache.inl index 3eaffc45..ebd8f3c2 100644 --- a/cpp/src/cache/Cache.inl +++ b/cpp/src/cache/Cache.inl @@ -33,29 +33,33 @@ Cache::Cache(int64_t capacity, uint64_t cache_max_count) } template -void Cache::set_capacity(int64_t capacity) { - if(capacity > 0) { +void +Cache::set_capacity(int64_t capacity) { + if (capacity > 0) { capacity_ = capacity; free_memory(); } } template -size_t Cache::size() const { +size_t +Cache::size() const { std::lock_guard lock(mutex_); return lru_.size(); } template -bool Cache::exists(const std::string& key) { +bool +Cache::exists(const std::string &key) { std::lock_guard lock(mutex_); return lru_.exists(key); } template -ItemObj Cache::get(const std::string& key) { +ItemObj +Cache::get(const std::string &key) { std::lock_guard lock(mutex_); - if(!lru_.exists(key)){ + if (!lru_.exists(key)) { return nullptr; } @@ -63,8 +67,9 @@ ItemObj Cache::get(const std::string& key) { } template -void Cache::insert(const std::string& key, const ItemObj& item) { - if(item == nullptr) { +void +Cache::insert(const std::string &key, const ItemObj &item) { + if (item == nullptr) { return; } @@ -80,7 +85,7 @@ void Cache::insert(const std::string& key, const ItemObj& item) { //if key already exist, subtract old item size if (lru_.exists(key)) { - const ItemObj& old_item = lru_.get(key); + const ItemObj &old_item = lru_.get(key); usage_ -= old_item->size(); } @@ -107,13 +112,14 @@ void Cache::insert(const std::string& key, const ItemObj& item) { } template -void Cache::erase(const std::string& key) { +void +Cache::erase(const std::string &key) { std::lock_guard lock(mutex_); - if(!lru_.exists(key)){ + if (!lru_.exists(key)) { return; } - const ItemObj& old_item = lru_.get(key); + const ItemObj &old_item = lru_.get(key); usage_ -= old_item->size(); SERVER_LOG_DEBUG << "Erase " << key << " size: " << old_item->size(); @@ -122,7 +128,8 @@ void Cache::erase(const std::string& key) { } template -void Cache::clear() { +void +Cache::clear() { std::lock_guard lock(mutex_); lru_.clear(); usage_ = 0; @@ -131,12 +138,13 @@ void Cache::clear() { /* free memory space when CACHE occupation exceed its capacity */ template -void Cache::free_memory() { +void +Cache::free_memory() { if (usage_ <= capacity_) return; int64_t threshhold = capacity_ * freemem_percent_; int64_t delta_size = usage_ - threshhold; - if(delta_size <= 0) { + if (delta_size <= 0) { delta_size = 1;//ensure at least one item erased } @@ -148,8 +156,8 @@ void Cache::free_memory() { auto it = lru_.rbegin(); while (it != lru_.rend() && released_size < delta_size) { - auto& key = it->first; - auto& obj_ptr = it->second; + auto &key = it->first; + auto &obj_ptr = it->second; key_array.emplace(key); released_size += obj_ptr->size(); @@ -159,7 +167,7 @@ void Cache::free_memory() { SERVER_LOG_DEBUG << "to be released memory size: " << released_size; - for (auto& key : key_array) { + for (auto &key : key_array) { erase(key); } @@ -167,7 +175,8 @@ void Cache::free_memory() { } template -void Cache::print() { +void +Cache::print() { size_t cache_count = 0; { std::lock_guard lock(mutex_); @@ -179,7 +188,7 @@ void Cache::print() { SERVER_LOG_DEBUG << "[Cache capacity]: " << capacity_ << " bytes"; } -} // cache -} // milvus -} // zilliz +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/CacheMgr.h b/cpp/src/cache/CacheMgr.h index 1dd630e1..53004d10 100644 --- a/cpp/src/cache/CacheMgr.h +++ b/cpp/src/cache/CacheMgr.h @@ -22,22 +22,25 @@ #include "utils/Log.h" #include "metrics/Metrics.h" +#include +#include + namespace zilliz { namespace milvus { namespace cache { template class CacheMgr { -public: + public: virtual uint64_t ItemCount() const; - virtual bool ItemExists(const std::string& key); + virtual bool ItemExists(const std::string &key); - virtual ItemObj GetItem(const std::string& key); + virtual ItemObj GetItem(const std::string &key); - virtual void InsertItem(const std::string& key, const ItemObj& data); + virtual void InsertItem(const std::string &key, const ItemObj &data); - virtual void EraseItem(const std::string& key); + virtual void EraseItem(const std::string &key); virtual void PrintInfo(); @@ -47,18 +50,17 @@ public: int64_t CacheCapacity() const; void SetCapacity(int64_t capacity); -protected: + protected: CacheMgr(); virtual ~CacheMgr(); -protected: + protected: using CachePtr = std::shared_ptr>; CachePtr cache_; }; +} // namespace cache +} // namespace milvus +} // namespace zilliz -} -} -} - -#include "cache/CacheMgr.inl" \ No newline at end of file +#include "cache/CacheMgr.inl" diff --git a/cpp/src/cache/CacheMgr.inl b/cpp/src/cache/CacheMgr.inl index 36c3d946..b0c47b3d 100644 --- a/cpp/src/cache/CacheMgr.inl +++ b/cpp/src/cache/CacheMgr.inl @@ -30,18 +30,20 @@ CacheMgr::~CacheMgr() { } template -uint64_t CacheMgr::ItemCount() const { - if(cache_ == nullptr) { +uint64_t +CacheMgr::ItemCount() const { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return 0; } - return (uint64_t)(cache_->size()); + return (uint64_t) (cache_->size()); } template -bool CacheMgr::ItemExists(const std::string& key) { - if(cache_ == nullptr) { +bool +CacheMgr::ItemExists(const std::string &key) { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return false; } @@ -50,8 +52,9 @@ bool CacheMgr::ItemExists(const std::string& key) { } template -ItemObj CacheMgr::GetItem(const std::string& key) { - if(cache_ == nullptr) { +ItemObj +CacheMgr::GetItem(const std::string &key) { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return nullptr; } @@ -60,8 +63,9 @@ ItemObj CacheMgr::GetItem(const std::string& key) { } template -void CacheMgr::InsertItem(const std::string& key, const ItemObj& data) { - if(cache_ == nullptr) { +void +CacheMgr::InsertItem(const std::string &key, const ItemObj &data) { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return; } @@ -71,8 +75,9 @@ void CacheMgr::InsertItem(const std::string& key, const ItemObj& data) } template -void CacheMgr::EraseItem(const std::string& key) { - if(cache_ == nullptr) { +void +CacheMgr::EraseItem(const std::string &key) { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return; } @@ -82,8 +87,9 @@ void CacheMgr::EraseItem(const std::string& key) { } template -void CacheMgr::PrintInfo() { - if(cache_ == nullptr) { +void +CacheMgr::PrintInfo() { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return; } @@ -92,8 +98,9 @@ void CacheMgr::PrintInfo() { } template -void CacheMgr::ClearCache() { - if(cache_ == nullptr) { +void +CacheMgr::ClearCache() { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return; } @@ -102,8 +109,9 @@ void CacheMgr::ClearCache() { } template -int64_t CacheMgr::CacheUsage() const { - if(cache_ == nullptr) { +int64_t +CacheMgr::CacheUsage() const { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return 0; } @@ -112,8 +120,9 @@ int64_t CacheMgr::CacheUsage() const { } template -int64_t CacheMgr::CacheCapacity() const { - if(cache_ == nullptr) { +int64_t +CacheMgr::CacheCapacity() const { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return 0; } @@ -122,14 +131,15 @@ int64_t CacheMgr::CacheCapacity() const { } template -void CacheMgr::SetCapacity(int64_t capacity) { - if(cache_ == nullptr) { +void +CacheMgr::SetCapacity(int64_t capacity) { + if (cache_ == nullptr) { SERVER_LOG_ERROR << "Cache doesn't exist"; return; } cache_->set_capacity(capacity); } -} -} -} +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/CpuCacheMgr.cpp b/cpp/src/cache/CpuCacheMgr.cpp index 1f93ec92..d26004b2 100644 --- a/cpp/src/cache/CpuCacheMgr.cpp +++ b/cpp/src/cache/CpuCacheMgr.cpp @@ -16,20 +16,22 @@ // under the License. -#include "CpuCacheMgr.h" +#include "cache/CpuCacheMgr.h" #include "server/Config.h" #include "utils/Log.h" +#include + namespace zilliz { namespace milvus { namespace cache { namespace { - constexpr int64_t unit = 1024 * 1024 * 1024; +constexpr int64_t unit = 1024 * 1024 * 1024; } CpuCacheMgr::CpuCacheMgr() { - server::Config& config = server::Config::GetInstance(); + server::Config &config = server::Config::GetInstance(); Status s; int32_t cpu_mem_cap; @@ -38,7 +40,7 @@ CpuCacheMgr::CpuCacheMgr() { SERVER_LOG_ERROR << s.message(); } int64_t cap = cpu_mem_cap * unit; - cache_ = std::make_shared>(cap, 1UL<<32); + cache_ = std::make_shared>(cap, 1UL << 32); float cpu_mem_threshold; s = config.GetCacheConfigCpuMemThreshold(cpu_mem_threshold); @@ -53,20 +55,22 @@ CpuCacheMgr::CpuCacheMgr() { } } -CpuCacheMgr* CpuCacheMgr::GetInstance() { +CpuCacheMgr * +CpuCacheMgr::GetInstance() { static CpuCacheMgr s_mgr; return &s_mgr; } -engine::VecIndexPtr CpuCacheMgr::GetIndex(const std::string& key) { +engine::VecIndexPtr +CpuCacheMgr::GetIndex(const std::string &key) { DataObjPtr obj = GetItem(key); - if(obj != nullptr) { + if (obj != nullptr) { return obj->data(); } return nullptr; } -} -} -} \ No newline at end of file +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/CpuCacheMgr.h b/cpp/src/cache/CpuCacheMgr.h index 5a2cdb59..32a83c4c 100644 --- a/cpp/src/cache/CpuCacheMgr.h +++ b/cpp/src/cache/CpuCacheMgr.h @@ -20,21 +20,24 @@ #include "CacheMgr.h" #include "DataObj.h" +#include +#include + namespace zilliz { namespace milvus { namespace cache { class CpuCacheMgr : public CacheMgr { -private: + private: CpuCacheMgr(); -public: + public: //TODO: use smart pointer instead - static CpuCacheMgr* GetInstance(); + static CpuCacheMgr *GetInstance(); - engine::VecIndexPtr GetIndex(const std::string& key); + engine::VecIndexPtr GetIndex(const std::string &key); }; -} -} -} +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/DataObj.h b/cpp/src/cache/DataObj.h index f9215bc1..6ed3256e 100644 --- a/cpp/src/cache/DataObj.h +++ b/cpp/src/cache/DataObj.h @@ -27,38 +27,43 @@ namespace milvus { namespace cache { class DataObj { -public: - DataObj(const engine::VecIndexPtr& index) - : index_(index) - {} + public: + explicit DataObj(const engine::VecIndexPtr &index) + : index_(index) { + } - DataObj(const engine::VecIndexPtr& index, int64_t size) - : index_(index), - size_(size) - {} + DataObj(const engine::VecIndexPtr &index, int64_t size) + : index_(index), + size_(size) { + } - engine::VecIndexPtr data() { return index_; } - const engine::VecIndexPtr& data() const { return index_; } + engine::VecIndexPtr data() { + return index_; + } + + const engine::VecIndexPtr &data() const { + return index_; + } int64_t size() const { - if(index_ == nullptr) { + if (index_ == nullptr) { return 0; } - if(size_ > 0) { + if (size_ > 0) { return size_; } return index_->Count() * index_->Dimension() * sizeof(float); } -private: + private: engine::VecIndexPtr index_ = nullptr; int64_t size_ = 0; }; using DataObjPtr = std::shared_ptr; -} -} -} \ No newline at end of file +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/GpuCacheMgr.cpp b/cpp/src/cache/GpuCacheMgr.cpp index 5c104ba0..ad68a6eb 100644 --- a/cpp/src/cache/GpuCacheMgr.cpp +++ b/cpp/src/cache/GpuCacheMgr.cpp @@ -16,11 +16,13 @@ // under the License. -#include +#include "cache/GpuCacheMgr.h" #include "utils/Log.h" -#include "GpuCacheMgr.h" #include "server/Config.h" +#include +#include + namespace zilliz { namespace milvus { namespace cache { @@ -29,11 +31,11 @@ std::mutex GpuCacheMgr::mutex_; std::unordered_map GpuCacheMgr::instance_; namespace { - constexpr int64_t G_BYTE = 1024 * 1024 * 1024; +constexpr int64_t G_BYTE = 1024 * 1024 * 1024; } GpuCacheMgr::GpuCacheMgr() { - server::Config& config = server::Config::GetInstance(); + server::Config &config = server::Config::GetInstance(); Status s; int32_t gpu_mem_cap; @@ -42,7 +44,7 @@ GpuCacheMgr::GpuCacheMgr() { SERVER_LOG_ERROR << s.message(); } int32_t cap = gpu_mem_cap * G_BYTE; - cache_ = std::make_shared>(cap, 1UL<<32); + cache_ = std::make_shared>(cap, 1UL << 32); float gpu_mem_threshold; s = config.GetCacheConfigGpuMemThreshold(gpu_mem_threshold); @@ -57,7 +59,8 @@ GpuCacheMgr::GpuCacheMgr() { } } -GpuCacheMgr* GpuCacheMgr::GetInstance(uint64_t gpu_id) { +GpuCacheMgr * +GpuCacheMgr::GetInstance(uint64_t gpu_id) { if (instance_.find(gpu_id) == instance_.end()) { std::lock_guard lock(mutex_); if (instance_.find(gpu_id) == instance_.end()) { @@ -70,15 +73,16 @@ GpuCacheMgr* GpuCacheMgr::GetInstance(uint64_t gpu_id) { } } -engine::VecIndexPtr GpuCacheMgr::GetIndex(const std::string& key) { +engine::VecIndexPtr +GpuCacheMgr::GetIndex(const std::string &key) { DataObjPtr obj = GetItem(key); - if(obj != nullptr) { + if (obj != nullptr) { return obj->data(); } return nullptr; } -} -} -} \ No newline at end of file +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/GpuCacheMgr.h b/cpp/src/cache/GpuCacheMgr.h index 57539b69..843a5ff6 100644 --- a/cpp/src/cache/GpuCacheMgr.h +++ b/cpp/src/cache/GpuCacheMgr.h @@ -21,6 +21,7 @@ #include #include +#include namespace zilliz { namespace milvus { @@ -30,18 +31,18 @@ class GpuCacheMgr; using GpuCacheMgrPtr = std::shared_ptr; class GpuCacheMgr : public CacheMgr { -public: + public: GpuCacheMgr(); - static GpuCacheMgr* GetInstance(uint64_t gpu_id); + static GpuCacheMgr *GetInstance(uint64_t gpu_id); - engine::VecIndexPtr GetIndex(const std::string& key); + engine::VecIndexPtr GetIndex(const std::string &key); -private: + private: static std::mutex mutex_; static std::unordered_map instance_; }; -} -} -} +} // namespace cache +} // namespace milvus +} // namespace zilliz diff --git a/cpp/src/cache/LRU.h b/cpp/src/cache/LRU.h index a4f4dcf5..5446dd0f 100644 --- a/cpp/src/cache/LRU.h +++ b/cpp/src/cache/LRU.h @@ -22,6 +22,7 @@ #include #include #include +#include namespace zilliz { namespace milvus { @@ -29,14 +30,15 @@ namespace cache { template class LRU { -public: + public: typedef typename std::pair key_value_pair_t; typedef typename std::list::iterator list_iterator_t; typedef typename std::list::reverse_iterator reverse_list_iterator_t; - LRU(size_t max_size) : max_size_(max_size) {} + explicit LRU(size_t max_size) : max_size_(max_size) { + } - void put(const key_t& key, const value_t& value) { + void put(const key_t &key, const value_t &value) { auto it = cache_items_map_.find(key); cache_items_list_.push_front(key_value_pair_t(key, value)); if (it != cache_items_map_.end()) { @@ -53,7 +55,7 @@ public: } } - const value_t& get(const key_t& key) { + const value_t &get(const key_t &key) { auto it = cache_items_map_.find(key); if (it == cache_items_map_.end()) { throw std::range_error("There is no such key in cache"); @@ -63,7 +65,7 @@ public: } } - void erase(const key_t& key) { + void erase(const key_t &key) { auto it = cache_items_map_.find(key); if (it != cache_items_map_.end()) { cache_items_list_.erase(it->second); @@ -71,7 +73,7 @@ public: } } - bool exists(const key_t& key) const { + bool exists(const key_t &key) const { return cache_items_map_.find(key) != cache_items_map_.end(); } @@ -101,14 +103,14 @@ public: cache_items_map_.clear(); } -private: + private: std::list cache_items_list_; std::unordered_map cache_items_map_; size_t max_size_; list_iterator_t iter_; }; -} // cache -} // milvus -} // zilliz +} // namespace cache +} // namespace milvus +} // namespace zilliz -- GitLab