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.h"
17
18 #include "ecmascript/mem/concurrent_marker.h"
19 #include "ecmascript/mem/incremental_marker.h"
20 #include "ecmascript/mem/parallel_marker-inl.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(heap_->OnStartupEvent())
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
Mark()99 void FullGC::Mark()
100 {
101 ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Mark");
102 TRACE_GC(GCStats::Scope::ScopeId::Mark, heap_->GetEcmaVM()->GetEcmaGCStats());
103 heap_->GetCompressGCMarker()->MarkRoots(MAIN_THREAD_INDEX, VMRootVisitType::UPDATE_ROOT);
104 heap_->GetCompressGCMarker()->ProcessMarkStack(MAIN_THREAD_INDEX);
105 heap_->WaitRunningTaskFinished();
106 // MarkJitCodeMap must be call after other mark work finish to make sure which jserror object js alive.
107 heap_->GetCompressGCMarker()->MarkJitCodeMap(MAIN_THREAD_INDEX);
108 }
109
Sweep()110 void FullGC::Sweep()
111 {
112 ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Sweep");
113 TRACE_GC(GCStats::Scope::ScopeId::Sweep, heap_->GetEcmaVM()->GetEcmaGCStats());
114 // process weak reference
115 uint32_t totalThreadCount = 1; // 1 : mainthread
116 if (heap_->IsParallelGCEnabled()) {
117 totalThreadCount += Taskpool::GetCurrentTaskpool()->GetTotalThreadNum();
118 }
119 for (uint32_t i = 0; i < totalThreadCount; i++) {
120 ProcessQueue *queue = workManager_->GetWeakReferenceQueue(i);
121
122 while (true) {
123 auto obj = queue->PopBack();
124 if (UNLIKELY(obj == nullptr)) {
125 break;
126 }
127 ObjectSlot slot(ToUintPtr(obj));
128 JSTaggedValue value(slot.GetTaggedType());
129 auto header = value.GetTaggedWeakRef();
130
131 Region *objectRegion = Region::ObjectAddressToRange(header);
132 if (!HasEvacuated(objectRegion)) {
133 if (!objectRegion->InSharedHeap() && !objectRegion->Test(header)) {
134 slot.Clear();
135 }
136 } else {
137 MarkWord markWord(header);
138 if (markWord.IsForwardingAddress()) {
139 TaggedObject *dst = markWord.ToForwardingAddress();
140 auto weakRef = JSTaggedValue(JSTaggedValue(dst).CreateAndGetWeakRef()).GetRawTaggedObject();
141 slot.Update(weakRef);
142 } else {
143 slot.Update(static_cast<JSTaggedType>(JSTaggedValue::Undefined().GetRawData()));
144 }
145 }
146 }
147 }
148
149 WeakRootVisitor gcUpdateWeak = [this](TaggedObject *header) -> TaggedObject* {
150 Region *objectRegion = Region::ObjectAddressToRange(header);
151 if (UNLIKELY(objectRegion == nullptr)) {
152 LOG_GC(ERROR) << "FullGC updateWeakReference: region is nullptr, header is " << header;
153 return nullptr;
154 }
155 if (!HasEvacuated(objectRegion)) {
156 // The weak object in shared heap is always alive during fullGC.
157 if (objectRegion->InSharedHeap() || objectRegion->Test(header)) {
158 return header;
159 }
160 return nullptr;
161 }
162
163 MarkWord markWord(header);
164 if (markWord.IsForwardingAddress()) {
165 return markWord.ToForwardingAddress();
166 }
167 return nullptr;
168 };
169 heap_->GetEcmaVM()->GetJSThread()->IterateWeakEcmaGlobalStorage(gcUpdateWeak);
170 heap_->GetEcmaVM()->ProcessReferences(gcUpdateWeak);
171
172 heap_->GetSweeper()->Sweep(true);
173 heap_->GetSweeper()->PostTask(true);
174 }
175
Finish()176 void FullGC::Finish()
177 {
178 ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Finish");
179 TRACE_GC(GCStats::Scope::ScopeId::Finish, heap_->GetEcmaVM()->GetEcmaGCStats());
180 if (!forAppSpawn_) {
181 heap_->SwapOldSpace();
182 }
183 youngAndOldAliveSize_ = workManager_->Finish();
184 if (forAppSpawn_) {
185 heap_->ResumeForAppSpawn();
186 } else {
187 heap_->Resume(FULL_GC);
188 }
189 heap_->GetSweeper()->TryFillSweptRegion();
190 }
191
HasEvacuated(Region *region)192 bool FullGC::HasEvacuated(Region *region)
193 {
194 auto marker = reinterpret_cast<CompressGCMarker*>(heap_->GetCompressGCMarker());
195 return marker->NeedEvacuate(region);
196 }
197
SetForAppSpawn(bool flag)198 void FullGC::SetForAppSpawn(bool flag)
199 {
200 forAppSpawn_ = flag;
201 }
202
ProcessSharedGCRSetWorkList()203 ARK_INLINE void FullGC::ProcessSharedGCRSetWorkList()
204 {
205 TRACE_GC(GCStats::Scope::ScopeId::ProcessSharedGCRSetWorkList, heap_->GetEcmaVM()->GetEcmaGCStats());
206 heap_->ProcessSharedGCRSetWorkList();
207 }
208 } // namespace panda::ecmascript
209