From 05b435896e7cb7f3e3d828fa974e5595becae5b0 Mon Sep 17 00:00:00 2001 From: surencong Date: Wed, 26 Jul 2023 19:09:25 +0800 Subject: [PATCH] modify lruCache class to private category:bugfix issue:https://gitee.com/openharmony/commonlibrary_memory_utils/issues/I7O9UI?from=project-issue Signed-off-by: surencong Change-Id: Ibb511234c7269c828bb770d051f3520c38e86ac5 --- .../cpp/include/purgeable_mem_base.h | 3 +- .../cpp/include/purgeable_resource_manager.h | 98 +++++++++---------- .../cpp/src/purgeable_resource_manager.cpp | 51 ++++++---- .../test/purgeableresourcemanager_test.cpp | 10 +- 4 files changed, 85 insertions(+), 77 deletions(-) diff --git a/libpurgeablemem/cpp/include/purgeable_mem_base.h b/libpurgeablemem/cpp/include/purgeable_mem_base.h index 4bf2f74..0b6d7c9 100644 --- a/libpurgeablemem/cpp/include/purgeable_mem_base.h +++ b/libpurgeablemem/cpp/include/purgeable_mem_base.h @@ -21,6 +21,7 @@ #include #include "purgeable_mem_builder.h" +#include "purgeable_resource_manager.h" #include "ux_page_table.h" namespace OHOS { @@ -119,7 +120,7 @@ protected: virtual int GetPinStatus() const; virtual void AfterRebuildSucc(); virtual std::string ToString() const; - friend class LruCache; + friend class PurgeableResourceManager::LruCache; }; } /* namespace PurgeableMem */ } /* namespace OHOS */ diff --git a/libpurgeablemem/cpp/include/purgeable_resource_manager.h b/libpurgeablemem/cpp/include/purgeable_resource_manager.h index 88a45d2..e0a6926 100644 --- a/libpurgeablemem/cpp/include/purgeable_resource_manager.h +++ b/libpurgeablemem/cpp/include/purgeable_resource_manager.h @@ -21,64 +21,12 @@ #include #include -#include "purgeable_mem_base.h" #include "thread_pool.h" namespace OHOS { namespace PurgeableMem { -/* System parameter name */ -const std::string THREAD_POOL_TASK_NUMBER_SYS_NAME = "persist.commonlibrary.purgeable.threadpooltasknum"; -const std::string LRU_CACHE_CAPACITY_SYS_NAME = "persist.commonlibrary.purgeable.lrucachecapacity"; const std::string THREAD_POOL_NAME = "PurgeThread"; -/* Threadpool task number and lrucache capacity */ -constexpr int32_t THREAD_POOL_TASK_NUMBER = 4; -constexpr int32_t MIN_THREAD_POOL_TASK_NUMBER = 1; -constexpr int32_t MAX_THREAD_POOL_TASK_NUMBER = 20; -constexpr int32_t LRU_CACHE_CAPACITY = 200; -constexpr int32_t MIN_LRU_CACHE_CAPACITY = 1; -constexpr int32_t MAX_LRU_CACHE_CAPACITY = 2000; - -class LruCache { -public: - /* - * Visited: visit the cache entry with the given key. - * If the entry is found, it will be move to the most-recent position in the cache. - */ - void Visited(std::shared_ptr key); - - /* - * Insert: insert the PurgeableMemBase key in the lrucache. - * Input: @key: ptr of PurgeableMemBase. - */ - void Insert(std::shared_ptr key); - - /* - * Erase: erase the PurgeableMemBase key in the lrucache. - * Input: @key: ptr of PurgeableMemBase. - */ - void Erase(std::shared_ptr key); - - /* - * SetCapacity: set the capacity of the lrucache. - * Input: the capacity of lrucache. - */ - void SetCapacity(int32_t capacity); - - /* - * Clear: clear the resourcePtrList and positionMap of the lrucache. - */ - void Clear(); - - using ListSharedPtrIterator = std::list>::iterator; - std::list> GetResourcePtrList() const; - std::shared_ptr GetLastResourcePtr() const; - size_t Size() const; - -private: - int32_t lruCacheCapacity_; - std::list> resourcePtrList_; - std::unordered_map, ListSharedPtrIterator> positionMap_; -}; +class PurgeableMemBase; class PurgeableResourceManager { public: @@ -103,8 +51,50 @@ private: int32_t GetThreadPoolTaskNumFromSysPara() const; int32_t GetLruCacheCapacityFromSysPara() const; void StartThreadPool(); + class LruCache { + public: + /* + * Visited: visit the cache entry with the given key. + * If the entry is found, it will be move to the most-recent position in the cache. + */ + void Visited(std::shared_ptr key); + + /* + * Insert: insert the PurgeableMemBase key in the lrucache. + * Input: @key: ptr of PurgeableMemBase. + */ + void Insert(std::shared_ptr key); + + /* + * Erase: erase the PurgeableMemBase key in the lrucache. + * Input: @key: ptr of PurgeableMemBase. + */ + void Erase(std::shared_ptr key); + + /* + * SetCapacity: set the capacity of the lrucache. + * Input: the capacity of lrucache. + */ + void SetCapacity(int32_t capacity); + + /* + * Clear: clear the resourcePtrList and positionMap of the lrucache. + */ + void Clear(); + + using ListSharedPtrIterator = std::list>::iterator; + std::list> GetResourcePtrList() const; + std::shared_ptr GetLastResourcePtr() const; + size_t Size() const; + + private: + int32_t lruCacheCapacity_; + std::list> resourcePtrList_; + std::unordered_map, ListSharedPtrIterator> positionMap_; + }; + friend class PurgeableMemBase; - mutable std::mutex mutex_; + mutable std::mutex lruCacheMutex_; LruCache lruCache_; ThreadPool threadPool_ {THREAD_POOL_NAME}; bool isThreadPoolStarted_ {false}; diff --git a/libpurgeablemem/cpp/src/purgeable_resource_manager.cpp b/libpurgeablemem/cpp/src/purgeable_resource_manager.cpp index 1daf7c6..e5d6ffd 100644 --- a/libpurgeablemem/cpp/src/purgeable_resource_manager.cpp +++ b/libpurgeablemem/cpp/src/purgeable_resource_manager.cpp @@ -16,11 +16,26 @@ #include "hitrace_meter.h" #include "parameters.h" #include "pm_log.h" +#include "purgeable_mem_base.h" #include "purgeable_resource_manager.h" namespace OHOS { namespace PurgeableMem { -void LruCache::Visited(std::shared_ptr key) +namespace { +/* System parameter name */ +const std::string THREAD_POOL_TASK_NUMBER_SYS_NAME = "persist.commonlibrary.purgeable.threadpooltasknum"; +const std::string LRU_CACHE_CAPACITY_SYS_NAME = "persist.commonlibrary.purgeable.lrucachecapacity"; + +/* Threadpool task number and lrucache capacity */ +constexpr int32_t THREAD_POOL_TASK_NUMBER = 4; +constexpr int32_t MIN_THREAD_POOL_TASK_NUMBER = 1; +constexpr int32_t MAX_THREAD_POOL_TASK_NUMBER = 20; +constexpr int32_t LRU_CACHE_CAPACITY = 200; +constexpr int32_t MIN_LRU_CACHE_CAPACITY = 1; +constexpr int32_t MAX_LRU_CACHE_CAPACITY = 2000; +} + +void PurgeableResourceManager::LruCache::Visited(std::shared_ptr key) { if (key == nullptr) { return; @@ -33,7 +48,7 @@ void LruCache::Visited(std::shared_ptr key) } } -void LruCache::Insert(std::shared_ptr key) +void PurgeableResourceManager::LruCache::Insert(std::shared_ptr key) { if (key == nullptr) { return; @@ -61,7 +76,7 @@ void LruCache::Insert(std::shared_ptr key) } } -void LruCache::Erase(std::shared_ptr key) +void PurgeableResourceManager::LruCache::Erase(std::shared_ptr key) { if (key == nullptr) { return; @@ -83,7 +98,7 @@ void LruCache::Erase(std::shared_ptr key) positionMap_.erase(key); } -void LruCache::SetCapacity(int32_t capacity) +void PurgeableResourceManager::LruCache::SetCapacity(int32_t capacity) { if (capacity < 0 || capacity > MAX_LRU_CACHE_CAPACITY) { PM_HILOG_DEBUG(LOG_CORE, "[PurgeableResourceManager] SetCapacity FAILED: capacity value is invalid!"); @@ -96,23 +111,23 @@ void LruCache::SetCapacity(int32_t capacity) } } -void LruCache::Clear() +void PurgeableResourceManager::LruCache::Clear() { positionMap_.clear(); resourcePtrList_.clear(); } -std::list> LruCache::GetResourcePtrList() const +std::list> PurgeableResourceManager::LruCache::GetResourcePtrList() const { return resourcePtrList_; } -std::shared_ptr LruCache::GetLastResourcePtr() const +std::shared_ptr PurgeableResourceManager::LruCache::GetLastResourcePtr() const { return resourcePtrList_.back(); } -size_t LruCache::Size() const +size_t PurgeableResourceManager::LruCache::Size() const { return positionMap_.size(); } @@ -131,7 +146,7 @@ PurgeableResourceManager::PurgeableResourceManager() PurgeableResourceManager::~PurgeableResourceManager() { - std::lock_guard lock(mutex_); + std::lock_guard lock(lruCacheMutex_); if (isThreadPoolStarted_) { threadPool_.Stop(); } @@ -146,7 +161,7 @@ PurgeableResourceManager &PurgeableResourceManager::GetInstance() void PurgeableResourceManager::BeginAccessPurgeableMem() { - std::lock_guard lock(mutex_); + std::lock_guard lock(lruCacheMutex_); StartTrace(HITRACE_TAG_COMMONLIBRARY, "OHOS::PurgeableMem::PurgeableResourceManager::BeginAccessPurgeableMem"); std::list> resourcePtrList = lruCache_.GetResourcePtrList(); @@ -173,7 +188,7 @@ void PurgeableResourceManager::BeginAccessPurgeableMem() void PurgeableResourceManager::EndAccessPurgeableMem() { - std::lock_guard lock(mutex_); + std::lock_guard lock(lruCacheMutex_); StartTrace(HITRACE_TAG_COMMONLIBRARY, "OHOS::PurgeableMem::PurgeableResourceManager::EndAccessPurgeableMem"); std::list> resourcePtrList = lruCache_.GetResourcePtrList(); @@ -200,7 +215,7 @@ void PurgeableResourceManager::EndAccessPurgeableMem() void PurgeableResourceManager::AddResource(std::shared_ptr resourcePtr) { - std::lock_guard lock(mutex_); + std::lock_guard lock(lruCacheMutex_); StartTrace(HITRACE_TAG_COMMONLIBRARY, "OHOS::PurgeableMem::PurgeableResourceManager::AddResource"); if (resourcePtr == nullptr) { FinishTrace(HITRACE_TAG_COMMONLIBRARY); @@ -215,7 +230,7 @@ void PurgeableResourceManager::AddResource(std::shared_ptr res void PurgeableResourceManager::RemoveResource(std::shared_ptr resourcePtr) { - std::lock_guard lock(mutex_); + std::lock_guard lock(lruCacheMutex_); StartTrace(HITRACE_TAG_COMMONLIBRARY, "OHOS::PurgeableMem::PurgeableResourceManager::RemoveResource"); if (resourcePtr == nullptr) { FinishTrace(HITRACE_TAG_COMMONLIBRARY); @@ -230,7 +245,7 @@ void PurgeableResourceManager::RemoveResource(std::shared_ptr void PurgeableResourceManager::SetRecentUsedResource(std::shared_ptr resourcePtr) { - std::lock_guard lock(mutex_); + std::lock_guard lock(lruCacheMutex_); if (resourcePtr == nullptr) { return; } @@ -240,19 +255,19 @@ void PurgeableResourceManager::SetRecentUsedResource(std::shared_ptr lock(mutex_); + std::lock_guard lock(lruCacheMutex_); lruCache_.SetCapacity(capacity); } void PurgeableResourceManager::Clear() { - std::lock_guard lock(mutex_); + std::lock_guard lock(lruCacheMutex_); lruCache_.Clear(); } void PurgeableResourceManager::RemoveLastResource() { - std::lock_guard lock(mutex_); + std::lock_guard lock(lruCacheMutex_); StartTrace(HITRACE_TAG_COMMONLIBRARY, "OHOS::PurgeableMem::PurgeableResourceManager::RemoveLastResource"); if (lruCache_.Size() == 0) { FinishTrace(HITRACE_TAG_COMMONLIBRARY); @@ -273,7 +288,7 @@ void PurgeableResourceManager::RemoveLastResource() void PurgeableResourceManager::ShowLruCache() const { - std::lock_guard lock(mutex_); + std::lock_guard lock(lruCacheMutex_); std::list> resourcePtrList = lruCache_.GetResourcePtrList(); int cnt = 0; for (auto &resourcePtr : resourcePtrList) { diff --git a/libpurgeablemem/test/purgeableresourcemanager_test.cpp b/libpurgeablemem/test/purgeableresourcemanager_test.cpp index 9727a54..df3d66a 100644 --- a/libpurgeablemem/test/purgeableresourcemanager_test.cpp +++ b/libpurgeablemem/test/purgeableresourcemanager_test.cpp @@ -17,6 +17,7 @@ #define private public #define protected public +#include "purgeable_mem_base.h" #include "purgeable_resource_manager.h" #undef private #undef protected @@ -25,6 +26,7 @@ namespace OHOS { namespace PurgeableMem { using namespace testing; using namespace testing::ext; +constexpr int32_t MAX_LRU_CACHE_CAPACITY = 2000; class PurgeableResourceManagerTest : public testing::Test { public: @@ -54,7 +56,7 @@ HWTEST_F(PurgeableResourceManagerTest, VisitedTest, TestSize.Level1) { std::shared_ptr key = std::make_shared(); int32_t capacity = 1; - LruCache lrucache; + PurgeableResourceManager::LruCache lrucache; lrucache.SetCapacity(capacity); lrucache.Visited(nullptr); lrucache.Visited(key); @@ -70,7 +72,7 @@ HWTEST_F(PurgeableResourceManagerTest, InsertTest, TestSize.Level1) std::shared_ptr key = std::make_shared(); std::shared_ptr key1 = std::make_shared(); int32_t capacity = 1; - LruCache lrucache; + PurgeableResourceManager::LruCache lrucache; lrucache.SetCapacity(capacity); lrucache.Insert(nullptr); lrucache.Insert(key); @@ -86,7 +88,7 @@ HWTEST_F(PurgeableResourceManagerTest, EraseTest, TestSize.Level1) { std::shared_ptr key = std::make_shared(); int32_t capacity = 1; - LruCache lrucache; + PurgeableResourceManager::LruCache lrucache; lrucache.SetCapacity(capacity); lrucache.Clear(); lrucache.Erase(nullptr); @@ -103,7 +105,7 @@ HWTEST_F(PurgeableResourceManagerTest, SetCapacityTest, TestSize.Level1) std::shared_ptr key = std::make_shared(); std::shared_ptr key1 = std::make_shared(); int32_t capacity = -1; - LruCache lrucache; + PurgeableResourceManager::LruCache lrucache; lrucache.SetCapacity(capacity); capacity = MAX_LRU_CACHE_CAPACITY + 1; lrucache.SetCapacity(capacity); -- Gitee