• 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     MEM_ALLOCATE_AND_GC_TRACE(heap_->GetEcmaVM(), FullGC_RunPhases);
37     ClockScope clockScope;
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_->GetEcmaVM()->GetEcmaGCStats()->StatisticFullGC(clockScope.GetPauseTime(), youngAndOldAliveSize_,
48                                                           youngSpaceCommitSize_, oldSpaceCommitSize_,
49                                                           nonMoveSpaceFreeSize_, nonMoveSpaceCommitSize_);
50     LOG_GC(DEBUG) << "FullGC::RunPhases " << clockScope.TotalSpentTime();
51 }
52 
RunPhasesForAppSpawn()53 void FullGC::RunPhasesForAppSpawn()
54 {
55     auto marker = reinterpret_cast<CompressGCMarker*>(heap_->GetCompressGCMarker());
56     marker->SetAppSpawn(true);
57     RunPhases();
58     marker->SetAppSpawn(false);
59 }
60 
Initialize()61 void FullGC::Initialize()
62 {
63     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Initialize");
64     heap_->Prepare();
65     auto callback = [](Region *current) {
66         current->ResetAliveObject();
67         current->ClearOldToNewRSet();
68     };
69     heap_->EnumerateNonMovableRegions(callback);
70     heap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) {
71         current->ClearMarkGCBitset();
72         current->ClearCrossRegionRSet();
73     });
74     youngSpaceCommitSize_ = heap_->GetNewSpace()->GetCommittedSize();
75     heap_->SwapNewSpace();
76     workManager_->Initialize(TriggerGCType::FULL_GC, ParallelGCTaskPhase::COMPRESS_HANDLE_GLOBAL_POOL_TASK);
77     heap_->GetCompressGCMarker()->Initialize();
78 
79     youngAndOldAliveSize_ = 0;
80     nonMoveSpaceFreeSize_ = 0;
81     oldSpaceCommitSize_ = heap_->GetOldSpace()->GetCommittedSize();
82     nonMoveSpaceCommitSize_ = heap_->GetNonMovableSpace()->GetCommittedSize();
83 }
84 
Mark()85 void FullGC::Mark()
86 {
87     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Mark");
88     heap_->GetCompressGCMarker()->MarkRoots(MAIN_THREAD_INDEX);
89     heap_->GetCompressGCMarker()->ProcessMarkStack(MAIN_THREAD_INDEX);
90     heap_->WaitRunningTaskFinished();
91 }
92 
Sweep()93 void FullGC::Sweep()
94 {
95     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Sweep");
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 }
153 
Finish()154 void FullGC::Finish()
155 {
156     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Finish");
157     if (forAppSpawn_) {
158         heap_->ResumeForAppSpawn();
159     } else {
160         heap_->Resume(FULL_GC);
161     }
162     youngAndOldAliveSize_ = workManager_->Finish();
163     heap_->GetSweeper()->TryFillSweptRegion();
164 }
165 
HasEvacuated(Region * region)166 bool FullGC::HasEvacuated(Region *region)
167 {
168     auto marker = reinterpret_cast<CompressGCMarker*>(heap_->GetCompressGCMarker());
169     return marker->NeedEvacuate(region);
170 }
171 
SetForAppSpawn(bool flag)172 void FullGC::SetForAppSpawn(bool flag)
173 {
174     forAppSpawn_ = flag;
175 }
176 }  // namespace panda::ecmascript
177