From 6fa344176b37a12395bc5781782c455d3a367f84 Mon Sep 17 00:00:00 2001 From: Ulan Degenbaev Date: Sun, 9 May 2021 03:17:26 +0000 Subject: [PATCH] [cppgc] Enable HeapTest.CollectNodeAndCssStatistics for the library This implements the ThreadState::CollectNodeAndCssStatistics wrapper using a new CppHeap API. Bug: 1181269 Change-Id: I103384c34f3bd1ef0760385649061d41cdc95292 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2879989 Commit-Queue: Ulan Degenbaev Reviewed-by: Omer Katz Reviewed-by: Michael Lippautz Cr-Commit-Position: refs/heads/master@{#880760} --- .../renderer/platform/heap/test/heap_test.cc | 3 -- .../platform/heap/v8_wrapper/thread_state.cc | 42 ++++++++++++++++++- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/third_party/blink/renderer/platform/heap/test/heap_test.cc b/third_party/blink/renderer/platform/heap/test/heap_test.cc index f2dfa36064ffbc..715cafe2a4da54 100644 --- a/third_party/blink/renderer/platform/heap/test/heap_test.cc +++ b/third_party/blink/renderer/platform/heap/test/heap_test.cc @@ -3248,8 +3248,6 @@ struct SpaceTrait { namespace blink { #endif -// TODO(1181269): Enable for the library once implemented. -#if !BUILDFLAG(USE_V8_OILPAN) TEST_F(HeapTest, CollectNodeAndCssStatistics) { PreciselyCollectGarbage(); size_t node_bytes_before, css_bytes_before; @@ -3274,6 +3272,5 @@ TEST_F(HeapTest, CollectNodeAndCssStatistics) { EXPECT_LE(node_bytes_before + sizeof(FakeNode), node_bytes_after); EXPECT_LE(css_bytes_before + sizeof(FakeCSSValue), css_bytes_after); } -#endif } // namespace blink diff --git a/third_party/blink/renderer/platform/heap/v8_wrapper/thread_state.cc b/third_party/blink/renderer/platform/heap/v8_wrapper/thread_state.cc index 204e72c506cf72..25fca65c4125e5 100644 --- a/third_party/blink/renderer/platform/heap/v8_wrapper/thread_state.cc +++ b/third_party/blink/renderer/platform/heap/v8_wrapper/thread_state.cc @@ -204,11 +204,49 @@ void ThreadState::CollectAllGarbageForTesting(BlinkGC::StackState stack_state) { } } +namespace { + +class CustomSpaceStatisticsReceiverImpl final + : public v8::CustomSpaceStatisticsReceiver { + public: + explicit CustomSpaceStatisticsReceiverImpl( + base::OnceCallback callback) + : callback_(std::move(callback)) {} + + ~CustomSpaceStatisticsReceiverImpl() final { + DCHECK(node_bytes_.has_value()); + DCHECK(css_bytes_.has_value()); + std::move(callback_).Run(*node_bytes_, *css_bytes_); + } + + void AllocatedBytes(cppgc::CustomSpaceIndex space_index, size_t bytes) final { + if (space_index.value == NodeSpace::kSpaceIndex.value) { + node_bytes_ = bytes; + } else { + DCHECK_EQ(space_index.value, CSSValueSpace::kSpaceIndex.value); + css_bytes_ = bytes; + } + } + + private: + base::OnceCallback + callback_; + base::Optional node_bytes_; + base::Optional css_bytes_; +}; + +} // anonymous namespace + void ThreadState::CollectNodeAndCssStatistics( base::OnceCallback callback) { - // TODO(1181269): Implement. - std::move(callback).Run(0u, 0u); + std::vector spaces{NodeSpace::kSpaceIndex, + CSSValueSpace::kSpaceIndex}; + cpp_heap().CollectCustomSpaceStatisticsAtLastGC( + std::move(spaces), + std::make_unique(std::move(callback))); } void ThreadState::EnableDetachedGarbageCollectionsForTesting() {