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
1 change: 1 addition & 0 deletions src/gpgmm/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ source_set("gpgmm_common_sources") {
"ConditionalMemoryAllocator.h",
"DedicatedMemoryAllocator.cpp",
"DedicatedMemoryAllocator.h",
"Error.cpp",
"Error.h",
"EventMessage.cpp",
"EventMessage.h",
Expand Down
1 change: 1 addition & 0 deletions src/gpgmm/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ target_sources(gpgmm_common PRIVATE
"ConditionalMemoryAllocator.h"
"DedicatedMemoryAllocator.cpp"
"DedicatedMemoryAllocator.h"
"Error.cpp"
"Error.h"
"EventTraceWriter.cpp"
"EventTraceWriter.h"
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/common/DedicatedMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace gpgmm {
GPGMM_TRY_ASSIGN(GetNextInChain()->TryAllocateMemory(memoryRequest), allocation);

if (memoryRequest.SizeInBytes > request.SizeInBytes) {
DebugLog(MessageId::kAlignmentMismatch, this)
DebugLog(MessageId::kPerformanceWarning, this)
<< "Memory allocation was larger then the requested: "
<< GetBytesToSizeInUnits(memoryRequest.SizeInBytes) << " vs "
<< GetBytesToSizeInUnits(request.SizeInBytes) << ".";
Expand Down
47 changes: 47 additions & 0 deletions src/gpgmm/common/Error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 "Error.h"

#include "gpgmm/common/Error.h"

namespace gpgmm {

const char* GetErrorFromID(ErrorCode errorCode) {
switch (errorCode) {
case ErrorCode::kNone:
return "";
case ErrorCode::kUnknown:
return "UNKNOWN";
case ErrorCode::kSizeExceeded:
return "SIZE_EXCEEDED";
case ErrorCode::kAlignmentMismatch:
return "ALIGNMENT_MISMATCH";
case ErrorCode::kAllocatorFailed:
return "ALLOCATOR_FAILED";
case ErrorCode::kPrefetchFailed:
return "PREFETCH_FAILED";
case ErrorCode::kBudgetInvalid:
return "BUDGET_INVALID";
case ErrorCode::kInvalidArgument:
return "INVALID_ARGUMENT";
case ErrorCode::kBadOperation:
return "BAD_OPERATION";
default:
UNREACHABLE();
return "";
}
}

} // namespace gpgmm
36 changes: 24 additions & 12 deletions src/gpgmm/common/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,32 @@
for (;;) \
break

#define GPGMM_RETURN_ERROR_IF(expr, msg) \
if (GPGMM_UNLIKELY(expr)) { \
gpgmm::DebugLog() << msg; \
return std::move(::gpgmm::ErrorCodeType(kInternalFailureResult)); \
} \
for (;;) \
#define GPGMM_RETURN_ERROR_IF(expr, msg) \
if (GPGMM_UNLIKELY(expr)) { \
gpgmm::DebugLog() << msg; \
return std::move(::gpgmm::ErrorCode(kInternalFailureResult)); \
} \
for (;;) \
break

namespace gpgmm {

enum class ErrorCodeType : uint32_t;
enum class ErrorCode : uint32_t {
kNone,
kUnknown,
kSizeExceeded,
kAlignmentMismatch,
kAllocatorFailed,
kPrefetchFailed,
kBudgetInvalid,
kInvalidArgument,
kBadOperation,
};

const char* GetErrorFromID(ErrorCode errorCode);

constexpr ErrorCodeType kInternalFailureResult = static_cast<ErrorCodeType>(-1);
constexpr ErrorCodeType kInternalSuccessResult = static_cast<ErrorCodeType>(0u);
constexpr ErrorCode kInternalFailureResult = static_cast<ErrorCode>(-1);
constexpr ErrorCode kInternalSuccessResult = static_cast<ErrorCode>(0u);

// Wraps a backend error code with a result object.
// Use Result::IsSuccess then Result::AcquireResult to use or else, use Result::GetErrorCode to
Expand Down Expand Up @@ -90,7 +102,7 @@ namespace gpgmm {
return *this;
}

ErrorCodeType GetErrorCode() const {
ErrorCode GetErrorCode() const {
return mErrorCode;
}

Expand Down Expand Up @@ -143,11 +155,11 @@ namespace gpgmm {
};

// Result with only an error code.
using MaybeError = Result<ErrorCodeType, void>;
using MaybeError = Result<ErrorCode, void>;

// Alias of Result + error code to avoid having to always specify error type.
template <typename ResultT>
using ResultOrError = Result<ErrorCodeType, ResultT>;
using ResultOrError = Result<ErrorCode, ResultT>;

} // namespace gpgmm

Expand Down
16 changes: 9 additions & 7 deletions src/gpgmm/common/EventMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@ namespace gpgmm {

EventMessage::EventMessage(const MessageSeverity& severity,
MessageId messageId,
ErrorCode errorCode,
bool isExternal,
const std::string& name,
const ObjectBase* object)
: mSeverity(severity),
mMessageId(messageId),
mIsExternal(isExternal),
mName(name),
mObject(object) {
mObject(object),
mErrorCode(errorCode) {
}

EventMessage::~EventMessage() {
const std::string description = mStream.str();

gpgmm::Log(mSeverity, mMessageId, mIsExternal, mName, mObject) << description;
gpgmm::Log(mSeverity, mMessageId, mErrorCode, mIsExternal, mName, mObject) << description;

#if defined(GPGMM_ENABLE_ASSERT_ON_WARNING)
ASSERT(mSeverity < MessageSeverity::kWarning);
Expand All @@ -77,28 +79,28 @@ namespace gpgmm {
bool isExternal,
const std::string& name,
const ObjectBase* object) {
return {MessageSeverity::kDebug, messageId, isExternal, name, object};
return {MessageSeverity::kDebug, messageId, ErrorCode::kNone, isExternal, name, object};
}

EventMessage InfoEvent(MessageId messageId,
bool isExternal,
const std::string& name,
const ObjectBase* object) {
return {MessageSeverity::kInfo, messageId, isExternal, name, object};
return {MessageSeverity::kInfo, messageId, ErrorCode::kNone, isExternal, name, object};
}

EventMessage WarnEvent(MessageId messageId,
bool isExternal,
const std::string& name,
const ObjectBase* object) {
return {MessageSeverity::kWarning, messageId, isExternal, name, object};
return {MessageSeverity::kWarning, messageId, ErrorCode::kNone, isExternal, name, object};
}

EventMessage ErrorEvent(MessageId messageId,
EventMessage ErrorEvent(ErrorCode errorCode,
bool isExternal,
const std::string& name,
const ObjectBase* object) {
return {MessageSeverity::kError, messageId, isExternal, name, object};
return {MessageSeverity::kError, MessageId::kUnknown, errorCode, isExternal, name, object};
}

} // namespace gpgmm
5 changes: 4 additions & 1 deletion src/gpgmm/common/EventMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef SRC_GPGMM_COMMON_EVENTMESSAGE_H_
#define SRC_GPGMM_COMMON_EVENTMESSAGE_H_

#include "gpgmm/common/Error.h"
#include "gpgmm/common/Message.h"
#include "gpgmm/common/Object.h"
#include "gpgmm/common/TraceEvent.h"
Expand All @@ -29,6 +30,7 @@ namespace gpgmm {
public:
EventMessage(const MessageSeverity& severity,
MessageId messageId,
ErrorCode errorCode,
bool isExternal,
const std::string& name,
const ObjectBase* object);
Expand All @@ -50,6 +52,7 @@ namespace gpgmm {
std::string mName;
const ObjectBase* mObject = nullptr;
std::ostringstream mStream;
ErrorCode mErrorCode = ErrorCode::kNone;
};

// Short-hands to create a EventMessage with the respective severity.
Expand All @@ -68,7 +71,7 @@ namespace gpgmm {
const std::string& name = "",
const ObjectBase* mObject = nullptr);

EventMessage ErrorEvent(MessageId messageId = MessageId::kUnknown,
EventMessage ErrorEvent(ErrorCode errorCode = ErrorCode::kUnknown,
bool isExternal = false,
const std::string& name = "",
const ObjectBase* mObject = nullptr);
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/common/MemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ namespace gpgmm {
void MemoryAllocatorBase::CheckAndReportAllocationMisalignment(
const MemoryAllocationBase& allocation) {
if (allocation.GetSize() > allocation.GetRequestSize()) {
WarnLog(MessageId::kAlignmentMismatch, this)
WarnLog(MessageId::kPerformanceWarning, this)
<< "Memory allocation was larger then requested: " +
GetBytesToSizeInUnits(allocation.GetSize()) + " vs " +
GetBytesToSizeInUnits(allocation.GetRequestSize()) + ".";
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/common/MemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ namespace gpgmm {
if (!result.IsSuccess()) {
// NeverAllocate always fails, so suppress it.
if (!neverAllocate) {
DebugLog(MessageId::kAllocatorFailed, this)
ErrorLog(ErrorCode::kAllocatorFailed, this)
<< "Failed to sub-allocate memory range = ["
<< std::to_string(block->Offset) << ", "
<< std::to_string(block->Offset + block->Size) << ").";
Expand Down
18 changes: 2 additions & 16 deletions src/gpgmm/common/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,16 @@ namespace gpgmm {
switch (messageId) {
case MessageId::kUnknown:
return "UNKNOWN";
case MessageId::kSizeExceeded:
return "SIZE_EXCEEDED";
case MessageId::kAlignmentMismatch:
return "ALIGNMENT_MISMATCH";
case MessageId::kAllocatorFailed:
return "ALLOCATOR_FAILED";
case MessageId::kPrefetchFailed:
return "PREFETCH_FAILED";
case MessageId::kBudgetExceeded:
return "BUDGET_EXCEEDED";
case MessageId::kBudgetUpdated:
return "BUDGET_UPDATED";
case MessageId::kBudgetInvalid:
return "BUDGET_INVALID";
case MessageId::kInvalidArgument:
return "INVALID_ARGUMENT";
case MessageId::kBadOperation:
return "BAD_OPERATION";
case MessageId::kPerformanceWarning:
return "PERFORMANCE_WARNING";
case MessageId::kMemoryUsageUpdated:
return "MEMORY_USAGE_UPDATED";
case MessageId::kObjectCreated:
return "OBJECT_CREATED";
case MessageId::kBudgetExceeded:
return "BUDGET_EXCEEDED";
default:
UNREACHABLE();
return "";
Expand Down
9 changes: 1 addition & 8 deletions src/gpgmm/common/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@ namespace gpgmm {

enum class MessageId {
kUnknown,
kSizeExceeded,
kAlignmentMismatch,
kAllocatorFailed,
kPrefetchFailed,
kBudgetExceeded,
kBudgetUpdated,
kBudgetInvalid,
kInvalidArgument,
kBadOperation,
kPerformanceWarning,
kMemoryUsageUpdated,
kObjectCreated,
kBudgetExceeded,
};

enum class MessageSeverity {
Expand Down
8 changes: 4 additions & 4 deletions src/gpgmm/common/SlabMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace gpgmm {
if (availableForAllocation < slabSize) {
const uint64_t slabSizeUnderBudget = FindNextFreeSlabOfSize(requestSize);
if (slabSizeUnderBudget == kInvalidSize) {
DebugLog(MessageId::kSizeExceeded, this)
DebugLog(MessageId::kPerformanceWarning, this)
<< "Slab size exceeded available memory: " << GetBytesToSizeInUnits(slabSize)
<< " vs " << GetBytesToSizeInUnits(availableForAllocation) << ".";
return kInvalidSize;
Expand Down Expand Up @@ -211,7 +211,7 @@ namespace gpgmm {

// Slab cannot exceed memory size.
if (slabSize > mMaxSlabSize) {
DebugLog(MessageId::kSizeExceeded, this)
DebugLog(MessageId::kPerformanceWarning, this)
<< "Slab allocation was disabled because the slab size exceeded the max slab size "
"allowed: "
<< GPGMM_BYTES_TO_MB(slabSize) << " vs " << GPGMM_BYTES_TO_MB(mMaxSlabSize)
Expand Down Expand Up @@ -298,7 +298,7 @@ namespace gpgmm {
}

if (prefetchedSlabAllocation != nullptr) {
DebugLog(MessageId::kPrefetchFailed, this)
DebugLog(MessageId::kPerformanceWarning, this)
<< "Pre-fetching failed because the slab size did not match: "
<< GetBytesToSizeInUnits(slabSize) << " vs "
<< GetBytesToSizeInUnits(prefetchedSlabAllocation->GetSize())
Expand Down Expand Up @@ -477,7 +477,7 @@ namespace gpgmm {
SafeDivide(mStats.PrefetchedMemoryMissesEliminated,
mStats.PrefetchedMemoryMissesEliminated + mStats.PrefetchedMemoryMisses);
if (currentCoverage < kPrefetchCoverageWarnMinThreshold) {
WarnEvent(MessageId::kPrefetchFailed, false, GetTypename(), this)
WarnEvent(MessageId::kPerformanceWarning, false, GetTypename(), this)
<< "Prefetch coverage is below threshold (%): " << currentCoverage * 100 << " vs "
<< kPrefetchCoverageWarnMinThreshold * 100;
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/d3d12/BudgetUpdateD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace gpgmm::d3d12 {
}

if (FAILED(hr)) {
ErrorLog(MessageId::kBudgetInvalid, mResidencyManager)
ErrorLog(ErrorCode::kBudgetInvalid, mResidencyManager)
<< "Unable to update budget: " +
GetDeviceErrorMessage(mResidencyManager->mDevice, hr);
}
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/d3d12/BufferAllocatorD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ namespace gpgmm::d3d12 {
&resourceHeap);

if (FAILED(hr)) {
return {static_cast<ErrorCodeType>(hr)};
return {static_cast<ErrorCode>(hr)};
}

if (info.SizeInBytes > request.SizeInBytes) {
DebugLog(MessageId::kAlignmentMismatch, false, GetTypename(), this)
DebugLog(MessageId::kPerformanceWarning, this)
<< "Memory allocation was larger then the requested: "
<< GetBytesToSizeInUnits(info.SizeInBytes) << " vs "
<< GetBytesToSizeInUnits(request.SizeInBytes) << ".";
Expand Down
13 changes: 9 additions & 4 deletions src/gpgmm/d3d12/EventMessageD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,35 @@
#define SRC_GPGMM_D3D12_EVENTMESSAGED3D12_H_

#include "gpgmm/common/EventMessage.h"
#include "gpgmm/utils/Assert.h"
#include "gpgmm/utils/WindowsUtils.h"

namespace gpgmm::d3d12 {

template <typename BackendT>
EventMessage DebugEvent(const BackendT* object, MessageId messageId = MessageId::kUnknown) {
EventMessage DebugEvent(MessageId messageId, const BackendT* object) {
ASSERT(object != nullptr);
return gpgmm::DebugEvent(messageId, true, gpgmm::WCharToUTF8(object->GetDebugName()),
object);
}

template <typename BackendT>
EventMessage InfoEvent(const BackendT* object, MessageId messageId = MessageId::kUnknown) {
EventMessage InfoEvent(MessageId messageId, const BackendT* object) {
ASSERT(object != nullptr);
return gpgmm::InfoEvent(messageId, true, gpgmm::WCharToUTF8(object->GetDebugName()),
object);
}

template <typename BackendT>
EventMessage WarnEvent(const BackendT* object, MessageId messageId = MessageId::kUnknown) {
EventMessage WarnEvent(MessageId messageId, const BackendT* object) {
ASSERT(object != nullptr);
return gpgmm::WarnEvent(messageId, true, gpgmm::WCharToUTF8(object->GetDebugName()),
object);
}

template <typename BackendT>
EventMessage ErrorEvent(const BackendT* object, MessageId messageId = MessageId::kUnknown) {
EventMessage ErrorEvent(MessageId messageId, const BackendT* object) {
ASSERT(object != nullptr);
return gpgmm::ErrorEvent(messageId, true, gpgmm::WCharToUTF8(object->GetDebugName()),
object);
}
Expand Down
Loading