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