2020-05-05 09:19:02 +02: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.
|
|
|
|
|
|
|
|
#ifndef V8_UNITTESTS_HEAP_CPPGC_TESTS_H_
|
|
|
|
#define V8_UNITTESTS_HEAP_CPPGC_TESTS_H_
|
|
|
|
|
2021-03-12 08:24:20 +01:00
|
|
|
#include "include/cppgc/heap-consistency.h"
|
2020-05-05 09:19:02 +02:00
|
|
|
#include "include/cppgc/heap.h"
|
2025-04-29 08:03:15 +02:00
|
|
|
#include "include/cppgc/macros.h"
|
2020-05-05 09:19:02 +02:00
|
|
|
#include "include/cppgc/platform.h"
|
2020-07-13 10:39:42 +02:00
|
|
|
#include "src/heap/cppgc/heap.h"
|
2021-02-24 14:47:06 +01:00
|
|
|
#include "src/heap/cppgc/trace-event.h"
|
2020-10-15 20:17:08 +02:00
|
|
|
#include "test/unittests/heap/cppgc/test-platform.h"
|
2020-05-05 09:19:02 +02:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace cppgc {
|
2020-07-13 10:39:42 +02:00
|
|
|
namespace internal {
|
2020-05-05 09:19:02 +02:00
|
|
|
namespace testing {
|
2021-02-24 14:47:06 +01:00
|
|
|
class DelegatingTracingController : public TracingController {
|
|
|
|
public:
|
|
|
|
#if !defined(V8_USE_PERFETTO)
|
|
|
|
const uint8_t* GetCategoryGroupEnabled(const char* name) override {
|
2025-04-29 08:03:15 +02:00
|
|
|
static const std::string disabled_by_default_tag =
|
|
|
|
TRACE_DISABLED_BY_DEFAULT("");
|
2021-02-24 14:47:06 +01:00
|
|
|
static uint8_t yes = 1;
|
2025-04-29 08:03:15 +02:00
|
|
|
static uint8_t no = 0;
|
|
|
|
if (strncmp(name, disabled_by_default_tag.c_str(),
|
|
|
|
disabled_by_default_tag.length()) == 0) {
|
|
|
|
return &no;
|
|
|
|
}
|
2021-02-24 14:47:06 +01:00
|
|
|
return &yes;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t AddTraceEvent(
|
|
|
|
char phase, const uint8_t* category_enabled_flag, const char* name,
|
|
|
|
const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
|
|
|
|
const char** arg_names, const uint8_t* arg_types,
|
|
|
|
const uint64_t* arg_values,
|
|
|
|
std::unique_ptr<ConvertableToTraceFormat>* arg_convertables,
|
|
|
|
unsigned int flags) override {
|
|
|
|
return tracing_controller_->AddTraceEvent(
|
|
|
|
phase, category_enabled_flag, name, scope, id, bind_id, num_args,
|
|
|
|
arg_names, arg_types, arg_values, arg_convertables, flags);
|
|
|
|
}
|
|
|
|
#endif // !defined(V8_USE_PERFETTO)
|
|
|
|
|
|
|
|
void SetTracingController(
|
|
|
|
std::unique_ptr<TracingController> tracing_controller_impl) {
|
|
|
|
tracing_controller_ = std::move(tracing_controller_impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<TracingController> tracing_controller_ =
|
|
|
|
std::make_unique<TracingController>();
|
|
|
|
};
|
2020-05-05 09:19:02 +02:00
|
|
|
|
|
|
|
class TestWithPlatform : public ::testing::Test {
|
2022-04-12 11:10:15 +02:00
|
|
|
public:
|
2020-05-05 09:19:02 +02:00
|
|
|
static void SetUpTestSuite();
|
|
|
|
static void TearDownTestSuite();
|
|
|
|
|
2020-10-15 20:17:08 +02:00
|
|
|
TestPlatform& GetPlatform() const { return *platform_; }
|
|
|
|
|
|
|
|
std::shared_ptr<TestPlatform> GetPlatformHandle() const { return platform_; }
|
|
|
|
|
2021-02-24 14:47:06 +01:00
|
|
|
void SetTracingController(
|
|
|
|
std::unique_ptr<TracingController> tracing_controller_impl) {
|
|
|
|
static_cast<DelegatingTracingController*>(platform_->GetTracingController())
|
|
|
|
->SetTracingController(std::move(tracing_controller_impl));
|
|
|
|
}
|
|
|
|
|
2020-10-15 20:17:08 +02:00
|
|
|
protected:
|
|
|
|
static std::shared_ptr<TestPlatform> platform_;
|
2020-05-05 09:19:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class TestWithHeap : public TestWithPlatform {
|
2022-04-12 11:10:15 +02:00
|
|
|
public:
|
2020-07-13 10:39:42 +02:00
|
|
|
TestWithHeap();
|
2022-09-21 13:28:42 +02:00
|
|
|
~TestWithHeap() override;
|
2020-05-05 09:19:02 +02:00
|
|
|
|
2020-07-13 10:39:42 +02:00
|
|
|
void PreciseGC() {
|
2021-02-11 19:03:35 +01:00
|
|
|
heap_->ForceGarbageCollectionSlow(
|
|
|
|
::testing::UnitTest::GetInstance()->current_test_info()->name(),
|
|
|
|
"Testing", cppgc::Heap::StackState::kNoHeapPointers);
|
2020-07-13 10:39:42 +02:00
|
|
|
}
|
|
|
|
|
2021-07-14 11:30:07 +02:00
|
|
|
void ConservativeGC() {
|
|
|
|
heap_->ForceGarbageCollectionSlow(
|
|
|
|
::testing::UnitTest::GetInstance()->current_test_info()->name(),
|
|
|
|
"Testing", cppgc::Heap::StackState::kMayContainHeapPointers);
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:20:49 +02:00
|
|
|
// GC that also discards unused memory and thus changes the resident size
|
|
|
|
// size of the heap and corresponding pages.
|
|
|
|
void ConservativeMemoryDiscardingGC() {
|
|
|
|
internal::Heap::From(GetHeap())->CollectGarbage(
|
2022-11-18 09:50:46 +00:00
|
|
|
{CollectionType::kMajor, Heap::StackState::kMayContainHeapPointers,
|
2022-01-29 08:33:07 +01:00
|
|
|
cppgc::Heap::MarkingType::kAtomic, cppgc::Heap::SweepingType::kAtomic,
|
2022-11-18 09:50:46 +00:00
|
|
|
GCConfig::FreeMemoryHandling::kDiscardWherePossible});
|
2021-08-29 14:20:49 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 10:39:42 +02:00
|
|
|
cppgc::Heap* GetHeap() const { return heap_.get(); }
|
2020-05-05 09:19:02 +02:00
|
|
|
|
2020-10-15 20:17:08 +02:00
|
|
|
cppgc::AllocationHandle& GetAllocationHandle() const {
|
|
|
|
return allocation_handle_;
|
|
|
|
}
|
|
|
|
|
2021-07-14 11:30:07 +02:00
|
|
|
cppgc::HeapHandle& GetHeapHandle() const {
|
|
|
|
return GetHeap()->GetHeapHandle();
|
|
|
|
}
|
|
|
|
|
2020-10-15 20:17:08 +02:00
|
|
|
std::unique_ptr<MarkerBase>& GetMarkerRef() {
|
2022-01-29 08:33:07 +01:00
|
|
|
return Heap::From(GetHeap())->GetMarkerRefForTesting();
|
2021-03-12 08:24:20 +01:00
|
|
|
}
|
|
|
|
|
2020-10-15 20:17:08 +02:00
|
|
|
void ResetLinearAllocationBuffers();
|
|
|
|
|
2020-05-05 09:19:02 +02:00
|
|
|
private:
|
|
|
|
std::unique_ptr<cppgc::Heap> heap_;
|
2020-10-15 20:17:08 +02:00
|
|
|
cppgc::AllocationHandle& allocation_handle_;
|
2020-05-05 09:19:02 +02:00
|
|
|
};
|
|
|
|
|
2020-07-13 10:39:42 +02:00
|
|
|
// Restrictive test fixture that supports allocation but will make sure no
|
|
|
|
// garbage collection is triggered. This is useful for writing idiomatic
|
|
|
|
// tests where object are allocated on the managed heap while still avoiding
|
2021-06-08 14:04:59 +02:00
|
|
|
// far reaching test consequences of full garbage collection calls.
|
2020-07-13 10:39:42 +02:00
|
|
|
class TestSupportingAllocationOnly : public TestWithHeap {
|
|
|
|
protected:
|
|
|
|
TestSupportingAllocationOnly();
|
|
|
|
|
|
|
|
private:
|
2023-03-30 12:11:08 +02:00
|
|
|
CPPGC_STACK_ALLOCATED_IGNORE("permitted for test code")
|
2021-03-12 08:24:20 +01:00
|
|
|
subtle::NoGarbageCollectionScope no_gc_scope_;
|
2020-07-13 10:39:42 +02:00
|
|
|
};
|
|
|
|
|
2020-05-05 09:19:02 +02:00
|
|
|
} // namespace testing
|
2020-07-13 10:39:42 +02:00
|
|
|
} // namespace internal
|
2020-05-05 09:19:02 +02:00
|
|
|
} // namespace cppgc
|
|
|
|
|
|
|
|
#endif // V8_UNITTESTS_HEAP_CPPGC_TESTS_H_
|