Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/gpgmm/common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ namespace gpgmm {
return output.str();
}

// Used to combine multiple ToString calls for concatenation.
// Eg. ToString("a", "b", "c") == "abc".
template <typename T, typename... Args>
std::string ToString(T object, Args... args) {
return ToString(object) + ToString(args...);
}

} // namespace gpgmm

#endif // GPGMM_COMMON_UTILS_H_
50 changes: 35 additions & 15 deletions src/gpgmm/d3d12/ResidencyManagerD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,36 +251,56 @@ namespace gpgmm { namespace d3d12 {

HRESULT ResidencyManager::QueryVideoMemoryInfo(
const DXGI_MEMORY_SEGMENT_GROUP& memorySegmentGroup,
DXGI_QUERY_VIDEO_MEMORY_INFO* videoMemoryInfo) const {
DXGI_QUERY_VIDEO_MEMORY_INFO queryVideoMemoryInfo;

DXGI_QUERY_VIDEO_MEMORY_INFO* pVideoMemoryInfo) const {
DXGI_QUERY_VIDEO_MEMORY_INFO queryVideoMemoryInfoOut;
ReturnIfFailed(
mAdapter->QueryVideoMemoryInfo(0, memorySegmentGroup, &queryVideoMemoryInfo));
mAdapter->QueryVideoMemoryInfo(0, memorySegmentGroup, &queryVideoMemoryInfoOut));

// The video memory budget provided by QueryVideoMemoryInfo is defined by the operating
// system, and may be lower than expected in certain scenarios. Under memory pressure, we
// cap the external reservation to half the available budget, which prevents the external
// component from consuming a disproportionate share of memory and ensures that Dawn can
// continue to make forward progress. Note the choice to halve memory is arbitrarily chosen
// and subject to future experimentation.
videoMemoryInfo->CurrentReservation =
std::min(queryVideoMemoryInfo.Budget / 2, videoMemoryInfo->AvailableForReservation);
pVideoMemoryInfo->CurrentReservation =
std::min(queryVideoMemoryInfoOut.Budget / 2, pVideoMemoryInfo->AvailableForReservation);

videoMemoryInfo->CurrentUsage =
queryVideoMemoryInfo.CurrentUsage - videoMemoryInfo->CurrentReservation;
pVideoMemoryInfo->CurrentUsage =
queryVideoMemoryInfoOut.CurrentUsage - pVideoMemoryInfo->CurrentReservation;

// If we're restricting the budget, leave the budget as is.
if (mBudget == 0) {
videoMemoryInfo->Budget = static_cast<uint64_t>(
(queryVideoMemoryInfo.Budget - videoMemoryInfo->CurrentReservation) *
pVideoMemoryInfo->Budget = static_cast<uint64_t>(
(queryVideoMemoryInfoOut.Budget - pVideoMemoryInfo->CurrentReservation) *
mVideoMemoryBudget);
}

TRACE_COUNTER1(TraceEventCategory::Default, "GPU memory budget (MB)",
videoMemoryInfo->Budget / 1e6);

TRACE_COUNTER1(TraceEventCategory::Default, "GPU memory usage (MB)",
videoMemoryInfo->CurrentUsage / 1e6);
TRACE_COUNTER1(
TraceEventCategory::Default,
ToString(
"GPU memory (",
(memorySegmentGroup == DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL) ? "NonLocal" : "Local",
") budget (MB)")
.c_str(),
pVideoMemoryInfo->Budget / 1e6);

TRACE_COUNTER1(
TraceEventCategory::Default,
ToString(
"GPU memory (",
(memorySegmentGroup == DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL) ? "NonLocal" : "Local",
") usage (MB)")
.c_str(),
pVideoMemoryInfo->CurrentUsage / 1e6);

TRACE_COUNTER1(
TraceEventCategory::Default,
ToString(
"GPU memory (",
(memorySegmentGroup == DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL) ? "NonLocal" : "Local",
") reserved (MB)")
.c_str(),
pVideoMemoryInfo->CurrentReservation / 1e6);

return S_OK;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/ResidencyManagerD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace gpgmm { namespace d3d12 {
LRUCache* GetVideoMemorySegmentCache(const DXGI_MEMORY_SEGMENT_GROUP& memorySegmentGroup);

HRESULT QueryVideoMemoryInfo(const DXGI_MEMORY_SEGMENT_GROUP& memorySegmentGroup,
DXGI_QUERY_VIDEO_MEMORY_INFO* videoMemoryInfo) const;
DXGI_QUERY_VIDEO_MEMORY_INFO* pVideoMemoryInfo) const;

ComPtr<ID3D12Device> mDevice;
ComPtr<IDXGIAdapter3> mAdapter;
Expand Down
1 change: 1 addition & 0 deletions src/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ test("gpgmm_unittests") {
"unittests/SegmentedMemoryAllocatorTests.cpp",
"unittests/SlabBlockAllocatorTests.cpp",
"unittests/SlabMemoryAllocatorTests.cpp",
"unittests/UtilsTest.cpp",
]

# When building inside Chromium, use their gtest main function because it is
Expand Down
23 changes: 23 additions & 0 deletions src/tests/unittests/UtilsTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2021 The GPGMM Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

#include "gpgmm/common/Utils.h"

using namespace gpgmm;

TEST(UtilsTest, ConcatString) {
EXPECT_TRUE(ToString("This ", "is ", "a ", "sentance") == std::string("This is a sentance"));
}