From 7482c13dff0a7a37982e2e765ff39a08273cfbfe Mon Sep 17 00:00:00 2001 From: yp9522 Date: Wed, 23 Jul 2025 11:39:14 +0800 Subject: [PATCH] add common_components tdd Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICO7O8 Change-Id: Ic5a365e394e1cbb5e4d562bf0153f9b870b88b07 Signed-off-by: yp9522 --- .../heap/collector/heuristic_gc_policy.cpp | 7 +- .../heap/collector/heuristic_gc_policy.h | 3 +- .../tests/heuristic_gc_policy_test.cpp | 65 ++++++++++++++++++- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/common_components/heap/collector/heuristic_gc_policy.cpp b/common_components/heap/collector/heuristic_gc_policy.cpp index 570b02bd58..175f7e0a1d 100644 --- a/common_components/heap/collector/heuristic_gc_policy.cpp +++ b/common_components/heap/collector/heuristic_gc_policy.cpp @@ -39,13 +39,14 @@ void HeuristicGCPolicy::Init() bool HeuristicGCPolicy::ShouldRestrainGCOnStartupOrSensitive() { // Startup + size_t allocated = Heap::GetHeap().GetAllocator().GetAllocatedBytes(); StartupStatus currentStatus = StartupStatusManager::GetStartupStatus(); - if (UNLIKELY_CC(currentStatus == StartupStatus::COLD_STARTUP)) { + if (UNLIKELY_CC(currentStatus == StartupStatus::COLD_STARTUP && + allocated < heapSize_ * COLD_STARTUP_PHASE1_GC_THRESHOLD_RATIO)) { return true; } - size_t allocated = Heap::GetHeap().GetAllocator().GetAllocatedBytes(); if (currentStatus == StartupStatus::COLD_STARTUP_PARTIALLY_FINISH && - allocated < heapSize_ * COLD_STARTUP_GC_THRESHOLD_RATIO) { + allocated < heapSize_ * COLD_STARTUP_PHASE2_GC_THRESHOLD_RATIO) { return true; } // Sensitive diff --git a/common_components/heap/collector/heuristic_gc_policy.h b/common_components/heap/collector/heuristic_gc_policy.h index bf2972bd7d..5ca15f4a1d 100644 --- a/common_components/heap/collector/heuristic_gc_policy.h +++ b/common_components/heap/collector/heuristic_gc_policy.h @@ -102,7 +102,8 @@ static constexpr size_t URGENCY_NATIVE_LIMIT = MAX_NATIVE_SIZE_INC + MAX_NATIVE_ class HeuristicGCPolicy { public: - static constexpr double COLD_STARTUP_GC_THRESHOLD_RATIO = 0.25; + static constexpr double COLD_STARTUP_PHASE1_GC_THRESHOLD_RATIO = 0.3; + static constexpr double COLD_STARTUP_PHASE2_GC_THRESHOLD_RATIO = 0.125; void Init(); bool ShouldRestrainGCOnStartupOrSensitive(); diff --git a/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp b/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp index 4adefc7453..fa1defbc71 100644 --- a/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp +++ b/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp @@ -13,26 +13,87 @@ * limitations under the License. */ +#include "common_components/heap/allocator/region_space.h" #include "common_components/heap/collector/heuristic_gc_policy.h" #include "common_components/tests/test_helper.h" using namespace common; namespace common::test { class HeuristicGCPolicyTest : public common::test::BaseTestWithScope { + void SetUp() override + { + BaseRuntime::GetInstance()->Init(); + holder_ = ThreadHolder::CreateAndRegisterNewThreadHolder(nullptr); + scope_ = new ThreadHolder::TryBindMutatorScope(holder_); + } + + void TearDown() override + { + if (scope_ != nullptr) { + delete scope_; + scope_ = nullptr; + } + + BaseRuntime::GetInstance()->Fini(); + } + + ThreadHolder *holder_ {nullptr}; + ThreadHolder::TryBindMutatorScope *scope_ {nullptr}; }; -HWTEST_F_L0(HeuristicGCPolicyTest, ShouldRestrainGCOnStartupOrSensitive) +HWTEST_F_L0(HeuristicGCPolicyTest, ShouldRestrainGCOnStartupOrSensitive_Test1) { HeuristicGCPolicy gcPolicy; + gcPolicy.Init(); + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP_FINISH); + gcPolicy.TryHeuristicGC(); + EXPECT_FALSE(gcPolicy.ShouldRestrainGCOnStartupOrSensitive()); +} + +HWTEST_F_L0(HeuristicGCPolicyTest, ShouldRestrainGCOnStartupOrSensitive_Test2) +{ + HeuristicGCPolicy gcPolicy; + gcPolicy.Init(); StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP); EXPECT_TRUE(gcPolicy.ShouldRestrainGCOnStartupOrSensitive()); - gcPolicy.TryHeuristicGC(); + + RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + auto allocated = Heap::GetHeap().GetAllocator().GetAllocatedBytes(); + auto param = BaseRuntime::GetInstance()->GetHeapParam(); + auto size = param.heapSize * KB * HeuristicGCPolicy::COLD_STARTUP_PHASE1_GC_THRESHOLD_RATIO; + for (int i = 0; allocated < size; i++) { + uintptr_t addr = theAllocator.AllocOldRegion(); + ASSERT_NE(addr, 0); + allocated = Heap::GetHeap().GetAllocator().GetAllocatedBytes(); + } StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP_FINISH); EXPECT_FALSE(gcPolicy.ShouldRestrainGCOnStartupOrSensitive()); + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP); + EXPECT_FALSE(gcPolicy.ShouldRestrainGCOnStartupOrSensitive()); +} + +HWTEST_F_L0(HeuristicGCPolicyTest, ShouldRestrainGCOnStartupOrSensitive_Test3) +{ + HeuristicGCPolicy gcPolicy; gcPolicy.Init(); + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP_PARTIALLY_FINISH); + EXPECT_TRUE(gcPolicy.ShouldRestrainGCOnStartupOrSensitive()); + + RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + auto allocated = Heap::GetHeap().GetAllocator().GetAllocatedBytes(); + auto param = BaseRuntime::GetInstance()->GetHeapParam(); + auto size = param.heapSize * KB * HeuristicGCPolicy::COLD_STARTUP_PHASE2_GC_THRESHOLD_RATIO; + for (int i = 0; allocated < size; i++) { + uintptr_t addr = theAllocator.AllocOldRegion(); + ASSERT_NE(addr, 0); + allocated = Heap::GetHeap().GetAllocator().GetAllocatedBytes(); + } + + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP_FINISH); EXPECT_FALSE(gcPolicy.ShouldRestrainGCOnStartupOrSensitive()); + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP_PARTIALLY_FINISH); EXPECT_FALSE(gcPolicy.ShouldRestrainGCOnStartupOrSensitive()); } -- Gitee