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
16 #include "ecmascript/builtins/builtins_ark_tools.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/mem/full_gc.h"
19 #include "ecmascript/object_factory-inl.h"
20 #include "ecmascript/mem/concurrent_marker.h"
21 #include "ecmascript/mem/stw_young_gc.h"
22 #include "ecmascript/mem/partial_gc.h"
23 #include "ecmascript/tests/test_helper.h"
24
25 using namespace panda;
26
27 using namespace panda::ecmascript;
28
29 namespace panda::test {
30 class GCTest : public testing::Test {
31 public:
SetUpTestCase()32 static void SetUpTestCase()
33 {
34 GTEST_LOG_(INFO) << "SetUpTestCase";
35 }
36
TearDownTestCase()37 static void TearDownTestCase()
38 {
39 GTEST_LOG_(INFO) << "TearDownCase";
40 }
41
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 scope = new EcmaHandleScope(thread);
49 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
50 heap->GetConcurrentMarker()->EnableConcurrentMarking(EnableConcurrentMarkType::ENABLE);
51 heap->GetSweeper()->EnableConcurrentSweep(EnableConcurrentSweepType::ENABLE);
52 }
53
TearDown()54 void TearDown() override
55 {
56 TestHelper::DestroyEcmaVMWithScope(instance, scope);
57 }
58
59 EcmaVM *instance {nullptr};
60 ecmascript::EcmaHandleScope *scope {nullptr};
61 JSThread *thread {nullptr};
62 };
63
HWTEST_F_L0(GCTest,NativeGCTestConcurrentMarkDisabled)64 HWTEST_F_L0(GCTest, NativeGCTestConcurrentMarkDisabled)
65 {
66 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
67 // Disable concurrent mark.
68 heap->GetConcurrentMarker()->ConfigConcurrentMark(false);
69 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
70 size_t oldNativeSize = heap->GetNativeBindingSize();
71 size_t newNativeSize = heap->GetNativeBindingSize();
72 {
73 [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
74
75 auto newData = thread->GetEcmaVM()->GetNativeAreaAllocator()->AllocateBuffer(1 * 1024 * 1024);
76 [[maybe_unused]] JSHandle<JSNativePointer> obj = factory->NewJSNativePointer(newData,
77 NativeAreaAllocator::FreeBufferFunc, nullptr, false, 1 * 1024 * 1024);
78 newNativeSize = heap->GetNativeBindingSize();
79 EXPECT_EQ(newNativeSize - oldNativeSize, 1UL * 1024 * 1024);
80
81 auto newData1 = thread->GetEcmaVM()->GetNativeAreaAllocator()->AllocateBuffer(1 * 1024 * 1024);
82 [[maybe_unused]] JSHandle<JSNativePointer> obj2 = factory->NewJSNativePointer(newData1,
83 NativeAreaAllocator::FreeBufferFunc, nullptr, false, 1 * 1024 * 1024);
84
85 EXPECT_TRUE(newNativeSize - oldNativeSize > 0);
86 EXPECT_TRUE(newNativeSize - oldNativeSize <= 2 * 1024 *1024);
87 for (int i = 0; i < 2048; i++) {
88 [[maybe_unused]] ecmascript::EcmaHandleScope baseScopeForeach(thread);
89 auto newData2 = thread->GetEcmaVM()->GetNativeAreaAllocator()->AllocateBuffer(1 * 1024);
90 [[maybe_unused]] JSHandle<JSNativePointer> obj3 = factory->NewJSNativePointer(newData2,
91 NativeAreaAllocator::FreeBufferFunc, nullptr, false, 1 * 1024 * 1024);
92 }
93 }
94 const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->CollectGarbage(TriggerGCType::OLD_GC);
95 newNativeSize = heap->GetNativeBindingSize();
96 EXPECT_EQ(newNativeSize - oldNativeSize, 0UL);
97 }
98
HWTEST_F_L0(GCTest,NonNewSpaceNativeGCTestConcurrentMarkDisabled)99 HWTEST_F_L0(GCTest, NonNewSpaceNativeGCTestConcurrentMarkDisabled)
100 {
101 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
102 // Disable concurrent mark.
103 heap->GetConcurrentMarker()->ConfigConcurrentMark(false);
104 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
105 size_t oldNativeSize = heap->GetNativeBindingSize();
106 size_t newNativeSize = heap->GetNativeBindingSize();
107 {
108 [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
109 auto newData = thread->GetEcmaVM()->GetNativeAreaAllocator()->AllocateBuffer(1 * 1024 * 1024);
110 [[maybe_unused]] JSHandle<JSNativePointer> obj = factory->NewJSNativePointer(newData,
111 NativeAreaAllocator::FreeBufferFunc, nullptr, true, 1 * 1024 * 1024);
112 newNativeSize = heap->GetNativeBindingSize();
113 EXPECT_EQ(newNativeSize - oldNativeSize, 1UL * 1024 * 1024);
114
115 auto newData1 = thread->GetEcmaVM()->GetNativeAreaAllocator()->AllocateBuffer(1 * 1024 * 1024);
116 [[maybe_unused]] JSHandle<JSNativePointer> obj2 = factory->NewJSNativePointer(newData1,
117 NativeAreaAllocator::FreeBufferFunc, nullptr, false, 1 * 1024 * 1024);
118
119 EXPECT_TRUE(newNativeSize - oldNativeSize > 0);
120 EXPECT_TRUE(newNativeSize - oldNativeSize <= 2 * 1024 *1024);
121 for (int i = 0; i < 2048; i++) {
122 [[maybe_unused]] ecmascript::EcmaHandleScope baseScopeForeach(thread);
123 auto newData2 = thread->GetEcmaVM()->GetNativeAreaAllocator()->AllocateBuffer(1 * 1024);
124 // malloc size is smaller to avoid test fail in the small devices.
125 [[maybe_unused]] JSHandle<JSNativePointer> obj3 = factory->NewJSNativePointer(newData2,
126 NativeAreaAllocator::FreeBufferFunc, nullptr, true, 1 * 1024 * 1024);
127 }
128 newNativeSize = heap->GetNativeBindingSize();
129 // Old GC should be trigger here, so the size should be reduced.
130 EXPECT_TRUE(newNativeSize - oldNativeSize < 2048 * 1024 *1024);
131 }
132 const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->CollectGarbage(TriggerGCType::OLD_GC);
133 newNativeSize = heap->GetNativeBindingSize();
134 EXPECT_EQ(newNativeSize - oldNativeSize, 0UL);
135 }
136
HWTEST_F_L0(GCTest,ArkToolsForceFullGC)137 HWTEST_F_L0(GCTest, ArkToolsForceFullGC)
138 {
139 const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->CollectGarbage(TriggerGCType::FULL_GC);
140 size_t originalHeapSize = thread->GetEcmaVM()->GetHeap()->GetCommittedSize();
141 size_t newSize = originalHeapSize;
142 {
143 [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
144
145 for (int i = 0; i < 10; i++) {
146 [[maybe_unused]] JSHandle<TaggedArray> obj = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(1024 * 1024);
147 }
148 newSize = thread->GetEcmaVM()->GetHeap()->GetCommittedSize();
149 }
150 EXPECT_TRUE(newSize > originalHeapSize);
151 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 0);
152
153 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
154 [[maybe_unused]] JSTaggedValue result1 = builtins::BuiltinsArkTools::ForceFullGC(ecmaRuntimeCallInfo);
155
156 ASSERT_TRUE(thread->GetEcmaVM()->GetHeap()->GetCommittedSize() < newSize);
157 }
158
HWTEST_F_L0(GCTest,ColdStartForceExpand)159 HWTEST_F_L0(GCTest, ColdStartForceExpand)
160 {
161 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
162 size_t originalHeapSize = heap->GetCommittedSize();
163 heap->GetConcurrentMarker()->ConfigConcurrentMark(false);
164 heap->NotifyPostFork();
165 heap->NotifyFinishColdStartSoon();
166 {
167 [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
168 for (int i = 0; i < 500; i++) {
169 [[maybe_unused]] JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(
170 10 * 1024, JSTaggedValue::Hole(), MemSpaceType::SEMI_SPACE);
171 }
172 }
173 size_t expandHeapSize = thread->GetEcmaVM()->GetHeap()->GetCommittedSize();
174 usleep(2500000);
175 {
176 [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
177 for (int i = 0; i < 100; i++) {
178 [[maybe_unused]] JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(
179 10 * 1024, JSTaggedValue::Hole(), MemSpaceType::SEMI_SPACE);
180 }
181 }
182 size_t newSize = thread->GetEcmaVM()->GetHeap()->GetCommittedSize();
183 EXPECT_TRUE(originalHeapSize < expandHeapSize);
184 EXPECT_TRUE(expandHeapSize > newSize);
185 }
186 } // namespace panda::test
187