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