Skip to content

Commit f374696

Browse files
Move MonitoredFence to WddmResidencyController
Change-Id: Iac99e7d730fda26d13feed56d5e4c50adf115e51 Signed-off-by: Maciej Dziuban <maciej.dziuban@intel.com>
1 parent 620708e commit f374696

File tree

12 files changed

+79
-77
lines changed

12 files changed

+79
-77
lines changed

runtime/os_interface/windows/os_context_win.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ OsContextWin::~OsContextImpl() {
3030
wddm.destroyContext(context);
3131
}
3232

33-
void OsContextWin::resetMonitoredFenceParams(D3DKMT_HANDLE &handle, uint64_t *cpuAddress, D3DGPU_VIRTUAL_ADDRESS &gpuAddress) {
34-
monitoredFence.lastSubmittedFence = 0;
35-
monitoredFence.currentFenceValue = 1;
36-
monitoredFence.fenceHandle = handle;
37-
monitoredFence.cpuAddress = cpuAddress;
38-
monitoredFence.gpuAddress = gpuAddress;
39-
}
40-
4133
OsContext::OsContext(OSInterface *osInterface, uint32_t contextId) : contextId(contextId) {
4234
if (osInterface) {
4335
osContextImpl = std::make_unique<OsContextWin>(*osInterface->get()->getWddm(), contextId);

runtime/os_interface/windows/os_context_win.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#pragma once
99
#include "runtime/os_interface/os_context.h"
1010
#include "runtime/os_interface/windows/wddm_residency_controller.h"
11-
#include "runtime/os_interface/windows/windows_wrapper.h"
12-
#include "runtime/os_interface/windows/windows_defs.h"
1311
#include <d3dkmthk.h>
1412

1513
namespace OCLRT {
@@ -33,8 +31,6 @@ class OsContext::OsContextImpl {
3331
bool isInitialized() const {
3432
return initialized;
3533
}
36-
MonitoredFence &getMonitoredFence() { return monitoredFence; }
37-
void resetMonitoredFenceParams(D3DKMT_HANDLE &handle, uint64_t *cpuAddress, D3DGPU_VIRTUAL_ADDRESS &gpuAddress);
3834
Wddm *getWddm() const { return &wddm; }
3935
WddmResidencyController &getResidencyController() { return residencyController; }
4036

@@ -43,7 +39,6 @@ class OsContext::OsContextImpl {
4339
D3DKMT_HANDLE context = 0;
4440
D3DKMT_HANDLE hwQueueHandle = 0;
4541
Wddm &wddm;
46-
MonitoredFence monitoredFence = {};
4742
WddmResidencyController residencyController;
4843
};
4944
} // namespace OCLRT

runtime/os_interface/windows/wddm/wddm.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -711,12 +711,12 @@ bool Wddm::submit(uint64_t commandBuffer, size_t size, void *commandHeader, OsCo
711711
if (currentPagingFenceValue > *pagingFenceAddress && !waitOnGPU(osContext.getContext())) {
712712
return false;
713713
}
714-
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "currentFenceValue =", osContext.getMonitoredFence().currentFenceValue);
714+
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "currentFenceValue =", osContext.getResidencyController().getMonitoredFence().currentFenceValue);
715715

716716
status = wddmInterface->submit(commandBuffer, size, commandHeader, osContext);
717717
if (status) {
718-
osContext.getMonitoredFence().lastSubmittedFence = osContext.getMonitoredFence().currentFenceValue;
719-
osContext.getMonitoredFence().currentFenceValue++;
718+
osContext.getResidencyController().getMonitoredFence().lastSubmittedFence = osContext.getResidencyController().getMonitoredFence().currentFenceValue;
719+
osContext.getResidencyController().getMonitoredFence().currentFenceValue++;
720720
}
721721
getDeviceState();
722722
UNRECOVERABLE_IF(!status);
@@ -742,9 +742,10 @@ void Wddm::getDeviceState() {
742742
}
743743

744744
void Wddm::handleCompletion(OsContextWin &osContext) {
745-
if (osContext.getMonitoredFence().cpuAddress) {
746-
auto *currentTag = osContext.getMonitoredFence().cpuAddress;
747-
while (*currentTag < osContext.getMonitoredFence().currentFenceValue - 1)
745+
auto &monitoredFence = osContext.getResidencyController().getMonitoredFence();
746+
if (monitoredFence.cpuAddress) {
747+
auto *currentTag = monitoredFence.cpuAddress;
748+
while (*currentTag < monitoredFence.currentFenceValue - 1)
748749
;
749750
}
750751
}
@@ -770,10 +771,10 @@ bool Wddm::waitOnGPU(D3DKMT_HANDLE context) {
770771
bool Wddm::waitFromCpu(uint64_t lastFenceValue, OsContextWin &osContext) {
771772
NTSTATUS status = STATUS_SUCCESS;
772773

773-
if (lastFenceValue > *osContext.getMonitoredFence().cpuAddress) {
774+
if (lastFenceValue > *osContext.getResidencyController().getMonitoredFence().cpuAddress) {
774775
D3DKMT_WAITFORSYNCHRONIZATIONOBJECTFROMCPU waitFromCpu = {0};
775776
waitFromCpu.ObjectCount = 1;
776-
waitFromCpu.ObjectHandleArray = &osContext.getMonitoredFence().fenceHandle;
777+
waitFromCpu.ObjectHandleArray = &osContext.getResidencyController().getMonitoredFence().fenceHandle;
777778
waitFromCpu.FenceValueArray = &lastFenceValue;
778779
waitFromCpu.hDevice = device;
779780
waitFromCpu.hAsyncEvent = NULL;

runtime/os_interface/windows/wddm/wddm_interface.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ bool WddmInterface::createMonitoredFence(OsContextWin &osContext) {
2929

3030
DEBUG_BREAK_IF(STATUS_SUCCESS != Status);
3131

32-
osContext.resetMonitoredFenceParams(CreateSynchronizationObject.hSyncObject,
33-
reinterpret_cast<uint64_t *>(CreateSynchronizationObject.Info.MonitoredFence.FenceValueCPUVirtualAddress),
34-
CreateSynchronizationObject.Info.MonitoredFence.FenceValueGPUVirtualAddress);
32+
osContext.getResidencyController().resetMonitoredFenceParams(CreateSynchronizationObject.hSyncObject,
33+
reinterpret_cast<uint64_t *>(CreateSynchronizationObject.Info.MonitoredFence.FenceValueCPUVirtualAddress),
34+
CreateSynchronizationObject.Info.MonitoredFence.FenceValueGPUVirtualAddress);
3535

3636
return Status == STATUS_SUCCESS;
3737
}
@@ -44,7 +44,7 @@ bool WddmInterface20::submit(uint64_t commandBuffer, size_t size, void *commandH
4444
D3DKMT_SUBMITCOMMAND SubmitCommand = {0};
4545
NTSTATUS status = STATUS_SUCCESS;
4646

47-
auto monitoredFence = osContext.getMonitoredFence();
47+
auto monitoredFence = osContext.getResidencyController().getMonitoredFence();
4848
SubmitCommand.Commands = commandBuffer;
4949
SubmitCommand.CommandLength = static_cast<UINT>(size);
5050
SubmitCommand.BroadcastContextCount = 1;
@@ -99,7 +99,7 @@ const bool WddmInterface23::hwQueuesSupported() {
9999
}
100100

101101
bool WddmInterface23::submit(uint64_t commandBuffer, size_t size, void *commandHeader, OsContextWin &osContext) {
102-
auto monitoredFence = osContext.getMonitoredFence();
102+
auto monitoredFence = osContext.getResidencyController().getMonitoredFence();
103103

104104
D3DKMT_SUBMITCOMMANDTOHWQUEUE submitCommand = {};
105105
submitCommand.hHwQueue = osContext.getHwQueue();

runtime/os_interface/windows/wddm_device_command_stream.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ FlushStamp WddmCommandStreamReceiver<GfxFamily>::flush(BatchBuffer &batchBuffer,
116116

117117
wddm->submit(commandStreamAddress, batchBuffer.usedSize - batchBuffer.startOffset, commandBufferHeader, *osContext.get());
118118

119-
return osContext.get()->getMonitoredFence().lastSubmittedFence;
119+
return osContext.get()->getResidencyController().getMonitoredFence().lastSubmittedFence;
120120
}
121121

122122
template <typename GfxFamily>

runtime/os_interface/windows/wddm_memory_manager.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ bool WddmMemoryManager::makeResidentResidencyAllocations(ResidencyContainer &all
483483

484484
osContext.get()->getResidencyController().acquireLock();
485485

486-
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "currentFenceValue =", osContext.get()->getMonitoredFence().currentFenceValue);
486+
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "currentFenceValue =", osContext.get()->getResidencyController().getMonitoredFence().currentFenceValue);
487487

488488
for (uint32_t i = 0; i < residencyCount; i++) {
489489
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(allocationsForResidency[i]);
@@ -538,7 +538,7 @@ bool WddmMemoryManager::makeResidentResidencyAllocations(ResidencyContainer &all
538538
for (uint32_t i = 0; i < residencyCount; i++) {
539539
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(allocationsForResidency[i]);
540540
// Update fence value not to early destroy / evict allocation
541-
auto currentFence = osContext.get()->getMonitoredFence().currentFenceValue;
541+
auto currentFence = osContext.get()->getResidencyController().getMonitoredFence().currentFenceValue;
542542
allocation->getResidencyData().updateCompletionData(currentFence, osContext.getContextId());
543543
allocation->getResidencyData().resident = true;
544544

@@ -636,7 +636,7 @@ void WddmMemoryManager::trimResidency(D3DDDI_TRIMRESIDENCYSET_FLAGS flags, uint6
636636
}
637637

638638
if (flags.PeriodicTrim || flags.RestartPeriodicTrim) {
639-
const auto newPeriodicTrimFenceValue = *osContext.get()->getMonitoredFence().cpuAddress;
639+
const auto newPeriodicTrimFenceValue = *osContext.get()->getResidencyController().getMonitoredFence().cpuAddress;
640640
osContext.get()->getResidencyController().setLastTrimFenceValue(newPeriodicTrimFenceValue);
641641
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "updated lastPeriodicTrimFenceValue =", newPeriodicTrimFenceValue);
642642
}
@@ -660,7 +660,7 @@ bool WddmMemoryManager::trimResidencyToBudget(uint64_t bytes) {
660660
}
661661

662662
lastFence = wddmAllocation->getResidencyData().getFenceValueForContextId(0);
663-
auto &monitoredFence = osContext.get()->getMonitoredFence();
663+
auto &monitoredFence = osContext.get()->getResidencyController().getMonitoredFence();
664664

665665
if (lastFence <= monitoredFence.lastSubmittedFence) {
666666
uint32_t fragmentsToEvict = 0;

runtime/os_interface/windows/wddm_residency_controller.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,12 @@ void WddmResidencyController::compactTrimCandidateList() {
163163
checkTrimCandidateCount();
164164
}
165165

166+
void WddmResidencyController::resetMonitoredFenceParams(D3DKMT_HANDLE &handle, uint64_t *cpuAddress, D3DGPU_VIRTUAL_ADDRESS &gpuAddress) {
167+
monitoredFence.lastSubmittedFence = 0;
168+
monitoredFence.currentFenceValue = 1;
169+
monitoredFence.fenceHandle = handle;
170+
monitoredFence.cpuAddress = cpuAddress;
171+
monitoredFence.gpuAddress = gpuAddress;
172+
}
173+
166174
} // namespace OCLRT

runtime/os_interface/windows/wddm_residency_controller.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#pragma once
99

1010
#include "runtime/memory_manager/residency_container.h"
11+
#include "runtime/os_interface/windows/windows_wrapper.h"
12+
#include "runtime/os_interface/windows/windows_defs.h"
1113

1214
#include <atomic>
1315

@@ -40,12 +42,16 @@ class WddmResidencyController {
4042
const ResidencyContainer &peekTrimCandidateList() const { return trimCandidateList; }
4143
uint32_t peekTrimCandidatesCount() const { return trimCandidatesCount; }
4244

45+
MonitoredFence &getMonitoredFence() { return monitoredFence; }
46+
void resetMonitoredFenceParams(D3DKMT_HANDLE &handle, uint64_t *cpuAddress, D3DGPU_VIRTUAL_ADDRESS &gpuAddress);
47+
4348
protected:
4449
uint32_t osContextId;
4550
std::atomic<bool> lock = false;
4651
std::atomic_flag trimCallbackLock = ATOMIC_FLAG_INIT;
4752
uint64_t lastTrimFenceValue = 0u;
4853
ResidencyContainer trimCandidateList;
4954
uint32_t trimCandidatesCount = 0;
55+
MonitoredFence monitoredFence = {};
5056
};
5157
} // namespace OCLRT

unit_tests/os_interface/windows/device_command_stream_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ TEST_F(WddmCommandStreamTest, Flush) {
194194

195195
EXPECT_EQ(1u, wddm->submitResult.called);
196196
EXPECT_TRUE(wddm->submitResult.success);
197-
EXPECT_EQ(flushStamp, device->getOsContext()->get()->getMonitoredFence().lastSubmittedFence);
197+
EXPECT_EQ(flushStamp, device->getOsContext()->get()->getResidencyController().getMonitoredFence().lastSubmittedFence);
198198

199199
memoryManager->freeGraphicsMemory(commandBuffer);
200200
}

unit_tests/os_interface/windows/wddm20_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ TEST_F(Wddm20Tests, makeResidentNonResident) {
383383
error = wddm->evict(&allocation.handle, 1, sizeToTrim);
384384
EXPECT_TRUE(error);
385385

386-
auto monitoredFence = osContextWin->getMonitoredFence();
386+
auto monitoredFence = osContextWin->getResidencyController().getMonitoredFence();
387387
UINT64 fenceValue = 100;
388388
monitoredFence.cpuAddress = &fenceValue;
389389
monitoredFence.currentFenceValue = 101;
@@ -634,7 +634,7 @@ TEST_F(Wddm20Tests, givenDestroyAllocationWhenItIsCalledThenAllocationIsPassedTo
634634
allocation.getResidencyData().updateCompletionData(10, osContext.get()->getContextId());
635635
allocation.handle = ALLOCATION_HANDLE;
636636

637-
*osContextWin->getMonitoredFence().cpuAddress = 10;
637+
*osContextWin->getResidencyController().getMonitoredFence().cpuAddress = 10;
638638

639639
D3DKMT_HANDLE handle = (D3DKMT_HANDLE)0x1234;
640640

@@ -662,7 +662,7 @@ TEST_F(Wddm20Tests, WhenLastFenceLessEqualThanMonitoredThenWaitFromCpuIsNotCalle
662662
allocation.getResidencyData().updateCompletionData(10, osContext.get()->getContextId());
663663
allocation.handle = ALLOCATION_HANDLE;
664664

665-
*osContextWin->getMonitoredFence().cpuAddress = 10;
665+
*osContextWin->getResidencyController().getMonitoredFence().cpuAddress = 10;
666666

667667
gdi->getWaitFromCpuArg().FenceValueArray = nullptr;
668668
gdi->getWaitFromCpuArg().Flags.Value = 0;
@@ -685,7 +685,7 @@ TEST_F(Wddm20Tests, WhenLastFenceGreaterThanMonitoredThenWaitFromCpuIsCalled) {
685685
allocation.getResidencyData().updateCompletionData(10, osContext.get()->getContextId());
686686
allocation.handle = ALLOCATION_HANDLE;
687687

688-
*osContextWin->getMonitoredFence().cpuAddress = 10;
688+
*osContextWin->getResidencyController().getMonitoredFence().cpuAddress = 10;
689689

690690
gdi->getWaitFromCpuArg().FenceValueArray = nullptr;
691691
gdi->getWaitFromCpuArg().Flags.Value = 0;
@@ -711,7 +711,7 @@ TEST_F(Wddm20Tests, createMonitoredFenceIsInitializedWithFenceValueZeroAndCurren
711711
wddm->wddmInterface->createMonitoredFence(*osContextWin);
712712

713713
EXPECT_EQ(0u, gdi->getCreateSynchronizationObject2Arg().Info.MonitoredFence.InitialFenceValue);
714-
EXPECT_EQ(1u, osContextWin->getMonitoredFence().currentFenceValue);
714+
EXPECT_EQ(1u, osContextWin->getResidencyController().getMonitoredFence().currentFenceValue);
715715
}
716716

717717
NTSTATUS APIENTRY queryResourceInfoMock(D3DKMT_QUERYRESOURCEINFO *pData) {

0 commit comments

Comments
 (0)