Skip to content

Commit 794798d

Browse files
performance: Make memory resident before cpu access
Signed-off-by: Bellekallu Rajkiran <[email protected]>
1 parent 45b0886 commit 794798d

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

shared/source/os_interface/linux/drm_memory_manager.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,15 +2665,24 @@ DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &
26652665
return nullptr;
26662666
}
26672667

2668-
[[maybe_unused]] auto retPtr = ioctlHelper->mmapFunction(*this, cpuPointer, alignedSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, drm.getFileDescriptor(), static_cast<off_t>(offset));
2669-
DEBUG_BREAK_IF(retPtr != cpuPointer);
2670-
26712668
obtainGpuAddress(allocationData, bo.get(), gpuAddress);
26722669
emitPinningRequest(bo.get(), allocationData);
26732670

26742671
auto gmmHelper = getGmmHelper(allocationData.rootDeviceIndex);
26752672
auto canonizedGpuAddress = gmmHelper->canonize(bo->peekAddress());
2676-
auto allocation = std::make_unique<DrmAllocation>(allocationData.rootDeviceIndex, 1u /*num gmms*/, allocationData.type, bo.get(), cpuPointer, canonizedGpuAddress, alignedSize, memoryPool);
2673+
auto allocation = std::make_unique<DrmAllocation>(allocationData.rootDeviceIndex, 1u /*num gmms*/, allocationData.type, bo.get(), nullptr, canonizedGpuAddress, alignedSize, memoryPool);
2674+
2675+
if (ioctlHelper->makeResidentBeforeLockNeeded()) {
2676+
auto memoryOperationsInterface = static_cast<DrmMemoryOperationsHandler *>(executionEnvironment.rootDeviceEnvironments[allocationData.rootDeviceIndex]->memoryOperationsInterface.get());
2677+
GraphicsAllocation *allocationPtr = allocation.get();
2678+
[[maybe_unused]] auto ret = memoryOperationsInterface->makeResidentWithinOsContext(getDefaultOsContext(allocationData.rootDeviceIndex), ArrayRef<NEO::GraphicsAllocation *>(&allocationPtr, 1), false, false, true) == MemoryOperationsStatus::success;
2679+
DEBUG_BREAK_IF(!ret);
2680+
}
2681+
2682+
[[maybe_unused]] auto retPtr = ioctlHelper->mmapFunction(*this, cpuPointer, alignedSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, drm.getFileDescriptor(), static_cast<off_t>(offset));
2683+
DEBUG_BREAK_IF(retPtr != cpuPointer);
2684+
2685+
allocation->setCpuPtrAndGpuAddress(cpuPointer, canonizedGpuAddress);
26772686
allocation->setMmapPtr(cpuPointer);
26782687
allocation->setMmapSize(alignedSize);
26792688
if (pointerDiff != 0) {

shared/test/unit_test/os_interface/linux/drm_memory_manager_localmem_prelim_tests.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "shared/source/memory_manager/memory_banks.h"
1414
#include "shared/source/memory_manager/unified_memory_manager.h"
1515
#include "shared/source/os_interface/linux/allocator_helper.h"
16+
#include "shared/source/os_interface/linux/drm_memory_operations_handler_bind.h"
1617
#include "shared/source/utilities/heap_allocator.h"
1718
#include "shared/test/common/helpers/batch_buffer_helper.h"
1819
#include "shared/test/common/helpers/stream_capture.h"
@@ -3735,3 +3736,57 @@ TEST_F(DrmMemoryManagerLocalMemoryPrelimTest, givenCreateMultiHostDebugSurfaceAl
37353736

37363737
memoryManager->freeGraphicsMemory(allocation);
37373738
}
3739+
3740+
TEST_F(DrmMemoryManagerLocalMemoryPrelimTest, givenDrmMemoryManagerAndResidentNeededbeforeLockWhenCreateAllocWithAlignmentIsCalledThenverifyAllocationIsResident) {
3741+
auto mockIoctlHelper = new MockIoctlHelper(*mock);
3742+
mockIoctlHelper->makeResidentBeforeLockNeededResult = true;
3743+
3744+
auto &drm = static_cast<DrmMockCustom &>(memoryManager->getDrm(rootDeviceIndex));
3745+
drm.ioctlHelper.reset(mockIoctlHelper);
3746+
3747+
auto memoryClassSystem = mockIoctlHelper->getDrmParamValue(DrmParam::memoryClassSystem);
3748+
auto memoryClassDevice = mockIoctlHelper->getDrmParamValue(DrmParam::memoryClassDevice);
3749+
3750+
std::vector<MemoryRegion> regionInfo(2);
3751+
regionInfo[0].region = {static_cast<uint16_t>(memoryClassSystem), 0};
3752+
regionInfo[1].region = {static_cast<uint16_t>(memoryClassDevice), DrmMockHelper::getEngineOrMemoryInstanceValue(0, 0)};
3753+
3754+
mock->memoryInfo.reset(new MockedMemoryInfo(regionInfo, *mock));
3755+
mock->engineInfoQueried = false;
3756+
mock->queryEngineInfo();
3757+
3758+
memoryManager->mmapFunction = [](void *addr, size_t len, int prot,
3759+
int flags, int fd, off_t offset) throw() {
3760+
if (addr == 0) {
3761+
return reinterpret_cast<void *>(0x10000000);
3762+
} else {
3763+
return addr;
3764+
}
3765+
};
3766+
3767+
memoryManager->munmapFunction = [](void *addr, size_t len) throw() {
3768+
return 0;
3769+
};
3770+
3771+
executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->memoryOperationsInterface.reset(new DrmMemoryOperationsHandlerBind(*executionEnvironment->rootDeviceEnvironments[rootDeviceIndex].get(), 0));
3772+
3773+
AllocationData allocationData;
3774+
allocationData.size = MemoryConstants::pageSize64k;
3775+
allocationData.rootDeviceIndex = 0u;
3776+
allocationData.type = AllocationType::buffer;
3777+
3778+
auto allocation = memoryManager->createAllocWithAlignment(allocationData,
3779+
MemoryConstants::pageSize,
3780+
MemoryConstants::pageSize64k,
3781+
MemoryConstants::pageSize64k,
3782+
0u);
3783+
ASSERT_NE(nullptr, allocation);
3784+
3785+
auto osContext = device->getDefaultEngine().osContext;
3786+
EXPECT_TRUE(allocation->isAlwaysResident(osContext->getContextId()));
3787+
3788+
memoryManager->freeGraphicsMemory(allocation);
3789+
3790+
memoryManager->mmapFunction = SysCalls::mmap;
3791+
memoryManager->munmapFunction = SysCalls::munmap;
3792+
}

0 commit comments

Comments
 (0)