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/mem/full_gc-inl.h"
17
18 #include "common_components/taskpool/taskpool.h"
19 #include "ecmascript/js_weak_container.h"
20 #include "ecmascript/linked_hash_table.h"
21 #include "ecmascript/mem/concurrent_marker.h"
22 #include "ecmascript/mem/incremental_marker.h"
23 #include "ecmascript/mem/parallel_marker.h"
24 #include "ecmascript/mem/verification.h"
25 #include "ecmascript/runtime_call_id.h"
26
27 namespace panda::ecmascript {
FullGC(Heap * heap)28 FullGC::FullGC(Heap *heap) : heap_(heap), workManager_(heap->GetWorkManager()) {}
29
RunPhases()30 void FullGC::RunPhases()
31 {
32 ASSERT("FullGC should be disabled" && !g_isEnableCMCGC);
33 GCStats *gcStats = heap_->GetEcmaVM()->GetEcmaGCStats();
34 ECMA_BYTRACE_NAME(HITRACE_LEVEL_COMMERCIAL, HITRACE_TAG_ARK, ("FullGC::RunPhases;GCReason"
35 + std::to_string(static_cast<int>(gcStats->GetGCReason()))
36 + ";Sensitive" + std::to_string(static_cast<int>(heap_->GetSensitiveStatus()))
37 + ";IsInBackground" + std::to_string(heap_->IsInBackground())
38 + ";Startup" + std::to_string(static_cast<int>(heap_->GetStartupStatus()))
39 + ";Young" + std::to_string(heap_->GetNewSpace()->GetCommittedSize())
40 + ";Old" + std::to_string(heap_->GetOldSpace()->GetCommittedSize())
41 + ";huge" + std::to_string(heap_->GetHugeObjectSpace()->GetCommittedSize())
42 + ";NonMov" + std::to_string(heap_->GetNonMovableSpace()->GetCommittedSize())
43 + ";TotCommit" + std::to_string(heap_->GetCommittedSize())
44 + ";ObjSizeBeforeSensitive"
45 + std::to_string(heap_->GetRecordHeapObjectSizeBeforeSensitive())).c_str(), "");
46 TRACE_GC(GCStats::Scope::ScopeId::TotalGC, gcStats);
47 MEM_ALLOCATE_AND_GC_TRACE(heap_->GetEcmaVM(), FullGC_RunPhases);
48
49 if (heap_->CheckOngoingConcurrentMarking()) {
50 LOG_GC(DEBUG) << "FullGC after ConcurrentMarking";
51 heap_->GetConcurrentMarker()->Reset(); // HPPGC use mark result to move TaggedObject.
52 }
53
54 if (heap_->GetIncrementalMarker()->IsTriggeredIncrementalMark()) {
55 LOG_GC(DEBUG) << "FullGC after IncrementalMarking";
56 heap_->ClearIdleTask();
57 heap_->DisableNotifyIdle();
58 heap_->GetIncrementalMarker()->Reset();
59 }
60 ProcessSharedGCRSetWorkList();
61 Initialize();
62 Mark();
63 Sweep();
64 Finish();
65 if (UNLIKELY(heap_->ShouldVerifyHeap())) {
66 // verify mark
67 LOG_ECMA(DEBUG) << "start verify post fullgc";
68 Verification(heap_, VerifyKind::VERIFY_SHARED_RSET_POST_FULL_GC).VerifyAll();
69 }
70 }
71
RunPhasesForAppSpawn()72 void FullGC::RunPhasesForAppSpawn()
73 {
74 auto marker = reinterpret_cast<CompressGCMarker*>(heap_->GetCompressGCMarker());
75 marker->SetAppSpawn(true);
76 RunPhases();
77 marker->SetAppSpawn(false);
78 }
79
Initialize()80 void FullGC::Initialize()
81 {
82 ECMA_BYTRACE_NAME(HITRACE_LEVEL_COMMERCIAL, HITRACE_TAG_ARK, "FullGC::Initialize", "");
83 TRACE_GC(GCStats::Scope::ScopeId::Initialize, heap_->GetEcmaVM()->GetEcmaGCStats());
84 heap_->Prepare();
85 auto callback = [](Region *current) {
86 current->ResetAliveObject();
87 current->ClearOldToNewRSet();
88 };
89 heap_->EnumerateNonMovableRegions(callback);
90 heap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) {
91 current->ClearMarkGCBitset();
92 current->ClearCrossRegionRSet();
93 });
94 youngSpaceCommitSize_ = heap_->GetNewSpace()->GetCommittedSize();
95 heap_->SwapNewSpace();
96 workManager_->Initialize(TriggerGCType::FULL_GC, ParallelGCTaskPhase::COMPRESS_HANDLE_GLOBAL_POOL_TASK);
97 heap_->GetCompressGCMarker()->Initialize();
98
99 youngAndOldAliveSize_ = 0;
100 nonMoveSpaceFreeSize_ = 0;
101 oldSpaceCommitSize_ = heap_->GetOldSpace()->GetCommittedSize();
102 nonMoveSpaceCommitSize_ = heap_->GetNonMovableSpace()->GetCommittedSize();
103 }
104
MarkRoots()105 void FullGC::MarkRoots()
106 {
107 CompressGCMarker *marker = static_cast<CompressGCMarker*>(heap_->GetCompressGCMarker());
108 FullGCRunner fullGCRunner(heap_, workManager_->GetWorkNodeHolder(MAIN_THREAD_INDEX), forAppSpawn_);
109 FullGCMarkRootVisitor &fullGCMarkRootVisitor = fullGCRunner.GetMarkRootVisitor();
110 marker->MarkRoots(fullGCMarkRootVisitor);
111 }
112
Mark()113 void FullGC::Mark()
114 {
115 ECMA_BYTRACE_NAME(HITRACE_LEVEL_COMMERCIAL, HITRACE_TAG_ARK, "FullGC::Mark", "");
116 TRACE_GC(GCStats::Scope::ScopeId::Mark, heap_->GetEcmaVM()->GetEcmaGCStats());
117 MarkRoots();
118 heap_->GetCompressGCMarker()->ProcessMarkStack(MAIN_THREAD_INDEX);
119 heap_->WaitRunningTaskFinished();
120 // MarkJitCodeMap must be call after other mark work finish to make sure which jserror object js alive.
121 heap_->GetCompressGCMarker()->MarkJitCodeMap(MAIN_THREAD_INDEX);
122 }
123
Sweep()124 void FullGC::Sweep()
125 {
126 ECMA_BYTRACE_NAME(HITRACE_LEVEL_COMMERCIAL, HITRACE_TAG_ARK, "FullGC::Sweep", "");
127 TRACE_GC(GCStats::Scope::ScopeId::Sweep, heap_->GetEcmaVM()->GetEcmaGCStats());
128 // process weak reference
129 uint32_t totalThreadCount = 1; // 1 : mainthread
130 if (heap_->IsParallelGCEnabled()) {
131 totalThreadCount += common::Taskpool::GetCurrentTaskpool()->GetTotalThreadNum();
132 }
133 for (uint32_t i = 0; i < totalThreadCount; i++) {
134 UpdateRecordWeakReference(i);
135 }
136 for (uint32_t i = 0; i < totalThreadCount; i++) {
137 UpdateRecordJSWeakMap(i);
138 }
139
140 WeakRootVisitor gcUpdateWeak = [this](TaggedObject *header) -> TaggedObject* {
141 Region *objectRegion = Region::ObjectAddressToRange(header);
142 if (UNLIKELY(objectRegion == nullptr)) {
143 LOG_GC(ERROR) << "FullGC updateWeakReference: region is nullptr, header is " << header;
144 return nullptr;
145 }
146 if (!HasEvacuated(objectRegion)) {
147 // The weak object in shared heap is always alive during fullGC.
148 if (objectRegion->InSharedHeap() || objectRegion->Test(header)) {
149 return header;
150 }
151 return nullptr;
152 }
153
154 MarkWord markWord(header);
155 if (markWord.IsForwardingAddress()) {
156 return markWord.ToForwardingAddress();
157 }
158 return nullptr;
159 };
160 heap_->GetEcmaVM()->GetJSThread()->IterateWeakEcmaGlobalStorage(gcUpdateWeak);
161 heap_->GetEcmaVM()->ProcessReferences(gcUpdateWeak);
162 heap_->GetEcmaVM()->ProcessSnapShotEnv(gcUpdateWeak);
163 heap_->GetEcmaVM()->GetJSThread()->UpdateJitCodeMapReference(gcUpdateWeak);
164
165 heap_->GetSweeper()->Sweep(true);
166 heap_->GetSweeper()->PostTask(true);
167 }
168
Finish()169 void FullGC::Finish()
170 {
171 ECMA_BYTRACE_NAME(HITRACE_LEVEL_COMMERCIAL, HITRACE_TAG_ARK, "FullGC::Finish", "");
172 TRACE_GC(GCStats::Scope::ScopeId::Finish, heap_->GetEcmaVM()->GetEcmaGCStats());
173 if (!forAppSpawn_) {
174 heap_->SwapOldSpace();
175 }
176 youngAndOldAliveSize_ = workManager_->Finish();
177 if (forAppSpawn_) {
178 heap_->ResumeForAppSpawn();
179 } else {
180 heap_->Resume(FULL_GC);
181 }
182 heap_->GetSweeper()->TryFillSweptRegion();
183 heap_->SetFullMarkRequestedState(false);
184 }
185
HasEvacuated(Region * region)186 bool FullGC::HasEvacuated(Region *region)
187 {
188 if (forAppSpawn_) {
189 return !region->InHugeObjectSpace() && !region->InReadOnlySpace() && !region->InNonMovableSpace() &&
190 !region->InSharedHeap();
191 }
192 return region->InYoungOrOldSpace();
193 }
194
SetForAppSpawn(bool flag)195 void FullGC::SetForAppSpawn(bool flag)
196 {
197 forAppSpawn_ = flag;
198 }
199
ProcessSharedGCRSetWorkList()200 ARK_INLINE void FullGC::ProcessSharedGCRSetWorkList()
201 {
202 TRACE_GC(GCStats::Scope::ScopeId::ProcessSharedGCRSetWorkList, heap_->GetEcmaVM()->GetEcmaGCStats());
203 heap_->ProcessSharedGCRSetWorkList();
204 }
205
UpdateRecordWeakReference(uint32_t threadId)206 void FullGC::UpdateRecordWeakReference(uint32_t threadId)
207 {
208 ProcessQueue *queue = workManager_->GetWorkNodeHolder(threadId)->GetWeakReferenceQueue();
209 while (true) {
210 auto obj = queue->PopBack();
211 if (UNLIKELY(obj == nullptr)) {
212 break;
213 }
214 ObjectSlot slot(ToUintPtr(obj));
215 JSTaggedValue value(slot.GetTaggedType());
216 auto header = value.GetTaggedWeakRef();
217 Region *objectRegion = Region::ObjectAddressToRange(header);
218 if (!HasEvacuated(objectRegion)) {
219 if (!objectRegion->InSharedHeap() && !objectRegion->Test(header)) {
220 slot.Clear();
221 }
222 } else {
223 MarkWord markWord(header);
224 if (markWord.IsForwardingAddress()) {
225 TaggedObject *dst = markWord.ToForwardingAddress();
226 auto weakRef = JSTaggedValue(JSTaggedValue(dst).CreateAndGetWeakRef()).GetRawTaggedObject();
227 slot.Update(weakRef);
228 } else {
229 slot.Update(static_cast<JSTaggedType>(JSTaggedValue::Undefined().GetRawData()));
230 }
231 }
232 }
233 }
234
UpdateRecordJSWeakMap(uint32_t threadId)235 void FullGC::UpdateRecordJSWeakMap(uint32_t threadId)
236 {
237 std::function<bool(JSTaggedValue)> visitor = [this](JSTaggedValue key) {
238 ASSERT(!key.IsHole());
239 return key.IsUndefined(); // Dead key, and set to undefined by GC
240 };
241
242 JSWeakMapProcessQueue *queue = workManager_->GetWorkNodeHolder(threadId)->GetJSWeakMapQueue();
243 while (true) {
244 TaggedObject *obj = queue->PopBack();
245 if (UNLIKELY(obj == nullptr)) {
246 break;
247 }
248 JSWeakMap *weakMap = JSWeakMap::Cast(obj);
249 JSThread *thread = heap_->GetJSThread();
250 JSTaggedValue maybeMap = weakMap->GetLinkedMap(thread);
251 if (maybeMap.IsUndefined()) {
252 continue;
253 }
254 LinkedHashMap *map = LinkedHashMap::Cast(maybeMap.GetTaggedObject());
255 map->ClearAllDeadEntries(thread, visitor);
256 }
257 }
258 } // namespace panda::ecmascript
259