1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/heap/heap_test_utilities.h"
#include <memory>
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "v8/include/cppgc/platform.h"
namespace blink {
namespace {
bool IsGCInProgress() {
return cppgc::subtle::HeapState::IsMarking(
ThreadState::Current()->heap_handle()) ||
cppgc::subtle::HeapState::IsSweeping(
ThreadState::Current()->heap_handle());
}
} // namespace
TestSupportingGC::~TestSupportingGC() {
PreciselyCollectGarbage();
}
// static
void TestSupportingGC::PreciselyCollectGarbage() {
ThreadState::Current()->CollectAllGarbageForTesting(
ThreadState::StackState::kNoHeapPointers);
}
// static
void TestSupportingGC::ConservativelyCollectGarbage() {
ThreadState::Current()->CollectAllGarbageForTesting(
ThreadState::StackState::kMayContainHeapPointers);
}
// static
void TestSupportingGC::ClearOutOldGarbage() {
PreciselyCollectGarbage();
auto& cpp_heap = ThreadState::Current()->cpp_heap();
size_t old_used = cpp_heap.CollectStatistics(cppgc::HeapStatistics::kDetailed)
.used_size_bytes;
while (true) {
PreciselyCollectGarbage();
size_t used = cpp_heap.CollectStatistics(cppgc::HeapStatistics::kDetailed)
.used_size_bytes;
if (used >= old_used)
break;
old_used = used;
}
}
CompactionTestDriver::CompactionTestDriver(ThreadState* thread_state)
: heap_(thread_state->heap_handle()) {}
void CompactionTestDriver::ForceCompactionForNextGC() {
heap_.ForceCompactionForNextGarbageCollection();
}
IncrementalMarkingTestDriver::IncrementalMarkingTestDriver(
ThreadState* thread_state)
: heap_(thread_state->heap_handle()) {}
IncrementalMarkingTestDriver::~IncrementalMarkingTestDriver() {
if (IsGCInProgress())
heap_.FinalizeGarbageCollection(cppgc::EmbedderStackState::kNoHeapPointers);
}
void IncrementalMarkingTestDriver::StartGC() {
heap_.StartGarbageCollection();
}
void IncrementalMarkingTestDriver::TriggerMarkingSteps() {
CHECK(ThreadState::Current()->IsIncrementalMarking());
while (!heap_.PerformMarkingStep(ThreadState::StackState::kNoHeapPointers)) {
}
}
void IncrementalMarkingTestDriver::TriggerMarkingStepsWithStack() {
CHECK(ThreadState::Current()->IsIncrementalMarking());
while (!heap_.PerformMarkingStep(
ThreadState::StackState::kMayContainHeapPointers)) {
}
}
void IncrementalMarkingTestDriver::FinishGC() {
CHECK(ThreadState::Current()->IsIncrementalMarking());
heap_.FinalizeGarbageCollection(cppgc::EmbedderStackState::kNoHeapPointers);
CHECK(!ThreadState::Current()->IsIncrementalMarking());
}
ConcurrentMarkingTestDriver::ConcurrentMarkingTestDriver(
ThreadState* thread_state)
: IncrementalMarkingTestDriver(thread_state) {}
void ConcurrentMarkingTestDriver::StartGC() {
IncrementalMarkingTestDriver::StartGC();
heap_.ToggleMainThreadMarking(false);
}
void ConcurrentMarkingTestDriver::TriggerMarkingSteps() {
CHECK(ThreadState::Current()->IsIncrementalMarking());
heap_.PerformMarkingStep(ThreadState::StackState::kNoHeapPointers);
}
void ConcurrentMarkingTestDriver::FinishGC() {
heap_.ToggleMainThreadMarking(true);
IncrementalMarkingTestDriver::FinishGC();
}
} // namespace blink
|