@@ -63,7 +63,7 @@ void APIENTRY WddmMemoryManager::trimCallback(_Inout_ D3DKMT_TRIMNOTIFICATION *t
6363 }
6464
6565 wddmMemMngr->getRegisteredOsContext (0 )->get ()->getResidencyController ().acquireTrimCallbackLock ();
66- wddmMemMngr->trimResidency (trimNotification->Flags , trimNotification->NumBytesToTrim );
66+ wddmMemMngr->getRegisteredOsContext ( 0 )-> get ()-> getResidencyController (). trimResidency (trimNotification->Flags , trimNotification->NumBytesToTrim );
6767 wddmMemMngr->getRegisteredOsContext (0 )->get ()->getResidencyController ().releaseTrimCallbackLock ();
6868}
6969
@@ -526,7 +526,7 @@ bool WddmMemoryManager::makeResidentResidencyAllocations(ResidencyContainer &all
526526 uint64_t bytesToTrim = 0 ;
527527 while ((result = wddm->makeResident (handlesForResidency.get (), totalHandlesCount, false , &bytesToTrim)) == false ) {
528528 this ->memoryBudgetExhausted = true ;
529- bool trimmingDone = trimResidencyToBudget (bytesToTrim);
529+ bool trimmingDone = this -> getRegisteredOsContext ( 0u )-> get ()-> getResidencyController (). trimResidencyToBudget (bytesToTrim);
530530 bool cantTrimFurther = !trimmingDone;
531531 if (cantTrimFurther) {
532532 result = wddm->makeResident (handlesForResidency.get (), totalHandlesCount, true , &bytesToTrim);
@@ -571,152 +571,6 @@ void WddmMemoryManager::makeNonResidentEvictionAllocations(ResidencyContainer &e
571571 osContext.get ()->getResidencyController ().releaseLock ();
572572}
573573
574- void WddmMemoryManager::trimResidency (D3DDDI_TRIMRESIDENCYSET_FLAGS flags, uint64_t bytes) {
575- OsContext &osContext = *getRegisteredOsContext (0 );
576- if (flags.PeriodicTrim ) {
577- bool periodicTrimDone = false ;
578- D3DKMT_HANDLE fragmentEvictHandles[3 ] = {0 };
579- uint64_t sizeToTrim = 0 ;
580-
581- osContext.get ()->getResidencyController ().acquireLock ();
582-
583- WddmAllocation *wddmAllocation = nullptr ;
584- while ((wddmAllocation = osContext.get ()->getResidencyController ().getTrimCandidateHead ()) != nullptr ) {
585-
586- DBG_LOG (ResidencyDebugEnable, " Residency:" , __FUNCTION__, " lastPeriodicTrimFenceValue = " , osContext.get ()->getResidencyController ().getLastTrimFenceValue ());
587-
588- // allocation was not used from last periodic trim
589- if (wddmAllocation->getResidencyData ().getFenceValueForContextId (0 ) <= osContext.get ()->getResidencyController ().getLastTrimFenceValue ()) {
590-
591- DBG_LOG (ResidencyDebugEnable, " Residency:" , __FUNCTION__, " allocation: handle =" , wddmAllocation->handle , " lastFence =" , (wddmAllocation)->getResidencyData ().getFenceValueForContextId (0 ));
592-
593- uint32_t fragmentsToEvict = 0 ;
594-
595- if (wddmAllocation->fragmentsStorage .fragmentCount == 0 ) {
596- DBG_LOG (ResidencyDebugEnable, " Residency:" , __FUNCTION__, " Evict allocation: handle =" , wddmAllocation->handle , " lastFence =" , (wddmAllocation)->getResidencyData ().getFenceValueForContextId (0 ));
597- wddm->evict (&wddmAllocation->handle , 1 , sizeToTrim);
598- }
599-
600- for (uint32_t allocationId = 0 ; allocationId < wddmAllocation->fragmentsStorage .fragmentCount ; allocationId++) {
601- if (wddmAllocation->fragmentsStorage .fragmentStorageData [allocationId].residency ->getFenceValueForContextId (0 ) <= osContext.get ()->getResidencyController ().getLastTrimFenceValue ()) {
602-
603- DBG_LOG (ResidencyDebugEnable, " Residency:" , __FUNCTION__, " Evict fragment: handle =" , wddmAllocation->fragmentsStorage .fragmentStorageData [allocationId].osHandleStorage ->handle , " lastFence =" , wddmAllocation->fragmentsStorage .fragmentStorageData [allocationId].residency ->getFenceValueForContextId (0 ));
604-
605- fragmentEvictHandles[fragmentsToEvict++] = wddmAllocation->fragmentsStorage .fragmentStorageData [allocationId].osHandleStorage ->handle ;
606- wddmAllocation->fragmentsStorage .fragmentStorageData [allocationId].residency ->resident = false ;
607- }
608- }
609-
610- if (fragmentsToEvict != 0 ) {
611- wddm->evict ((D3DKMT_HANDLE *)fragmentEvictHandles, fragmentsToEvict, sizeToTrim);
612- }
613-
614- wddmAllocation->getResidencyData ().resident = false ;
615-
616- osContext.get ()->getResidencyController ().removeFromTrimCandidateList (wddmAllocation, false );
617- } else {
618- periodicTrimDone = true ;
619- break ;
620- }
621- }
622-
623- if (osContext.get ()->getResidencyController ().checkTrimCandidateListCompaction ()) {
624- osContext.get ()->getResidencyController ().compactTrimCandidateList ();
625- }
626-
627- osContext.get ()->getResidencyController ().releaseLock ();
628- }
629-
630- if (flags.TrimToBudget ) {
631-
632- osContext.get ()->getResidencyController ().acquireLock ();
633-
634- trimResidencyToBudget (bytes);
635-
636- osContext.get ()->getResidencyController ().releaseLock ();
637- }
638-
639- if (flags.PeriodicTrim || flags.RestartPeriodicTrim ) {
640- const auto newPeriodicTrimFenceValue = *osContext.get ()->getResidencyController ().getMonitoredFence ().cpuAddress ;
641- osContext.get ()->getResidencyController ().setLastTrimFenceValue (newPeriodicTrimFenceValue);
642- DBG_LOG (ResidencyDebugEnable, " Residency:" , __FUNCTION__, " updated lastPeriodicTrimFenceValue =" , newPeriodicTrimFenceValue);
643- }
644- }
645-
646- bool WddmMemoryManager::trimResidencyToBudget (uint64_t bytes) {
647- bool trimToBudgetDone = false ;
648- D3DKMT_HANDLE fragmentEvictHandles[3 ] = {0 };
649- uint64_t numberOfBytesToTrim = bytes;
650- WddmAllocation *wddmAllocation = nullptr ;
651- auto &osContext = *getRegisteredOsContext (0 );
652-
653- trimToBudgetDone = (numberOfBytesToTrim == 0 );
654-
655- while (!trimToBudgetDone) {
656- uint64_t lastFence = 0 ;
657- wddmAllocation = osContext.get ()->getResidencyController ().getTrimCandidateHead ();
658-
659- if (wddmAllocation == nullptr ) {
660- break ;
661- }
662-
663- lastFence = wddmAllocation->getResidencyData ().getFenceValueForContextId (0 );
664- auto &monitoredFence = osContext.get ()->getResidencyController ().getMonitoredFence ();
665-
666- if (lastFence <= monitoredFence.lastSubmittedFence ) {
667- uint32_t fragmentsToEvict = 0 ;
668- uint64_t sizeEvicted = 0 ;
669- uint64_t sizeToTrim = 0 ;
670-
671- if (lastFence > *monitoredFence.cpuAddress ) {
672- wddm->waitFromCpu (lastFence, *osContext.get ());
673- }
674-
675- if (wddmAllocation->fragmentsStorage .fragmentCount == 0 ) {
676- wddm->evict (&wddmAllocation->handle , 1 , sizeToTrim);
677-
678- sizeEvicted = wddmAllocation->getAlignedSize ();
679- } else {
680- auto &fragmentStorageData = wddmAllocation->fragmentsStorage .fragmentStorageData ;
681- for (uint32_t allocationId = 0 ; allocationId < wddmAllocation->fragmentsStorage .fragmentCount ; allocationId++) {
682- if (fragmentStorageData[allocationId].residency ->getFenceValueForContextId (0 ) <= monitoredFence.lastSubmittedFence ) {
683- fragmentEvictHandles[fragmentsToEvict++] = fragmentStorageData[allocationId].osHandleStorage ->handle ;
684- }
685- }
686-
687- if (fragmentsToEvict != 0 ) {
688- wddm->evict ((D3DKMT_HANDLE *)fragmentEvictHandles, fragmentsToEvict, sizeToTrim);
689-
690- for (uint32_t allocationId = 0 ; allocationId < wddmAllocation->fragmentsStorage .fragmentCount ; allocationId++) {
691- if (fragmentStorageData[allocationId].residency ->getFenceValueForContextId (0 ) <= monitoredFence.lastSubmittedFence ) {
692- fragmentStorageData[allocationId].residency ->resident = false ;
693- sizeEvicted += fragmentStorageData[allocationId].fragmentSize ;
694- }
695- }
696- }
697- }
698-
699- if (sizeEvicted >= numberOfBytesToTrim) {
700- numberOfBytesToTrim = 0 ;
701- } else {
702- numberOfBytesToTrim -= sizeEvicted;
703- }
704-
705- wddmAllocation->getResidencyData ().resident = false ;
706- osContext.get ()->getResidencyController ().removeFromTrimCandidateList (wddmAllocation, false );
707- trimToBudgetDone = (numberOfBytesToTrim == 0 );
708- } else {
709- trimToBudgetDone = true ;
710- }
711- }
712-
713- if (bytes > numberOfBytesToTrim && osContext.get ()->getResidencyController ().checkTrimCandidateListCompaction ()) {
714- osContext.get ()->getResidencyController ().compactTrimCandidateList ();
715- }
716-
717- return numberOfBytesToTrim == 0 ;
718- }
719-
720574bool WddmMemoryManager::mapAuxGpuVA (GraphicsAllocation *graphicsAllocation) {
721575 return wddm->updateAuxTable (graphicsAllocation->getGpuAddress (), graphicsAllocation->gmm , true );
722576}
0 commit comments