modify lruCache class to private

category:bugfix
issue:https://gitee.com/openharmony/commonlibrary_memory_utils/issues/I7O9UI?from=project-issue

Signed-off-by: surencong <surencong@huawei.com>
Change-Id: Ibb511234c7269c828bb770d051f3520c38e86ac5
This commit is contained in:
surencong 2023-07-26 19:09:25 +08:00
parent 532ffc7c60
commit 05b435896e
4 changed files with 85 additions and 77 deletions

View File

@ -21,6 +21,7 @@
#include <string>
#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 */

View File

@ -21,64 +21,12 @@
#include <string>
#include <unordered_map>
#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<PurgeableMemBase> key);
/*
* Insert: insert the PurgeableMemBase key in the lrucache.
* Input: @key: ptr of PurgeableMemBase.
*/
void Insert(std::shared_ptr<PurgeableMemBase> key);
/*
* Erase: erase the PurgeableMemBase key in the lrucache.
* Input: @key: ptr of PurgeableMemBase.
*/
void Erase(std::shared_ptr<PurgeableMemBase> 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<std::shared_ptr<PurgeableMemBase>>::iterator;
std::list<std::shared_ptr<PurgeableMemBase>> GetResourcePtrList() const;
std::shared_ptr<PurgeableMemBase> GetLastResourcePtr() const;
size_t Size() const;
private:
int32_t lruCacheCapacity_;
std::list<std::shared_ptr<PurgeableMemBase>> resourcePtrList_;
std::unordered_map<std::shared_ptr<PurgeableMemBase>, 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<PurgeableMemBase> key);
mutable std::mutex mutex_;
/*
* Insert: insert the PurgeableMemBase key in the lrucache.
* Input: @key: ptr of PurgeableMemBase.
*/
void Insert(std::shared_ptr<PurgeableMemBase> key);
/*
* Erase: erase the PurgeableMemBase key in the lrucache.
* Input: @key: ptr of PurgeableMemBase.
*/
void Erase(std::shared_ptr<PurgeableMemBase> 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<std::shared_ptr<PurgeableMemBase>>::iterator;
std::list<std::shared_ptr<PurgeableMemBase>> GetResourcePtrList() const;
std::shared_ptr<PurgeableMemBase> GetLastResourcePtr() const;
size_t Size() const;
private:
int32_t lruCacheCapacity_;
std::list<std::shared_ptr<PurgeableMemBase>> resourcePtrList_;
std::unordered_map<std::shared_ptr<PurgeableMemBase>, ListSharedPtrIterator> positionMap_;
};
friend class PurgeableMemBase;
mutable std::mutex lruCacheMutex_;
LruCache lruCache_;
ThreadPool threadPool_ {THREAD_POOL_NAME};
bool isThreadPoolStarted_ {false};

View File

@ -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<PurgeableMemBase> 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<PurgeableMemBase> key)
{
if (key == nullptr) {
return;
@ -33,7 +48,7 @@ void LruCache::Visited(std::shared_ptr<PurgeableMemBase> key)
}
}
void LruCache::Insert(std::shared_ptr<PurgeableMemBase> key)
void PurgeableResourceManager::LruCache::Insert(std::shared_ptr<PurgeableMemBase> key)
{
if (key == nullptr) {
return;
@ -61,7 +76,7 @@ void LruCache::Insert(std::shared_ptr<PurgeableMemBase> key)
}
}
void LruCache::Erase(std::shared_ptr<PurgeableMemBase> key)
void PurgeableResourceManager::LruCache::Erase(std::shared_ptr<PurgeableMemBase> key)
{
if (key == nullptr) {
return;
@ -83,7 +98,7 @@ void LruCache::Erase(std::shared_ptr<PurgeableMemBase> 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<std::shared_ptr<PurgeableMemBase>> LruCache::GetResourcePtrList() const
std::list<std::shared_ptr<PurgeableMemBase>> PurgeableResourceManager::LruCache::GetResourcePtrList() const
{
return resourcePtrList_;
}
std::shared_ptr<PurgeableMemBase> LruCache::GetLastResourcePtr() const
std::shared_ptr<PurgeableMemBase> 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<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> lock(lruCacheMutex_);
if (isThreadPoolStarted_) {
threadPool_.Stop();
}
@ -146,7 +161,7 @@ PurgeableResourceManager &PurgeableResourceManager::GetInstance()
void PurgeableResourceManager::BeginAccessPurgeableMem()
{
std::lock_guard<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> lock(lruCacheMutex_);
StartTrace(HITRACE_TAG_COMMONLIBRARY, "OHOS::PurgeableMem::PurgeableResourceManager::BeginAccessPurgeableMem");
std::list<std::shared_ptr<PurgeableMemBase>> resourcePtrList = lruCache_.GetResourcePtrList();
@ -173,7 +188,7 @@ void PurgeableResourceManager::BeginAccessPurgeableMem()
void PurgeableResourceManager::EndAccessPurgeableMem()
{
std::lock_guard<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> lock(lruCacheMutex_);
StartTrace(HITRACE_TAG_COMMONLIBRARY, "OHOS::PurgeableMem::PurgeableResourceManager::EndAccessPurgeableMem");
std::list<std::shared_ptr<PurgeableMemBase>> resourcePtrList = lruCache_.GetResourcePtrList();
@ -200,7 +215,7 @@ void PurgeableResourceManager::EndAccessPurgeableMem()
void PurgeableResourceManager::AddResource(std::shared_ptr<PurgeableMemBase> resourcePtr)
{
std::lock_guard<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> 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<PurgeableMemBase> res
void PurgeableResourceManager::RemoveResource(std::shared_ptr<PurgeableMemBase> resourcePtr)
{
std::lock_guard<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> 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<PurgeableMemBase>
void PurgeableResourceManager::SetRecentUsedResource(std::shared_ptr<PurgeableMemBase> resourcePtr)
{
std::lock_guard<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> lock(lruCacheMutex_);
if (resourcePtr == nullptr) {
return;
}
@ -240,19 +255,19 @@ void PurgeableResourceManager::SetRecentUsedResource(std::shared_ptr<PurgeableMe
void PurgeableResourceManager::SetLruCacheCapacity(int32_t capacity)
{
std::lock_guard<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> lock(lruCacheMutex_);
lruCache_.SetCapacity(capacity);
}
void PurgeableResourceManager::Clear()
{
std::lock_guard<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> lock(lruCacheMutex_);
lruCache_.Clear();
}
void PurgeableResourceManager::RemoveLastResource()
{
std::lock_guard<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> lock(lruCacheMutex_);
std::list<std::shared_ptr<PurgeableMemBase>> resourcePtrList = lruCache_.GetResourcePtrList();
int cnt = 0;
for (auto &resourcePtr : resourcePtrList) {

View File

@ -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<PurgeableMemBase> key = std::make_shared<PurgeableMemBase>();
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<PurgeableMemBase> key = std::make_shared<PurgeableMemBase>();
std::shared_ptr<PurgeableMemBase> key1 = std::make_shared<PurgeableMemBase>();
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<PurgeableMemBase> key = std::make_shared<PurgeableMemBase>();
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<PurgeableMemBase> key = std::make_shared<PurgeableMemBase>();
std::shared_ptr<PurgeableMemBase> key1 = std::make_shared<PurgeableMemBase>();
int32_t capacity = -1;
LruCache lrucache;
PurgeableResourceManager::LruCache lrucache;
lrucache.SetCapacity(capacity);
capacity = MAX_LRU_CACHE_CAPACITY + 1;
lrucache.SetCapacity(capacity);