Reuse GraphicsAllocation mechanism for shared resources

Change-Id: I4bfd2d3387ae0fc10d461ebc1ada94614ab7f6b5
This commit is contained in:
Dunajski, Bartosz
2018-03-01 14:58:15 +01:00
committed by sys_ocldev
parent 7b30a7c2b8
commit e579578bc8
4 changed files with 52 additions and 1 deletions

View File

@@ -67,7 +67,10 @@ MemObj::~MemObj() {
} }
if (memoryManager) { if (memoryManager) {
if (graphicsAllocation && !associatedMemObject && !isObjectRedescribed && !isHostPtrSVM) { if (peekSharingHandler()) {
peekSharingHandler()->releaseReusedGraphicsAllocation();
}
if (graphicsAllocation && !associatedMemObject && !isObjectRedescribed && !isHostPtrSVM && graphicsAllocation->reuseCount == 0) {
bool doAsyncDestrucions = DebugManager.flags.EnableAsyncDestroyAllocations.get(); bool doAsyncDestrucions = DebugManager.flags.EnableAsyncDestroyAllocations.get();
if (!doAsyncDestrucions) { if (!doAsyncDestrucions) {
needWait = true; needWait = true;

View File

@@ -24,6 +24,7 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <atomic>
#include "runtime/helpers/debug_helpers.h" #include "runtime/helpers/debug_helpers.h"
#include "runtime/memory_manager/host_ptr_defines.h" #include "runtime/memory_manager/host_ptr_defines.h"
@@ -116,6 +117,7 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
uint64_t gpuBaseAddress = 0; uint64_t gpuBaseAddress = 0;
Gmm *gmm = nullptr; Gmm *gmm = nullptr;
uint64_t allocationOffset = 0u; uint64_t allocationOffset = 0u;
std::atomic<uint32_t> reuseCount{0}; // GraphicsAllocation can be reused by shared resources
int residencyTaskCount = ObjectNotResident; int residencyTaskCount = ObjectNotResident;

View File

@@ -55,6 +55,7 @@ class SharingHandler {
virtual ~SharingHandler() = default; virtual ~SharingHandler() = default;
virtual void getMemObjectInfo(size_t &paramValueSize, void *&paramValue){}; virtual void getMemObjectInfo(size_t &paramValueSize, void *&paramValue){};
virtual void releaseReusedGraphicsAllocation(){};
protected: protected:
virtual void synchronizeHandler(UpdateData *updateData); virtual void synchronizeHandler(UpdateData *updateData);

View File

@@ -27,6 +27,7 @@
#include "unit_tests/mocks/mock_deferred_deleter.h" #include "unit_tests/mocks/mock_deferred_deleter.h"
#include "unit_tests/mocks/mock_graphics_allocation.h" #include "unit_tests/mocks/mock_graphics_allocation.h"
#include "unit_tests/mocks/mock_memory_manager.h" #include "unit_tests/mocks/mock_memory_manager.h"
#include "unit_tests/fixtures/memory_management_fixture.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
using namespace OCLRT; using namespace OCLRT;
@@ -305,3 +306,47 @@ TEST(MemObj, givenDefaultWhenAskedForCpuMappingThenReturnTrue) {
EXPECT_FALSE(memObj.peekSharingHandler()); EXPECT_FALSE(memObj.peekSharingHandler());
EXPECT_TRUE(memObj.mappingOnCpuAllowed()); EXPECT_TRUE(memObj.mappingOnCpuAllowed());
} }
TEST(MemObj, givenMultipleMemObjectsWithReusedGraphicsAllocationWhenDestroyedThenFreeAllocationOnce) {
// Each SharingHandler should have own implementation of reuseCount management
struct MySharingHandler : public SharingHandler {
MySharingHandler(GraphicsAllocation *allocation) : allocation(allocation) {
allocation->reuseCount++;
}
void releaseReusedGraphicsAllocation() override {
allocation->reuseCount--;
}
GraphicsAllocation *allocation = nullptr;
};
MockMemoryManager memoryManager;
MockContext context;
context.setMemoryManager(&memoryManager);
MemoryManagementFixture memoryLeaksCheck;
memoryLeaksCheck.SetUp();
auto allocation = memoryManager.allocateGraphicsMemory(1);
auto memObj1 = new MemObj(&context, CL_MEM_OBJECT_BUFFER, 0, 1, nullptr, nullptr, allocation, true, false, false);
memObj1->setSharingHandler(new MySharingHandler(allocation));
auto memObj2 = new MemObj(&context, CL_MEM_OBJECT_BUFFER, 0, 1, nullptr, nullptr, allocation, true, false, false);
memObj2->setSharingHandler(new MySharingHandler(allocation));
auto memObj3 = new MemObj(&context, CL_MEM_OBJECT_BUFFER, 0, 1, nullptr, nullptr, allocation, true, false, false);
memObj3->setSharingHandler(new MySharingHandler(allocation));
EXPECT_EQ(3u, allocation->reuseCount.load());
delete memObj3;
EXPECT_EQ(2u, allocation->reuseCount.load());
delete memObj1;
EXPECT_EQ(1u, allocation->reuseCount.load());
delete memObj2;
// GraphicsAllocation should be removed by last memObj
memoryLeaksCheck.TearDown();
}