logic: implemented shrinking shared memory to fit required amount from time to time

This commit is contained in:
mlangkabel
2018-11-05 10:38:48 +01:00
parent f13aec70dd
commit d2cd236318
6 changed files with 40 additions and 17 deletions
@@ -13,10 +13,6 @@ BaseInterprocessDataManager::BaseInterprocessDataManager(
{
}
BaseInterprocessDataManager::~BaseInterprocessDataManager()
{
}
Id BaseInterprocessDataManager::getProcessId() const
{
return m_processId;
@@ -15,7 +15,8 @@ public:
const std::string& instanceUuid,
Id processId,
bool isOwner);
virtual ~BaseInterprocessDataManager();
virtual ~BaseInterprocessDataManager() = default;
Id getProcessId() const;
@@ -17,34 +17,39 @@ InterprocessIntermediateStorageManager::InterprocessIntermediateStorageManager(
instanceUuid,
processId,
isOwner)
{
}
InterprocessIntermediateStorageManager::~InterprocessIntermediateStorageManager()
, m_insertsWithoutGrowth(0)
{
}
void InterprocessIntermediateStorageManager::pushIntermediateStorage(
const std::shared_ptr<IntermediateStorage>& intermediateStorage)
{
const size_t requiredInsertsToShrink = 10;
const size_t overestimationMultiplier = 2;
size_t size = (intermediateStorage->getByteSize(sizeof(SharedMemory::String)) +
const size_t requiredSize = (intermediateStorage->getByteSize(sizeof(SharedMemory::String)) +
sizeof(SharedIntermediateStorage)) * overestimationMultiplier + 1048576/* 1 MB */;
SharedMemory::ScopedAccess access(&m_sharedMemory);
size_t freeMemory = access.getFreeMemorySize();
if (freeMemory < size)
const size_t freeMemory = access.getFreeMemorySize();
if (freeMemory < requiredSize)
{
size_t requiredSize = size - freeMemory;
const size_t requiredGrowth = requiredSize - freeMemory;
LOG_INFO_STREAM(
<< "grow memory - est: " << size << " size: " << access.getMemorySize()
<< " free: " << access.getFreeMemorySize() << " alloc: " << requiredSize);
<< "grow memory - est: " << requiredSize << " size: " << access.getMemorySize()
<< " free: " << access.getFreeMemorySize() << " alloc: " << requiredGrowth);
access.growMemory(requiredSize);
access.growMemory(requiredGrowth);
LOG_INFO("growing memory succeeded");
m_insertsWithoutGrowth = 0;
}
else
{
m_insertsWithoutGrowth++;
}
SharedMemory::Queue<SharedIntermediateStorage>* queue =
@@ -69,6 +74,15 @@ void InterprocessIntermediateStorageManager::pushIntermediateStorage(
storage.setNextId(intermediateStorage->getNextId());
if (m_insertsWithoutGrowth >= requiredInsertsToShrink)
{
m_insertsWithoutGrowth = 0;
LOG_INFO("shrinking shared memory");
access.shrinkToFitMemory();
LOG_INFO_STREAM(<< "shrunk memory - size: " << access.getMemorySize() << " free: " << access.getFreeMemorySize());
}
LOG_INFO(access.logString());
}
@@ -10,7 +10,7 @@ class InterprocessIntermediateStorageManager
{
public:
InterprocessIntermediateStorageManager(const std::string& instanceUuid, Id processId, bool isOwner);
virtual ~InterprocessIntermediateStorageManager();
virtual ~InterprocessIntermediateStorageManager() = default;
void pushIntermediateStorage(const std::shared_ptr<IntermediateStorage>& intermediateStorage);
std::shared_ptr<IntermediateStorage> popIntermediateStorage();
@@ -20,6 +20,8 @@ public:
private:
static const char* s_sharedMemoryNamePrefix;
static const char* s_intermediatStoragesKeyName;
size_t m_insertsWithoutGrowth;
};
#endif // INTERPROCESS_INTERMEDIATE_STORAGE_MANAGER_H
@@ -46,6 +46,15 @@ void SharedMemory::ScopedAccess::growMemory(size_t size)
m_memory = boost::interprocess::managed_shared_memory(boost::interprocess::open_only, m_memoryName.c_str());
}
void SharedMemory::ScopedAccess::shrinkToFitMemory()
{
m_memory = boost::interprocess::managed_shared_memory();
boost::interprocess::managed_shared_memory::shrink_to_fit(m_memoryName.c_str());
m_memory = boost::interprocess::managed_shared_memory(boost::interprocess::open_only, m_memoryName.c_str());
}
std::string SharedMemory::ScopedAccess::logString() const
{
std::string log = m_memoryName + " -";
@@ -62,6 +62,7 @@ public:
size_t getUsedMemorySize() const;
void growMemory(size_t size);
void shrinkToFitMemory();
template <typename T>
T* accessValue(const std::string& key)