From 3011cbd179124b83bdcdf96533542fcc6395428f Mon Sep 17 00:00:00 2001 From: "Bernhart, Bryan" Date: Tue, 10 Jan 2023 17:36:53 -0800 Subject: [PATCH] Make ResidencyManager::GetStats thread safe. --- src/gpgmm/d3d12/ResidencyManagerD3D12.cpp | 22 ++++++++++++++++------ src/gpgmm/d3d12/ResidencyManagerD3D12.h | 4 +++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp b/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp index b28879714..e0d0b8526 100644 --- a/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp +++ b/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp @@ -829,29 +829,39 @@ namespace gpgmm::d3d12 { } RESIDENCY_STATS ResidencyManager::GetStats() const { + std::lock_guard lock(mMutex); + return GetStatsInternal(); + } + + RESIDENCY_STATS ResidencyManager::GetStatsInternal() const { + TRACE_EVENT0(TraceEventCategory::kDefault, "ResidencyManager.GetStats"); + // Heaps inserted into the residency cache are not resident until MakeResident() is called // on them. This occurs if the heap was created resident, heap gets locked, or call to // ExecuteCommandLists(). // Locked heaps are not stored in the residency cache, so usage must be tracked by the // residency manager on Lock/Unlock then added here to get the sum. - RESIDENCY_STATS info = mStats; + RESIDENCY_STATS result = mStats; for (const auto& entry : mLocalVideoMemorySegment.cache) { if (entry.value()->GetInfo().Status == RESIDENCY_STATUS_CURRENT_RESIDENT) { - info.CurrentMemoryUsage += entry.value()->GetSize(); - info.CurrentMemoryCount++; + result.CurrentMemoryUsage += entry.value()->GetSize(); + result.CurrentMemoryCount++; } } for (const auto& entry : mNonLocalVideoMemorySegment.cache) { if (entry.value()->GetInfo().Status == RESIDENCY_STATUS_CURRENT_RESIDENT) { - info.CurrentMemoryUsage += entry.value()->GetSize(); - info.CurrentMemoryCount++; + result.CurrentMemoryUsage += entry.value()->GetSize(); + result.CurrentMemoryCount++; } } - return info; + GPGMM_TRACE_EVENT_METRIC("GPU currently resident (MB)", + GPGMM_BYTES_TO_MB(result.CurrentMemoryUsage)); + + return result; } // Starts updating video memory budget from OS notifications. diff --git a/src/gpgmm/d3d12/ResidencyManagerD3D12.h b/src/gpgmm/d3d12/ResidencyManagerD3D12.h index 832293ff3..8969f6b84 100644 --- a/src/gpgmm/d3d12/ResidencyManagerD3D12.h +++ b/src/gpgmm/d3d12/ResidencyManagerD3D12.h @@ -83,6 +83,8 @@ namespace gpgmm::d3d12 { HRESULT InsertHeapInternal(Heap* heap); + RESIDENCY_STATS GetStatsInternal() const; + friend BudgetUpdateTask; HRESULT UpdateMemorySegments(); @@ -128,7 +130,7 @@ namespace gpgmm::d3d12 { const bool mIsBudgetChangeEventsDisabled; const bool mFlushEventBuffersOnDestruct; - std::mutex mMutex; + mutable std::mutex mMutex; std::unique_ptr mResidencyFence;