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/concurrent_marker.h"
17
18 #include "ecmascript/mem/allocator-inl.h"
19 #include "ecmascript/mem/clock_scope.h"
20 #include "ecmascript/mem/heap-inl.h"
21 #include "ecmascript/mem/mark_stack.h"
22 #include "ecmascript/mem/mark_word.h"
23 #include "ecmascript/mem/parallel_marker-inl.h"
24 #include "ecmascript/mem/space-inl.h"
25 #include "ecmascript/mem/verification.h"
26 #include "ecmascript/mem/visitor.h"
27 #include "ecmascript/mem/gc_stats.h"
28 #include "ecmascript/ecma_string_table.h"
29 #include "ecmascript/taskpool/taskpool.h"
30 #include "ecmascript/runtime_call_id.h"
31
32 namespace panda::ecmascript {
33 size_t ConcurrentMarker::taskCounts_ = 0;
34 os::memory::Mutex ConcurrentMarker::taskCountMutex_;
35
ConcurrentMarker(Heap * heap,EnableConcurrentMarkType type)36 ConcurrentMarker::ConcurrentMarker(Heap *heap, EnableConcurrentMarkType type)
37 : heap_(heap),
38 vm_(heap->GetEcmaVM()),
39 thread_(vm_->GetJSThread()),
40 workManager_(heap->GetWorkManager()),
41 enableMarkType_(type)
42 {
43 thread_->SetMarkStatus(MarkStatus::READY_TO_MARK);
44 }
45
EnableConcurrentMarking(EnableConcurrentMarkType type)46 void ConcurrentMarker::EnableConcurrentMarking(EnableConcurrentMarkType type)
47 {
48 if (IsConfigDisabled()) {
49 return;
50 }
51 if (IsEnabled() && !thread_->IsReadyToMark() && type == EnableConcurrentMarkType::DISABLE) {
52 enableMarkType_ = EnableConcurrentMarkType::REQUEST_DISABLE;
53 } else {
54 enableMarkType_ = type;
55 }
56 }
57
Mark()58 void ConcurrentMarker::Mark()
59 {
60 TRACE_GC(GCStats::Scope::ScopeId::ConcurrentMark, heap_->GetEcmaVM()->GetEcmaGCStats());
61 LOG_GC(DEBUG) << "ConcurrentMarker: Concurrent Marking Begin";
62 ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "ConcurrentMarker::Mark");
63 MEM_ALLOCATE_AND_GC_TRACE(vm_, ConcurrentMarking);
64 InitializeMarking();
65 Taskpool::GetCurrentTaskpool()->PostTask(std::make_unique<MarkerTask>(heap_->GetJSThread()->GetThreadId(), heap_));
66 }
67
Finish()68 void ConcurrentMarker::Finish()
69 {
70 workManager_->Finish();
71 }
72
ReMark()73 void ConcurrentMarker::ReMark()
74 {
75 TRACE_GC(GCStats::Scope::ScopeId::ReMark, heap_->GetEcmaVM()->GetEcmaGCStats());
76 LOG_GC(DEBUG) << "ConcurrentMarker: Remarking Begin";
77 MEM_ALLOCATE_AND_GC_TRACE(vm_, ReMarking);
78 Marker *nonMovableMarker = heap_->GetNonMovableMarker();
79 nonMovableMarker->MarkRoots(MAIN_THREAD_INDEX);
80 nonMovableMarker->ProcessMarkStack(MAIN_THREAD_INDEX);
81 heap_->WaitRunningTaskFinished();
82 }
83
HandleMarkingFinished()84 void ConcurrentMarker::HandleMarkingFinished() // js-thread wait for sweep
85 {
86 os::memory::LockHolder lock(waitMarkingFinishedMutex_);
87 if (notifyMarkingFinished_) {
88 heap_->CollectGarbage(heap_->IsFullMark() ? TriggerGCType::OLD_GC : TriggerGCType::YOUNG_GC,
89 GCReason::ALLOCATION_LIMIT);
90 }
91 }
92
WaitMarkingFinished()93 void ConcurrentMarker::WaitMarkingFinished() // call in EcmaVm thread, wait for mark finished
94 {
95 os::memory::LockHolder lock(waitMarkingFinishedMutex_);
96 if (!notifyMarkingFinished_) {
97 vmThreadWaitMarkingFinished_ = true;
98 waitMarkingFinishedCV_.Wait(&waitMarkingFinishedMutex_);
99 }
100 }
101
Reset(bool revertCSet)102 void ConcurrentMarker::Reset(bool revertCSet)
103 {
104 Finish();
105 thread_->SetMarkStatus(MarkStatus::READY_TO_MARK);
106 isConcurrentMarking_ = false;
107 notifyMarkingFinished_ = false;
108 if (revertCSet) {
109 // Partial gc clear cset when evacuation allocator finalize
110 heap_->GetOldSpace()->RevertCSet();
111 auto callback = [](Region *region) {
112 region->ClearMarkGCBitset();
113 region->ClearCrossRegionRSet();
114 region->ResetAliveObject();
115 };
116 if (heap_->IsFullMark()) {
117 heap_->EnumerateRegions(callback);
118 } else {
119 heap_->EnumerateNewSpaceRegions(callback);
120 }
121 }
122 DecreaseTaskCounts();
123 }
124
InitializeMarking()125 void ConcurrentMarker::InitializeMarking()
126 {
127 MEM_ALLOCATE_AND_GC_TRACE(vm_, ConcurrentMarkingInitialize);
128 heap_->Prepare();
129 isConcurrentMarking_ = true;
130 thread_->SetMarkStatus(MarkStatus::MARKING);
131
132 if (heap_->IsFullMark()) {
133 heapObjectSize_ = heap_->GetHeapObjectSize();
134 heap_->GetOldSpace()->SelectCSet();
135 heap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) {
136 current->ClearMarkGCBitset();
137 current->ClearCrossRegionRSet();
138 });
139 // The alive object size of Region in OldSpace will be recalculated.
140 heap_->EnumerateNonNewSpaceRegions([](Region *current) {
141 current->ResetAliveObject();
142 });
143 } else {
144 heapObjectSize_ = heap_->GetNewSpace()->GetHeapObjectSize();
145 }
146 workManager_->Initialize(TriggerGCType::OLD_GC, ParallelGCTaskPhase::CONCURRENT_HANDLE_GLOBAL_POOL_TASK);
147 if (!heap_->IsFullMark()) {
148 heap_->GetNonMovableMarker()->ProcessOldToNewNoMarkStack(MAIN_THREAD_INDEX);
149 heap_->GetNonMovableMarker()->ProcessSnapshotRSetNoMarkStack(MAIN_THREAD_INDEX);
150 }
151 heap_->GetNonMovableMarker()->MarkRoots(MAIN_THREAD_INDEX);
152 }
153
Run(uint32_t threadId)154 bool ConcurrentMarker::MarkerTask::Run(uint32_t threadId)
155 {
156 ClockScope clockScope;
157 heap_->GetNonMovableMarker()->ProcessMarkStack(threadId);
158 heap_->WaitRunningTaskFinished();
159 heap_->GetConcurrentMarker()->FinishMarking(clockScope.TotalSpentTime());
160 return true;
161 }
162
FinishMarking(float spendTime)163 void ConcurrentMarker::FinishMarking(float spendTime)
164 {
165 os::memory::LockHolder lock(waitMarkingFinishedMutex_);
166 thread_->SetMarkStatus(MarkStatus::MARK_FINISHED);
167 thread_->SetCheckSafePointStatus();
168 if (vmThreadWaitMarkingFinished_) {
169 vmThreadWaitMarkingFinished_ = false;
170 waitMarkingFinishedCV_.Signal();
171 }
172 notifyMarkingFinished_ = true;
173 if (!heap_->IsFullMark()) {
174 heapObjectSize_ = heap_->GetNewSpace()->GetHeapObjectSize();
175 } else {
176 heapObjectSize_ = heap_->GetHeapObjectSize();
177 }
178 SetDuration(spendTime);
179 if (heap_->IsFullMarkRequested()) {
180 heap_->SetFullMarkRequestedState(false);
181 }
182 }
183 } // namespace panda::ecmascript
184