Skip to content

Commit b0acc5e

Browse files
Move isMemoryBudgetExhausted to WddmResidencyController
Change-Id: Ic9da29ab954835e93cfbcd6690c1764d99939613 Signed-off-by: Maciej Dziuban <[email protected]>
1 parent 630a7e1 commit b0acc5e

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

runtime/os_interface/windows/wddm_memory_manager.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,15 @@ bool WddmMemoryManager::tryDeferDeletions(D3DKMT_HANDLE *handles, uint32_t alloc
342342
return status;
343343
}
344344

345+
bool WddmMemoryManager::isMemoryBudgetExhausted() const {
346+
for (auto osContext : this->registeredOsContexts) {
347+
if (osContext != nullptr && osContext->get()->getResidencyController().isMemoryBudgetExhausted()) {
348+
return true;
349+
}
350+
}
351+
return false;
352+
}
353+
345354
bool WddmMemoryManager::validateAllocation(WddmAllocation *alloc) {
346355
if (alloc == nullptr)
347356
return false;
@@ -491,7 +500,7 @@ bool WddmMemoryManager::makeResidentResidencyAllocations(ResidencyContainer &all
491500
if (totalHandlesCount) {
492501
uint64_t bytesToTrim = 0;
493502
while ((result = wddm->makeResident(handlesForResidency.get(), totalHandlesCount, false, &bytesToTrim)) == false) {
494-
this->memoryBudgetExhausted = true;
503+
osContext.get()->getResidencyController().setMemoryBudgetExhausted();
495504
bool trimmingDone = this->getRegisteredOsContext(0u)->get()->getResidencyController().trimResidencyToBudget(bytesToTrim);
496505
bool cantTrimFurther = !trimmingDone;
497506
if (cantTrimFurther) {

runtime/os_interface/windows/wddm_memory_manager.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class WddmMemoryManager : public MemoryManager {
7272

7373
bool tryDeferDeletions(D3DKMT_HANDLE *handles, uint32_t allocationCount, D3DKMT_HANDLE resourceHandle);
7474

75-
bool isMemoryBudgetExhausted() const override { return memoryBudgetExhausted; }
75+
bool isMemoryBudgetExhausted() const override;
7676

7777
bool mapAuxGpuVA(GraphicsAllocation *graphicsAllocation) override;
7878

@@ -82,7 +82,6 @@ class WddmMemoryManager : public MemoryManager {
8282
GraphicsAllocation *createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle);
8383
static bool validateAllocation(WddmAllocation *alloc);
8484
bool createWddmAllocation(WddmAllocation *allocation, AllocationOrigin origin);
85-
bool memoryBudgetExhausted = false;
8685
AlignedMallocRestrictions mallocRestrictions;
8786

8887
private:

runtime/os_interface/windows/wddm_residency_controller.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class WddmResidencyController {
4949
void trimResidency(D3DDDI_TRIMRESIDENCYSET_FLAGS flags, uint64_t bytes);
5050
bool trimResidencyToBudget(uint64_t bytes);
5151

52+
bool isMemoryBudgetExhausted() const { return memoryBudgetExhausted; }
53+
void setMemoryBudgetExhausted() { memoryBudgetExhausted = true; }
54+
5255
protected:
5356
Wddm &wddm;
5457
uint32_t osContextId;
@@ -57,6 +60,7 @@ class WddmResidencyController {
5760
SpinLock lock;
5861
SpinLock trimCallbackLock;
5962

63+
bool memoryBudgetExhausted = false;
6064
uint64_t lastTrimFenceValue = 0u;
6165
ResidencyContainer trimCandidateList;
6266
uint32_t trimCandidatesCount = 0;

unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "unit_tests/mocks/mock_device.h"
2222
#include "unit_tests/os_interface/windows/mock_wddm_allocation.h"
2323
#include "unit_tests/os_interface/windows/wddm_memory_manager_tests.h"
24+
#include "unit_tests/utilities/base_object_utils.h"
2425

2526
using namespace OCLRT;
2627
using namespace ::testing;
@@ -1373,10 +1374,24 @@ TEST_F(MockWddmMemoryManagerTest, givenDefaultMemoryManagerWhenItIsCreatedThenAs
13731374
EXPECT_NE(nullptr, memoryManager.getDeferredDeleter());
13741375
}
13751376

1376-
TEST_F(MockWddmMemoryManagerTest, givenDefaultWddmMemoryManagerWhenItIsCreatedThenMemoryBudgetIsNotExhausted) {
1377-
auto wddm = std::make_unique<WddmMock>();
1378-
WddmMemoryManager memoryManager(false, false, wddm.get(), executionEnvironment);
1379-
EXPECT_FALSE(memoryManager.isMemoryBudgetExhausted());
1377+
TEST_F(WddmMemoryManagerTest, givenWddmMemoryManagerWithNoRegisteredOsContextsWhenCallingIsMemoryBudgetExhaustedThenReturnFalse) {
1378+
ASSERT_EQ(0u, memoryManager->getOsContextCount());
1379+
EXPECT_FALSE(memoryManager->isMemoryBudgetExhausted());
1380+
}
1381+
1382+
TEST_F(WddmMemoryManagerTest, givenWddmMemoryManagerWithRegisteredOsContextWhenCallingIsMemoryBudgetExhaustedThenReturnFalse) {
1383+
memoryManager->registerOsContext(new OsContext(osInterface.get(), 0u));
1384+
memoryManager->registerOsContext(new OsContext(osInterface.get(), 1u));
1385+
memoryManager->registerOsContext(new OsContext(osInterface.get(), 2u));
1386+
EXPECT_FALSE(memoryManager->isMemoryBudgetExhausted());
1387+
}
1388+
1389+
TEST_F(WddmMemoryManagerTest, givenWddmMemoryManagerWithRegisteredOsContextWithExhaustedMemoryBudgetWhenCallingIsMemoryBudgetExhaustedThenReturnTrue) {
1390+
memoryManager->registerOsContext(new OsContext(osInterface.get(), 0u));
1391+
memoryManager->registerOsContext(new OsContext(osInterface.get(), 1u));
1392+
memoryManager->registerOsContext(new OsContext(osInterface.get(), 2u));
1393+
memoryManager->getRegisteredOsContext(1)->get()->getResidencyController().setMemoryBudgetExhausted();
1394+
EXPECT_TRUE(memoryManager->isMemoryBudgetExhausted());
13801395
}
13811396

13821397
TEST_F(MockWddmMemoryManagerTest, givenEnabledAsyncDeleterFlagWhenMemoryManagerIsCreatedThenAsyncDeleterEnabledIsTrueAndDeleterIsNotNullptr) {

unit_tests/os_interface/windows/wddm_residency_controller_tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ TEST_F(WddmResidencyControllerTest, givenUsedAllocationWhenCallingRemoveFromTrim
130130
EXPECT_EQ(trimListUnusedPosition, allocation.getTrimCandidateListPosition(osContextId));
131131
}
132132

133+
TEST_F(WddmResidencyControllerTest, givenWddmResidencyControllerWhenIsMemoryExhaustedIsCalledThenReturnCorrectResult) {
134+
EXPECT_FALSE(residencyController->isMemoryBudgetExhausted());
135+
residencyController->setMemoryBudgetExhausted();
136+
EXPECT_TRUE(residencyController->isMemoryBudgetExhausted());
137+
}
138+
133139
TEST_F(WddmResidencyControllerTest, givenUnusedAllocationWhenCallingRemoveFromTrimCandidateListIfUsedThenIgnore) {
134140
MockWddmAllocation allocation;
135141
residencyController->removeFromTrimCandidateListIfUsed(&allocation, false);

0 commit comments

Comments
 (0)