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 }
71
AsyncSweepSpace(MemSpaceType type,bool isMain)72 void ConcurrentSweeper::AsyncSweepSpace(MemSpaceType type, bool isMain)
73 {
74 auto space = heap_->GetSpaceWithType(type);
75 space->AsyncSweep(isMain);
76
77 os::memory::LockHolder holder(mutexs_[type]);
78 if (--remainingTaskNum_[type] == 0) {
79 cvs_[type].SignalAll();
80 }
81 }
82
WaitAllTaskFinished()83 void ConcurrentSweeper::WaitAllTaskFinished()
84 {
85 if (!isSweeping_) {
86 return;
87 }
88 for (int i = startSpaceType_; i < FREE_LIST_NUM; i++) {
89 if (remainingTaskNum_[i] > 0) {
90 os::memory::LockHolder holder(mutexs_[i]);
91 while (remainingTaskNum_[i] > 0) {
92 cvs_[i].Wait(&mutexs_[i]);
93 }
94 }
95 }
96 }
97
EnsureAllTaskFinished()98 void ConcurrentSweeper::EnsureAllTaskFinished()
99 {
100 CHECK_JS_THREAD(heap_->GetEcmaVM());
101 if (!isSweeping_) {
102 return;
103 }
104 for (int i = startSpaceType_; i < FREE_LIST_NUM; i++) {
105 WaitingTaskFinish(static_cast<MemSpaceType>(i));
106 }
107 isSweeping_ = false;
108 if (IsRequestDisabled()) {
109 enableType_ = EnableConcurrentSweepType::DISABLE;
110 }
111 }
112
EnsureTaskFinished(MemSpaceType type)113 void ConcurrentSweeper::EnsureTaskFinished(MemSpaceType type)
114 {
115 CHECK_JS_THREAD(heap_->GetEcmaVM());
116 if (!isSweeping_) {
117 return;
118 }
119 WaitingTaskFinish(type);
120 }
121
WaitingTaskFinish(MemSpaceType type)122 void ConcurrentSweeper::WaitingTaskFinish(MemSpaceType type)
123 {
124 if (remainingTaskNum_[type] > 0) {
125 {
126 os::memory::LockHolder holder(mutexs_[type]);
127 remainingTaskNum_[type]++;
128 }
129 AsyncSweepSpace(type, true);
130 os::memory::LockHolder holder(mutexs_[type]);
131 while (remainingTaskNum_[type] > 0) {
132 cvs_[type].Wait(&mutexs_[type]);
133 }
134 }
135 SparseSpace *space = heap_->GetSpaceWithType(type);
136 space->FinishFillSweptRegion();
137 }
138
TryFillSweptRegion()139 void ConcurrentSweeper::TryFillSweptRegion()
140 {
141 for (int i = startSpaceType_; i < FREE_LIST_NUM; i++) {
142 SparseSpace *space = heap_->GetSpaceWithType(static_cast<MemSpaceType>(i));
143 space->TryFillSweptRegion();
144 }
145 }
146
ClearRSetInRange(Region * current,uintptr_t freeStart,uintptr_t freeEnd)147 void ConcurrentSweeper::ClearRSetInRange(Region *current, uintptr_t freeStart, uintptr_t freeEnd)
148 {
149 if (ConcurrentSweepEnabled()) {
150 current->AtomicClearSweepingRSetInRange(freeStart, freeEnd);
151 } else {
152 current->ClearOldToNewRSetInRange(freeStart, freeEnd);
153 }
154 current->ClearCrossRegionRSetInRange(freeStart, freeEnd);
155 }
156
Run(uint32_t threadIndex)157 bool ConcurrentSweeper::SweeperTask::Run([[maybe_unused]] uint32_t threadIndex)
158 {
159 uint32_t sweepTypeNum = FREE_LIST_NUM - sweeper_->startSpaceType_;
160 for (size_t i = sweeper_->startSpaceType_; i < FREE_LIST_NUM; i++) {
161 auto type = static_cast<MemSpaceType>(((i + type_) % sweepTypeNum) + sweeper_->startSpaceType_);
162 sweeper_->AsyncSweepSpace(type, false);
163 }
164 return true;
165 }
166
EnableConcurrentSweep(EnableConcurrentSweepType type)167 void ConcurrentSweeper::EnableConcurrentSweep(EnableConcurrentSweepType type)
168 {
169 if (IsConfigDisabled()) {
170 return;
171 }
172 if (ConcurrentSweepEnabled() && isSweeping_ && type == EnableConcurrentSweepType::DISABLE) {
173 enableType_ = EnableConcurrentSweepType::REQUEST_DISABLE;
174 } else {
175 enableType_ = type;
176 }
177 }
178 } // namespace panda::ecmascript
179