fix: remove destroyed allocations from eviction lists

mark explicitly made resident allocations

Related-To: NEO-13246, GSD-10319
Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
This commit is contained in:
Mateusz Jablonski
2024-11-25 15:51:22 +00:00
committed by Compute-Runtime-Automation
parent a61656c2de
commit db6fe7892c
6 changed files with 126 additions and 2 deletions

View File

@@ -339,6 +339,13 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
std::atomic<uint32_t> hostPtrTaskCountAssignment{0};
bool isExplicitlyMadeResident() const {
return this->explicitlyMadeResident;
}
void setExplicitlyMadeResident(bool explicitlyMadeResident) {
this->explicitlyMadeResident = explicitlyMadeResident;
}
protected:
struct UsageInfo {
TaskCountType taskCount = objectNotUsed;
@@ -404,5 +411,6 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
std::atomic<uint32_t> registeredContextsNum{0};
bool shareableHostMemory = false;
bool cantBeReadOnly = false;
bool explicitlyMadeResident = false;
};
} // namespace NEO

View File

@@ -725,6 +725,10 @@ void WddmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation
WddmAllocation *input = static_cast<WddmAllocation *>(gfxAllocation);
DEBUG_BREAK_IF(!validateAllocation(input));
if (gfxAllocation->isExplicitlyMadeResident()) {
freeAssociatedResourceImpl(*gfxAllocation);
}
auto &registeredEngines = getRegisteredEngines(gfxAllocation->getRootDeviceIndex());
for (auto &engine : registeredEngines) {
auto &residencyController = static_cast<OsContextWin *>(engine.osContext)->getResidencyController();
@@ -1237,6 +1241,7 @@ bool WddmMemoryManager::copyMemoryToAllocationBanks(GraphicsAllocation *graphics
memcpy_s(ptrOffset(ptr, destinationOffset), graphicsAllocation->getUnderlyingBufferSize() - destinationOffset, memoryToCopy, sizeToCopy);
wddm.unlockResource(wddmAllocation->getHandles()[handleId]);
}
wddmAllocation->setExplicitlyMadeResident(wddmAllocation->needsMakeResidentBeforeLock());
return true;
}

View File

@@ -28,6 +28,7 @@ MemoryOperationsStatus WddmMemoryOperationsHandler::makeResident(Device *device,
size_t totalSize = 0;
for (const auto &allocation : gfxAllocations) {
allocation->setExplicitlyMadeResident(true);
WddmAllocation *wddmAllocation = reinterpret_cast<WddmAllocation *>(allocation);
totalSize += wddmAllocation->getAlignedSize();
@@ -48,6 +49,7 @@ MemoryOperationsStatus WddmMemoryOperationsHandler::makeResident(Device *device,
}
MemoryOperationsStatus WddmMemoryOperationsHandler::evict(Device *device, GraphicsAllocation &gfxAllocation) {
gfxAllocation.setExplicitlyMadeResident(false);
constexpr uint32_t stackHandlesCount = NEO::maxFragmentsCount * EngineLimits::maxHandleCount;
StackVec<D3DKMT_HANDLE, stackHandlesCount> handlesForEviction;
WddmAllocation &wddmAllocation = reinterpret_cast<WddmAllocation &>(gfxAllocation);
@@ -81,4 +83,25 @@ MemoryOperationsStatus WddmMemoryOperationsHandler::isResident(Device *device, G
return residentAllocations->isAllocationResident(defaultHandle);
}
MemoryOperationsStatus WddmMemoryOperationsHandler::free(Device *device, GraphicsAllocation &gfxAllocation) {
if (gfxAllocation.isExplicitlyMadeResident()) {
WddmAllocation &wddmAllocation = reinterpret_cast<WddmAllocation &>(gfxAllocation);
if (wddmAllocation.fragmentsStorage.fragmentCount > 0) {
OsHandleStorage &fragmentStorage = wddmAllocation.fragmentsStorage;
for (uint32_t allocId = 0; allocId < fragmentStorage.fragmentCount; allocId++) {
residentAllocations->removeResource(static_cast<OsHandleWin *>(fragmentStorage.fragmentStorageData[allocId].osHandleStorage)->handle);
}
} else {
const auto &handles = wddmAllocation.getHandles();
size_t handleCount = wddmAllocation.getNumGmms();
for (uint32_t i = 0; i < handleCount; i++) {
residentAllocations->removeResource(handles[i]);
}
}
}
return MemoryOperationsStatus::success;
}
} // namespace NEO

View File

@@ -36,6 +36,7 @@ class WddmMemoryOperationsHandler : public MemoryOperationsHandler {
MemoryOperationsStatus evictWithinOsContext(OsContext *osContext, GraphicsAllocation &gfxAllocation) override {
return evict(nullptr, gfxAllocation);
}
MemoryOperationsStatus free(Device *device, GraphicsAllocation &gfxAllocation) override;
protected:
Wddm *wddm;

View File

@@ -1400,6 +1400,29 @@ TEST_F(WddmMemoryManagerSimpleTest, whenDestroyingLockedAllocationIfDeviceRequir
}
EXPECT_EQ(0u, mockTemporaryResources->evictResourceResult.called);
}
TEST_F(WddmMemoryManagerSimpleTest, givenLocalMemoryKernelIsaWithMemoryCopiedWhenDestroyingAllocationIfDeviceRequiresMakeResidentPriorToLockThenRemoveFromTemporaryResources) {
DebugManagerStateRestore restorer;
debugManager.flags.EnableLocalMemory.set(1);
char data{};
AllocationProperties properties{0, true, sizeof(data), AllocationType::kernelIsa, false, false, 0};
properties.subDevicesBitfield.set(0);
memoryManager->localMemorySupported[properties.rootDeviceIndex] = true;
auto allocation = static_cast<WddmAllocation *>(memoryManager->allocateGraphicsMemoryWithProperties(properties));
ASSERT_NE(nullptr, allocation);
memoryManager->copyMemoryToAllocation(allocation, 0, &data, sizeof(data));
auto makeResidentPriorToLockRequired = memoryManager->peekExecutionEnvironment().rootDeviceEnvironments[0u]->getHelper<GfxCoreHelper>().makeResidentBeforeLockNeeded(allocation->needsMakeResidentBeforeLock());
EXPECT_EQ(makeResidentPriorToLockRequired, allocation->needsMakeResidentBeforeLock());
EXPECT_EQ(makeResidentPriorToLockRequired, allocation->isExplicitlyMadeResident());
memoryManager->freeGraphicsMemory(allocation);
if (makeResidentPriorToLockRequired) {
EXPECT_EQ(1u, mockTemporaryResources->removeResourceResult.called);
} else {
EXPECT_EQ(0u, mockTemporaryResources->removeResourceResult.called);
}
EXPECT_EQ(0u, mockTemporaryResources->evictResourceResult.called);
}
TEST_F(WddmMemoryManagerSimpleTest, whenDestroyingNotLockedAllocationThatDoesntNeedMakeResidentBeforeLockThenDontEvictAllocationFromWddmTemporaryResources) {
DebugManagerStateRestore restorer;
debugManager.flags.ForcePreferredAllocationMethod.set(static_cast<int32_t>(GfxMemoryAllocationMethod::useUmdSystemPtr));