From a770270556195461a7b747b3cbfe5652a8e71417 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Thu, 17 Jul 2025 16:48:42 +0800 Subject: [PATCH] add HeuristicGCPolicy tdd Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICN0NG?from=project-issue Signed-off-by: yp9522 --- .../heap/collector/tests/BUILD.gn | 25 ++++ .../tests/heuristic_gc_policy_test.cpp | 107 ++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 common_components/heap/collector/tests/heuristic_gc_policy_test.cpp diff --git a/common_components/heap/collector/tests/BUILD.gn b/common_components/heap/collector/tests/BUILD.gn index 20a34d4289..b84b68b788 100755 --- a/common_components/heap/collector/tests/BUILD.gn +++ b/common_components/heap/collector/tests/BUILD.gn @@ -39,6 +39,29 @@ host_unittest_action("Collector_Test") { ] } +host_unittest_action("Heuristic_Gc_Policy_Test") { + module_out_path = module_output_path + + sources = [ + # test file + "heuristic_gc_policy_test.cpp", + ] + + configs = [ + "//arkcompiler/ets_runtime/common_components:common_components_test_config", + "//arkcompiler/ets_runtime:icu_path_test_config", + ] + + deps = [ "//arkcompiler/ets_runtime/common_components:libark_common_components_test" ] + + # hiviewdfx libraries + external_deps = [ + "icu:shared_icui18n", + "icu:shared_icuuc", + "runtime_core:libarkassembler_static", + ] +} + host_unittest_action("Trace_Collector_Test") { module_out_path = module_output_path @@ -114,6 +137,7 @@ group("unittest") { # deps file deps = [ ":Collector_Test", + ":Heuristic_Gc_Policy_Test", ":Trace_Collector_Test", ":Task_Queue_Test", ":Gc_Request_Test", @@ -126,6 +150,7 @@ group("host_unittest") { # deps file deps = [ ":Collector_TestAction", + ":Heuristic_Gc_Policy_TestAction", ":Trace_Collector_TestAction", ":Task_Queue_TestAction", ":Gc_Request_TestAction", diff --git a/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp b/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp new file mode 100644 index 0000000000..2dfd28c21a --- /dev/null +++ b/common_components/heap/collector/tests/heuristic_gc_policy_test.cpp @@ -0,0 +1,107 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common_components/heap/allocator/allocator.h" +#include "common_components/heap/collector/heuristic_gc_policy.h" +#include "common_components/heap/heap.h" +#include "common_components/tests/test_helper.h" + +using namespace common; +namespace common::test { + +class HeuristicGCPolicyTest : public common::test::BaseTestWithScope { +protected: + static void SetUpTestCase() + { + BaseRuntime::GetInstance()->Init(); + } + + static void TearDownTestCase() + { + BaseRuntime::GetInstance()->Fini(); + } + + void SetUp() override {} + void TearDown() override {} +}; + +HWTEST_F_L0(HeuristicGCPolicyTest, TryHeuristicGC) +{ + HeuristicGCPolicy gcPolicy; + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP_FINISH); + Heap::GetHeap().GetCollector().GetGCStats().heapThreshold = 0; + gcPolicy.TryHeuristicGC(); + Heap::GetHeap().GetCollector().GetGCStats().shouldRequestYoung = true; + gcPolicy.TryHeuristicGC(); + EXPECT_EQ(Heap::GetHeap().GetAllocator().GetAllocatedBytes(), + Heap::GetHeap().GetCollector().GetGCStats().GetThreshold()); +} + +HWTEST_F_L0(HeuristicGCPolicyTest, ChangeGCParams) +{ + HeuristicGCPolicy gcPolicy; + gcPolicy.RecordAliveSizeAfterLastGC(1); + gcPolicy.ChangeGCParams(true); + EXPECT_EQ(Heap::GetHeap().GetAllocator().GetAllocatedBytes(), 0); +} + +HWTEST_F_L0(HeuristicGCPolicyTest, CheckAndTriggerHintGC) +{ + HeuristicGCPolicy gcPolicy; + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP); + bool result = gcPolicy.CheckAndTriggerHintGC(MemoryReduceDegree::HIGH); + ASSERT_FALSE(result); + + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP_FINISH); + result = gcPolicy.CheckAndTriggerHintGC(MemoryReduceDegree::HIGH); + ASSERT_FALSE(result); + + gcPolicy.RecordAliveSizeAfterLastGC(1); + result = gcPolicy.CheckAndTriggerHintGC(MemoryReduceDegree::HIGH); + ASSERT_FALSE(result); + + result = gcPolicy.CheckAndTriggerHintGC(MemoryReduceDegree::LOW); + ASSERT_FALSE(result); +} + +HWTEST_F_L0(HeuristicGCPolicyTest, ShouldRestrainGCOnStartup) +{ + HeuristicGCPolicy gcPolicy; + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP); + EXPECT_TRUE(gcPolicy.ShouldRestrainGCOnStartup()); + gcPolicy.TryHeuristicGC(); + + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP_FINISH); + EXPECT_FALSE(gcPolicy.ShouldRestrainGCOnStartup()); + + gcPolicy.Init(); + EXPECT_FALSE(gcPolicy.ShouldRestrainGCOnStartup()); + StartupStatusManager::SetStartupStatus(StartupStatus::COLD_STARTUP_PARTIALLY_FINISH); + EXPECT_TRUE(gcPolicy.ShouldRestrainGCOnStartup()); +} + +HWTEST_F_L0(HeuristicGCPolicyTest, NotifyNativeAllocation_TriggerByBytes) +{ + HeuristicGCPolicy gcPolicy; + size_t initialNotified = gcPolicy.GetNotifiedNativeSize(); + size_t initialObjects = gcPolicy.GetNativeHeapThreshold(); + + gcPolicy.SetNativeHeapThreshold(1); + gcPolicy.NotifyNativeAllocation(NATIVE_IMMEDIATE_THRESHOLD + 1); + + EXPECT_EQ(gcPolicy.GetNotifiedNativeSize(), initialNotified + NATIVE_IMMEDIATE_THRESHOLD + 1); + EXPECT_NE(gcPolicy.GetNativeHeapThreshold(), initialObjects + 1); +} +} // namespace common::test \ No newline at end of file -- Gitee