Skip to content

Commit 9bdedc6

Browse files
Move residency functions to WddmResidencyController
Change-Id: Ic73a51546b3325515f293808550376fa6b57dec3 Signed-off-by: Maciej Dziuban <[email protected]>
1 parent 4341ad0 commit 9bdedc6

File tree

7 files changed

+304
-285
lines changed

7 files changed

+304
-285
lines changed

runtime/os_interface/windows/wddm_device_command_stream.inl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ void WddmCommandStreamReceiver<GfxFamily>::makeResident(GraphicsAllocation &gfxA
129129

130130
template <typename GfxFamily>
131131
void WddmCommandStreamReceiver<GfxFamily>::processResidency(ResidencyContainer &allocationsForResidency, OsContext &osContext) {
132-
bool success = getMemoryManager()->makeResidentResidencyAllocations(allocationsForResidency, osContext);
132+
bool success = osContext.get()->getResidencyController().makeResidentResidencyAllocations(allocationsForResidency);
133133
DEBUG_BREAK_IF(!success);
134134
}
135135

136136
template <typename GfxFamily>
137137
void WddmCommandStreamReceiver<GfxFamily>::processEviction(OsContext &osContext) {
138-
getMemoryManager()->makeNonResidentEvictionAllocations(this->getEvictionAllocations(), osContext);
138+
osContext.get()->getResidencyController().makeNonResidentEvictionAllocations(this->getEvictionAllocations());
139139
this->getEvictionAllocations().clear();
140140
}
141141

runtime/os_interface/windows/wddm_memory_manager.cpp

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -451,95 +451,6 @@ uint64_t WddmMemoryManager::getInternalHeapBaseAddress() {
451451
return this->wddm->getGfxPartition().Heap32[1].Base;
452452
}
453453

454-
bool WddmMemoryManager::makeResidentResidencyAllocations(ResidencyContainer &allocationsForResidency, OsContext &osContext) {
455-
size_t residencyCount = allocationsForResidency.size();
456-
std::unique_ptr<D3DKMT_HANDLE[]> handlesForResidency(new D3DKMT_HANDLE[residencyCount * maxFragmentsCount]);
457-
458-
uint32_t totalHandlesCount = 0;
459-
460-
auto lock = osContext.get()->getResidencyController().acquireLock();
461-
462-
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "currentFenceValue =", osContext.get()->getResidencyController().getMonitoredFence().currentFenceValue);
463-
464-
for (uint32_t i = 0; i < residencyCount; i++) {
465-
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(allocationsForResidency[i]);
466-
bool mainResidency = false;
467-
bool fragmentResidency[3] = {false, false, false};
468-
469-
mainResidency = allocation->getResidencyData().resident;
470-
471-
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation =", allocation, mainResidency ? "resident" : "not resident");
472-
473-
if (allocation->getTrimCandidateListPosition(osContext.getContextId()) != trimListUnusedPosition) {
474-
475-
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation =", allocation, "on trimCandidateList");
476-
osContext.get()->getResidencyController().removeFromTrimCandidateList(allocation, false);
477-
} else {
478-
479-
for (uint32_t allocationId = 0; allocationId < allocation->fragmentsStorage.fragmentCount; allocationId++) {
480-
fragmentResidency[allocationId] = allocation->fragmentsStorage.fragmentStorageData[allocationId].residency->resident;
481-
482-
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "fragment handle =",
483-
allocation->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle,
484-
fragmentResidency[allocationId] ? "resident" : "not resident");
485-
}
486-
}
487-
488-
if (allocation->fragmentsStorage.fragmentCount == 0) {
489-
if (!mainResidency)
490-
handlesForResidency[totalHandlesCount++] = allocation->handle;
491-
} else {
492-
for (uint32_t allocationId = 0; allocationId < allocation->fragmentsStorage.fragmentCount; allocationId++) {
493-
if (!fragmentResidency[allocationId])
494-
handlesForResidency[totalHandlesCount++] = allocation->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle;
495-
}
496-
}
497-
}
498-
499-
bool result = true;
500-
if (totalHandlesCount) {
501-
uint64_t bytesToTrim = 0;
502-
while ((result = wddm->makeResident(handlesForResidency.get(), totalHandlesCount, false, &bytesToTrim)) == false) {
503-
osContext.get()->getResidencyController().setMemoryBudgetExhausted();
504-
bool trimmingDone = this->getRegisteredOsContext(0u)->get()->getResidencyController().trimResidencyToBudget(bytesToTrim);
505-
bool cantTrimFurther = !trimmingDone;
506-
if (cantTrimFurther) {
507-
result = wddm->makeResident(handlesForResidency.get(), totalHandlesCount, true, &bytesToTrim);
508-
break;
509-
}
510-
}
511-
}
512-
513-
if (result == true) {
514-
for (uint32_t i = 0; i < residencyCount; i++) {
515-
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(allocationsForResidency[i]);
516-
// Update fence value not to early destroy / evict allocation
517-
auto currentFence = osContext.get()->getResidencyController().getMonitoredFence().currentFenceValue;
518-
allocation->getResidencyData().updateCompletionData(currentFence, osContext.getContextId());
519-
allocation->getResidencyData().resident = true;
520-
521-
for (uint32_t allocationId = 0; allocationId < allocation->fragmentsStorage.fragmentCount; allocationId++) {
522-
auto residencyData = allocation->fragmentsStorage.fragmentStorageData[allocationId].residency;
523-
// Update fence value not to remove the fragment referenced by different GA in trimming callback
524-
residencyData->updateCompletionData(currentFence, osContext.getContextId());
525-
residencyData->resident = true;
526-
}
527-
}
528-
}
529-
530-
return result;
531-
}
532-
533-
void WddmMemoryManager::makeNonResidentEvictionAllocations(ResidencyContainer &evictionAllocations, OsContext &osContext) {
534-
auto lock = osContext.get()->getResidencyController().acquireLock();
535-
const size_t residencyCount = evictionAllocations.size();
536-
537-
for (uint32_t i = 0; i < residencyCount; i++) {
538-
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(evictionAllocations[i]);
539-
osContext.get()->getResidencyController().addToTrimCandidateList(allocation);
540-
}
541-
}
542-
543454
bool WddmMemoryManager::mapAuxGpuVA(GraphicsAllocation *graphicsAllocation) {
544455
return wddm->updateAuxTable(graphicsAllocation->getGpuAddress(), graphicsAllocation->gmm, true);
545456
}

