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