1 /*
2 * Copyright (c) 2024 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/shared_heap/shared_full_gc.h"
17
18 #include "ecmascript/checkpoint/thread_state_transition.h"
19 #include "ecmascript/ecma_string_table.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/mem/barriers-inl.h"
22 #include "ecmascript/mem/gc_stats.h"
23 #include "ecmascript/mem/mark_stack.h"
24 #include "ecmascript/mem/mem.h"
25 #include "ecmascript/mem/object_xray.h"
26 #include "ecmascript/mem/shared_heap/shared_concurrent_marker.h"
27 #include "ecmascript/mem/shared_heap/shared_concurrent_sweeper.h"
28 #include "ecmascript/mem/shared_heap/shared_gc_marker-inl.h"
29 #include "ecmascript/mem/slots.h"
30 #include "ecmascript/mem/space-inl.h"
31 #include "ecmascript/mem/verification.h"
32 #include "ecmascript/mem/visitor.h"
33 #include "ecmascript/runtime.h"
34
35 namespace panda::ecmascript {
RunPhases()36 void SharedFullGC::RunPhases()
37 {
38 ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedFullGC::RunPhases"
39 + std::to_string(static_cast<int>(sHeap_->GetEcmaGCStats()->GetGCReason()))
40 + ";Sensitive" + std::to_string(static_cast<int>(sHeap_->GetSensitiveStatus()))
41 + ";IsInBackground" + std::to_string(sHeap_->IsInBackground())
42 + ";Startup" + std::to_string(sHeap_->OnStartupEvent())
43 + ";Old" + std::to_string(sHeap_->GetOldSpace()->GetCommittedSize())
44 + ";huge" + std::to_string(sHeap_->GetHugeObjectSpace()->GetCommittedSize())
45 + ";NonMov" + std::to_string(sHeap_->GetNonMovableSpace()->GetCommittedSize())
46 + ";TotCommit" + std::to_string(sHeap_->GetCommittedSize()));
47 TRACE_GC(GCStats::Scope::ScopeId::TotalGC, sHeap_->GetEcmaGCStats());
48 Initialize();
49 Mark();
50 Sweep();
51 Finish();
52 }
53
Initialize()54 void SharedFullGC::Initialize()
55 {
56 ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedFullGC::Initialize");
57 TRACE_GC(GCStats::Scope::ScopeId::Initialize, sHeap_->GetEcmaGCStats());
58 sHeap_->Prepare(true);
59 if (UNLIKELY(sHeap_->CheckOngoingConcurrentMarking())) {
60 // Concurrent shared mark should always trigger shared gc without moving.
61 sHeap_->GetConcurrentMarker()->Reset(true);
62 }
63 sHeap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) {
64 current->ClearMarkGCBitset();
65 });
66 sHeap_->EnumerateOldSpaceRegions([](Region *current) {
67 ASSERT(current->InSharedSweepableSpace());
68 current->ResetAliveObject();
69 });
70 sWorkManager_->Initialize(TriggerGCType::SHARED_FULL_GC, SharedParallelMarkPhase::SHARED_COMPRESS_TASK);
71 }
72
Mark()73 void SharedFullGC::Mark()
74 {
75 ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedFullGC::Mark");
76 TRACE_GC(GCStats::Scope::ScopeId::Mark, sHeap_->GetEcmaGCStats());
77 SharedGCMovableMarker *marker = sHeap_->GetSharedGCMovableMarker();
78
79 marker->MarkRoots(DAEMON_THREAD_INDEX, SharedMarkType::NOT_CONCURRENT_MARK, VMRootVisitType::UPDATE_ROOT);
80 marker->DoMark<SharedMarkType::NOT_CONCURRENT_MARK>(DAEMON_THREAD_INDEX);
81 marker->MergeBackAndResetRSetWorkListHandler();
82 sHeap_->WaitRunningTaskFinished();
83 }
84
Sweep()85 void SharedFullGC::Sweep()
86 {
87 ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedFullGC::Sweep");
88 TRACE_GC(GCStats::Scope::ScopeId::Sweep, sHeap_->GetEcmaGCStats());
89 UpdateRecordWeakReference();
90 WeakRootVisitor gcUpdateWeak = [](TaggedObject *header) {
91 Region *objectRegion = Region::ObjectAddressToRange(header);
92 if (!objectRegion) {
93 LOG_GC(ERROR) << "SharedFullGC updateWeakReference: region is nullptr, header is " << header;
94 return reinterpret_cast<TaggedObject *>(ToUintPtr(nullptr));
95 }
96 if (objectRegion->InSharedOldSpace()) {
97 MarkWord markWord(header);
98 if (markWord.IsForwardingAddress()) {
99 return markWord.ToForwardingAddress();
100 }
101 return reinterpret_cast<TaggedObject *>(ToUintPtr(nullptr));
102 }
103 if (!objectRegion->InSharedSweepableSpace() || objectRegion->Test(header)) {
104 return header;
105 }
106 return reinterpret_cast<TaggedObject *>(ToUintPtr(nullptr));
107 };
108 auto stringTableCleaner = Runtime::GetInstance()->GetEcmaStringTable()->GetCleaner();
109 stringTableCleaner->PostSweepWeakRefTask(gcUpdateWeak);
110 Runtime::GetInstance()->ProcessNativeDeleteInSharedGC(gcUpdateWeak);
111
112 Runtime::GetInstance()->GCIterateThreadList([&](JSThread *thread) {
113 ASSERT(!thread->IsInRunningState());
114 thread->IterateWeakEcmaGlobalStorage(gcUpdateWeak, GCKind::SHARED_GC);
115 thread->GetEcmaVM()->ProcessSharedNativeDelete(gcUpdateWeak);
116 const_cast<Heap*>(thread->GetEcmaVM()->GetHeap())->ResetTlab();
117 thread->ClearContextCachedConstantPool();
118 });
119
120 stringTableCleaner->JoinAndWaitSweepWeakRefTask(gcUpdateWeak);
121 sHeap_->GetSweeper()->Sweep(true);
122 sHeap_->GetSweeper()->PostTask(true);
123 }
124
Finish()125 void SharedFullGC::Finish()
126 {
127 ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedFullGC::Finish");
128 TRACE_GC(GCStats::Scope::ScopeId::Finish, sHeap_->GetEcmaGCStats());
129 sHeap_->SwapOldSpace();
130 sWorkManager_->Finish();
131 if (!isAppspawn_) {
132 sHeap_->Reclaim(TriggerGCType::SHARED_FULL_GC);
133 } else {
134 sHeap_->ReclaimForAppSpawn();
135 }
136
137 sHeap_->GetSweeper()->TryFillSweptRegion();
138 }
139
UpdateRecordWeakReference()140 void SharedFullGC::UpdateRecordWeakReference()
141 {
142 auto totalThreadCount = Taskpool::GetCurrentTaskpool()->GetTotalThreadNum() + 1;
143 for (uint32_t i = 0; i < totalThreadCount; i++) {
144 ProcessQueue *queue = sHeap_->GetWorkManager()->GetWeakReferenceQueue(i);
145
146 while (true) {
147 auto obj = queue->PopBack();
148 if (UNLIKELY(obj == nullptr)) {
149 break;
150 }
151 ObjectSlot slot(ToUintPtr(obj));
152 JSTaggedValue value(slot.GetTaggedType());
153 ASSERT(value.IsWeak());
154 auto header = value.GetTaggedWeakRef();
155 Region *objectRegion = Region::ObjectAddressToRange(header);
156 if (!objectRegion->InSharedOldSpace()) {
157 if (!objectRegion->Test(header)) {
158 slot.Clear();
159 }
160 } else {
161 MarkWord markWord(header);
162 if (markWord.IsForwardingAddress()) {
163 TaggedObject *dst = markWord.ToForwardingAddress();
164 auto weakRef = JSTaggedValue(JSTaggedValue(dst).CreateAndGetWeakRef()).GetRawTaggedObject();
165 slot.Update(weakRef);
166 } else {
167 slot.Clear();
168 }
169 }
170 }
171 }
172 }
173
HasEvacuated(Region * region)174 bool SharedFullGC::HasEvacuated(Region *region)
175 {
176 auto marker = reinterpret_cast<SharedGCMovableMarker *>(sHeap_->GetSharedGCMovableMarker());
177 return marker->NeedEvacuate(region);
178 }
179
ResetWorkManager(SharedGCWorkManager * sWorkManager)180 void SharedFullGC::ResetWorkManager(SharedGCWorkManager *sWorkManager)
181 {
182 sWorkManager_ = sWorkManager;
183 }
184 } // namespace panda::ecmascript
185