Skip to content

Commit bd431ed

Browse files
Return trimCallbackHandle from register method instead of storing in Wddm
Change-Id: Iddfb6a926311480e355d52e5bf3e6379ec96f7a9 Signed-off-by: Maciej Dziuban <maciej.dziuban@intel.com>
1 parent 4aae1a8 commit bd431ed

File tree

8 files changed

+46
-21
lines changed

8 files changed

+46
-21
lines changed

runtime/os_interface/windows/wddm/wddm.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,9 @@ uint64_t Wddm::getHeap32Size() {
811811
return alignDown(gfxPartition.Heap32[0].Limit, MemoryConstants::pageSize);
812812
}
813813

814-
void Wddm::registerTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, WddmMemoryManager *memoryManager) {
814+
VOID *Wddm::registerTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, WddmMemoryManager *memoryManager) {
815815
if (DebugManager.flags.DoNotRegisterTrimCallback.get()) {
816-
return;
816+
return nullptr;
817817
}
818818
D3DKMT_REGISTERTRIMNOTIFICATION registerTrimNotification;
819819
registerTrimNotification.Callback = callback;
@@ -823,17 +823,19 @@ void Wddm::registerTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, Wdd
823823

824824
NTSTATUS status = gdi->registerTrimNotification(&registerTrimNotification);
825825
if (status == STATUS_SUCCESS) {
826-
trimCallbackHandle = registerTrimNotification.Handle;
826+
return registerTrimNotification.Handle;
827827
}
828+
return nullptr;
828829
}
829830

830-
void Wddm::unregisterTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback) {
831-
if (callback == nullptr) {
831+
void Wddm::unregisterTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, VOID *trimCallbackHandle) {
832+
DEBUG_BREAK_IF(callback == nullptr);
833+
if (trimCallbackHandle == nullptr) {
832834
return;
833835
}
834836
D3DKMT_UNREGISTERTRIMNOTIFICATION unregisterTrimNotification;
835837
unregisterTrimNotification.Callback = callback;
836-
unregisterTrimNotification.Handle = this->trimCallbackHandle;
838+
unregisterTrimNotification.Handle = trimCallbackHandle;
837839

838840
NTSTATUS status = gdi->unregisterTrimNotification(&unregisterTrimNotification);
839841
DEBUG_BREAK_IF(status != STATUS_SUCCESS);

runtime/os_interface/windows/wddm/wddm.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ class Wddm {
7979
MOCKABLE_VIRTUAL bool waitFromCpu(uint64_t lastFenceValue, OsContextWin &osContext);
8080

8181
NTSTATUS escape(D3DKMT_ESCAPE &escapeCommand);
82-
void registerTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, WddmMemoryManager *memoryManager);
83-
void unregisterTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback);
82+
VOID *registerTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, WddmMemoryManager *memoryManager);
83+
void unregisterTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, VOID *trimCallbackHandle);
8484
MOCKABLE_VIRTUAL void releaseReservedAddress(void *reservedAddress);
8585
MOCKABLE_VIRTUAL bool reserveValidAddressRange(size_t size, void *&reservedMem);
8686

