• 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 
16 #include "ecmascript/mem/full_gc.h"
17 
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/mem/clock_scope.h"
20 #include "ecmascript/mem/concurrent_marker.h"
21 #include "ecmascript/mem/mark_stack.h"
22 #include "ecmascript/mem/mem.h"
23 #include "ecmascript/mem/parallel_marker-inl.h"
24 #include "ecmascript/mem/space-inl.h"
25 #include "ecmascript/mem/visitor.h"
26 #include "ecmascript/mem/gc_stats.h"
27 #include "ecmascript/ecma_string_table.h"
28 #include "ecmascript/runtime_call_id.h"
29 
30 namespace panda::ecmascript {
FullGC(Heap * heap)31 FullGC::FullGC(Heap *heap) : heap_(heap), workManager_(heap->GetWorkManager()) {}
32 
RunPhases()33 void FullGC::RunPhases()
34 {
35     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::RunPhases");
36     TRACE_GC(GCStats::Scope::ScopeId::TotalGC, heap_->GetEcmaVM()->GetEcmaGCStats());
37     MEM_ALLOCATE_AND_GC_TRACE(heap_->GetEcmaVM(), FullGC_RunPhases);
38 
39     if (heap_->CheckOngoingConcurrentMarking()) {
40         LOG_GC(DEBUG) << "FullGC after ConcurrentMarking";
41         heap_->GetConcurrentMarker()->Reset();  // HPPGC use mark result to move TaggedObject.
42     }
43     Initialize();
44     Mark();
45     Sweep();
46     Finish();
47     heap_->NotifyHeapAliveSizeAfterGC(heap_->GetHeapObjectSize());
48 }
49 
RunPhasesForAppSpawn()50 void FullGC::RunPhasesForAppSpawn()
51 {
52     auto marker = reinterpret_cast<CompressGCMarker*>(heap_->GetCompressGCMarker());
53     marker->SetAppSpawn(true);
54     RunPhases();
55     marker->SetAppSpawn(false);
56 }
57 
Initialize()58 void FullGC::Initialize()
59 {
60     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Initialize");
61     TRACE_GC(GCStats::Scope::ScopeId::Initialize, heap_->GetEcmaVM()->GetEcmaGCStats());
62     heap_->Prepare();
63     auto callback = [](Region *current) {
64         current->ResetAliveObject();
65         current->ClearOldToNewRSet();
66     };
67     heap_->EnumerateNonMovableRegions(callback);
68     heap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) {
69         current->ClearMarkGCBitset();
70         current->ClearCrossRegionRSet();
71     });
72     youngSpaceCommitSize_ = heap_->GetNewSpace()->GetCommittedSize();
73     heap_->SwapNewSpace();
74     workManager_->Initialize(TriggerGCType::FULL_GC, ParallelGCTaskPhase::COMPRESS_HANDLE_GLOBAL_POOL_TASK);
75     heap_->GetCompressGCMarker()->Initialize();
76 
77     youngAndOldAliveSize_ = 0;
78     nonMoveSpaceFreeSize_ = 0;
79     oldSpaceCommitSize_ = heap_->GetOldSpace()->GetCommittedSize();
80     nonMoveSpaceCommitSize_ = heap_->GetNonMovableSpace()->GetCommittedSize();
81 }
82 
Mark()83 void FullGC::Mark()
84 {
85     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Mark");
86     TRACE_GC(GCStats::Scope::ScopeId::Mark, heap_->GetEcmaVM()->GetEcmaGCStats());
87     heap_->GetCompressGCMarker()->MarkRoots(MAIN_THREAD_INDEX);
88     heap_->GetCompressGCMarker()->ProcessMarkStack(MAIN_THREAD_INDEX);
89     heap_->WaitRunningTaskFinished();
90 }
91 
Sweep()92 void FullGC::Sweep()
93 {
94     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Sweep");
95     TRACE_GC(GCStats::Scope::ScopeId::Sweep, heap_->GetEcmaVM()->GetEcmaGCStats());
96     // process weak reference
97     uint32_t totalThreadCount = 1; // 1 : mainthread
98     if (heap_->IsParallelGCEnabled()) {
99         totalThreadCount += Taskpool::GetCurrentTaskpool()->GetTotalThreadNum();
100     }
101     for (uint32_t i = 0; i < totalThreadCount; i++) {
102         ProcessQueue *queue = workManager_->GetWeakReferenceQueue(i);
103 
104         while (true) {
105             auto obj = queue->PopBack();
106             if (UNLIKELY(obj == nullptr)) {
107                 break;
108             }
109             ObjectSlot slot(ToUintPtr(obj));
110             JSTaggedValue value(slot.GetTaggedType());
111             auto header = value.GetTaggedWeakRef();
112 
113             Region *objectRegion = Region::ObjectAddressToRange(header);
114             if (!HasEvacuated(objectRegion)) {
115                 if (!objectRegion->Test(header)) {
116                     slot.Clear();
117                 }
118             } else {
119                 MarkWord markWord(header);
120                 if (markWord.IsForwardingAddress()) {
121                     TaggedObject *dst = markWord.ToForwardingAddress();
122                     auto weakRef = JSTaggedValue(JSTaggedValue(dst).CreateAndGetWeakRef()).GetRawTaggedObject();
123                     slot.Update(weakRef);
124                 } else {
125                     slot.Update(static_cast<JSTaggedType>(JSTaggedValue::Undefined().GetRawData()));
126                 }
127             }
128         }
129     }
130 
131     auto stringTable = heap_->GetEcmaVM()->GetEcmaStringTable();
132     WeakRootVisitor gcUpdateWeak = [this](TaggedObject *header) {
133         Region *objectRegion = Region::ObjectAddressToRange(header);
134         if (!HasEvacuated(objectRegion)) {
135             if (objectRegion->Test(header)) {
136                 return header;
137             }
138             return reinterpret_cast<TaggedObject *>(ToUintPtr(nullptr));
139         }
140 
141         MarkWord markWord(header);
142         if (markWord.IsForwardingAddress()) {
143             return markWord.ToForwardingAddress();
144         }
145         return reinterpret_cast<TaggedObject *>(ToUintPtr(nullptr));
146     };
147     stringTable->SweepWeakReference(gcUpdateWeak);
148     heap_->GetEcmaVM()->GetJSThread()->IterateWeakEcmaGlobalStorage(gcUpdateWeak);
149     heap_->GetEcmaVM()->ProcessReferences(gcUpdateWeak);
150 
151     heap_->GetSweeper()->Sweep(true);
152     heap_->GetSweeper()->PostTask(true);
153 }
154 
Finish()155 void FullGC::Finish()
156 {
157     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Finish");
158     TRACE_GC(GCStats::Scope::ScopeId::Finish, heap_->GetEcmaVM()->GetEcmaGCStats());
159     if (!forAppSpawn_) {
160         heap_->SwapOldSpace();
161     }
162     youngAndOldAliveSize_ = workManager_->Finish();
163     if (forAppSpawn_) {
164         heap_->ResumeForAppSpawn();
165     } else {
166         heap_->Resume(FULL_GC);
167     }
168     heap_->GetSweeper()->TryFillSweptRegion();
169 }
170 
HasEvacuated(Region * region)171 bool FullGC::HasEvacuated(Region *region)
172 {
173     auto marker = reinterpret_cast<CompressGCMarker*>(heap_->GetCompressGCMarker());
174     return marker->NeedEvacuate(region);
175 }
176 
SetForAppSpawn(bool flag)177 void FullGC::SetForAppSpawn(bool flag)
178 {
179     forAppSpawn_ = flag;
180 }
181 }  // namespace panda::ecmascript
182