diff --git a/src/lib/data/indexer/interprocess/BaseInterprocessDataManager.cpp b/src/lib/data/indexer/interprocess/BaseInterprocessDataManager.cpp index 1132036c..7b72016c 100644 --- a/src/lib/data/indexer/interprocess/BaseInterprocessDataManager.cpp +++ b/src/lib/data/indexer/interprocess/BaseInterprocessDataManager.cpp @@ -13,10 +13,6 @@ BaseInterprocessDataManager::BaseInterprocessDataManager( { } -BaseInterprocessDataManager::~BaseInterprocessDataManager() -{ -} - Id BaseInterprocessDataManager::getProcessId() const { return m_processId; diff --git a/src/lib/data/indexer/interprocess/BaseInterprocessDataManager.h b/src/lib/data/indexer/interprocess/BaseInterprocessDataManager.h index 9dc8f063..ac60cdef 100644 --- a/src/lib/data/indexer/interprocess/BaseInterprocessDataManager.h +++ b/src/lib/data/indexer/interprocess/BaseInterprocessDataManager.h @@ -15,7 +15,8 @@ public: const std::string& instanceUuid, Id processId, bool isOwner); - virtual ~BaseInterprocessDataManager(); + + virtual ~BaseInterprocessDataManager() = default; Id getProcessId() const; diff --git a/src/lib/data/indexer/interprocess/InterprocessIntermediateStorageManager.cpp b/src/lib/data/indexer/interprocess/InterprocessIntermediateStorageManager.cpp index 3ab7d98a..2896f5f7 100644 --- a/src/lib/data/indexer/interprocess/InterprocessIntermediateStorageManager.cpp +++ b/src/lib/data/indexer/interprocess/InterprocessIntermediateStorageManager.cpp @@ -17,34 +17,39 @@ InterprocessIntermediateStorageManager::InterprocessIntermediateStorageManager( instanceUuid, processId, isOwner) -{ -} - -InterprocessIntermediateStorageManager::~InterprocessIntermediateStorageManager() + , m_insertsWithoutGrowth(0) { } void InterprocessIntermediateStorageManager::pushIntermediateStorage( const std::shared_ptr& 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* 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()); } diff --git a/src/lib/data/indexer/interprocess/InterprocessIntermediateStorageManager.h b/src/lib/data/indexer/interprocess/InterprocessIntermediateStorageManager.h index d817c438..81f04850 100644 --- a/src/lib/data/indexer/interprocess/InterprocessIntermediateStorageManager.h +++ b/src/lib/data/indexer/interprocess/InterprocessIntermediateStorageManager.h @@ -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); std::shared_ptr 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 diff --git a/src/lib/utility/interprocess/SharedMemory.cpp b/src/lib/utility/interprocess/SharedMemory.cpp index cdfeaeba..699d2954 100644 --- a/src/lib/utility/interprocess/SharedMemory.cpp +++ b/src/lib/utility/interprocess/SharedMemory.cpp @@ -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 + " -"; diff --git a/src/lib/utility/interprocess/SharedMemory.h b/src/lib/utility/interprocess/SharedMemory.h index 68ad08ef..89e7b240 100644 --- a/src/lib/utility/interprocess/SharedMemory.h +++ b/src/lib/utility/interprocess/SharedMemory.h @@ -62,6 +62,7 @@ public: size_t getUsedMemorySize() const; void growMemory(size_t size); + void shrinkToFitMemory(); template T* accessValue(const std::string& key)