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_sweeper.h"
17
18 #include "ecmascript/ecma_macros.h"
19 #include "ecmascript/mem/heap.h"
20 #include "ecmascript/mem/region-inl.h"
21 #include "ecmascript/mem/space-inl.h"
22 #include "ecmascript/taskpool/taskpool.h"
23 #include "ecmascript/runtime_call_id.h"
24
25 namespace panda::ecmascript {
ConcurrentSweeper(Heap * heap,EnableConcurrentSweepType type)26 ConcurrentSweeper::ConcurrentSweeper(Heap *heap, EnableConcurrentSweepType type)
27 : heap_(heap),
28 enableType_(type)
29 {
30 }
31
PostTask(bool fullGC)32 void ConcurrentSweeper::PostTask(bool fullGC)
33 {
34 if (ConcurrentSweepEnabled()) {
35 if (!fullGC) {
36 Taskpool::GetCurrentTaskpool()->PostTask(
37 std::make_unique<SweeperTask>(heap_->GetJSThread()->GetThreadId(), this, OLD_SPACE));
38 }
39 Taskpool::GetCurrentTaskpool()->PostTask(
40 std::make_unique<SweeperTask>(heap_->GetJSThread()->GetThreadId(), this, NON_MOVABLE));
41 Taskpool::GetCurrentTaskpool()->PostTask(
42 std::make_unique<SweeperTask>(heap_->GetJSThread()->GetThreadId(), this, MACHINE_CODE_SPACE));
43 }
44 }
45
Sweep(bool fullGC)46 void ConcurrentSweeper::Sweep(bool fullGC)
47 {
48 MEM_ALLOCATE_AND_GC_TRACE(heap_->GetEcmaVM(), ConcurrentSweepingInitialize);
49 if (ConcurrentSweepEnabled()) {
50 // Add all region to region list. Ensure all task finish
51 if (!fullGC) {
52 heap_->GetOldSpace()->PrepareSweeping();
53 }
54 heap_->GetNonMovableSpace()->PrepareSweeping();
55 heap_->GetMachineCodeSpace()->PrepareSweeping();
56 // Prepare
57 isSweeping_ = true;
58 startSpaceType_ = fullGC ? NON_MOVABLE : OLD_SPACE;
59 for (int type = startSpaceType_; type < FREE_LIST_NUM; type++) {
60 remainingTaskNum_[type] = FREE_LIST_NUM - startSpaceType_;
61 }
62 } else {
63 if (!fullGC) {
64 heap_->GetOldSpace()->Sweep();
65 }
66 heap_->GetNonMovableSpace()->Sweep();
67 heap_->GetMachineCodeSpace()->Sweep();
68 }
69 heap_->GetHugeObjectSpace()->Sweep();
70 heap_->GetHugeMachineCodeSpace()->Sweep();
71 }
72
AsyncSweepSpace(MemSpaceType type,bool isMain)73 void ConcurrentSweeper::AsyncSweepSpace(MemSpaceType type, bool isMain)
74 {
75 auto space = heap_->GetSpaceWithType(type);
76 space->AsyncSweep(isMain);
77
78 LockHolder holder(mutexs_[type]);
79 if (--remainingTaskNum_[type] == 0) {
80 cvs_[type].SignalAll();
81 }
82 }
83
WaitAllTaskFinished()84 void ConcurrentSweeper::WaitAllTaskFinished()
85 {
86 if (!isSweeping_) {
87 return;
88 }
89 for (int i = startSpaceType_; i < FREE_LIST_NUM; i++) {
90 if (remainingTaskNum_[i] > 0) {
91 LockHolder holder(mutexs_[i]);
92 while (remainingTaskNum_[i] > 0) {
93 cvs_[i].Wait(&mutexs_[i]);
94 }
95 }
96 }
97 }
98
EnsureAllTaskFinished()99 void ConcurrentSweeper::EnsureAllTaskFinished()
100 {
101 CHECK_JS_THREAD(heap_->GetEcmaVM());
102 if (!isSweeping_) {
103 return;
104 }
105 for (int i = startSpaceType_; i < FREE_LIST_NUM; i++) {
106 WaitingTaskFinish(static_cast<MemSpaceType>(i));
107 }
108 isSweeping_ = false;
109 if (IsRequestDisabled()) {
110 enableType_ = EnableConcurrentSweepType::DISABLE;
111 }
112 }
113
EnsureTaskFinished(MemSpaceType type)114 void ConcurrentSweeper::EnsureTaskFinished(MemSpaceType type)
115 {
116 CHECK_JS_THREAD(heap_->GetEcmaVM());
117 if (!isSweeping_) {
118 return;
119 }
120 WaitingTaskFinish(type);
121 }
122
WaitingTaskFinish(MemSpaceType type)123 void ConcurrentSweeper::WaitingTaskFinish(MemSpaceType type)
124 {
125 if (remainingTaskNum_[type] > 0) {
126 {
127 LockHolder holder(mutexs_[type]);
128 remainingTaskNum_[type]++;
129 }
130 AsyncSweepSpace(type, true);
131 LockHolder holder(mutexs_[type]);
132 while (remainingTaskNum_[type] > 0) {
133 cvs_[type].Wait(&mutexs_[type]);
134 }
135 }
136 SparseSpace *space = heap_->GetSpaceWithType(type);
137 space->FinishFillSweptRegion();
138 }
139
TryFillSweptRegion()140 void ConcurrentSweeper::TryFillSweptRegion()
141 {
142 for (int i = startSpaceType_; i < FREE_LIST_NUM; i++) {
143 SparseSpace *space = heap_->GetSpaceWithType(static_cast<MemSpaceType>(i));
144 space->TryFillSweptRegion();
145 }
146 }
147
ClearRSetInRange(Region * current,uintptr_t freeStart,uintptr_t freeEnd)148 void ConcurrentSweeper::ClearRSetInRange(Region *current, uintptr_t freeStart, uintptr_t freeEnd)
149 {
150 if (ConcurrentSweepEnabled()) {
151 current->AtomicClearSweepingRSetInRange(freeStart, freeEnd);
152 } else {
153 current->ClearOldToNewRSetInRange(freeStart, freeEnd);
154 }
155 current->ClearCrossRegionRSetInRange(freeStart, freeEnd);
156 }
157
Run(uint32_t threadIndex)158 bool ConcurrentSweeper::SweeperTask::Run([[maybe_unused]] uint32_t threadIndex)
159 {
160 uint32_t sweepTypeNum = FREE_LIST_NUM - sweeper_->startSpaceType_;
161 for (size_t i = sweeper_->startSpaceType_; i < FREE_LIST_NUM; i++) {
162 auto type = static_cast<MemSpaceType>(((i + type_) % sweepTypeNum) + sweeper_->startSpaceType_);
163 sweeper_->AsyncSweepSpace(type, false);
164 }
165 return true;
166 }
167
EnableConcurrentSweep(EnableConcurrentSweepType type)168 void ConcurrentSweeper::EnableConcurrentSweep(EnableConcurrentSweepType type)
169 {
170 if (IsConfigDisabled()) {
171 return;
172 }
173 if (ConcurrentSweepEnabled() && isSweeping_ && type == EnableConcurrentSweepType::DISABLE) {
174 enableType_ = EnableConcurrentSweepType::REQUEST_DISABLE;
175 } else {
176 enableType_ = type;
177 }
178 }
179 } // namespace panda::ecmascript
180