Add rootDeviceIndex to BO

Related-To: LOCI-2502

This to be able to differentiate when a B0 comes from a shareable handle
and we need to know which device owns it. This is needed when inside
the same process we have more than one active P2P transaction.

Signed-off-by: Jaime Arteaga <jaime.a.arteaga.molina@intel.com>
This commit is contained in:
Jaime Arteaga
2021-09-11 09:37:42 +00:00
committed by Compute-Runtime-Automation
parent 6940fbf387
commit 72d16c7cf2
4 changed files with 114 additions and 4 deletions

View File

@ -42,6 +42,7 @@
#include "opencl/source/helpers/cl_memory_properties_helpers.h"
#include "opencl/source/mem_obj/buffer.h"
#include "opencl/source/mem_obj/image.h"
#include "opencl/test/unit_test/fixtures/memory_allocator_multi_device_fixture.h"
#include "opencl/test/unit_test/mocks/mock_cl_device.h"
#include "opencl/test/unit_test/mocks/mock_context.h"
#include "opencl/test/unit_test/mocks/mock_platform.h"
@ -56,6 +57,104 @@
namespace NEO {
using MemoryManagerMultiDeviceSharedHandleTest = MemoryAllocatorMultiDeviceFixture<2>;
TEST_P(MemoryManagerMultiDeviceSharedHandleTest, whenCreatingAllocationFromSharedHandleWithSameHandleAndSameRootDeviceThenSameBOIsUsed) {
uint32_t handle0 = 0;
uint32_t rootDeviceIndex0 = 0;
AllocationProperties properties0{rootDeviceIndex0, true, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, false, false, mockDeviceBitfield};
auto gfxAllocation0 = memoryManager->createGraphicsAllocationFromSharedHandle(handle0, properties0, false, false);
ASSERT_NE(gfxAllocation0, nullptr);
EXPECT_EQ(rootDeviceIndex0, gfxAllocation0->getRootDeviceIndex());
uint32_t handle1 = 0;
uint32_t rootDeviceIndex1 = 0;
AllocationProperties properties1{rootDeviceIndex1, true, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, false, false, mockDeviceBitfield};
auto gfxAllocation1 = memoryManager->createGraphicsAllocationFromSharedHandle(handle1, properties1, false, false);
ASSERT_NE(gfxAllocation1, nullptr);
EXPECT_EQ(rootDeviceIndex1, gfxAllocation1->getRootDeviceIndex());
DrmAllocation *drmAllocation0 = static_cast<DrmAllocation *>(gfxAllocation0);
DrmAllocation *drmAllocation1 = static_cast<DrmAllocation *>(gfxAllocation1);
EXPECT_EQ(drmAllocation0->getBO(), drmAllocation1->getBO());
memoryManager->freeGraphicsMemory(gfxAllocation0);
memoryManager->freeGraphicsMemory(gfxAllocation1);
}
TEST_P(MemoryManagerMultiDeviceSharedHandleTest, whenCreatingAllocationFromSharedHandleWithSameHandleAndDifferentRootDeviceThenDifferentBOIsUsed) {
uint32_t handle0 = 0;
uint32_t rootDeviceIndex0 = 0;
AllocationProperties properties0{rootDeviceIndex0, true, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, false, false, mockDeviceBitfield};
auto gfxAllocation0 = memoryManager->createGraphicsAllocationFromSharedHandle(handle0, properties0, false, false);
ASSERT_NE(gfxAllocation0, nullptr);
EXPECT_EQ(rootDeviceIndex0, gfxAllocation0->getRootDeviceIndex());
uint32_t handle1 = 0;
uint32_t rootDeviceIndex1 = 1;
AllocationProperties properties1{rootDeviceIndex1, true, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, false, false, mockDeviceBitfield};
auto gfxAllocation1 = memoryManager->createGraphicsAllocationFromSharedHandle(handle1, properties1, false, false);
ASSERT_NE(gfxAllocation1, nullptr);
EXPECT_EQ(rootDeviceIndex1, gfxAllocation1->getRootDeviceIndex());
DrmAllocation *drmAllocation0 = static_cast<DrmAllocation *>(gfxAllocation0);
DrmAllocation *drmAllocation1 = static_cast<DrmAllocation *>(gfxAllocation1);
EXPECT_NE(drmAllocation0->getBO(), drmAllocation1->getBO());
memoryManager->freeGraphicsMemory(gfxAllocation0);
memoryManager->freeGraphicsMemory(gfxAllocation1);
}
TEST_P(MemoryManagerMultiDeviceSharedHandleTest, whenCreatingAllocationFromSharedHandleWithDifferentHandleAndSameRootDeviceThenDifferentBOIsUsed) {
uint32_t handle0 = 0;
uint32_t rootDeviceIndex0 = 0;
AllocationProperties properties0{rootDeviceIndex0, true, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, false, false, mockDeviceBitfield};
auto gfxAllocation0 = memoryManager->createGraphicsAllocationFromSharedHandle(handle0, properties0, false, false);
ASSERT_NE(gfxAllocation0, nullptr);
EXPECT_EQ(rootDeviceIndex0, gfxAllocation0->getRootDeviceIndex());
uint32_t handle1 = 1;
uint32_t rootDeviceIndex1 = 0;
AllocationProperties properties1{rootDeviceIndex1, true, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, false, false, mockDeviceBitfield};
auto gfxAllocation1 = memoryManager->createGraphicsAllocationFromSharedHandle(handle1, properties1, false, false);
ASSERT_NE(gfxAllocation1, nullptr);
EXPECT_EQ(rootDeviceIndex1, gfxAllocation1->getRootDeviceIndex());
DrmAllocation *drmAllocation0 = static_cast<DrmAllocation *>(gfxAllocation0);
DrmAllocation *drmAllocation1 = static_cast<DrmAllocation *>(gfxAllocation1);
EXPECT_NE(drmAllocation0->getBO(), drmAllocation1->getBO());
memoryManager->freeGraphicsMemory(gfxAllocation0);
memoryManager->freeGraphicsMemory(gfxAllocation1);
}
TEST_P(MemoryManagerMultiDeviceSharedHandleTest, whenCreatingAllocationFromSharedHandleWithDifferentHandleAndDifferentRootDeviceThenDifferentBOIsUsed) {
uint32_t handle0 = 0;
uint32_t rootDeviceIndex0 = 0;
AllocationProperties properties0{rootDeviceIndex0, true, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, false, false, mockDeviceBitfield};
auto gfxAllocation0 = memoryManager->createGraphicsAllocationFromSharedHandle(handle0, properties0, false, false);
ASSERT_NE(gfxAllocation0, nullptr);
EXPECT_EQ(rootDeviceIndex0, gfxAllocation0->getRootDeviceIndex());
uint32_t handle1 = 1;
uint32_t rootDeviceIndex1 = 1;
AllocationProperties properties1{rootDeviceIndex1, true, MemoryConstants::pageSize, GraphicsAllocation::AllocationType::BUFFER, false, false, mockDeviceBitfield};
auto gfxAllocation1 = memoryManager->createGraphicsAllocationFromSharedHandle(handle1, properties1, false, false);
ASSERT_NE(gfxAllocation1, nullptr);
EXPECT_EQ(rootDeviceIndex1, gfxAllocation1->getRootDeviceIndex());
DrmAllocation *drmAllocation0 = static_cast<DrmAllocation *>(gfxAllocation0);
DrmAllocation *drmAllocation1 = static_cast<DrmAllocation *>(gfxAllocation1);
EXPECT_NE(drmAllocation0->getBO(), drmAllocation1->getBO());
memoryManager->freeGraphicsMemory(gfxAllocation0);
memoryManager->freeGraphicsMemory(gfxAllocation1);
}
AllocationProperties createAllocationProperties(uint32_t rootDeviceIndex, size_t size, bool forcePin) {
MockAllocationProperties properties(rootDeviceIndex, size);
properties.alignment = MemoryConstants::preferredAlignment;

View File

@ -97,6 +97,15 @@ class BufferObject {
void requireExplicitResidency(bool required) {
requiresExplicitResidency = required;
}
void setRootDeviceIndex(uint32_t index) {
rootDeviceIndex = index;
}
uint32_t getRootDeviceIndex() {
return rootDeviceIndex;
}
int getHandle() {
return handle;
}
void setCacheRegion(CacheRegion regionIndex) { cacheRegion = regionIndex; }
CacheRegion peekCacheRegion() const { return cacheRegion; }
@ -132,6 +141,7 @@ class BufferObject {
bool perContextVmsUsed = false;
std::atomic<uint32_t> refCount;
uint32_t rootDeviceIndex = std::numeric_limits<uint32_t>::max();
int handle; // i915 gem object handle
uint64_t size;
bool isReused;

View File

@ -606,10 +606,10 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const Allocatio
return allocation;
}
BufferObject *DrmMemoryManager::findAndReferenceSharedBufferObject(int boHandle) {
BufferObject *DrmMemoryManager::findAndReferenceSharedBufferObject(int boHandle, uint32_t rootDeviceIndex) {
BufferObject *bo = nullptr;
for (const auto &i : sharingBufferObjects) {
if (i->peekHandle() == boHandle) {
if (i->getHandle() == boHandle && i->getRootDeviceIndex() == rootDeviceIndex) {
bo = i;
bo->reference();
break;
@ -639,7 +639,7 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o
}
auto boHandle = openFd.handle;
auto bo = findAndReferenceSharedBufferObject(boHandle);
auto bo = findAndReferenceSharedBufferObject(boHandle, properties.rootDeviceIndex);
if (bo == nullptr) {
size_t size = lseekFunction(handle, 0, SEEK_END);
@ -658,6 +658,7 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o
bo->setAddress(gpuRange);
bo->setUnmapSize(size);
bo->setRootDeviceIndex(properties.rootDeviceIndex);
pushSharedBufferObject(bo);
}

View File

@ -82,7 +82,7 @@ class DrmMemoryManager : public MemoryManager {
void createDeviceSpecificMemResources(uint32_t rootDeviceIndex) override;
protected:
BufferObject *findAndReferenceSharedBufferObject(int boHandle);
BufferObject *findAndReferenceSharedBufferObject(int boHandle, uint32_t rootDeviceIndex);
void eraseSharedBufferObject(BufferObject *bo);
void pushSharedBufferObject(BufferObject *bo);
BufferObject *allocUserptr(uintptr_t address, size_t size, uint64_t flags, uint32_t rootDeviceIndex);