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
2 changes: 2 additions & 0 deletions src/gpgmm/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ source_set("gpgmm_common_sources") {
"SegmentedMemoryAllocator.h",
"SentinelMemoryAllocator.cpp",
"SentinelMemoryAllocator.h",
"SizeClass.cpp",
"SizeClass.h",
"SlabBlockAllocator.cpp",
"SlabBlockAllocator.h",
"SlabMemoryAllocator.cpp",
Expand Down
2 changes: 2 additions & 0 deletions src/gpgmm/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ target_sources(gpgmm_common PRIVATE
"SegmentedMemoryAllocator.h"
"SentinelMemoryAllocator.cpp"
"SentinelMemoryAllocator.h"
"SizeClass.cpp"
"SizeClass.h"
"SlabBlockAllocator.cpp"
"SlabBlockAllocator.h"
"SlabMemoryAllocator.cpp"
Expand Down
42 changes: 42 additions & 0 deletions src/gpgmm/common/SizeClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 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 "gpgmm/common/SizeClass.h"

#include "gpgmm/utils/Assert.h"
#include "gpgmm/utils/Limits.h"

#include <iomanip>
#include <sstream>

namespace gpgmm {

std::string GetBytesToSizeInUnits(uint64_t bytes) {
// UINT_MAX is given special "invalid" size.
if (bytes == kInvalidSize) {
return "INVALID_SIZE";
}

const char* unitsArray[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
size_t unitIndex = 0;
while (bytes >= 1024 && unitIndex < sizeof(unitsArray) / sizeof(unitsArray[0]) - 1) {
bytes /= 1024;
unitIndex++;
}
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << bytes << " " << unitsArray[unitIndex];
return oss.str();
}

} // namespace gpgmm
5 changes: 5 additions & 0 deletions src/gpgmm/common/SizeClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define SRC_GPGMM_COMMON_SIZECLASS_H_

#include <array>
#include <string>

// Convert sizes, in bytes, to/from SI prefix.
#define GPGMM_KB_TO_BYTES(bytes) ((bytes)*1024ull)
Expand All @@ -33,6 +34,10 @@ namespace gpgmm {
uint64_t Alignment;
};

// Returns the printable string of |bytes| in storage units (eg. 2*1024 =>
// 2 MB).
std::string GetBytesToSizeInUnits(uint64_t bytes);

// Generates array containing values in range [2^N1...2^N2], where N2 > N1.
template <size_t N1, size_t N2>
static constexpr std::array<SizeClassInfo, N2 - N1 + 1> GeneratePowerOfTwoSizes() {
Expand Down
12 changes: 6 additions & 6 deletions src/gpgmm/d3d12/ResidencyManagerD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ namespace gpgmm::d3d12 {

DebugEvent(this, MessageId::kBudgetExceeded)
<< "GPU page-out. Number of allocations: " << objectsToEvict.size() << " ("
<< bytesEvicted << " bytes).";
<< GetBytesToSizeInUnits(bytesEvicted) << ").";
}

if (bytesEvictedOut != nullptr) {
Expand Down Expand Up @@ -746,7 +746,7 @@ namespace gpgmm::d3d12 {

DebugEvent(this, MessageId::kBudgetExceeded)
<< "GPU page-in. Number of allocations: " << numberOfObjectsToMakeResident << " ("
<< sizeToMakeResident << " bytes).";
<< GetBytesToSizeInUnits(sizeToMakeResident) << ").";

// Decrease the overhead from using MakeResident, a synchronous call, by calling the
// asynchronous MakeResident, called EnqueueMakeResident, instead first. Should
Expand Down Expand Up @@ -869,13 +869,13 @@ namespace gpgmm::d3d12 {
DebugLog(MessageId::kBudgetUpdated, this)
<< GetMemorySegmentName(segmentGroup, IsUMA()) << " GPU memory segment:";
DebugLog(MessageId::kBudgetUpdated, this)
<< "\tBudget: " << GPGMM_BYTES_TO_MB(info->Budget) << " MBs ("
<< GPGMM_BYTES_TO_MB(info->CurrentUsage) << " used).";
<< "\tBudget: " << GetBytesToSizeInUnits(info->Budget) << " ("
<< GetBytesToSizeInUnits(info->CurrentUsage) << " used).";

if (info->CurrentReservation == 0) {
DebugLog(MessageId::kBudgetUpdated, this)
<< "\tReserved: " << GPGMM_BYTES_TO_MB(info->CurrentReservation) << " MBs ("
<< GPGMM_BYTES_TO_MB(info->AvailableForReservation) << " available).";
<< "\tReserved: " << GetBytesToSizeInUnits(info->CurrentReservation) << " ("
<< GetBytesToSizeInUnits(info->AvailableForReservation) << " available).";
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,8 +1018,8 @@ namespace gpgmm::d3d12 {
ErrorLog(MessageId::kSizeExceeded, this)
<< "Unable to create resource allocation because the resource size exceeded "
"the capabilities of the device: "
<< GPGMM_BYTES_TO_GB(resourceInfo.SizeInBytes) << " vs "
<< GPGMM_BYTES_TO_GB(mMaxResourceHeapSize) << " GBs.";
<< GetBytesToSizeInUnits(resourceInfo.SizeInBytes) << " vs "
<< GetBytesToSizeInUnits(mMaxResourceHeapSize);
return E_OUTOFMEMORY;
}

Expand Down Expand Up @@ -1141,8 +1141,8 @@ namespace gpgmm::d3d12 {
ErrorLog(MessageId::kSizeExceeded, this)
<< "Unable to create resource allocation because the resource size exceeded "
"the capabilities of the adapter: "
<< GPGMM_BYTES_TO_GB(request.SizeInBytes) << " vs "
<< GPGMM_BYTES_TO_GB(maxSegmentSize) << " GBs.";
<< GetBytesToSizeInUnits(request.SizeInBytes) << " vs "
<< GetBytesToSizeInUnits(maxSegmentSize);
return E_OUTOFMEMORY;
}

Expand All @@ -1164,9 +1164,9 @@ namespace gpgmm::d3d12 {

DebugLog(MessageId::kBudgetExceeded, this)
<< "Current usage exceeded budget: "
<< GPGMM_BYTES_TO_MB(currentVideoInfo->CurrentUsage) << " vs "
<< GPGMM_BYTES_TO_MB(currentVideoInfo->Budget) << " MBs ("
<< GPGMM_BYTES_TO_MB(request.AvailableForAllocation) << " MBs free).";
<< GetBytesToSizeInUnits(currentVideoInfo->CurrentUsage) << " vs "
<< GetBytesToSizeInUnits(currentVideoInfo->Budget) << " ("
<< GetBytesToSizeInUnits(request.AvailableForAllocation) << " free).";

} else {
// Otherwise, only memory in budget is considered available.
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/PooledMemoryAllocatorTests.cpp",
"unittests/RefCountTests.cpp",
"unittests/SegmentedMemoryAllocatorTests.cpp",
"unittests/SizeClassTests.cpp",
"unittests/SlabBlockAllocatorTests.cpp",
"unittests/SlabMemoryAllocatorTests.cpp",
"unittests/StableListTests.cpp",
Expand Down
1 change: 1 addition & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ target_sources(gpgmm_unittests PRIVATE
"unittests/PooledMemoryAllocatorTests.cpp"
"unittests/RefCountTests.cpp"
"unittests/SegmentedMemoryAllocatorTests.cpp"
"unittests/SizeClassTests.cpp"
"unittests/SlabBlockAllocatorTests.cpp"
"unittests/SlabMemoryAllocatorTests.cpp"
"unittests/StableListTests.cpp"
Expand Down
38 changes: 38 additions & 0 deletions src/tests/unittests/SizeClassTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2022 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/SizeClass.h"
#include "gpgmm/utils/Limits.h"

using namespace gpgmm;

TEST(SizeClassTests, SizeToUnits) {
EXPECT_EQ(GetBytesToSizeInUnits(1), "1 B");
EXPECT_EQ(GetBytesToSizeInUnits(GPGMM_KB_TO_BYTES(1)), "1 KB");
EXPECT_EQ(GetBytesToSizeInUnits(GPGMM_MB_TO_BYTES(1)), "1 MB");
EXPECT_EQ(GetBytesToSizeInUnits(GPGMM_GB_TO_BYTES(1)), "1 GB");
EXPECT_EQ(GetBytesToSizeInUnits(kInvalidSize), "INVALID_SIZE");

EXPECT_EQ(GetBytesToSizeInUnits(1024), "1 KB");
EXPECT_EQ(GetBytesToSizeInUnits(GPGMM_KB_TO_BYTES(1024)), "1 MB");
EXPECT_EQ(GetBytesToSizeInUnits(GPGMM_MB_TO_BYTES(1024)), "1 GB");

EXPECT_EQ(GetBytesToSizeInUnits(GPGMM_MB_TO_BYTES(2)), "2 MB");
EXPECT_EQ(GetBytesToSizeInUnits(GPGMM_MB_TO_BYTES(20)), "20 MB");
EXPECT_EQ(GetBytesToSizeInUnits(GPGMM_MB_TO_BYTES(200)), "200 MB");
EXPECT_EQ(GetBytesToSizeInUnits(GPGMM_MB_TO_BYTES(2000)), "1 GB"); // Must be 2048 for 2GB
EXPECT_EQ(GetBytesToSizeInUnits(GPGMM_MB_TO_BYTES(2048)), "2 GB");
}