This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[Impeller] track the sizes of all outstanding MTLTexture allocations and report per frame in MB, matching Vulkan implementation. #53618
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a8579b
track in destructor.
90c68ef
++
eb73420
license
5bbf312
Update allocator_mtl.mm
0599098
++
491390a
Merge branch 'add_mem_bench' of github.com:jonahwilliams/engine into …
ebe3c03
Update allocator_mtl.mm
d3d1366
Update allocator_mtl.mm
1af8e93
Update allocator_mtl.mm
e3b4bcd
Update allocator_mtl.mm
51ed874
Update allocator_mtl.mm
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,17 +7,38 @@ | |
|
|
||
| #include <Metal/Metal.h> | ||
|
|
||
| #include "impeller/base/thread.h" | ||
| #include "impeller/core/allocator.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| class DebugAllocatorStats { | ||
| public: | ||
| DebugAllocatorStats() {} | ||
|
|
||
| ~DebugAllocatorStats() {} | ||
|
|
||
| void Increment(size_t size); | ||
|
|
||
| void Decrement(size_t size); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
|
||
| size_t GetAllocationSizeMB(); | ||
|
|
||
| private: | ||
| mutable Mutex mutex_ = {}; | ||
| size_t size_ IPLR_GUARDED_BY(mutex_) = 0; | ||
gaaclarke marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| class AllocatorMTL final : public Allocator { | ||
| public: | ||
| AllocatorMTL(); | ||
|
|
||
| // |Allocator| | ||
| ~AllocatorMTL() override; | ||
|
|
||
| // |Allocator| | ||
| size_t DebugGetHeapUsage() const override; | ||
|
|
||
| private: | ||
| friend class ContextMTL; | ||
|
|
||
|
|
@@ -26,6 +47,12 @@ class AllocatorMTL final : public Allocator { | |
| bool supports_memoryless_targets_ = false; | ||
| bool supports_uma_ = false; | ||
| bool is_valid_ = false; | ||
|
|
||
| #ifdef IMPELLER_DEBUG | ||
| std::shared_ptr<DebugAllocatorStats> debug_allocater_ = | ||
| std::make_shared<DebugAllocatorStats>(); | ||
| #endif // IMPELLER_DEBUG | ||
|
|
||
| ISize max_texture_supported_; | ||
|
|
||
| AllocatorMTL(id<MTLDevice> device, std::string label); | ||
|
|
@@ -47,6 +74,9 @@ class AllocatorMTL final : public Allocator { | |
| // |Allocator| | ||
| ISize GetMaxTextureSizeSupported() const override; | ||
|
|
||
| // |Allocator| | ||
| void DebugTraceMemoryStatistics() const override; | ||
|
|
||
| AllocatorMTL(const AllocatorMTL&) = delete; | ||
|
|
||
| AllocatorMTL& operator=(const AllocatorMTL&) = delete; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,9 @@ | |
|
|
||
| #include "flutter/fml/build_config.h" | ||
| #include "flutter/fml/logging.h" | ||
| #include "fml/trace_event.h" | ||
| #include "impeller/base/validation.h" | ||
| #include "impeller/core/formats.h" | ||
| #include "impeller/renderer/backend/metal/device_buffer_mtl.h" | ||
| #include "impeller/renderer/backend/metal/formats_mtl.h" | ||
| #include "impeller/renderer/backend/metal/texture_mtl.h" | ||
|
|
@@ -88,6 +90,27 @@ static bool SupportsLossyTextureCompression(id<MTLDevice> device) { | |
| #endif | ||
| } | ||
|
|
||
| void DebugAllocatorStats::Increment(size_t size) { | ||
| Lock lock(mutex_); | ||
| size_ += size; | ||
| } | ||
|
|
||
| void DebugAllocatorStats::Decrement(size_t size) { | ||
| Lock lock(mutex_); | ||
| if (size > size_) { | ||
| size_ = 0; | ||
| } else { | ||
| size_ -= size; | ||
| } | ||
| } | ||
|
|
||
| size_t DebugAllocatorStats::GetAllocationSizeMB() { | ||
| size_t current_size = 0; | ||
| Lock lock(mutex_); | ||
| current_size = size_ * 1e-6; | ||
| return current_size; | ||
| } | ||
|
|
||
| AllocatorMTL::AllocatorMTL(id<MTLDevice> device, std::string label) | ||
| : device_(device), allocator_label_(std::move(label)) { | ||
| if (!device_) { | ||
|
|
@@ -212,11 +235,23 @@ static MTLStorageMode ToMTLStorageMode(StorageMode mode, | |
| } | ||
| } | ||
|
|
||
| #ifdef IMPELLER_DEBUG | ||
| if (desc.storage_mode != StorageMode::kDeviceTransient) { | ||
| debug_allocater_->Increment(desc.GetByteSizeOfBaseMipLevel()); | ||
|
||
| } | ||
| #endif // IMPELLER_DEBUG | ||
|
|
||
| auto texture = [device_ newTextureWithDescriptor:mtl_texture_desc]; | ||
| if (!texture) { | ||
| return nullptr; | ||
| } | ||
| return TextureMTL::Create(desc, texture); | ||
| std::shared_ptr<TextureMTL> result_texture = | ||
| TextureMTL::Create(desc, texture); | ||
| #ifdef IMPELLER_DEBUG | ||
| result_texture->SetDebugAllocator(debug_allocater_); | ||
| #endif // IMPELLER_DEBUG | ||
|
|
||
| return result_texture; | ||
| } | ||
|
|
||
| uint16_t AllocatorMTL::MinimumBytesPerRow(PixelFormat format) const { | ||
|
|
@@ -228,4 +263,17 @@ static MTLStorageMode ToMTLStorageMode(StorageMode mode, | |
| return max_texture_supported_; | ||
| } | ||
|
|
||
| size_t AllocatorMTL::DebugGetHeapUsage() const { | ||
| return debug_allocater_->GetAllocationSizeMB(); | ||
| } | ||
|
|
||
| void AllocatorMTL::DebugTraceMemoryStatistics() const { | ||
| #ifdef IMPELLER_DEBUG | ||
| size_t allocated_size = DebugGetHeapUsage(); | ||
| FML_TRACE_COUNTER("flutter", "AllocatorVK", | ||
| reinterpret_cast<int64_t>(this), // Trace Counter ID | ||
| "MemoryBudgetUsageMB", allocated_size); | ||
| #endif // IMPELLER_DEBUG | ||
| } | ||
|
|
||
| } // namespace impeller | ||
62 changes: 62 additions & 0 deletions
62
impeller/renderer/backend/metal/allocator_mtl_unittests.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/testing/testing.h" | ||
| #include "impeller/core/formats.h" | ||
| #include "impeller/core/texture_descriptor.h" | ||
| #include "impeller/playground/playground_test.h" | ||
| #include "impeller/renderer/backend/metal/allocator_mtl.h" | ||
| #include "impeller/renderer/backend/metal/context_mtl.h" | ||
| #include "impeller/renderer/backend/metal/formats_mtl.h" | ||
| #include "impeller/renderer/backend/metal/lazy_drawable_holder.h" | ||
| #include "impeller/renderer/backend/metal/texture_mtl.h" | ||
| #include "impeller/renderer/capabilities.h" | ||
|
|
||
| #include <QuartzCore/CAMetalLayer.h> | ||
| #include <memory> | ||
| #include <thread> | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace impeller { | ||
| namespace testing { | ||
|
|
||
| using AllocatorMTLTest = PlaygroundTest; | ||
| INSTANTIATE_METAL_PLAYGROUND_SUITE(AllocatorMTLTest); | ||
|
|
||
| TEST_P(AllocatorMTLTest, DebugTraceMemoryStatistics) { | ||
| auto& context_mtl = ContextMTL::Cast(*GetContext()); | ||
| const auto& allocator = context_mtl.GetResourceAllocator(); | ||
|
|
||
| EXPECT_EQ(allocator->DebugGetHeapUsage(), 0u); | ||
|
|
||
| // Memoryless texture does not increase allocated size. | ||
| { | ||
| TextureDescriptor desc; | ||
| desc.format = PixelFormat::kR8G8B8A8UNormInt; | ||
| desc.storage_mode = StorageMode::kDeviceTransient; | ||
| desc.size = {1000, 1000}; | ||
| auto texture_1 = allocator->CreateTexture(desc); | ||
|
|
||
| EXPECT_EQ(allocator->DebugGetHeapUsage(), 0u); | ||
|
|
||
| // Private storage texture increases allocated size. | ||
| desc.storage_mode = StorageMode::kDevicePrivate; | ||
| auto texture_2 = allocator->CreateTexture(desc); | ||
|
|
||
| EXPECT_EQ(allocator->DebugGetHeapUsage(), 4u); | ||
|
|
||
| // Host storage texture increases allocated size. | ||
| desc.storage_mode = StorageMode::kHostVisible; | ||
| auto texture_3 = allocator->CreateTexture(desc); | ||
|
|
||
| EXPECT_EQ(allocator->DebugGetHeapUsage(), 8u); | ||
| } | ||
|
|
||
| // After all textures are out of scope, memory has been decremented. | ||
| EXPECT_EQ(allocator->DebugGetHeapUsage(), 0u); | ||
| } | ||
|
|
||
| } // namespace testing | ||
| } // namespace impeller |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: note the units of
sizein a docstring.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done