2020-05-26 14:40:57 +00:00
|
|
|
// Copyright 2020 the V8 project 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 <algorithm>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "include/cppgc/allocation.h"
|
|
|
|
#include "include/cppgc/platform.h"
|
|
|
|
#include "include/v8-platform.h"
|
|
|
|
#include "src/heap/cppgc/globals.h"
|
2020-06-29 17:17:20 +00:00
|
|
|
#include "src/heap/cppgc/heap-object-header.h"
|
|
|
|
#include "src/heap/cppgc/heap-page.h"
|
2020-05-26 14:40:57 +00:00
|
|
|
#include "src/heap/cppgc/heap-space.h"
|
|
|
|
#include "src/heap/cppgc/heap-visitor.h"
|
2020-06-29 17:17:20 +00:00
|
|
|
#include "src/heap/cppgc/page-memory.h"
|
2020-05-26 14:40:57 +00:00
|
|
|
#include "src/heap/cppgc/raw-heap.h"
|
2020-06-03 15:33:09 +00:00
|
|
|
#include "src/heap/cppgc/stats-collector.h"
|
2020-05-26 14:40:57 +00:00
|
|
|
#include "src/heap/cppgc/sweeper.h"
|
|
|
|
#include "test/unittests/heap/cppgc/test-platform.h"
|
|
|
|
#include "test/unittests/heap/cppgc/tests.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace cppgc {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
size_t g_destructor_callcount;
|
|
|
|
|
|
|
|
template <size_t Size>
|
|
|
|
class Finalizable : public GarbageCollected<Finalizable<Size>> {
|
|
|
|
public:
|
|
|
|
Finalizable() : creation_thread_{v8::base::OS::GetCurrentThreadId()} {}
|
|
|
|
|
|
|
|
virtual ~Finalizable() {
|
|
|
|
++g_destructor_callcount;
|
|
|
|
EXPECT_EQ(creation_thread_, v8::base::OS::GetCurrentThreadId());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Trace(cppgc::Visitor*) const {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char array_[Size];
|
|
|
|
int creation_thread_;
|
|
|
|
};
|
|
|
|
|
|
|
|
using NormalFinalizable = Finalizable<32>;
|
|
|
|
using LargeFinalizable = Finalizable<kLargeObjectSizeThreshold * 2>;
|
|
|
|
|
|
|
|
template <size_t Size>
|
|
|
|
class NonFinalizable : public GarbageCollected<NonFinalizable<Size>> {
|
|
|
|
public:
|
|
|
|
virtual void Trace(cppgc::Visitor*) const {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char array_[Size];
|
|
|
|
};
|
|
|
|
|
|
|
|
using NormalNonFinalizable = NonFinalizable<32>;
|
|
|
|
using LargeNonFinalizable = NonFinalizable<kLargeObjectSizeThreshold * 2>;
|
|
|
|
|
2020-10-08 11:16:20 +00:00
|
|
|
} // namespace
|
|
|
|
|
2020-05-26 14:40:57 +00:00
|
|
|
class ConcurrentSweeperTest : public testing::TestWithHeap {
|
|
|
|
public:
|
|
|
|
ConcurrentSweeperTest() { g_destructor_callcount = 0; }
|
|
|
|
|
|
|
|
void StartSweeping() {
|
|
|
|
Heap* heap = Heap::From(GetHeap());
|
2020-06-10 07:10:20 +00:00
|
|
|
ResetLinearAllocationBuffers();
|
2020-06-03 15:33:09 +00:00
|
|
|
// Pretend do finish marking as StatsCollector verifies that Notify*
|
|
|
|
// methods are called in the right order.
|
2020-11-19 12:27:35 +00:00
|
|
|
heap->stats_collector()->NotifyMarkingStarted(
|
|
|
|
GarbageCollector::Config::CollectionType::kMajor,
|
|
|
|
GarbageCollector::Config::IsForcedGC::kNotForced);
|
2020-06-03 15:33:09 +00:00
|
|
|
heap->stats_collector()->NotifyMarkingCompleted(0);
|
2020-05-26 14:40:57 +00:00
|
|
|
Sweeper& sweeper = heap->sweeper();
|
2020-10-23 12:55:45 +00:00
|
|
|
const Sweeper::SweepingConfig sweeping_config{
|
|
|
|
Sweeper::SweepingConfig::SweepingType::kIncrementalAndConcurrent,
|
|
|
|
Sweeper::SweepingConfig::CompactableSpaceHandling::kSweep};
|
|
|
|
sweeper.Start(sweeping_config);
|
2020-05-26 14:40:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 11:16:20 +00:00
|
|
|
void WaitForConcurrentSweeping() {
|
|
|
|
Heap* heap = Heap::From(GetHeap());
|
|
|
|
Sweeper& sweeper = heap->sweeper();
|
|
|
|
sweeper.WaitForConcurrentSweepingForTesting();
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:40:57 +00:00
|
|
|
void FinishSweeping() {
|
|
|
|
Heap* heap = Heap::From(GetHeap());
|
|
|
|
Sweeper& sweeper = heap->sweeper();
|
2020-08-19 12:59:00 +00:00
|
|
|
sweeper.FinishIfRunning();
|
2020-05-26 14:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const RawHeap& GetRawHeap() const {
|
|
|
|
const Heap* heap = Heap::From(GetHeap());
|
|
|
|
return heap->raw_heap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckFreeListEntries(const std::vector<void*>& objects) {
|
|
|
|
const Heap* heap = Heap::From(GetHeap());
|
|
|
|
const PageBackend* backend = heap->page_backend();
|
|
|
|
|
|
|
|
for (auto* object : objects) {
|
|
|
|
// The corresponding page could be removed.
|
|
|
|
if (!backend->Lookup(static_cast<ConstAddress>(object))) continue;
|
|
|
|
|
|
|
|
const auto* header =
|
|
|
|
BasePage::FromPayload(object)->TryObjectHeaderFromInnerAddress(
|
|
|
|
object);
|
|
|
|
// TryObjectHeaderFromInnerAddress returns nullptr for freelist entries.
|
|
|
|
EXPECT_EQ(nullptr, header);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckPageRemoved(const BasePage* page) {
|
|
|
|
const Heap* heap = Heap::From(GetHeap());
|
|
|
|
const PageBackend* backend = heap->page_backend();
|
|
|
|
EXPECT_EQ(nullptr, backend->Lookup(reinterpret_cast<ConstAddress>(page)));
|
|
|
|
}
|
|
|
|
|
2021-05-26 10:40:20 +00:00
|
|
|
bool FreeListContains(const BaseSpace& space,
|
2020-05-26 14:40:57 +00:00
|
|
|
const std::vector<void*>& objects) {
|
|
|
|
const Heap* heap = Heap::From(GetHeap());
|
|
|
|
const PageBackend* backend = heap->page_backend();
|
2021-05-26 10:40:20 +00:00
|
|
|
const auto& freelist = NormalPageSpace::From(space).free_list();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
for (void* object : objects) {
|
|
|
|
// The corresponding page could be removed.
|
|
|
|
if (!backend->Lookup(static_cast<ConstAddress>(object))) continue;
|
|
|
|
|
2021-03-04 23:09:36 +00:00
|
|
|
if (!freelist.ContainsForTesting({object, 0})) return false;
|
2020-05-26 14:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(ConcurrentSweeperTest, BackgroundSweepOfNormalPage) {
|
|
|
|
// Non finalizable objects are swept right away.
|
|
|
|
using GCedType = NormalNonFinalizable;
|
|
|
|
|
2020-06-10 22:28:41 +00:00
|
|
|
auto* unmarked_object = MakeGarbageCollected<GCedType>(GetAllocationHandle());
|
|
|
|
auto* marked_object = MakeGarbageCollected<GCedType>(GetAllocationHandle());
|
2021-05-12 18:08:11 +00:00
|
|
|
HeapObjectHeader::FromObject(marked_object).TryMarkAtomic();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
auto* page = BasePage::FromPayload(unmarked_object);
|
2021-05-26 10:40:20 +00:00
|
|
|
auto& space = page->space();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
// The test requires objects to be allocated on the same page;
|
|
|
|
ASSERT_EQ(page, BasePage::FromPayload(marked_object));
|
|
|
|
|
|
|
|
StartSweeping();
|
|
|
|
|
|
|
|
// Wait for concurrent sweeping to finish.
|
2020-10-08 11:16:20 +00:00
|
|
|
WaitForConcurrentSweeping();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
2020-06-16 22:41:00 +00:00
|
|
|
#if !defined(CPPGC_YOUNG_GENERATION)
|
2020-05-26 14:40:57 +00:00
|
|
|
// Check that the marked object was unmarked.
|
2021-05-12 18:08:11 +00:00
|
|
|
EXPECT_FALSE(HeapObjectHeader::FromObject(marked_object).IsMarked());
|
2020-06-16 22:41:00 +00:00
|
|
|
#else
|
|
|
|
// Check that the marked object is still marked.
|
2021-05-12 18:08:11 +00:00
|
|
|
EXPECT_TRUE(HeapObjectHeader::FromObject(marked_object).IsMarked());
|
2020-06-16 22:41:00 +00:00
|
|
|
#endif
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
// Check that free list entries are created right away for non-finalizable
|
|
|
|
// objects, but not immediately returned to the space's freelist.
|
|
|
|
CheckFreeListEntries({unmarked_object});
|
|
|
|
EXPECT_FALSE(FreeListContains(space, {unmarked_object}));
|
|
|
|
|
|
|
|
FinishSweeping();
|
|
|
|
|
|
|
|
// Check that finalizable objects are swept and put into the freelist of the
|
|
|
|
// corresponding space.
|
|
|
|
EXPECT_TRUE(FreeListContains(space, {unmarked_object}));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ConcurrentSweeperTest, BackgroundSweepOfLargePage) {
|
|
|
|
// Non finalizable objects are swept right away.
|
|
|
|
using GCedType = LargeNonFinalizable;
|
|
|
|
|
2020-06-10 22:28:41 +00:00
|
|
|
auto* unmarked_object = MakeGarbageCollected<GCedType>(GetAllocationHandle());
|
|
|
|
auto* marked_object = MakeGarbageCollected<GCedType>(GetAllocationHandle());
|
2021-05-12 18:08:11 +00:00
|
|
|
HeapObjectHeader::FromObject(marked_object).TryMarkAtomic();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
auto* unmarked_page = BasePage::FromPayload(unmarked_object);
|
|
|
|
auto* marked_page = BasePage::FromPayload(marked_object);
|
2021-05-26 10:40:20 +00:00
|
|
|
auto& space = unmarked_page->space();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
2021-05-26 10:40:20 +00:00
|
|
|
ASSERT_EQ(&space, &marked_page->space());
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
StartSweeping();
|
|
|
|
|
|
|
|
// Wait for concurrent sweeping to finish.
|
2020-10-08 11:16:20 +00:00
|
|
|
WaitForConcurrentSweeping();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
2020-06-16 22:41:00 +00:00
|
|
|
#if !defined(CPPGC_YOUNG_GENERATION)
|
2020-05-26 14:40:57 +00:00
|
|
|
// Check that the marked object was unmarked.
|
2021-05-12 18:08:11 +00:00
|
|
|
EXPECT_FALSE(HeapObjectHeader::FromObject(marked_object).IsMarked());
|
2020-06-16 22:41:00 +00:00
|
|
|
#else
|
|
|
|
// Check that the marked object is still marked.
|
2021-05-12 18:08:11 +00:00
|
|
|
EXPECT_TRUE(HeapObjectHeader::FromObject(marked_object).IsMarked());
|
2020-06-16 22:41:00 +00:00
|
|
|
#endif
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
// Check that free list entries are created right away for non-finalizable
|
|
|
|
// objects, but not immediately returned to the space's freelist.
|
|
|
|
CheckPageRemoved(unmarked_page);
|
|
|
|
|
|
|
|
// Check that marked pages are returned to space right away.
|
2021-05-26 10:40:20 +00:00
|
|
|
EXPECT_NE(space.end(), std::find(space.begin(), space.end(), marked_page));
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
FinishSweeping();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ConcurrentSweeperTest, DeferredFinalizationOfNormalPage) {
|
|
|
|
static constexpr size_t kNumberOfObjects = 10;
|
|
|
|
// Finalizable types are left intact by concurrent sweeper.
|
|
|
|
using GCedType = NormalFinalizable;
|
|
|
|
|
|
|
|
std::set<BasePage*> pages;
|
|
|
|
std::vector<void*> objects;
|
|
|
|
|
|
|
|
BaseSpace* space = nullptr;
|
|
|
|
for (size_t i = 0; i < kNumberOfObjects; ++i) {
|
2020-06-10 22:28:41 +00:00
|
|
|
auto* object = MakeGarbageCollected<GCedType>(GetAllocationHandle());
|
2020-05-26 14:40:57 +00:00
|
|
|
objects.push_back(object);
|
|
|
|
auto* page = BasePage::FromPayload(object);
|
|
|
|
pages.insert(page);
|
2021-05-26 10:40:20 +00:00
|
|
|
if (!space) space = &page->space();
|
2020-05-26 14:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StartSweeping();
|
|
|
|
|
|
|
|
// Wait for concurrent sweeping to finish.
|
2020-10-08 11:16:20 +00:00
|
|
|
WaitForConcurrentSweeping();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
// Check that pages are not returned right away.
|
|
|
|
for (auto* page : pages) {
|
|
|
|
EXPECT_EQ(space->end(), std::find(space->begin(), space->end(), page));
|
|
|
|
}
|
|
|
|
// Check that finalizable objects are left intact in pages.
|
2021-05-26 10:40:20 +00:00
|
|
|
EXPECT_FALSE(FreeListContains(*space, objects));
|
2020-05-26 14:40:57 +00:00
|
|
|
// No finalizers have been executed.
|
|
|
|
EXPECT_EQ(0u, g_destructor_callcount);
|
|
|
|
|
|
|
|
FinishSweeping();
|
|
|
|
|
|
|
|
// Check that finalizable objects are swept and turned into freelist entries.
|
|
|
|
CheckFreeListEntries(objects);
|
|
|
|
// Check that space's freelist contains these entries.
|
2021-05-26 10:40:20 +00:00
|
|
|
EXPECT_TRUE(FreeListContains(*space, objects));
|
2020-05-26 14:40:57 +00:00
|
|
|
// Check that finalizers have been executed.
|
|
|
|
EXPECT_EQ(kNumberOfObjects, g_destructor_callcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ConcurrentSweeperTest, DeferredFinalizationOfLargePage) {
|
|
|
|
using GCedType = LargeFinalizable;
|
|
|
|
|
2020-06-10 22:28:41 +00:00
|
|
|
auto* object = MakeGarbageCollected<GCedType>(GetAllocationHandle());
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
auto* page = BasePage::FromPayload(object);
|
2021-05-26 10:40:20 +00:00
|
|
|
auto& space = page->space();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
StartSweeping();
|
|
|
|
|
|
|
|
// Wait for concurrent sweeping to finish.
|
2020-10-08 11:16:20 +00:00
|
|
|
WaitForConcurrentSweeping();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
// Check that the page is not returned to the space.
|
2021-05-26 10:40:20 +00:00
|
|
|
EXPECT_EQ(space.end(), std::find(space.begin(), space.end(), page));
|
2020-05-26 14:40:57 +00:00
|
|
|
// Check that no destructors have been executed yet.
|
|
|
|
EXPECT_EQ(0u, g_destructor_callcount);
|
|
|
|
|
|
|
|
FinishSweeping();
|
|
|
|
|
|
|
|
// Check that the destructor was executed.
|
|
|
|
EXPECT_EQ(1u, g_destructor_callcount);
|
|
|
|
// Check that page was unmapped.
|
|
|
|
CheckPageRemoved(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ConcurrentSweeperTest, IncrementalSweeping) {
|
|
|
|
testing::TestPlatform::DisableBackgroundTasksScope disable_concurrent_sweeper(
|
|
|
|
&GetPlatform());
|
|
|
|
|
2020-05-28 19:04:43 +00:00
|
|
|
auto task_runner = GetPlatform().GetForegroundTaskRunner();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
// Create two unmarked objects.
|
2020-06-10 22:28:41 +00:00
|
|
|
MakeGarbageCollected<NormalFinalizable>(GetAllocationHandle());
|
|
|
|
MakeGarbageCollected<LargeFinalizable>(GetAllocationHandle());
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
// Create two marked objects.
|
|
|
|
auto* marked_normal_object =
|
2020-06-10 22:28:41 +00:00
|
|
|
MakeGarbageCollected<NormalFinalizable>(GetAllocationHandle());
|
|
|
|
auto* marked_large_object =
|
|
|
|
MakeGarbageCollected<LargeFinalizable>(GetAllocationHandle());
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
auto& marked_normal_header =
|
2021-05-12 18:08:11 +00:00
|
|
|
HeapObjectHeader::FromObject(marked_normal_object);
|
|
|
|
auto& marked_large_header = HeapObjectHeader::FromObject(marked_large_object);
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
marked_normal_header.TryMarkAtomic();
|
|
|
|
marked_large_header.TryMarkAtomic();
|
|
|
|
|
|
|
|
StartSweeping();
|
|
|
|
|
|
|
|
EXPECT_EQ(0u, g_destructor_callcount);
|
|
|
|
EXPECT_TRUE(marked_normal_header.IsMarked());
|
|
|
|
EXPECT_TRUE(marked_large_header.IsMarked());
|
|
|
|
|
|
|
|
// Wait for incremental sweeper to finish.
|
2020-10-08 11:16:20 +00:00
|
|
|
GetPlatform().RunAllForegroundTasks();
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(2u, g_destructor_callcount);
|
2020-06-16 22:41:00 +00:00
|
|
|
#if !defined(CPPGC_YOUNG_GENERATION)
|
2020-05-26 14:40:57 +00:00
|
|
|
EXPECT_FALSE(marked_normal_header.IsMarked());
|
|
|
|
EXPECT_FALSE(marked_large_header.IsMarked());
|
2020-06-16 22:41:00 +00:00
|
|
|
#else
|
|
|
|
EXPECT_TRUE(marked_normal_header.IsMarked());
|
|
|
|
EXPECT_TRUE(marked_large_header.IsMarked());
|
|
|
|
#endif
|
2020-05-26 14:40:57 +00:00
|
|
|
|
|
|
|
FinishSweeping();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace cppgc
|