Skip to content

Commit eb287d8

Browse files
Register Allocations
Related-To: NEO-4964 Change-Id: I792dd7f7d6d594f51701ec7a40b2c0d36531b02b Signed-off-by: Mateusz Hoppe <[email protected]>
1 parent 98c9e3f commit eb287d8

File tree

13 files changed

+113
-23
lines changed

13 files changed

+113
-23
lines changed

opencl/test/unit_test/mocks/linux/mock_drm_allocation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class MockDrmAllocation : public DrmAllocation {
2626
public:
2727
using DrmAllocation::bufferObjects;
2828
using DrmAllocation::memoryPool;
29+
using DrmAllocation::registeredBoBindHandles;
2930

3031
MockDrmAllocation(AllocationType allocationType, MemoryPool::Type pool) : DrmAllocation(0, allocationType, nullptr, nullptr, 0, static_cast<size_t>(0), pool) {
3132
}

opencl/test/unit_test/mocks/linux/mock_drm_memory_manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class TestedDrmMemoryManager : public MemoryManagerCreate<DrmMemoryManager> {
6868
using DrmMemoryManager::pinBBs;
6969
using DrmMemoryManager::pinThreshold;
7070
using DrmMemoryManager::pushSharedBufferObject;
71+
using DrmMemoryManager::registerAllocation;
7172
using DrmMemoryManager::releaseGpuRange;
7273
using DrmMemoryManager::setDomainCpu;
7374
using DrmMemoryManager::sharingBufferObjects;

opencl/test/unit_test/os_interface/linux/drm_buffer_object_tests.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,3 @@ TEST(DrmBufferObject, whenBindExtHandleAddedThenItIsStored) {
341341
EXPECT_EQ(1u, bo.bindExtHandles.size());
342342
EXPECT_EQ(4u, bo.bindExtHandles[0]);
343343
}
344-
345-
TEST(DrmBufferObject, givenBoWithBindExtHandlesWhenBoIsDestructedThenHandlesAreUnregistered) {
346-
auto executionEnvironment = std::make_unique<ExecutionEnvironment>();
347-
executionEnvironment->prepareRootDeviceEnvironments(1);
348-
DrmMockResources drm(*executionEnvironment->rootDeviceEnvironments[0]);
349-
350-
{
351-
MockBufferObject bo(&drm, 0, 0, 1);
352-
bo.addBindExtHandle(4);
353-
bo.addBindExtHandle(5);
354-
bo.addBindExtHandle(6);
355-
}
356-
EXPECT_EQ(6u, drm.unregisteredHandle);
357-
EXPECT_EQ(3u, drm.unregisterCalledCount);
358-
}

opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3870,4 +3870,85 @@ TEST(DrmAllocationTest, givenResourceRegistrationNotEnabledWhenRegisteringBindEx
38703870
EXPECT_EQ(Drm::ResourceClass::MaxSize, drm.registeredClass);
38713871
}
38723872

3873+
TEST(DrmMemoryManager, givenTrackedAllocationTypeWhenAllocatingThenAllocationIsRegistered) {
3874+
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
3875+
executionEnvironment->prepareRootDeviceEnvironments(1u);
3876+
executionEnvironment->rootDeviceEnvironments[0]->setHwInfo(defaultHwInfo.get());
3877+
auto memoryManager = std::make_unique<TestedDrmMemoryManager>(false, false, false, *executionEnvironment);
3878+
auto mockDrm = new DrmMockResources(*executionEnvironment->rootDeviceEnvironments[0]);
3879+
executionEnvironment->rootDeviceEnvironments[0]->osInterface = std::make_unique<OSInterface>();
3880+
executionEnvironment->rootDeviceEnvironments[0]->osInterface->get()->setDrm(mockDrm);
3881+
3882+
for (uint32_t i = 3; i < 3 + static_cast<uint32_t>(Drm::ResourceClass::MaxSize); i++) {
3883+
mockDrm->classHandles.push_back(i);
3884+
}
3885+
3886+
EXPECT_TRUE(mockDrm->resourceRegistrationEnabled());
3887+
3888+
NEO::AllocationProperties properties{0, true, MemoryConstants::pageSize,
3889+
NEO::GraphicsAllocation::AllocationType::DEBUG_SBA_TRACKING_BUFFER,
3890+
false,
3891+
CommonConstants::allDevicesBitfield};
3892+
3893+
properties.gpuAddress = 0x20000;
3894+
auto sbaAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties);
3895+
EXPECT_EQ(Drm::ResourceClass::SbaTrackingBuffer, mockDrm->registeredClass);
3896+
3897+
EXPECT_EQ(sizeof(uint64_t), mockDrm->registeredDataSize);
3898+
uint64_t *data = reinterpret_cast<uint64_t *>(mockDrm->registeredData);
3899+
EXPECT_EQ(properties.gpuAddress, *data);
3900+
3901+
memoryManager->freeGraphicsMemory(sbaAllocation);
3902+
}
3903+
3904+
TEST(DrmMemoryManager, givenTrackedAllocationTypeWhenFreeingThenRegisteredHandlesAreUnregistered) {
3905+
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
3906+
executionEnvironment->prepareRootDeviceEnvironments(1u);
3907+
executionEnvironment->rootDeviceEnvironments[0]->setHwInfo(defaultHwInfo.get());
3908+
auto memoryManager = std::make_unique<TestedDrmMemoryManager>(false, false, false, *executionEnvironment);
3909+
auto mockDrm = new DrmMockResources(*executionEnvironment->rootDeviceEnvironments[0]);
3910+
executionEnvironment->rootDeviceEnvironments[0]->osInterface = std::make_unique<OSInterface>();
3911+
executionEnvironment->rootDeviceEnvironments[0]->osInterface->get()->setDrm(mockDrm);
3912+
3913+
for (uint32_t i = 3; i < 3 + static_cast<uint32_t>(Drm::ResourceClass::MaxSize); i++) {
3914+
mockDrm->classHandles.push_back(i);
3915+
}
3916+
3917+
EXPECT_TRUE(mockDrm->resourceRegistrationEnabled());
3918+
3919+
NEO::AllocationProperties properties{0, true, MemoryConstants::pageSize,
3920+
NEO::GraphicsAllocation::AllocationType::DEBUG_SBA_TRACKING_BUFFER,
3921+
false,
3922+
CommonConstants::allDevicesBitfield};
3923+
3924+
properties.gpuAddress = 0x20000;
3925+
auto sbaAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties);
3926+
3927+
EXPECT_EQ(0u, mockDrm->unregisterCalledCount);
3928+
3929+
memoryManager->freeGraphicsMemory(sbaAllocation);
3930+
3931+
EXPECT_EQ(DrmMockResources::registerResourceReturnHandle, mockDrm->unregisteredHandle);
3932+
EXPECT_EQ(1u, mockDrm->unregisterCalledCount);
3933+
}
3934+
3935+
TEST(DrmMemoryManager, givenNullBoWhenRegisteringBindExtHandleThenEarlyReturn) {
3936+
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
3937+
executionEnvironment->prepareRootDeviceEnvironments(1u);
3938+
executionEnvironment->rootDeviceEnvironments[0]->setHwInfo(defaultHwInfo.get());
3939+
auto mockDrm = std::make_unique<DrmMockResources>(*executionEnvironment->rootDeviceEnvironments[0]);
3940+
3941+
for (uint32_t i = 3; i < 3 + static_cast<uint32_t>(Drm::ResourceClass::MaxSize); i++) {
3942+
mockDrm->classHandles.push_back(i);
3943+
}
3944+
3945+
EXPECT_TRUE(mockDrm->resourceRegistrationEnabled());
3946+
3947+
MockDrmAllocation gfxAllocation(GraphicsAllocation::AllocationType::DEBUG_SBA_TRACKING_BUFFER, MemoryPool::MemoryNull);
3948+
3949+
gfxAllocation.registerBOBindExtHandle(mockDrm.get());
3950+
EXPECT_EQ(1u, gfxAllocation.registeredBoBindHandles.size());
3951+
gfxAllocation.freeRegisteredBOBindExtHandles(mockDrm.get());
3952+
}
3953+
38733954
} // namespace NEO

