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