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