@@ -177,7 +177,6 @@ class Wddm {
177177

178178
unsigned long hwContextId = 0;
179179
LUID adapterLuid;
180-
void *trimCallbackHandle = nullptr;
181180
uintptr_t maximumApplicationAddress = 0;
182181
GPUNODE_ORDINAL node = GPUNODE_3D;
183182
PreemptionMode preemptionMode = PreemptionMode::Disabled;

runtime/os_interface/windows/wddm_memory_manager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ WddmMemoryManager::~WddmMemoryManager() {
3232
if (osContext) {
3333
auto &residencyController = osContext->get()->getResidencyController();
3434
residencyController.acquireTrimCallbackLock();
35-
wddm->unregisterTrimCallback(trimCallback);
35+
wddm->unregisterTrimCallback(trimCallback, this->trimCallbackHandle);
3636
residencyController.releaseTrimCallbackLock();
3737

3838
// Wait for lock to ensure trimCallback ended
@@ -46,7 +46,7 @@ WddmMemoryManager::WddmMemoryManager(bool enable64kbPages, bool enableLocalMemor
4646
DEBUG_BREAK_IF(wddm == nullptr);
4747
this->wddm = wddm;
4848
allocator32Bit = std::unique_ptr<Allocator32bit>(new Allocator32bit(wddm->getHeap32Base(), wddm->getHeap32Size()));
49-
wddm->registerTrimCallback(trimCallback, this);
49+
this->trimCallbackHandle = wddm->registerTrimCallback(trimCallback, this);
5050
asyncDeleterEnabled = DebugManager.flags.EnableDeferredDeleter.get();
5151
if (asyncDeleterEnabled)
5252
deferredDeleter = createDeferredDeleter();

runtime/os_interface/windows/wddm_memory_manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class WddmMemoryManager : public MemoryManager {
8181
protected:
8282
void trimResidency(D3DDDI_TRIMRESIDENCYSET_FLAGS flags, uint64_t bytes);
8383
bool trimResidencyToBudget(uint64_t bytes);
84+
VOID *trimCallbackHandle = nullptr;
8485

8586
GraphicsAllocation *createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle);
8687
static bool validateAllocation(WddmAllocation *alloc);

unit_tests/mocks/mock_wddm.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class WddmMock : public Wddm {
4444
using Wddm::pagingFenceAddress;
4545
using Wddm::pagingQueue;
4646
using Wddm::preemptionMode;
47-
using Wddm::trimCallbackHandle;
4847
using Wddm::wddmInterface;
4948

5049
WddmMock() : Wddm(){};

unit_tests/os_interface/windows/mock_wddm_memory_manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class MockWddmMemoryManager : public WddmMemoryManager {
1515

1616
public:
1717
using BaseClass::createWddmAllocation;
18+
using BaseClass::trimCallbackHandle;
1819
using BaseClass::trimResidency;
1920
using BaseClass::trimResidencyToBudget;
2021
using BaseClass::WddmMemoryManager;

unit_tests/os_interface/windows/wddm20_tests.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,3 +778,34 @@ TEST_F(Wddm20Tests, whenContextIsInitializedThenApplyAdditionalContextFlagsIsCal
778778
EXPECT_TRUE(result);
779779
EXPECT_EQ(1u, wddm->applyAdditionalContextFlagsResult.called);
780780
}
781+
782+
TEST_F(Wddm20Tests, givenTrimCallbackRegistrationIsDisabledInDebugVariableWhenRegisteringCallbackThenReturnNullptr) {
783+
DebugManagerStateRestore stateRestore;
784+
DebugManager.flags.DoNotRegisterTrimCallback.set(true);
785+
EXPECT_EQ(nullptr, wddm->registerTrimCallback([](D3DKMT_TRIMNOTIFICATION *) {}, nullptr));
786+
}
787+
788+
TEST_F(Wddm20Tests, givenSuccessWhenRegisteringTrimCallbackThenReturnTrimCallbackHandle) {
789+
auto trimCallbackHandle = wddm->registerTrimCallback([](D3DKMT_TRIMNOTIFICATION *) {}, nullptr);
790+
EXPECT_NE(nullptr, trimCallbackHandle);
791+
}
792+
793+
TEST_F(Wddm20Tests, givenCorrectArgumentsWhenUnregisteringTrimCallbackThenPassArgumentsToGdiCall) {
794+
PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback = [](D3DKMT_TRIMNOTIFICATION *) {};
795+
auto trimCallbackHandle = reinterpret_cast<VOID *>(0x9876);
796+
797+
wddm->unregisterTrimCallback(callback, trimCallbackHandle);
798+
EXPECT_EQ(callback, gdi->getUnregisterTrimNotificationArg().Callback);
799+
EXPECT_EQ(trimCallbackHandle, gdi->getUnregisterTrimNotificationArg().Handle);
800+
}
801+
802+
TEST_F(Wddm20Tests, givenNullTrimCallbackHandleWhenUnregisteringTrimCallbackThenDoNotDoGdiCall) {
803+
PFND3DKMT_TRIMNOTIFICATIONCALLBACK callbackBefore = [](D3DKMT_TRIMNOTIFICATION *) {};
804+
auto trimCallbackHandleBefore = reinterpret_cast<VOID *>(0x9876);
805+
gdi->getUnregisterTrimNotificationArg().Callback = callbackBefore;
806+
gdi->getUnregisterTrimNotificationArg().Handle = trimCallbackHandleBefore;
807+
808+
wddm->unregisterTrimCallback([](D3DKMT_TRIMNOTIFICATION *) {}, nullptr);
809+
EXPECT_EQ(callbackBefore, gdi->getUnregisterTrimNotificationArg().Callback);
810+
EXPECT_EQ(trimCallbackHandleBefore, gdi->getUnregisterTrimNotificationArg().Handle);
811+
}

unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ TEST_F(WddmMemoryManagerResidencyTest, trimCallbackIsRegisteredInWddmMemoryManag
946946
}
947947

948948
TEST_F(WddmMemoryManagerResidencyTest, givenWddmMemoryManagerWhenCallingDestructorThenUnregisterTrimCallback) {
949-
auto trimCallbackHandle = wddm->trimCallbackHandle;
949+
auto trimCallbackHandle = memoryManager->trimCallbackHandle;
950950
auto trimCallbackAddress = reinterpret_cast<PFND3DKMT_TRIMNOTIFICATIONCALLBACK>(memoryManager->trimCallback);
951951
memoryManager.reset();
952952

@@ -955,14 +955,6 @@ TEST_F(WddmMemoryManagerResidencyTest, givenWddmMemoryManagerWhenCallingDestruct
955955
EXPECT_EQ(trimCallbackHandle, unregisterNotification.Handle);
956956
}
957957

958-
TEST(WddmDebugModesTests, givenDebugModeWhenItIsActiveThenTrimCallbackIsNotRegistred) {
959-
DebugManagerStateRestore stateRestore;
960-
DebugManager.flags.DoNotRegisterTrimCallback.set(true);
961-
WddmMock wddm;
962-
wddm.init();
963-
EXPECT_EQ(nullptr, wddm.trimCallbackHandle);
964-
}
965-
966958
TEST_F(WddmMemoryManagerResidencyTest, givenNotUsedAllocationsFromPreviousPeriodicTrimWhenTrimResidencyPeriodicTrimIsCalledThenAllocationsAreEvictedMarkedAndRemovedFromTrimCandidateList) {
967959
D3DKMT_TRIMNOTIFICATION trimNotification = {0};
968960
trimNotification.Flags.PeriodicTrim = 1;

0 commit comments

Comments
 (0)