diff --git a/ai/neural_network_runtime/v1_0/common/mock_idevice.cpp b/ai/neural_network_runtime/v1_0/common/mock_idevice.cpp index b374cbe86c65926c0153b085ec8350e53ae6ad53..447f0ec247b084baad988724521c7ef988de931a 100644 --- a/ai/neural_network_runtime/v1_0/common/mock_idevice.cpp +++ b/ai/neural_network_runtime/v1_0/common/mock_idevice.cpp @@ -42,6 +42,14 @@ MockIDevice::~MockIDevice() } } +MockIPreparedModel::~MockIPreparedModel() +{ + for (auto ash : m_ashmems) { + ash.second->UnmapAshmem(); + ash.second->CloseAshmem(); + } +} + MockIDevice *MockIDevice::GetInstance() { static MockIDevice iDevice; @@ -148,6 +156,11 @@ int32_t MockIDevice::IsModelCacheSupported(bool& isSupported) int32_t MockIDevice::AllocateBuffer(uint32_t length, SharedBuffer &buffer) { std::lock_guard lock(m_mtx); + for(auto ash:m_ashmems){ + if(ash.second->GetAshmemSize() <= 0){ + ash.second->CloseAshmem(); + } + } sptr ashptr = Ashmem::CreateAshmem("allocateBuffer", length); if (ashptr == nullptr) { LOGE("[NNRtTest] Create shared memory failed."); @@ -231,6 +244,9 @@ int32_t MockIPreparedModel::ExportModelCache(std::vector& modelCac LOGE("[NNRtTest] Map fd to write cache failed."); return HDF_FAILURE; } + + int fd = cache->GetAshmemFd(); + m_ashmems[fd] = cache; ret = cache->WriteToAshmem(buffer, size, 0); cache->UnmapAshmem(); @@ -238,6 +254,7 @@ int32_t MockIPreparedModel::ExportModelCache(std::vector& modelCac LOGE("[NNRtTest] Write cache failed."); return HDF_FAILURE; } + // SharedBuffer: fd, bufferSize, offset, dataSize modelCache.emplace_back(SharedBuffer {cache->GetAshmemFd(), cache->GetAshmemSize(), 0, cache->GetAshmemSize()}); return HDF_SUCCESS; diff --git a/ai/neural_network_runtime/v1_0/common/mock_idevice.h b/ai/neural_network_runtime/v1_0/common/mock_idevice.h index 4277488b07ac9e0f9ad17ad3c43f2e82b73d55fc..d2e6530235d2c1750494ea153e39d27b2bc3953c 100644 --- a/ai/neural_network_runtime/v1_0/common/mock_idevice.h +++ b/ai/neural_network_runtime/v1_0/common/mock_idevice.h @@ -111,6 +111,9 @@ public: std::vector>& outputsDims, std::vector& isOutputBufferEnough) override; int32_t GetVersion(uint32_t &majorVersion, uint32_t &minorVersion) override; MockIPreparedModel() = default; + virtual ~MockIPreparedModel(); +private: + std::unordered_map> m_ashmems; }; } // namespace V1_0 diff --git a/ai/neural_network_runtime/v2_0/common/mock_idevice.cpp b/ai/neural_network_runtime/v2_0/common/mock_idevice.cpp index b33576ca3cbf26e239af8b85c02b381faef7829b..e4ca3ac272be05d94f4a4872151ce07e7cc28f1e 100644 --- a/ai/neural_network_runtime/v2_0/common/mock_idevice.cpp +++ b/ai/neural_network_runtime/v2_0/common/mock_idevice.cpp @@ -42,6 +42,14 @@ MockIDevice::~MockIDevice() } } +MockIPreparedModel::~MockIPreparedModel() +{ + for (auto ash : m_ashmems) { + ash.second->UnmapAshmem(); + ash.second->CloseAshmem(); + } +} + MockIDevice *MockIDevice::GetInstance() { static MockIDevice iDevice; @@ -148,6 +156,11 @@ int32_t MockIDevice::IsModelCacheSupported(bool& isSupported) int32_t MockIDevice::AllocateBuffer(uint32_t length, SharedBuffer &buffer) { std::lock_guard lock(m_mtx); + for(auto ash:m_ashmems){ + if(ash.second->GetAshmemSize() <= 0){ + ash.second->CloseAshmem(); + } + } sptr ashptr = Ashmem::CreateAshmem("allocateBuffer", length); if (ashptr == nullptr) { LOGE("[NNRtTest] Create shared memory failed."); @@ -238,6 +251,8 @@ int32_t MockIPreparedModel::ExportModelCache(std::vector& modelCac LOGE("[NNRtTest] Map fd to write cache failed."); return HDF_FAILURE; } + int fd = cache->GetAshmemFd(); + m_ashmems[fd] = cache; ret = cache->WriteToAshmem(buffer, size, 0); cache->UnmapAshmem(); diff --git a/ai/neural_network_runtime/v2_0/common/mock_idevice.h b/ai/neural_network_runtime/v2_0/common/mock_idevice.h index 83739d9b943d74a49ca6ff5a400e763129115e95..8bfae99576e2568caf6cc182e275dff740e92ebb 100644 --- a/ai/neural_network_runtime/v2_0/common/mock_idevice.h +++ b/ai/neural_network_runtime/v2_0/common/mock_idevice.h @@ -115,6 +115,9 @@ public: int32_t GetInputDimRanges(std::vector>& minInputDims, std::vector>& maxInputDims) override; int32_t GetVersion(uint32_t &majorVersion, uint32_t &minorVersion) override; MockIPreparedModel() = default; + virtual ~MockIPreparedModel(); +private: + std::unordered_map> m_ashmems; }; } // namespace V2_0