提交 46455352 编写于 作者: S starlord

format cache code


Former-commit-id: 99ff6affb78c09941a59c53183427a14b8ff00d1
上级 6ac9c1a0
...@@ -32,30 +32,40 @@ namespace cache { ...@@ -32,30 +32,40 @@ namespace cache {
template<typename ItemObj> template<typename ItemObj>
class Cache { class Cache {
public: public:
//mem_capacity, units:GB //mem_capacity, units:GB
Cache(int64_t capacity_gb, uint64_t cache_max_count); Cache(int64_t capacity_gb, uint64_t cache_max_count);
~Cache() = default; ~Cache() = default;
int64_t usage() const { return usage_; } int64_t usage() const {
int64_t capacity() const { return capacity_; } //unit: BYTE return usage_;
}
int64_t capacity() const {
return capacity_;
} //unit: BYTE
void set_capacity(int64_t capacity); //unit: BYTE void set_capacity(int64_t capacity); //unit: BYTE
double freemem_percent() const { return freemem_percent_; }; double freemem_percent() const {
void set_freemem_percent(double percent) { freemem_percent_ = percent; } return freemem_percent_;
}
void set_freemem_percent(double percent) {
freemem_percent_ = percent;
}
size_t size() const; size_t size() const;
bool exists(const std::string& key); bool exists(const std::string &key);
ItemObj get(const std::string& key); ItemObj get(const std::string &key);
void insert(const std::string& key, const ItemObj& item); void insert(const std::string &key, const ItemObj &item);
void erase(const std::string& key); void erase(const std::string &key);
void print(); void print();
void clear(); void clear();
private: private:
void free_memory(); void free_memory();
private: private:
int64_t usage_; int64_t usage_;
int64_t capacity_; int64_t capacity_;
double freemem_percent_; double freemem_percent_;
...@@ -64,8 +74,8 @@ private: ...@@ -64,8 +74,8 @@ private:
mutable std::mutex mutex_; mutable std::mutex mutex_;
}; };
} // cache } // namespace cache
} // milvus } // namespace milvus
} // zilliz } // namespace zilliz
#include "cache/Cache.inl" #include "cache/Cache.inl"
...@@ -33,29 +33,33 @@ Cache<ItemObj>::Cache(int64_t capacity, uint64_t cache_max_count) ...@@ -33,29 +33,33 @@ Cache<ItemObj>::Cache(int64_t capacity, uint64_t cache_max_count)
} }
template<typename ItemObj> template<typename ItemObj>
void Cache<ItemObj>::set_capacity(int64_t capacity) { void
if(capacity > 0) { Cache<ItemObj>::set_capacity(int64_t capacity) {
if (capacity > 0) {
capacity_ = capacity; capacity_ = capacity;
free_memory(); free_memory();
} }
} }
template<typename ItemObj> template<typename ItemObj>
size_t Cache<ItemObj>::size() const { size_t
Cache<ItemObj>::size() const {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
return lru_.size(); return lru_.size();
} }
template<typename ItemObj> template<typename ItemObj>
bool Cache<ItemObj>::exists(const std::string& key) { bool
Cache<ItemObj>::exists(const std::string &key) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
return lru_.exists(key); return lru_.exists(key);
} }
template<typename ItemObj> template<typename ItemObj>
ItemObj Cache<ItemObj>::get(const std::string& key) { ItemObj
Cache<ItemObj>::get(const std::string &key) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
if(!lru_.exists(key)){ if (!lru_.exists(key)) {
return nullptr; return nullptr;
} }
...@@ -63,8 +67,9 @@ ItemObj Cache<ItemObj>::get(const std::string& key) { ...@@ -63,8 +67,9 @@ ItemObj Cache<ItemObj>::get(const std::string& key) {
} }
template<typename ItemObj> template<typename ItemObj>
void Cache<ItemObj>::insert(const std::string& key, const ItemObj& item) { void
if(item == nullptr) { Cache<ItemObj>::insert(const std::string &key, const ItemObj &item) {
if (item == nullptr) {
return; return;
} }
...@@ -80,7 +85,7 @@ void Cache<ItemObj>::insert(const std::string& key, const ItemObj& item) { ...@@ -80,7 +85,7 @@ void Cache<ItemObj>::insert(const std::string& key, const ItemObj& item) {
//if key already exist, subtract old item size //if key already exist, subtract old item size
if (lru_.exists(key)) { if (lru_.exists(key)) {
const ItemObj& old_item = lru_.get(key); const ItemObj &old_item = lru_.get(key);
usage_ -= old_item->size(); usage_ -= old_item->size();
} }
...@@ -107,13 +112,14 @@ void Cache<ItemObj>::insert(const std::string& key, const ItemObj& item) { ...@@ -107,13 +112,14 @@ void Cache<ItemObj>::insert(const std::string& key, const ItemObj& item) {
} }
template<typename ItemObj> template<typename ItemObj>
void Cache<ItemObj>::erase(const std::string& key) { void
Cache<ItemObj>::erase(const std::string &key) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
if(!lru_.exists(key)){ if (!lru_.exists(key)) {
return; return;
} }
const ItemObj& old_item = lru_.get(key); const ItemObj &old_item = lru_.get(key);
usage_ -= old_item->size(); usage_ -= old_item->size();
SERVER_LOG_DEBUG << "Erase " << key << " size: " << old_item->size(); SERVER_LOG_DEBUG << "Erase " << key << " size: " << old_item->size();
...@@ -122,7 +128,8 @@ void Cache<ItemObj>::erase(const std::string& key) { ...@@ -122,7 +128,8 @@ void Cache<ItemObj>::erase(const std::string& key) {
} }
template<typename ItemObj> template<typename ItemObj>
void Cache<ItemObj>::clear() { void
Cache<ItemObj>::clear() {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
lru_.clear(); lru_.clear();
usage_ = 0; usage_ = 0;
...@@ -131,12 +138,13 @@ void Cache<ItemObj>::clear() { ...@@ -131,12 +138,13 @@ void Cache<ItemObj>::clear() {
/* free memory space when CACHE occupation exceed its capacity */ /* free memory space when CACHE occupation exceed its capacity */
template<typename ItemObj> template<typename ItemObj>
void Cache<ItemObj>::free_memory() { void
Cache<ItemObj>::free_memory() {
if (usage_ <= capacity_) return; if (usage_ <= capacity_) return;
int64_t threshhold = capacity_ * freemem_percent_; int64_t threshhold = capacity_ * freemem_percent_;
int64_t delta_size = usage_ - threshhold; int64_t delta_size = usage_ - threshhold;
if(delta_size <= 0) { if (delta_size <= 0) {
delta_size = 1;//ensure at least one item erased delta_size = 1;//ensure at least one item erased
} }
...@@ -148,8 +156,8 @@ void Cache<ItemObj>::free_memory() { ...@@ -148,8 +156,8 @@ void Cache<ItemObj>::free_memory() {
auto it = lru_.rbegin(); auto it = lru_.rbegin();
while (it != lru_.rend() && released_size < delta_size) { while (it != lru_.rend() && released_size < delta_size) {
auto& key = it->first; auto &key = it->first;
auto& obj_ptr = it->second; auto &obj_ptr = it->second;
key_array.emplace(key); key_array.emplace(key);
released_size += obj_ptr->size(); released_size += obj_ptr->size();
...@@ -159,7 +167,7 @@ void Cache<ItemObj>::free_memory() { ...@@ -159,7 +167,7 @@ void Cache<ItemObj>::free_memory() {
SERVER_LOG_DEBUG << "to be released memory size: " << released_size; SERVER_LOG_DEBUG << "to be released memory size: " << released_size;
for (auto& key : key_array) { for (auto &key : key_array) {
erase(key); erase(key);
} }
...@@ -167,7 +175,8 @@ void Cache<ItemObj>::free_memory() { ...@@ -167,7 +175,8 @@ void Cache<ItemObj>::free_memory() {
} }
template<typename ItemObj> template<typename ItemObj>
void Cache<ItemObj>::print() { void
Cache<ItemObj>::print() {
size_t cache_count = 0; size_t cache_count = 0;
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
...@@ -179,7 +188,7 @@ void Cache<ItemObj>::print() { ...@@ -179,7 +188,7 @@ void Cache<ItemObj>::print() {
SERVER_LOG_DEBUG << "[Cache capacity]: " << capacity_ << " bytes"; SERVER_LOG_DEBUG << "[Cache capacity]: " << capacity_ << " bytes";
} }
} // cache } // namespace cache
} // milvus } // namespace milvus
} // zilliz } // namespace zilliz
...@@ -22,22 +22,25 @@ ...@@ -22,22 +22,25 @@
#include "utils/Log.h" #include "utils/Log.h"
#include "metrics/Metrics.h" #include "metrics/Metrics.h"
#include <string>
#include <memory>
namespace zilliz { namespace zilliz {
namespace milvus { namespace milvus {
namespace cache { namespace cache {
template<typename ItemObj> template<typename ItemObj>
class CacheMgr { class CacheMgr {
public: public:
virtual uint64_t ItemCount() const; 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(); virtual void PrintInfo();
...@@ -47,18 +50,17 @@ public: ...@@ -47,18 +50,17 @@ public:
int64_t CacheCapacity() const; int64_t CacheCapacity() const;
void SetCapacity(int64_t capacity); void SetCapacity(int64_t capacity);
protected: protected:
CacheMgr(); CacheMgr();
virtual ~CacheMgr(); virtual ~CacheMgr();
protected: protected:
using CachePtr = std::shared_ptr<Cache<ItemObj>>; using CachePtr = std::shared_ptr<Cache<ItemObj>>;
CachePtr cache_; CachePtr cache_;
}; };
} // namespace cache
} } // namespace milvus
} } // namespace zilliz
}
#include "cache/CacheMgr.inl" #include "cache/CacheMgr.inl"
...@@ -30,18 +30,20 @@ CacheMgr<ItemObj>::~CacheMgr() { ...@@ -30,18 +30,20 @@ CacheMgr<ItemObj>::~CacheMgr() {
} }
template<typename ItemObj> template<typename ItemObj>
uint64_t CacheMgr<ItemObj>::ItemCount() const { uint64_t
if(cache_ == nullptr) { CacheMgr<ItemObj>::ItemCount() const {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return 0; return 0;
} }
return (uint64_t)(cache_->size()); return (uint64_t) (cache_->size());
} }
template<typename ItemObj> template<typename ItemObj>
bool CacheMgr<ItemObj>::ItemExists(const std::string& key) { bool
if(cache_ == nullptr) { CacheMgr<ItemObj>::ItemExists(const std::string &key) {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return false; return false;
} }
...@@ -50,8 +52,9 @@ bool CacheMgr<ItemObj>::ItemExists(const std::string& key) { ...@@ -50,8 +52,9 @@ bool CacheMgr<ItemObj>::ItemExists(const std::string& key) {
} }
template<typename ItemObj> template<typename ItemObj>
ItemObj CacheMgr<ItemObj>::GetItem(const std::string& key) { ItemObj
if(cache_ == nullptr) { CacheMgr<ItemObj>::GetItem(const std::string &key) {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return nullptr; return nullptr;
} }
...@@ -60,8 +63,9 @@ ItemObj CacheMgr<ItemObj>::GetItem(const std::string& key) { ...@@ -60,8 +63,9 @@ ItemObj CacheMgr<ItemObj>::GetItem(const std::string& key) {
} }
template<typename ItemObj> template<typename ItemObj>
void CacheMgr<ItemObj>::InsertItem(const std::string& key, const ItemObj& data) { void
if(cache_ == nullptr) { CacheMgr<ItemObj>::InsertItem(const std::string &key, const ItemObj &data) {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return; return;
} }
...@@ -71,8 +75,9 @@ void CacheMgr<ItemObj>::InsertItem(const std::string& key, const ItemObj& data) ...@@ -71,8 +75,9 @@ void CacheMgr<ItemObj>::InsertItem(const std::string& key, const ItemObj& data)
} }
template<typename ItemObj> template<typename ItemObj>
void CacheMgr<ItemObj>::EraseItem(const std::string& key) { void
if(cache_ == nullptr) { CacheMgr<ItemObj>::EraseItem(const std::string &key) {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return; return;
} }
...@@ -82,8 +87,9 @@ void CacheMgr<ItemObj>::EraseItem(const std::string& key) { ...@@ -82,8 +87,9 @@ void CacheMgr<ItemObj>::EraseItem(const std::string& key) {
} }
template<typename ItemObj> template<typename ItemObj>
void CacheMgr<ItemObj>::PrintInfo() { void
if(cache_ == nullptr) { CacheMgr<ItemObj>::PrintInfo() {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return; return;
} }
...@@ -92,8 +98,9 @@ void CacheMgr<ItemObj>::PrintInfo() { ...@@ -92,8 +98,9 @@ void CacheMgr<ItemObj>::PrintInfo() {
} }
template<typename ItemObj> template<typename ItemObj>
void CacheMgr<ItemObj>::ClearCache() { void
if(cache_ == nullptr) { CacheMgr<ItemObj>::ClearCache() {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return; return;
} }
...@@ -102,8 +109,9 @@ void CacheMgr<ItemObj>::ClearCache() { ...@@ -102,8 +109,9 @@ void CacheMgr<ItemObj>::ClearCache() {
} }
template<typename ItemObj> template<typename ItemObj>
int64_t CacheMgr<ItemObj>::CacheUsage() const { int64_t
if(cache_ == nullptr) { CacheMgr<ItemObj>::CacheUsage() const {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return 0; return 0;
} }
...@@ -112,8 +120,9 @@ int64_t CacheMgr<ItemObj>::CacheUsage() const { ...@@ -112,8 +120,9 @@ int64_t CacheMgr<ItemObj>::CacheUsage() const {
} }
template<typename ItemObj> template<typename ItemObj>
int64_t CacheMgr<ItemObj>::CacheCapacity() const { int64_t
if(cache_ == nullptr) { CacheMgr<ItemObj>::CacheCapacity() const {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return 0; return 0;
} }
...@@ -122,14 +131,15 @@ int64_t CacheMgr<ItemObj>::CacheCapacity() const { ...@@ -122,14 +131,15 @@ int64_t CacheMgr<ItemObj>::CacheCapacity() const {
} }
template<typename ItemObj> template<typename ItemObj>
void CacheMgr<ItemObj>::SetCapacity(int64_t capacity) { void
if(cache_ == nullptr) { CacheMgr<ItemObj>::SetCapacity(int64_t capacity) {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return; return;
} }
cache_->set_capacity(capacity); cache_->set_capacity(capacity);
} }
} } // namespace cache
} } // namespace milvus
} } // namespace zilliz
...@@ -16,20 +16,22 @@ ...@@ -16,20 +16,22 @@
// under the License. // under the License.
#include "CpuCacheMgr.h" #include "cache/CpuCacheMgr.h"
#include "server/Config.h" #include "server/Config.h"
#include "utils/Log.h" #include "utils/Log.h"
#include <utility>
namespace zilliz { namespace zilliz {
namespace milvus { namespace milvus {
namespace cache { namespace cache {
namespace { namespace {
constexpr int64_t unit = 1024 * 1024 * 1024; constexpr int64_t unit = 1024 * 1024 * 1024;
} }
CpuCacheMgr::CpuCacheMgr() { CpuCacheMgr::CpuCacheMgr() {
server::Config& config = server::Config::GetInstance(); server::Config &config = server::Config::GetInstance();
Status s; Status s;
int32_t cpu_mem_cap; int32_t cpu_mem_cap;
...@@ -38,7 +40,7 @@ CpuCacheMgr::CpuCacheMgr() { ...@@ -38,7 +40,7 @@ CpuCacheMgr::CpuCacheMgr() {
SERVER_LOG_ERROR << s.message(); SERVER_LOG_ERROR << s.message();
} }
int64_t cap = cpu_mem_cap * unit; int64_t cap = cpu_mem_cap * unit;
cache_ = std::make_shared<Cache<DataObjPtr>>(cap, 1UL<<32); cache_ = std::make_shared<Cache<DataObjPtr>>(cap, 1UL << 32);
float cpu_mem_threshold; float cpu_mem_threshold;
s = config.GetCacheConfigCpuMemThreshold(cpu_mem_threshold); s = config.GetCacheConfigCpuMemThreshold(cpu_mem_threshold);
...@@ -53,20 +55,22 @@ CpuCacheMgr::CpuCacheMgr() { ...@@ -53,20 +55,22 @@ CpuCacheMgr::CpuCacheMgr() {
} }
} }
CpuCacheMgr* CpuCacheMgr::GetInstance() { CpuCacheMgr *
CpuCacheMgr::GetInstance() {
static CpuCacheMgr s_mgr; static CpuCacheMgr s_mgr;
return &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); DataObjPtr obj = GetItem(key);
if(obj != nullptr) { if (obj != nullptr) {
return obj->data(); return obj->data();
} }
return nullptr; return nullptr;
} }
} } // namespace cache
} } // namespace milvus
} } // namespace zilliz
\ No newline at end of file
...@@ -20,21 +20,24 @@ ...@@ -20,21 +20,24 @@
#include "CacheMgr.h" #include "CacheMgr.h"
#include "DataObj.h" #include "DataObj.h"
#include <string>
#include <memory>
namespace zilliz { namespace zilliz {
namespace milvus { namespace milvus {
namespace cache { namespace cache {
class CpuCacheMgr : public CacheMgr<DataObjPtr> { class CpuCacheMgr : public CacheMgr<DataObjPtr> {
private: private:
CpuCacheMgr(); CpuCacheMgr();
public: public:
//TODO: use smart pointer instead //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
...@@ -27,38 +27,43 @@ namespace milvus { ...@@ -27,38 +27,43 @@ namespace milvus {
namespace cache { namespace cache {
class DataObj { class DataObj {
public: public:
DataObj(const engine::VecIndexPtr& index) explicit DataObj(const engine::VecIndexPtr &index)
: index_(index) : index_(index) {
{} }
DataObj(const engine::VecIndexPtr& index, int64_t size) DataObj(const engine::VecIndexPtr &index, int64_t size)
: index_(index), : index_(index),
size_(size) size_(size) {
{} }
engine::VecIndexPtr data() { return index_; } engine::VecIndexPtr data() {
const engine::VecIndexPtr& data() const { return index_; } return index_;
}
const engine::VecIndexPtr &data() const {
return index_;
}
int64_t size() const { int64_t size() const {
if(index_ == nullptr) { if (index_ == nullptr) {
return 0; return 0;
} }
if(size_ > 0) { if (size_ > 0) {
return size_; return size_;
} }
return index_->Count() * index_->Dimension() * sizeof(float); return index_->Count() * index_->Dimension() * sizeof(float);
} }
private: private:
engine::VecIndexPtr index_ = nullptr; engine::VecIndexPtr index_ = nullptr;
int64_t size_ = 0; int64_t size_ = 0;
}; };
using DataObjPtr = std::shared_ptr<DataObj>; using DataObjPtr = std::shared_ptr<DataObj>;
} } // namespace cache
} } // namespace milvus
} } // namespace zilliz
\ No newline at end of file
...@@ -16,11 +16,13 @@ ...@@ -16,11 +16,13 @@
// under the License. // under the License.
#include <sstream> #include "cache/GpuCacheMgr.h"
#include "utils/Log.h" #include "utils/Log.h"
#include "GpuCacheMgr.h"
#include "server/Config.h" #include "server/Config.h"
#include <sstream>
#include <utility>
namespace zilliz { namespace zilliz {
namespace milvus { namespace milvus {
namespace cache { namespace cache {
...@@ -29,11 +31,11 @@ std::mutex GpuCacheMgr::mutex_; ...@@ -29,11 +31,11 @@ std::mutex GpuCacheMgr::mutex_;
std::unordered_map<uint64_t, GpuCacheMgrPtr> GpuCacheMgr::instance_; std::unordered_map<uint64_t, GpuCacheMgrPtr> GpuCacheMgr::instance_;
namespace { namespace {
constexpr int64_t G_BYTE = 1024 * 1024 * 1024; constexpr int64_t G_BYTE = 1024 * 1024 * 1024;
} }
GpuCacheMgr::GpuCacheMgr() { GpuCacheMgr::GpuCacheMgr() {
server::Config& config = server::Config::GetInstance(); server::Config &config = server::Config::GetInstance();
Status s; Status s;
int32_t gpu_mem_cap; int32_t gpu_mem_cap;
...@@ -42,7 +44,7 @@ GpuCacheMgr::GpuCacheMgr() { ...@@ -42,7 +44,7 @@ GpuCacheMgr::GpuCacheMgr() {
SERVER_LOG_ERROR << s.message(); SERVER_LOG_ERROR << s.message();
} }
int32_t cap = gpu_mem_cap * G_BYTE; int32_t cap = gpu_mem_cap * G_BYTE;
cache_ = std::make_shared<Cache<DataObjPtr>>(cap, 1UL<<32); cache_ = std::make_shared<Cache<DataObjPtr>>(cap, 1UL << 32);
float gpu_mem_threshold; float gpu_mem_threshold;
s = config.GetCacheConfigGpuMemThreshold(gpu_mem_threshold); s = config.GetCacheConfigGpuMemThreshold(gpu_mem_threshold);
...@@ -57,7 +59,8 @@ GpuCacheMgr::GpuCacheMgr() { ...@@ -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()) { if (instance_.find(gpu_id) == instance_.end()) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
if (instance_.find(gpu_id) == instance_.end()) { if (instance_.find(gpu_id) == instance_.end()) {
...@@ -70,15 +73,16 @@ GpuCacheMgr* GpuCacheMgr::GetInstance(uint64_t gpu_id) { ...@@ -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); DataObjPtr obj = GetItem(key);
if(obj != nullptr) { if (obj != nullptr) {
return obj->data(); return obj->data();
} }
return nullptr; return nullptr;
} }
} } // namespace cache
} } // namespace milvus
} } // namespace zilliz
\ No newline at end of file
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include <unordered_map> #include <unordered_map>
#include <memory> #include <memory>
#include <string>
namespace zilliz { namespace zilliz {
namespace milvus { namespace milvus {
...@@ -30,18 +31,18 @@ class GpuCacheMgr; ...@@ -30,18 +31,18 @@ class GpuCacheMgr;
using GpuCacheMgrPtr = std::shared_ptr<GpuCacheMgr>; using GpuCacheMgrPtr = std::shared_ptr<GpuCacheMgr>;
class GpuCacheMgr : public CacheMgr<DataObjPtr> { class GpuCacheMgr : public CacheMgr<DataObjPtr> {
public: public:
GpuCacheMgr(); 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::mutex mutex_;
static std::unordered_map<uint64_t, GpuCacheMgrPtr> instance_; static std::unordered_map<uint64_t, GpuCacheMgrPtr> instance_;
}; };
} } // namespace cache
} } // namespace milvus
} } // namespace zilliz
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <list> #include <list>
#include <cstddef> #include <cstddef>
#include <stdexcept> #include <stdexcept>
#include <utility>
namespace zilliz { namespace zilliz {
namespace milvus { namespace milvus {
...@@ -29,14 +30,15 @@ namespace cache { ...@@ -29,14 +30,15 @@ namespace cache {
template<typename key_t, typename value_t> template<typename key_t, typename value_t>
class LRU { class LRU {
public: public:
typedef typename std::pair<key_t, value_t> key_value_pair_t; typedef typename std::pair<key_t, value_t> key_value_pair_t;
typedef typename std::list<key_value_pair_t>::iterator list_iterator_t; typedef typename std::list<key_value_pair_t>::iterator list_iterator_t;
typedef typename std::list<key_value_pair_t>::reverse_iterator reverse_list_iterator_t; typedef typename std::list<key_value_pair_t>::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); auto it = cache_items_map_.find(key);
cache_items_list_.push_front(key_value_pair_t(key, value)); cache_items_list_.push_front(key_value_pair_t(key, value));
if (it != cache_items_map_.end()) { if (it != cache_items_map_.end()) {
...@@ -53,7 +55,7 @@ public: ...@@ -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); auto it = cache_items_map_.find(key);
if (it == cache_items_map_.end()) { if (it == cache_items_map_.end()) {
throw std::range_error("There is no such key in cache"); throw std::range_error("There is no such key in cache");
...@@ -63,7 +65,7 @@ public: ...@@ -63,7 +65,7 @@ public:
} }
} }
void erase(const key_t& key) { void erase(const key_t &key) {
auto it = cache_items_map_.find(key); auto it = cache_items_map_.find(key);
if (it != cache_items_map_.end()) { if (it != cache_items_map_.end()) {
cache_items_list_.erase(it->second); cache_items_list_.erase(it->second);
...@@ -71,7 +73,7 @@ public: ...@@ -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(); return cache_items_map_.find(key) != cache_items_map_.end();
} }
...@@ -101,14 +103,14 @@ public: ...@@ -101,14 +103,14 @@ public:
cache_items_map_.clear(); cache_items_map_.clear();
} }
private: private:
std::list<key_value_pair_t> cache_items_list_; std::list<key_value_pair_t> cache_items_list_;
std::unordered_map<key_t, list_iterator_t> cache_items_map_; std::unordered_map<key_t, list_iterator_t> cache_items_map_;
size_t max_size_; size_t max_size_;
list_iterator_t iter_; list_iterator_t iter_;
}; };
} // cache } // namespace cache
} // milvus } // namespace milvus
} // zilliz } // namespace zilliz
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册