runtime/os_interface/windows/wddm_memory_manager.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ class WddmMemoryManager : public MemoryManager {
4949
void *lockResource(GraphicsAllocation *graphicsAllocation) override;
5050
void unlockResource(GraphicsAllocation *graphicsAllocation) override;
5151

52-
bool makeResidentResidencyAllocations(ResidencyContainer &allocationsForResidency, OsContext &osContext);
53-
void makeNonResidentEvictionAllocations(ResidencyContainer &evictionAllocations, OsContext &osContext);
54-
5552
AllocationStatus populateOsHandles(OsHandleStorage &handleStorage) override;
5653
void cleanOsHandles(OsHandleStorage &handleStorage) override;
5754

runtime/os_interface/windows/wddm_residency_controller.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,93 @@ bool WddmResidencyController::trimResidencyToBudget(uint64_t bytes) {
310310
return numberOfBytesToTrim == 0;
311311
}
312312

313+
bool WddmResidencyController::makeResidentResidencyAllocations(ResidencyContainer &allocationsForResidency) {
314+
size_t residencyCount = allocationsForResidency.size();
315+
std::unique_ptr<D3DKMT_HANDLE[]> handlesForResidency(new D3DKMT_HANDLE[residencyCount * maxFragmentsCount]);
316+
317+
uint32_t totalHandlesCount = 0;
318+
319+
auto lock = this->acquireLock();
320+
321+
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "currentFenceValue =", this->getMonitoredFence().currentFenceValue);
322+
323+
for (uint32_t i = 0; i < residencyCount; i++) {
324+
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(allocationsForResidency[i]);
325+
bool mainResidency = false;
326+
bool fragmentResidency[3] = {false, false, false};
327+
328+
mainResidency = allocation->getResidencyData().resident;
329+
330+
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation =", allocation, mainResidency ? "resident" : "not resident");
331+
332+
if (allocation->getTrimCandidateListPosition(this->osContextId) != trimListUnusedPosition) {
333+
334+
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "allocation =", allocation, "on trimCandidateList");
335+
this->removeFromTrimCandidateList(allocation, false);
336+
} else {
337+
338+
for (uint32_t allocationId = 0; allocationId < allocation->fragmentsStorage.fragmentCount; allocationId++) {
339+
fragmentResidency[allocationId] = allocation->fragmentsStorage.fragmentStorageData[allocationId].residency->resident;
340+
341+
DBG_LOG(ResidencyDebugEnable, "Residency:", __FUNCTION__, "fragment handle =",
342+
allocation->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle,
343+
fragmentResidency[allocationId] ? "resident" : "not resident");
344+
}
345+
}
346+
347+
if (allocation->fragmentsStorage.fragmentCount == 0) {
348+
if (!mainResidency)
349+
handlesForResidency[totalHandlesCount++] = allocation->handle;
350+
} else {
351+
for (uint32_t allocationId = 0; allocationId < allocation->fragmentsStorage.fragmentCount; allocationId++) {
352+
if (!fragmentResidency[allocationId])
353+
handlesForResidency[totalHandlesCount++] = allocation->fragmentsStorage.fragmentStorageData[allocationId].osHandleStorage->handle;
354+
}
355+
}
356+
}
357+
358+
bool result = true;
359+
if (totalHandlesCount) {
360+
uint64_t bytesToTrim = 0;
361+
while ((result = wddm.makeResident(handlesForResidency.get(), totalHandlesCount, false, &bytesToTrim)) == false) {
362+
this->setMemoryBudgetExhausted();
363+
bool trimmingDone = this->trimResidencyToBudget(bytesToTrim);
364+
bool cantTrimFurther = !trimmingDone;
365+
if (cantTrimFurther) {
366+
result = wddm.makeResident(handlesForResidency.get(), totalHandlesCount, true, &bytesToTrim);
367+
break;
368+
}
369+
}
370+
}
371+
372+
if (result == true) {
373+
for (uint32_t i = 0; i < residencyCount; i++) {
374+
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(allocationsForResidency[i]);
375+
// Update fence value not to early destroy / evict allocation
376+
auto currentFence = this->getMonitoredFence().currentFenceValue;
377+
allocation->getResidencyData().updateCompletionData(currentFence, this->osContextId);
378+
allocation->getResidencyData().resident = true;
379+
380+
for (uint32_t allocationId = 0; allocationId < allocation->fragmentsStorage.fragmentCount; allocationId++) {
381+
auto residencyData = allocation->fragmentsStorage.fragmentStorageData[allocationId].residency;
382+
// Update fence value not to remove the fragment referenced by different GA in trimming callback
383+
residencyData->updateCompletionData(currentFence, this->osContextId);
384+
residencyData->resident = true;
385+
}
386+
}
387+
}
388+
389+
return result;
390+
}
391+
392+
void WddmResidencyController::makeNonResidentEvictionAllocations(ResidencyContainer &evictionAllocations) {
393+
auto lock = this->acquireLock();
394+
const size_t residencyCount = evictionAllocations.size();
395+
396+
for (uint32_t i = 0; i < residencyCount; i++) {
397+
WddmAllocation *allocation = reinterpret_cast<WddmAllocation *>(evictionAllocations[i]);
398+
this->addToTrimCandidateList(allocation);
399+
}
400+
}
401+
313402
} // namespace OCLRT

runtime/os_interface/windows/wddm_residency_controller.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class WddmResidencyController {
5252
bool isMemoryBudgetExhausted() const { return memoryBudgetExhausted; }
5353
void setMemoryBudgetExhausted() { memoryBudgetExhausted = true; }
5454

55+
bool makeResidentResidencyAllocations(ResidencyContainer &allocationsForResidency);
56+
void makeNonResidentEvictionAllocations(ResidencyContainer &evictionAllocations);
57+
5558
protected:
5659
Wddm &wddm;
5760
uint32_t osContextId;

0 commit comments

Comments
 (0)