提交 1437d6c4 编写于 作者: T tangshihua

bugfix map fd

Signed-off-by: Ntangshihua <tangshihua@huawei.com>
上级 82644f24
...@@ -42,6 +42,14 @@ MockIDevice::~MockIDevice() ...@@ -42,6 +42,14 @@ MockIDevice::~MockIDevice()
} }
} }
MockIPreparedModel::~MockIPreparedModel()
{
for (auto ash : m_ashmems) {
ash.second->UnmapAshmem();
ash.second->CloseAshmem();
}
}
MockIDevice *MockIDevice::GetInstance() MockIDevice *MockIDevice::GetInstance()
{ {
static MockIDevice iDevice; static MockIDevice iDevice;
...@@ -148,6 +156,11 @@ int32_t MockIDevice::IsModelCacheSupported(bool& isSupported) ...@@ -148,6 +156,11 @@ int32_t MockIDevice::IsModelCacheSupported(bool& isSupported)
int32_t MockIDevice::AllocateBuffer(uint32_t length, SharedBuffer &buffer) int32_t MockIDevice::AllocateBuffer(uint32_t length, SharedBuffer &buffer)
{ {
std::lock_guard<std::mutex> lock(m_mtx); std::lock_guard<std::mutex> lock(m_mtx);
for(auto ash:m_ashmems){
if(ash.second->GetAshmemSize() <= 0){
ash.second->CloseAshmem();
}
}
sptr<Ashmem> ashptr = Ashmem::CreateAshmem("allocateBuffer", length); sptr<Ashmem> ashptr = Ashmem::CreateAshmem("allocateBuffer", length);
if (ashptr == nullptr) { if (ashptr == nullptr) {
LOGE("[NNRtTest] Create shared memory failed."); LOGE("[NNRtTest] Create shared memory failed.");
...@@ -174,7 +187,6 @@ int32_t MockIDevice::ReleaseBuffer(const SharedBuffer &buffer) ...@@ -174,7 +187,6 @@ int32_t MockIDevice::ReleaseBuffer(const SharedBuffer &buffer)
std::lock_guard<std::mutex> lock(m_mtx); std::lock_guard<std::mutex> lock(m_mtx);
auto ash = m_ashmems[buffer.fd]; auto ash = m_ashmems[buffer.fd];
ash->UnmapAshmem(); ash->UnmapAshmem();
ash->CloseAshmem();
return HDF_SUCCESS; return HDF_SUCCESS;
} }
...@@ -232,6 +244,9 @@ int32_t MockIPreparedModel::ExportModelCache(std::vector<SharedBuffer>& modelCac ...@@ -232,6 +244,9 @@ int32_t MockIPreparedModel::ExportModelCache(std::vector<SharedBuffer>& modelCac
LOGE("[NNRtTest] Map fd to write cache failed."); LOGE("[NNRtTest] Map fd to write cache failed.");
return HDF_FAILURE; return HDF_FAILURE;
} }
int fd = cache->GetAshmemFd();
m_ashmems[fd] = cache;
ret = cache->WriteToAshmem(buffer, size, 0); ret = cache->WriteToAshmem(buffer, size, 0);
cache->UnmapAshmem(); cache->UnmapAshmem();
...@@ -239,6 +254,7 @@ int32_t MockIPreparedModel::ExportModelCache(std::vector<SharedBuffer>& modelCac ...@@ -239,6 +254,7 @@ int32_t MockIPreparedModel::ExportModelCache(std::vector<SharedBuffer>& modelCac
LOGE("[NNRtTest] Write cache failed."); LOGE("[NNRtTest] Write cache failed.");
return HDF_FAILURE; return HDF_FAILURE;
} }
// SharedBuffer: fd, bufferSize, offset, dataSize // SharedBuffer: fd, bufferSize, offset, dataSize
modelCache.emplace_back(SharedBuffer {cache->GetAshmemFd(), cache->GetAshmemSize(), 0, cache->GetAshmemSize()}); modelCache.emplace_back(SharedBuffer {cache->GetAshmemFd(), cache->GetAshmemSize(), 0, cache->GetAshmemSize()});
return HDF_SUCCESS; return HDF_SUCCESS;
......
...@@ -111,6 +111,9 @@ public: ...@@ -111,6 +111,9 @@ public:
std::vector<std::vector<int32_t>>& outputsDims, std::vector<bool>& isOutputBufferEnough) override; std::vector<std::vector<int32_t>>& outputsDims, std::vector<bool>& isOutputBufferEnough) override;
int32_t GetVersion(uint32_t &majorVersion, uint32_t &minorVersion) override; int32_t GetVersion(uint32_t &majorVersion, uint32_t &minorVersion) override;
MockIPreparedModel() = default; MockIPreparedModel() = default;
virtual ~MockIPreparedModel();
private:
std::unordered_map<int, sptr<Ashmem>> m_ashmems;
}; };
} // namespace V1_0 } // namespace V1_0
......
...@@ -42,6 +42,14 @@ MockIDevice::~MockIDevice() ...@@ -42,6 +42,14 @@ MockIDevice::~MockIDevice()
} }
} }
MockIPreparedModel::~MockIPreparedModel()
{
for (auto ash : m_ashmems) {
ash.second->UnmapAshmem();
ash.second->CloseAshmem();
}
}
MockIDevice *MockIDevice::GetInstance() MockIDevice *MockIDevice::GetInstance()
{ {
static MockIDevice iDevice; static MockIDevice iDevice;
...@@ -148,6 +156,11 @@ int32_t MockIDevice::IsModelCacheSupported(bool& isSupported) ...@@ -148,6 +156,11 @@ int32_t MockIDevice::IsModelCacheSupported(bool& isSupported)
int32_t MockIDevice::AllocateBuffer(uint32_t length, SharedBuffer &buffer) int32_t MockIDevice::AllocateBuffer(uint32_t length, SharedBuffer &buffer)
{ {
std::lock_guard<std::mutex> lock(m_mtx); std::lock_guard<std::mutex> lock(m_mtx);
for(auto ash:m_ashmems){
if(ash.second->GetAshmemSize() <= 0){
ash.second->CloseAshmem();
}
}
sptr<Ashmem> ashptr = Ashmem::CreateAshmem("allocateBuffer", length); sptr<Ashmem> ashptr = Ashmem::CreateAshmem("allocateBuffer", length);
if (ashptr == nullptr) { if (ashptr == nullptr) {
LOGE("[NNRtTest] Create shared memory failed."); LOGE("[NNRtTest] Create shared memory failed.");
...@@ -238,6 +251,8 @@ int32_t MockIPreparedModel::ExportModelCache(std::vector<SharedBuffer>& modelCac ...@@ -238,6 +251,8 @@ int32_t MockIPreparedModel::ExportModelCache(std::vector<SharedBuffer>& modelCac
LOGE("[NNRtTest] Map fd to write cache failed."); LOGE("[NNRtTest] Map fd to write cache failed.");
return HDF_FAILURE; return HDF_FAILURE;
} }
int fd = cache->GetAshmemFd();
m_ashmems[fd] = cache;
ret = cache->WriteToAshmem(buffer, size, 0); ret = cache->WriteToAshmem(buffer, size, 0);
cache->UnmapAshmem(); cache->UnmapAshmem();
......
...@@ -115,6 +115,9 @@ public: ...@@ -115,6 +115,9 @@ public:
int32_t GetInputDimRanges(std::vector<std::vector<uint32_t>>& minInputDims, std::vector<std::vector<uint32_t>>& maxInputDims) override; int32_t GetInputDimRanges(std::vector<std::vector<uint32_t>>& minInputDims, std::vector<std::vector<uint32_t>>& maxInputDims) override;
int32_t GetVersion(uint32_t &majorVersion, uint32_t &minorVersion) override; int32_t GetVersion(uint32_t &majorVersion, uint32_t &minorVersion) override;
MockIPreparedModel() = default; MockIPreparedModel() = default;
virtual ~MockIPreparedModel();
private:
std::unordered_map<int, sptr<Ashmem>> m_ashmems;
}; };
} // namespace V2_0 } // namespace V2_0
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册