opencl/test/unit_test/os_interface/linux/drm_mock.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "shared/source/execution_environment/root_device_environment.h"
1212
#include "shared/source/helpers/constants.h"
1313
#include "shared/source/helpers/hw_helper.h"
14+
#include "shared/source/helpers/string.h"
1415
#include "shared/source/os_interface/linux/drm_neo.h"
1516

1617
#include "opencl/source/platform/platform.h"
@@ -220,6 +221,8 @@ class DrmMockResources : public DrmMock {
220221

221222
uint32_t registerResource(ResourceClass classType, void *data, size_t size) override {
222223
registeredClass = classType;
224+
memcpy_s(registeredData, sizeof(registeredData), data, size);
225+
registeredDataSize = size;
223226
return registerResourceReturnHandle;
224227
}
225228

@@ -234,4 +237,6 @@ class DrmMockResources : public DrmMock {
234237
uint32_t unregisterCalledCount = 0;
235238
ResourceClass registeredClass = ResourceClass::MaxSize;
236239
bool registerClassesCalled = false;
240+
uint64_t registeredData[128];
241+
size_t registeredDataSize;
237242
};

shared/source/memory_manager/memory_manager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemoryInPreferredPool(const A
411411
this->registerSysMemAlloc(allocation);
412412
}
413413
FileLoggerInstance().logAllocation(allocation);
414+
registerAllocation(allocation);
414415
return allocation;
415416
}
416417

