• 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/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 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     RecursionScope recurScope(this);
61     TRACE_GC(GCStats::Scope::ScopeId::ConcurrentMark, heap_->GetEcmaVM()->GetEcmaGCStats());
62     LOG_GC(DEBUG) << "ConcurrentMarker: Concurrent Marking Begin";
63     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "ConcurrentMarker::Mark");
64     MEM_ALLOCATE_AND_GC_TRACE(vm_, ConcurrentMarking);
65     InitializeMarking();
66     Taskpool::GetCurrentTaskpool()->PostTask(std::make_unique<MarkerTask>(heap_->GetJSThread()->GetThreadId(), heap_));
67 }
68 
Finish()69 void ConcurrentMarker::Finish()
70 {
71     workManager_->Finish();
72 }
73 
ReMark()74 void ConcurrentMarker::ReMark()
75 {
76     TRACE_GC(GCStats::Scope::ScopeId::ReMark, heap_->GetEcmaVM()->GetEcmaGCStats());
77     LOG_GC(DEBUG) << "ConcurrentMarker: Remarking Begin";
78     MEM_ALLOCATE_AND_GC_TRACE(vm_, ReMarking);
79     Marker *nonMovableMarker = heap_->GetNonMovableMarker();
80     nonMovableMarker->MarkRoots(MAIN_THREAD_INDEX);
81     nonMovableMarker->ProcessMarkStack(MAIN_THREAD_INDEX);
82     heap_->WaitRunningTaskFinished();
83 }
84 
HandleMarkingFinished()85 void ConcurrentMarker::HandleMarkingFinished()  // js-thread wait for sweep
86 {
87     LockHolder lock(waitMarkingFinishedMutex_);
88     if (notifyMarkingFinished_) {
89         heap_->CollectGarbage(heap_->IsConcurrentFullMark() ? TriggerGCType::OLD_GC : TriggerGCType::YOUNG_GC,
90                               GCReason::ALLOCATION_LIMIT);
91     }
92 }
93 
WaitMarkingFinished()94 void ConcurrentMarker::WaitMarkingFinished()  // call in EcmaVm thread, wait for mark finished
95 {
96     LockHolder lock(waitMarkingFinishedMutex_);
97     if (!notifyMarkingFinished_) {
98         vmThreadWaitMarkingFinished_ = true;
99         waitMarkingFinishedCV_.Wait(&waitMarkingFinishedMutex_);
100     }
101 }
102 
Reset(bool revertCSet)103 void ConcurrentMarker::Reset(bool revertCSet)
104 {
105     Finish();
106     thread_->SetMarkStatus(MarkStatus::READY_TO_MARK);
107     isConcurrentMarking_ = false;
108     notifyMarkingFinished_ = false;
109     if (revertCSet) {
110         // Partial gc clear cset when evacuation allocator finalize
111         heap_->GetOldSpace()->RevertCSet();
112         auto callback = [](Region *region) {
113             region->ClearMarkGCBitset();
114             region->ClearCrossRegionRSet();
115             region->ResetAliveObject();
116         };
117         if (heap_->IsConcurrentFullMark()) {
118             heap_->EnumerateRegions(callback);
119         } else {
120             heap_->EnumerateNewSpaceRegions(callback);
121         }
122     }
123     DecreaseTaskCounts();
124 }
125 
InitializeMarking()126 void ConcurrentMarker::InitializeMarking()
127 {
128     MEM_ALLOCATE_AND_GC_TRACE(vm_, ConcurrentMarkingInitialize);
129     heap_->Prepare();
130     isConcurrentMarking_ = true;
131     thread_->SetMarkStatus(MarkStatus::MARKING);
132 
133     if (heap_->IsConcurrentFullMark()) {
134         heapObjectSize_ = heap_->GetHeapObjectSize();
135         heap_->GetOldSpace()->SelectCSet();
136         heap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) {
137             current->ClearMarkGCBitset();
138             current->ClearCrossRegionRSet();
139         });
140         // The alive object size of Region in OldSpace will be recalculated.
141         heap_->EnumerateNonNewSpaceRegions([](Region *current) {
142             current->ResetAliveObject();
143         });
144     } else {
145         heapObjectSize_ = heap_->GetNewSpace()->GetHeapObjectSize();
146     }
147     workManager_->Initialize(TriggerGCType::OLD_GC, ParallelGCTaskPhase::CONCURRENT_HANDLE_GLOBAL_POOL_TASK);
148     if (!heap_->IsConcurrentFullMark()) {
149         {
150             ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "GC::MarkOldToNew");
151             heap_->GetNonMovableMarker()->ProcessOldToNewNoMarkStack(MAIN_THREAD_INDEX);
152         }
153         heap_->GetNonMovableMarker()->ProcessSnapshotRSetNoMarkStack(MAIN_THREAD_INDEX);
154     }
155     heap_->GetNonMovableMarker()->MarkRoots(MAIN_THREAD_INDEX);
156 }
157 
Run(uint32_t threadId)158 bool ConcurrentMarker::MarkerTask::Run(uint32_t threadId)
159 {
160     // Synchronizes-with. Ensure that WorkManager::Initialize must be seen by MarkerThreads.
161     while (!heap_->GetWorkManager()->HasInitialized());
162     ClockScope clockScope;
163     heap_->GetNonMovableMarker()->ProcessMarkStack(threadId);
164     heap_->WaitRunningTaskFinished();
165     heap_->GetConcurrentMarker()->FinishMarking(clockScope.TotalSpentTime());
166     return true;
167 }
168 
FinishMarking(float spendTime)169 void ConcurrentMarker::FinishMarking(float spendTime)
170 {
171     LockHolder lock(waitMarkingFinishedMutex_);
172     thread_->SetMarkStatus(MarkStatus::MARK_FINISHED);
173     thread_->SetCheckSafePointStatus();
174     if (vmThreadWaitMarkingFinished_) {
175         vmThreadWaitMarkingFinished_ = false;
176         waitMarkingFinishedCV_.Signal();
177     }
178     notifyMarkingFinished_ = true;
179     if (!heap_->IsConcurrentFullMark()) {
180         heapObjectSize_ = heap_->GetNewSpace()->GetHeapObjectSize();
181     } else {
182         heapObjectSize_ = heap_->GetHeapObjectSize();
183     }
184     SetDuration(spendTime);
185     if (heap_->IsFullMarkRequested()) {
186         heap_->SetFullMarkRequestedState(false);
187     }
188 }
189 }  // namespace panda::ecmascript
190