• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <chrono>
16 #include <thread>
17 
18 #include "ecmascript/builtins/builtins_ark_tools.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/mem/full_gc.h"
21 #include "ecmascript/object_factory-inl.h"
22 #include "ecmascript/mem/concurrent_marker.h"
23 #include "ecmascript/mem/partial_gc.h"
24 #include "ecmascript/tests/ecma_test_common.h"
25 #include "ecmascript/napi/include/jsnapi_expo.h"
26 #include "ecmascript/mem/free_object_list.h"
27 #include "ecmascript/mem/gc_stats.h"
28 #include "ecmascript/mem/free_object_set.h"
29 #include "ecmascript/mem/shared_mem_controller.h"
30 #include "ecmascript/mem/mem_controller_utils.h"
31 #include "ecmascript/mem/mem_controller.h"
32 #include "ecmascript/mem/incremental_marker.h"
33 
34 using namespace panda;
35 
36 using namespace panda::ecmascript;
37 using TRIGGER_IDLE_GC_TYPE = panda::JSNApi::TRIGGER_IDLE_GC_TYPE;
38 
39 namespace panda::test {
40 class GCTest : public BaseTestWithScope<false> {
41 public:
SetUp()42     void SetUp() override
43     {
44         JSRuntimeOptions options;
45         instance = JSNApi::CreateEcmaVM(options);
46         ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
47         thread = instance->GetJSThread();
48         thread->ManagedCodeBegin();
49         scope = new EcmaHandleScope(thread);
50         auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
51         heap->GetConcurrentMarker()->EnableConcurrentMarking(EnableConcurrentMarkType::ENABLE);
52         heap->GetSweeper()->EnableConcurrentSweep(EnableConcurrentSweepType::ENABLE);
53     }
54 };
55 
HWTEST_F_L0(GCTest,NativeGCTestConcurrentMarkDisabled)56 HWTEST_F_L0(GCTest, NativeGCTestConcurrentMarkDisabled)
57 {
58     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
59     // Disable concurrent mark.
60     heap->GetConcurrentMarker()->ConfigConcurrentMark(false);
61     size_t oldNativeSize = heap->GetNativeBindingSize();
62     EcmaTestCommon::GcCommonCase(thread, heap, false);
63     const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->CollectGarbage(TriggerGCType::OLD_GC);
64     auto newNativeSize = heap->GetNativeBindingSize();
65     EXPECT_EQ(newNativeSize - oldNativeSize, 0UL);
66 }
67 
HWTEST_F_L0(GCTest,NonNewSpaceNativeGCTestConcurrentMarkDisabled)68 HWTEST_F_L0(GCTest, NonNewSpaceNativeGCTestConcurrentMarkDisabled)
69 {
70     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
71     // Disable concurrent mark.
72     heap->GetConcurrentMarker()->ConfigConcurrentMark(false);
73     size_t oldNativeSize = heap->GetNativeBindingSize();
74     EcmaTestCommon::GcCommonCase(thread, heap);
75     const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->CollectGarbage(TriggerGCType::OLD_GC);
76     auto newNativeSize = heap->GetNativeBindingSize();
77     EXPECT_EQ(newNativeSize - oldNativeSize, 0UL);
78 }
79 
HWTEST_F_L0(GCTest,ArkToolsForceFullGC)80 HWTEST_F_L0(GCTest, ArkToolsForceFullGC)
81 {
82     const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->CollectGarbage(TriggerGCType::FULL_GC);
83     size_t originalHeapSize = thread->GetEcmaVM()->GetHeap()->GetCommittedSize();
84     size_t newSize = originalHeapSize;
85     {
86         [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
87 
88         for (int i = 0; i < 10; i++) {
89             [[maybe_unused]] JSHandle<TaggedArray> obj = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(1024 * 1024);
90         }
91         newSize = thread->GetEcmaVM()->GetHeap()->GetCommittedSize();
92     }
93     EXPECT_TRUE(newSize > originalHeapSize);
94     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 0);
95 
96     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
97     [[maybe_unused]] JSTaggedValue result1 = builtins::BuiltinsArkTools::ForceFullGC(ecmaRuntimeCallInfo);
98 
99     ASSERT_TRUE(thread->GetEcmaVM()->GetHeap()->GetCommittedSize() < newSize);
100 }
101 
HWTEST_F_L0(GCTest,ColdStartForceExpand)102 HWTEST_F_L0(GCTest, ColdStartForceExpand)
103 {
104     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
105     size_t originalHeapSize = heap->GetCommittedSize();
106     heap->GetConcurrentMarker()->ConfigConcurrentMark(false);
107     heap->NotifyPostFork();
108     heap->NotifyFinishColdStartSoon();
109     {
110         [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
111         for (int i = 0; i < 500; i++) {
112             [[maybe_unused]] JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(
113                 10 * 1024, JSTaggedValue::Hole(), MemSpaceType::SEMI_SPACE);
114         }
115     }
116     size_t expandHeapSize = thread->GetEcmaVM()->GetHeap()->GetCommittedSize();
117     usleep(10000000);
118     size_t newSize = EcmaTestCommon::GcCommonCase(thread);
119     EXPECT_TRUE(originalHeapSize < expandHeapSize);
120     EXPECT_TRUE(expandHeapSize > newSize);
121 }
122 
HWTEST_F_L0(GCTest,HighSensitiveForceExpand)123 HWTEST_F_L0(GCTest, HighSensitiveForceExpand)
124 {
125     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
126     size_t originalHeapSize = heap->GetCommittedSize();
127     heap->GetConcurrentMarker()->ConfigConcurrentMark(false);
128     heap->NotifyHighSensitive(true);
129     {
130         [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
131         for (int i = 0; i < 500; i++) {
132             [[maybe_unused]] JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(
133                 10 * 1024, JSTaggedValue::Hole(), MemSpaceType::SEMI_SPACE);
134         }
135     }
136     size_t expandHeapSize = thread->GetEcmaVM()->GetHeap()->GetCommittedSize();
137     const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->NotifyHighSensitive(false);
138     size_t newSize = EcmaTestCommon::GcCommonCase(thread);
139     EXPECT_TRUE(originalHeapSize < expandHeapSize);
140     EXPECT_TRUE(expandHeapSize > newSize);
141 }
142 
HWTEST_F_L0(GCTest,HighSensitiveExceedMaxHeapSize)143 HWTEST_F_L0(GCTest, HighSensitiveExceedMaxHeapSize)
144 {
145     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
146     heap->NotifyHighSensitive(true);
147     // First allocate about 250M TaggedArray, not reach max heap size
148     {
149         [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
150         for (int i = 0; i < 16 * 1000; i++) {
151             [[maybe_unused]] JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(
152                 1024, JSTaggedValue::Hole(), MemSpaceType::SEMI_SPACE);
153         }
154     }
155     // Continue allocate about 250M TaggedArray, now reach max heap size, must trigger gc to avoid OOM
156     {
157         [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
158         for (int i = 0; i < 10 * 1000; i++) {
159             [[maybe_unused]] JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(
160                 1024, JSTaggedValue::Hole(), MemSpaceType::SEMI_SPACE);
161         }
162     }
163     size_t commitSize = thread->GetEcmaVM()->GetHeap()->GetCommittedSize();
164     const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->NotifyHighSensitive(false);
165     EXPECT_TRUE(commitSize < thread->GetEcmaVM()->GetEcmaParamConfiguration().GetMaxHeapSize());
166 }
167 
HWTEST_F_L0(GCTest,ColdStartNoConcurrentMark)168 HWTEST_F_L0(GCTest, ColdStartNoConcurrentMark)
169 {
170     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
171     heap->NotifyPostFork();
172     heap->NotifyHighSensitive(true);
173     {
174         [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
175         for (int i = 0; i < 500; i++) {
176             [[maybe_unused]] JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(
177                 10 * 1024, JSTaggedValue::Hole(), MemSpaceType::SEMI_SPACE);
178         }
179     }
180     EXPECT_FALSE(heap->HandleExitHighSensitiveEvent());
181     heap->NotifyHighSensitive(false);
182     EXPECT_FALSE(heap->HandleExitHighSensitiveEvent());
183     heap->FinishStartupEvent();
184 
185     heap->NotifyHighSensitive(true);
186     heap->NotifyHighSensitive(false);
187     EXPECT_TRUE(heap->HandleExitHighSensitiveEvent());
188 }
189 
HWTEST_F_L0(GCTest,ColdStartGCRestrainInternal)190 HWTEST_F_L0(GCTest, ColdStartGCRestrainInternal)
191 {
192     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
193     heap->NotifyPostFork();
194     heap->NotifyFinishColdStartSoon();
195     std::this_thread::sleep_for(std::chrono::seconds(3));
196     if (!heap->OnStartupEvent()) {
197         StartupStatus startupStatus = heap->GetStartupStatus();
198         EXPECT_TRUE(startupStatus == StartupStatus::JUST_FINISH_STARTUP);
199     }
200 }
201 
HWTEST_F_L0(GCTest,ColdStartGCRestrainExternal)202 HWTEST_F_L0(GCTest, ColdStartGCRestrainExternal)
203 {
204     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
205     heap->NotifyPostFork();
206     heap->NotifyFinishColdStartSoon();
207     std::this_thread::sleep_for(std::chrono::seconds(1));
208     heap->NotifyFinishColdStart(true);
209     EXPECT_FALSE(heap->OnStartupEvent());
210     StartupStatus startupStatus = heap->GetStartupStatus();
211     EXPECT_TRUE(startupStatus == StartupStatus::JUST_FINISH_STARTUP);
212 }
213 
HWTEST_F_L0(GCTest,CallbackTask)214 HWTEST_F_L0(GCTest, CallbackTask)
215 {
216     auto vm = thread->GetEcmaVM();
217     Heap *heap = const_cast<Heap *>(vm->GetHeap());
218     auto factory = vm->GetFactory();
219     {
220         [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
221 
222         for (int i = 0; i < 10; i++) {
223             // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
224             void *externalPointer = malloc(10);
225             [[maybe_unused]] JSHandle<JSNativePointer> nativePointer = factory->NewJSNativePointer(
226                 externalPointer, []([[maybe_unused]] void *env, void* pointer, [[maybe_unused]] void* data) {
227                 if (pointer != nullptr) {
228                     free(pointer);
229                 }
230             },
231             nullptr, false, 10, Concurrent::YES);
232         }
233     }
234     size_t number = heap->concurrentNativePointerList_.size();
235     EXPECT_TRUE(number > 0);
236     heap->CollectGarbage(TriggerGCType::OLD_GC);
237     size_t newNumber = heap->concurrentNativePointerList_.size();
238     EXPECT_TRUE(number > newNumber);
239 }
240 
HWTEST_F_L0(GCTest,RecomputeLimitsTest)241 HWTEST_F_L0(GCTest, RecomputeLimitsTest)
242 {
243     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
244     auto oldCapacity = heap->GetOldSpace()->GetInitialCapacity();
245     heap->CollectGarbage(TriggerGCType::FULL_GC);
246     EXPECT_FALSE(heap->IsConcurrentFullMark());
247     EXPECT_FALSE(heap->IsFullMarkRequested());
248     auto newCapacity = heap->GetOldSpace()->GetInitialCapacity();
249     EXPECT_NE(newCapacity, oldCapacity);
250     double gcSpeed = heap->GetMemController()->CalculateMarkCompactSpeedPerMS();
251     double mutatorSpeed = heap->GetMemController()->GetCurrentOldSpaceAllocationThroughputPerMS();
252     size_t oldSpaceSize = heap->GetOldSpace()->GetHeapObjectSize() + heap->GetHugeObjectSpace()->GetHeapObjectSize() +
253         heap->GetHugeMachineCodeSpace()->GetHeapObjectSize();
254     size_t newSpaceCapacity = heap->GetNewSpace()->GetInitialCapacity();
255     double growingFactor =  heap->GetMemController()->CalculateGrowingFactor(gcSpeed, mutatorSpeed);
256     size_t maxOldSpaceCapacity = heap->GetOldSpace()->GetMaximumCapacity() - newSpaceCapacity;
257     auto newOldSpaceLimit = heap->GetMemController()->CalculateAllocLimit(oldSpaceSize, MIN_OLD_SPACE_LIMIT,
258         maxOldSpaceCapacity, newSpaceCapacity, growingFactor);
259     EXPECT_EQ(newCapacity, newOldSpaceLimit);
260 }
261 
HWTEST_F_L0(GCTest,GlobalNativeSizeLargerThanLimitTest)262 HWTEST_F_L0(GCTest, GlobalNativeSizeLargerThanLimitTest)
263 {
264     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
265     auto ret = heap->GlobalNativeSizeLargerThanLimit();
266     EXPECT_FALSE(ret);
267     heap->GetNativeAreaAllocator()->IncreaseNativeMemoryUsage(300*1000*1000);
268     ret = heap->GlobalNativeSizeLargerThanLimit();
269     EXPECT_TRUE(ret);
270 }
271 #ifdef NDEBUG
HWTEST_F_L0(GCTest,IdleGCTriggerTest)272 HWTEST_F_L0(GCTest, IdleGCTriggerTest)
273 {
274     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
275     auto idleGCTrigger = heap->GetIdleGCTrigger();
276     auto sHeap = SharedHeap::GetInstance();
277     heap->CollectGarbage(TriggerGCType::FULL_GC);
278     int baseLocalGCCount = heap->GetEcmaGCStats()->GetGCCount();
279     int baseSharedGCCount = sHeap->GetEcmaGCStats()->GetGCCount();
280     heap->GetConcurrentMarker()->ConfigConcurrentMark(false);
281     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
282     // Apply for some memory that cannot be released to simulate the actual situation
283     for (int i = 0; i < 5120; i++) {
284         factory->NewTaggedArray(1024, JSTaggedValue::Hole(), MemSpaceType::OLD_SPACE);
285         factory->NewSOldSpaceTaggedArray(1024, JSTaggedValue::Hole());
286     }
287     for (size_t i = 0; i < 10240; i++)
288     {
289         factory->NewTaggedArray(512, JSTaggedValue::Hole(), MemSpaceType::OLD_SPACE);
290         factory->NewSOldSpaceTaggedArray(512, JSTaggedValue::Hole());
291         [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
292         [[maybe_unused]] JSHandle<TaggedArray> array = factory->NewTaggedArray(1024, JSTaggedValue::Hole(),
293                     MemSpaceType::OLD_SPACE);
294         [[maybe_unused]] JSHandle<TaggedArray> sArray = factory->NewSOldSpaceTaggedArray(1024,
295                     JSTaggedValue::Hole());
296         if (i%340 == 0) {
297             idleGCTrigger->NotifyVsyncIdleStart();
298         }
299     }
300     idleGCTrigger->TryTriggerIdleGC(TRIGGER_IDLE_GC_TYPE::FULL_GC);
301     idleGCTrigger->TryTriggerIdleGC(TRIGGER_IDLE_GC_TYPE::SHARED_FULL_GC);
302     int afterLocalGCCount = heap->GetEcmaGCStats()->GetGCCount();
303     int afterSharedGCCount = sHeap->GetEcmaGCStats()->GetGCCount();
304     EXPECT_TRUE(afterLocalGCCount - baseLocalGCCount < 10);
305     EXPECT_TRUE(afterSharedGCCount - baseSharedGCCount < 10);
306     heap->CollectGarbage(TriggerGCType::FULL_GC);
307 }
308 #endif  // #ifndef NDEBUG
309 
HWTEST_F_L0(GCTest,AdjustCapacity)310 HWTEST_F_L0(GCTest, AdjustCapacity)
311 {
312 #if defined(PANDA_TARGET_ARM64)
313     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
314     SemiSpace * space = heap->GetNewSpace();
315 
316     EXPECT_EQ(space->GetSurvivalObjectSize(), 0);
317     {
318         [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
319         for (int i = 0; i < 300; i++) {
320             [[maybe_unused]] JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(
321                 10 * 1024, JSTaggedValue::Hole(), MemSpaceType::SEMI_SPACE);
322         }
323     }
324     EXPECT_GT(space->GetSurvivalObjectSize(), 0);
325 
326     EXPECT_FALSE(space->AdjustCapacity(0, thread));
327     size_t size = space->GetInitialCapacity() * GROW_OBJECT_SURVIVAL_RATE / 2;
328     EXPECT_FALSE(space->AdjustCapacity(size, thread));
329 
330     space->SetInitialCapacity(space->GetSurvivalObjectSize() / GROW_OBJECT_SURVIVAL_RATE - 1);
331     size = space->GetSurvivalObjectSize() / GROW_OBJECT_SURVIVAL_RATE - 1;
332     size_t oldMaxCapacity = space->GetMaximumCapacity();
333     space->SetMaximumCapacity(space->GetInitialCapacity());
334     EXPECT_TRUE(space->AdjustCapacity(size, thread));
335     space->SetMaximumCapacity(oldMaxCapacity);
336     EXPECT_TRUE(space->AdjustCapacity(size, thread));
337 #endif
338 }
339 
HWTEST_F_L0(GCTest,NativeMemAllocInSensitive)340 HWTEST_F_L0(GCTest, NativeMemAllocInSensitive)
341 {
342     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
343     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
344     heap->NotifyHighSensitive(true);
345     for (size_t i = 0; i < 20; i++) {
346         [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
347         factory->NewJSArrayBuffer(300 * 1024 * 1024); // 300MB
348     }
349     EXPECT_TRUE(heap->GetGlobalNativeSize() < 1 * 1024 * 1024* 1024); // 1GB
350 }
351 
HWTEST_F_L0(GCTest,RecordAllocationForIdleTest001)352 HWTEST_F_L0(GCTest, RecordAllocationForIdleTest001)
353 {
354     SharedHeap *heap = SharedHeap::GetInstance();
355     SharedMemController *controller = new SharedMemController(heap);
356     controller->RecordAllocationForIdle();
357     controller->RecordAllocationForIdle();
358 }
359 
HWTEST_F_L0(GCTest,RecordAllocationForIdleTest002)360 HWTEST_F_L0(GCTest, RecordAllocationForIdleTest002)
361 {
362     SharedHeap *heap = SharedHeap::GetInstance();
363     SharedMemController *controller = new SharedMemController(heap);
364     controller->RecordAllocationForIdle();
365     size_t before = heap->GetHeapObjectSize();
366     heap->ReclaimForAppSpawn();
367     size_t after = heap->GetHeapObjectSize();
368     ASSERT_NE(before, after);
369     controller->RecordAllocationForIdle();
370 }
371 
HWTEST_F_L0(GCTest,PrintGCStatisticTest001)372 HWTEST_F_L0(GCTest, PrintGCStatisticTest001)
373 {
374     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
375     int prop = 1 << 15;
376     heap->GetEcmaVM()->GetJSOptions().SetArkProperties(prop);
377     ASSERT_EQ(heap->GetEcmaVM()->GetJSOptions().EnableGCTracer(), true);
378     GCStats *stats = new GCStats(heap);
379     stats->PrintGCStatistic();
380 
381     prop = 1 << 14;
382     heap->GetEcmaVM()->GetJSOptions().SetArkProperties(prop);
383     ASSERT_EQ(heap->GetEcmaVM()->GetJSOptions().EnableGCTracer(), false);
384     stats->PrintGCStatistic();
385 }
386 
HWTEST_F_L0(GCTest,GCReasonToStringTest001)387 HWTEST_F_L0(GCTest, GCReasonToStringTest001)
388 {
389     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
390     GCStats *stats = new GCStats(heap);
391     ASSERT_EQ(strcmp(stats->GCReasonToString(GCReason::SWITCH_BACKGROUND), "Switch to background"), 0);
392     ASSERT_EQ(strcmp(stats->GCReasonToString(GCReason::EXTERNAL_TRIGGER), "Externally triggered"), 0);
393     ASSERT_EQ(strcmp(stats->GCReasonToString(GCReason::WORKER_DESTRUCTION), "Worker Destruction"), 0);
394     ASSERT_EQ(strcmp(stats->GCReasonToString(GCReason::TRIGGER_BY_ARKUI), "Trigger by ArkUI"), 0);
395     ASSERT_EQ(strcmp(stats->GCReasonToString(GCReason::TRIGGER_BY_ABILITY), "Trigger by AbilityRuntime"), 0);
396     ASSERT_EQ(strcmp(stats->GCReasonToString(GCReason::TRIGGER_BY_MEM_TOOLS), "Trigger by Mem tools"), 0);
397     ASSERT_EQ(strcmp(stats->GCReasonToString(GCReason::TRIGGER_BY_TASKPOOL), "Trigger by taskPool"), 0);
398 }
399 
HWTEST_F_L0(GCTest,PrintGCMemoryStatisticTest002)400 HWTEST_F_L0(GCTest, PrintGCMemoryStatisticTest002)
401 {
402     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
403     heap->SetMarkType(MarkType::MARK_YOUNG);
404     GCStats *stats = new GCStats(heap);
405     stats->RecordStatisticBeforeGC(TriggerGCType::YOUNG_GC, GCReason::TRIGGER_BY_ARKUI);
406     stats->PrintGCMemoryStatistic();
407 }
408 
HWTEST_F_L0(GCTest,CheckIfNeedPrintTest001)409 HWTEST_F_L0(GCTest, CheckIfNeedPrintTest001)
410 {
411     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
412     heap->SetMarkType(MarkType::MARK_YOUNG);
413     GCStats *stats = new GCStats(heap);
414     stats->SetRecordData(RecordData::YOUNG_COUNT, 1);
415     stats->PrintStatisticResult();
416 }
417 
HWTEST_F_L0(GCTest,PrintGCSummaryStatisticTest001)418 HWTEST_F_L0(GCTest, PrintGCSummaryStatisticTest001)
419 {
420     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
421     heap->SetMarkType(MarkType::MARK_YOUNG);
422     GCStats *stats = new GCStats(heap);
423     stats->PrintStatisticResult();
424 }
425 
HWTEST_F_L0(GCTest,CalculateGrowingFactorTest001)426 HWTEST_F_L0(GCTest, CalculateGrowingFactorTest001)
427 {
428     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
429     heap->SetMemGrowingType(MemGrowingType::CONSERVATIVE);
430     auto controller = new MemController(heap);
431     controller->CalculateGrowingFactor(1, 1);
432 }
433 
HWTEST_F_L0(GCTest,CalculateGrowingFactorTest002)434 HWTEST_F_L0(GCTest, CalculateGrowingFactorTest002)
435 {
436     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
437     heap->SetMemGrowingType(MemGrowingType::PRESSURE);
438     auto controller = new MemController(heap);
439     controller->CalculateGrowingFactor(1, 1);
440 }
441 
HWTEST_F_L0(GCTest,CalculateGrowingFactorTest003)442 HWTEST_F_L0(GCTest, CalculateGrowingFactorTest003)
443 {
444     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
445     heap->SetMemGrowingType(MemGrowingType::CONSERVATIVE);
446     auto controller = new MemController(heap);
447     controller->CalculateGrowingFactor(1, 0);
448 }
449 
HWTEST_F_L0(GCTest,StopCalculationAfterGCTest001)450 HWTEST_F_L0(GCTest, StopCalculationAfterGCTest001)
451 {
452     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
453     auto controller = new MemController(heap);
454     std::this_thread::sleep_for(std::chrono::milliseconds(10));
455     controller->StartCalculationBeforeGC();
456     controller->StopCalculationAfterGC(TriggerGCType::YOUNG_GC);
457 }
458 
HWTEST_F_L0(GCTest,RecordAllocationForIdleTest003)459 HWTEST_F_L0(GCTest, RecordAllocationForIdleTest003)
460 {
461     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
462     auto controller = new MemController(heap);
463     controller->RecordAllocationForIdle();
464 }
465 
HWTEST_F_L0(GCTest,TryTriggerIdleCollectionTest001)466 HWTEST_F_L0(GCTest, TryTriggerIdleCollectionTest001)
467 {
468     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
469     heap->SetIdleTask(IdleTaskType::YOUNG_GC);
470     heap->TryTriggerIdleCollection();
471 }
472 
HWTEST_F_L0(GCTest,WaitAllTasksFinishedTest001)473 HWTEST_F_L0(GCTest, WaitAllTasksFinishedTest001)
474 {
475     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
476     heap->GetJSThread()->SetMarkStatus(MarkStatus::MARKING);
477     heap->WaitAllTasksFinished();
478 }
479 
HWTEST_F_L0(GCTest,WaitAllTasksFinishedTest002)480 HWTEST_F_L0(GCTest, WaitAllTasksFinishedTest002)
481 {
482     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
483     heap->GetJSThread()->SetMarkStatus(MarkStatus::MARKING);
484     heap->GetConcurrentMarker()->Mark();
485     heap->WaitAllTasksFinished();
486 }
487 
HWTEST_F_L0(GCTest,ChangeGCParamsTest001)488 HWTEST_F_L0(GCTest, ChangeGCParamsTest001)
489 {
490     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
491     heap->GetOldSpace()->IncreaseLiveObjectSize(2098000);
492     heap->ChangeGCParams(true);
493 }
494 
HWTEST_F_L0(GCTest,ChangeGCParamsTest002)495 HWTEST_F_L0(GCTest, ChangeGCParamsTest002)
496 {
497     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
498     heap->GetOldSpace()->IncreaseLiveObjectSize(2098000);
499     heap->GetOldSpace()->IncreaseCommitted(31457300);
500     heap->ChangeGCParams(true);
501 }
502 
HWTEST_F_L0(GCTest,ChangeGCParamsTest003)503 HWTEST_F_L0(GCTest, ChangeGCParamsTest003)
504 {
505     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
506     auto sHeap = SharedHeap::GetInstance();
507     sHeap->GetOldSpace()->IncreaseLiveObjectSize(2098000);
508     heap->ChangeGCParams(true);
509 }
510 
HWTEST_F_L0(GCTest,ChangeGCParamsTest004)511 HWTEST_F_L0(GCTest, ChangeGCParamsTest004)
512 {
513     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
514     heap->SetMemGrowingType(MemGrowingType::HIGH_THROUGHPUT);
515     heap->ChangeGCParams(true);
516 }
517 
HWTEST_F_L0(GCTest,IncrementMarkerTest001)518 HWTEST_F_L0(GCTest, IncrementMarkerTest001)
519 {
520     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
521     heap->GetIncrementalMarker()->TriggerIncrementalMark(100);
522     heap->GetIncrementalMarker()->TriggerIncrementalMark(100);
523 }
524 
HWTEST_F_L0(GCTest,IncrementMarkerTest002)525 HWTEST_F_L0(GCTest, IncrementMarkerTest002)
526 {
527     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
528     heap->GetIncrementalMarker()->TriggerIncrementalMark(0);
529 }
530 
HWTEST_F_L0(GCTest,IncrementMarkerTest003)531 HWTEST_F_L0(GCTest, IncrementMarkerTest003)
532 {
533     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
534     heap->GetIncrementalMarker()->SetMarkingFinished(true);
535     heap->GetIncrementalMarker()->TriggerIncrementalMark(100);
536 }
537 
HWTEST_F_L0(GCTest,IncrementMarkerTest004)538 HWTEST_F_L0(GCTest, IncrementMarkerTest004)
539 {
540     thread->GetEcmaVM()->GetJSOptions().SetArkProperties(0);
541     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
542     heap->GetIncrementalMarker()->SetMarkingFinished(true);
543     heap->GetIncrementalMarker()->TriggerIncrementalMark(100);
544 }
545 
546 } // namespace panda::test
547