shared/source/memory_manager/memory_manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class MemoryManager {
212212
virtual void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
213213
virtual void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
214214
virtual void freeAssociatedResourceImpl(GraphicsAllocation &graphicsAllocation) { return unlockResourceImpl(graphicsAllocation); };
215+
virtual void registerAllocation(GraphicsAllocation *allocation) {}
215216

216217
bool forceNonSvmForExternalHostPtr = false;
217218
bool force32bitAllocations = false;

shared/source/os_interface/linux/drm_allocation.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,20 @@ void DrmAllocation::registerBOBindExtHandle(Drm *drm) {
8989
if (resourceClass != Drm::ResourceClass::MaxSize) {
9090
uint64_t gpuAddress = getGpuAddress();
9191
auto handle = drm->registerResource(resourceClass, &gpuAddress, sizeof(gpuAddress));
92+
registeredBoBindHandles.push_back(handle);
9293
auto &bos = getBOs();
9394

9495
for (auto bo : bos) {
95-
bo->addBindExtHandle(handle);
96+
if (bo) {
97+
bo->addBindExtHandle(handle);
98+
}
9699
}
97100
}
98101
}
99102

103+
void DrmAllocation::freeRegisteredBOBindExtHandles(Drm *drm) {
104+
for (auto &i : registeredBoBindHandles) {
105+
drm->unregisterResource(i);
106+
}
107+
}
100108
} // namespace NEO

shared/source/os_interface/linux/drm_allocation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ class DrmAllocation : public GraphicsAllocation {
6767
void bindBO(BufferObject *bo, OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind);
6868
void bindBOs(OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind);
6969
void registerBOBindExtHandle(Drm *drm);
70+
void freeRegisteredBOBindExtHandles(Drm *drm);
7071

7172
protected:
7273
BufferObjects bufferObjects{};
74+
StackVec<uint32_t, 1> registeredBoBindHandles;
7375
};
7476
} // namespace NEO

shared/source/os_interface/linux/drm_buffer_object.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ BufferObject::BufferObject(Drm *drm, int handle, size_t size, size_t maxOsContex
4747
}
4848
}
4949

50-
BufferObject::~BufferObject() {
51-
for (auto &i : bindExtHandles) {
52-
drm->unregisterResource(i);
53-
}
54-
};
55-
5650
uint32_t BufferObject::getRefCount() const {
5751
return this->refCount.load();
5852
}

0 commit comments

Comments
 (0)