Commit ce23afa7 authored by Omer Katz's avatar Omer Katz Committed by Commit Bot

cppgc: 2nd batch of unittests from heap_test.cc

Drive-by: cleanup PersistentTest and MarkerTest

Bug: chromium:1056170
Change-Id: I85d674cda7d580e8cd7be807791ba883b0f7a111
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2827898
Commit-Queue: Omer Katz <omerkatz@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73979}
parent af59290f
......@@ -94,32 +94,36 @@ TEST_F(GCHeapTest, ObjectPayloadSize) {
static constexpr size_t kObjectSizes[] = {1, 32, 64, 128,
2 * kLargeObjectSizeThreshold};
Heap::From(GetHeap())->CollectGarbage(
GarbageCollector::Config::ConservativeAtomicConfig());
EXPECT_EQ(0u, Heap::From(GetHeap())->ObjectPayloadSize());
subtle::NoGarbageCollectionScope no_gc(*Heap::From(GetHeap()));
{
subtle::NoGarbageCollectionScope no_gc(*Heap::From(GetHeap()));
for (size_t k = 0; k < kNumberOfObjectsPerArena; ++k) {
MakeGarbageCollected<GCed<kObjectSizes[0]>>(GetAllocationHandle());
MakeGarbageCollected<GCed<kObjectSizes[1]>>(GetAllocationHandle());
MakeGarbageCollected<GCed<kObjectSizes[2]>>(GetAllocationHandle());
MakeGarbageCollected<GCed<kObjectSizes[3]>>(GetAllocationHandle());
MakeGarbageCollected<GCed<kObjectSizes[4]>>(GetAllocationHandle());
}
for (size_t k = 0; k < kNumberOfObjectsPerArena; ++k) {
MakeGarbageCollected<GCed<kObjectSizes[0]>>(GetAllocationHandle());
MakeGarbageCollected<GCed<kObjectSizes[1]>>(GetAllocationHandle());
MakeGarbageCollected<GCed<kObjectSizes[2]>>(GetAllocationHandle());
MakeGarbageCollected<GCed<kObjectSizes[3]>>(GetAllocationHandle());
MakeGarbageCollected<GCed<kObjectSizes[4]>>(GetAllocationHandle());
size_t aligned_object_sizes[arraysize(kObjectSizes)];
std::transform(std::cbegin(kObjectSizes), std::cend(kObjectSizes),
std::begin(aligned_object_sizes), [](size_t size) {
return RoundUp(size, kAllocationGranularity);
});
const size_t expected_size = std::accumulate(
std::cbegin(aligned_object_sizes), std::cend(aligned_object_sizes), 0u,
[](size_t acc, size_t size) {
return acc + kNumberOfObjectsPerArena * size;
});
// TODO(chromium:1056170): Change to EXPECT_EQ when proper sweeping is
// implemented.
EXPECT_LE(expected_size, Heap::From(GetHeap())->ObjectPayloadSize());
}
size_t aligned_object_sizes[arraysize(kObjectSizes)];
std::transform(std::cbegin(kObjectSizes), std::cend(kObjectSizes),
std::begin(aligned_object_sizes), [](size_t size) {
return RoundUp(size, kAllocationGranularity);
});
const size_t expected_size = std::accumulate(
std::cbegin(aligned_object_sizes), std::cend(aligned_object_sizes), 0u,
[](size_t acc, size_t size) {
return acc + kNumberOfObjectsPerArena * size;
});
// TODO(chromium:1056170): Change to EXPECT_EQ when proper sweeping is
// implemented.
EXPECT_LE(expected_size, Heap::From(GetHeap())->ObjectPayloadSize());
PreciseGC();
EXPECT_EQ(0u, Heap::From(GetHeap())->ObjectPayloadSize());
}
TEST_F(GCHeapTest, AllocateWithAdditionalBytes) {
......
......@@ -356,10 +356,6 @@ class IncrementalMarkingTest : public testing::TestWithHeap {
MarkingConfig::CollectionType::kMajor,
MarkingConfig::StackState::kNoHeapPointers,
MarkingConfig::MarkingType::kIncremental};
static constexpr MarkingConfig IncrementalConservativeMarkingConfig = {
MarkingConfig::CollectionType::kMajor,
MarkingConfig::StackState::kMayContainHeapPointers,
MarkingConfig::MarkingType::kIncremental};
void FinishSteps(MarkingConfig::StackState stack_state) {
while (!SingleStep(stack_state)) {
......@@ -391,8 +387,6 @@ class IncrementalMarkingTest : public testing::TestWithHeap {
constexpr IncrementalMarkingTest::MarkingConfig
IncrementalMarkingTest::IncrementalPreciseMarkingConfig;
constexpr IncrementalMarkingTest::MarkingConfig
IncrementalMarkingTest::IncrementalConservativeMarkingConfig;
TEST_F(IncrementalMarkingTest, RootIsMarkedAfterMarkingStarted) {
Persistent<GCed> root = MakeGarbageCollected<GCed>(GetAllocationHandle());
......
......@@ -371,5 +371,61 @@ TEST_F(MemberTest, CheckingPolicy) {
EXPECT_EQ(CustomCheckingPolicy::Cached.size(),
CustomCheckingPolicy::ChecksTriggered);
}
namespace {
class MemberHeapTest : public testing::TestWithHeap {};
class GCedWithMember final : public GarbageCollected<GCedWithMember> {
public:
static size_t live_count_;
GCedWithMember() : GCedWithMember(nullptr) {}
explicit GCedWithMember(GCedWithMember* nested)
: nested_(nested), weak_nested_(nested) {
++live_count_;
}
~GCedWithMember() { --live_count_; }
void Trace(cppgc::Visitor* visitor) const { visitor->Trace(nested_); }
bool WasNestedCleared() const { return !weak_nested_; }
private:
Member<GCedWithMember> nested_;
WeakMember<GCedWithMember> weak_nested_;
};
size_t GCedWithMember::live_count_ = 0;
} // namespace
TEST_F(MemberHeapTest, MemberRetainsObject) {
EXPECT_EQ(0u, GCedWithMember::live_count_);
{
Persistent<GCedWithMember> gced_with_member =
MakeGarbageCollected<GCedWithMember>(
GetAllocationHandle(),
MakeGarbageCollected<GCedWithMember>(GetAllocationHandle()));
EXPECT_EQ(2u, GCedWithMember::live_count_);
PreciseGC();
EXPECT_EQ(2u, GCedWithMember::live_count_);
EXPECT_FALSE(gced_with_member->WasNestedCleared());
}
PreciseGC();
EXPECT_EQ(0u, GCedWithMember::live_count_);
{
GCedWithMember* gced_with_member = MakeGarbageCollected<GCedWithMember>(
GetAllocationHandle(),
MakeGarbageCollected<GCedWithMember>(GetAllocationHandle()));
EXPECT_EQ(2u, GCedWithMember::live_count_);
ConservativeGC();
EXPECT_EQ(2u, GCedWithMember::live_count_);
EXPECT_FALSE(gced_with_member->WasNestedCleared());
}
PreciseGC();
EXPECT_EQ(0u, GCedWithMember::live_count_);
}
} // namespace internal
} // namespace cppgc
......@@ -933,19 +933,13 @@ namespace {
class TraceCounter final : public GarbageCollected<TraceCounter> {
public:
TraceCounter() : nested_(nullptr) {}
explicit TraceCounter(TraceCounter* nested) : nested_(nested) {}
void Trace(cppgc::Visitor* visitor) const {
visitor->Trace(nested_);
trace_calls_++;
}
TraceCounter* nested() const { return nested_; }
size_t trace_calls() const { return trace_calls_; }
private:
Member<TraceCounter> nested_;
mutable size_t trace_calls_ = 0;
};
......@@ -964,21 +958,14 @@ size_t DestructionCounter::destructor_calls_;
TEST_F(PersistentTest, PersistentRetainsObject) {
Persistent<TraceCounter> trace_counter =
MakeGarbageCollected<TraceCounter>(GetAllocationHandle());
WeakPersistent<TraceCounter> weak_trace_counter(trace_counter.Get());
EXPECT_EQ(0u, trace_counter->trace_calls());
PreciseGC();
size_t saved_trace_count = trace_counter->trace_calls();
EXPECT_LT(0u, saved_trace_count);
Persistent<TraceCounter> class_with_member =
MakeGarbageCollected<TraceCounter>(
GetAllocationHandle(),
MakeGarbageCollected<TraceCounter>(GetAllocationHandle()));
EXPECT_EQ(0u, class_with_member->nested()->trace_calls());
PreciseGC();
EXPECT_LT(0u, class_with_member->nested()->trace_calls());
EXPECT_LT(saved_trace_count, trace_counter->trace_calls());
USE(trace_counter);
USE(class_with_member);
EXPECT_TRUE(weak_trace_counter);
}
TEST_F(PersistentTest, ObjectReclaimedAfterClearedPersistent) {
......
......@@ -76,6 +76,12 @@ class TestWithHeap : public TestWithPlatform {
"Testing", cppgc::Heap::StackState::kNoHeapPointers);
}
void ConservativeGC() {
heap_->ForceGarbageCollectionSlow(
::testing::UnitTest::GetInstance()->current_test_info()->name(),
"Testing", cppgc::Heap::StackState::kMayContainHeapPointers);
}
cppgc::Heap* GetHeap() const { return heap_.get(); }
cppgc::AllocationHandle& GetAllocationHandle() const {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment