• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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/shared_heap/shared_concurrent_sweeper.h"
17 #include "ecmascript/mem/heap.h"
18 
19 namespace panda::ecmascript {
SharedConcurrentSweeper(SharedHeap * heap,EnableConcurrentSweepType type)20 SharedConcurrentSweeper::SharedConcurrentSweeper(SharedHeap *heap, EnableConcurrentSweepType type)
21     : sHeap_(heap),
22       enableType_(type)
23 {
24 }
25 
PostTask(bool isFullGC)26 void SharedConcurrentSweeper::PostTask(bool isFullGC)
27 {
28     auto tid = DaemonThread::GetInstance()->GetThreadId();
29     if (ConcurrentSweepEnabled()) {
30         if (!isFullGC) {
31             Taskpool::GetCurrentTaskpool()->PostTask(
32                 std::make_unique<SweeperTask>(tid, this, SHARED_OLD_SPACE, isFullGC));
33         }
34         Taskpool::GetCurrentTaskpool()->PostTask(
35             std::make_unique<SweeperTask>(tid, this, SHARED_NON_MOVABLE, isFullGC));
36     } else {
37         if (!isFullGC_) {
38             sHeap_->GetOldSpace()->Sweep();
39         }
40         sHeap_->GetNonMovableSpace()->Sweep();
41     }
42     sHeap_->GetHugeObjectSpace()->Sweep();
43 }
44 
Sweep(bool isFullGC)45 void SharedConcurrentSweeper::Sweep(bool isFullGC)
46 {
47     isFullGC_ = isFullGC;
48     if (ConcurrentSweepEnabled()) {
49         // Add all region to region list. Ensure all task finish
50         if (!isFullGC_) {
51             sHeap_->GetOldSpace()->PrepareSweeping();
52             for (int spaceIndex = 0; spaceIndex < SHARED_SWEEPING_SPACE_NUM; spaceIndex++) {
53                 remainingTaskNum_[spaceIndex] = SHARED_SWEEPING_SPACE_NUM;
54             }
55         } else {
56             remainingTaskNum_[0] = 0;  // No need sweep shared old space in FullGC.
57             remainingTaskNum_[1] = 1;  // Need sweep nonmovable space in FullGC.
58         }
59         sHeap_->GetNonMovableSpace()->PrepareSweeping();
60         // Prepare
61         isSweeping_ = true;
62     }
63 }
64 
AsyncSweepSpace(MemSpaceType type,bool isMain)65 void SharedConcurrentSweeper::AsyncSweepSpace(MemSpaceType type, bool isMain)
66 {
67     auto space = sHeap_->GetSpaceWithType(type);
68     space->AsyncSweep(isMain);
69     int spaceIndex = type - SHARED_SWEEPING_SPACE_BEGIN;
70     LockHolder holder(mutexs_[spaceIndex]);
71     if (--remainingTaskNum_[spaceIndex] == 0) {
72         cvs_[spaceIndex].SignalAll();
73     }
74 }
75 
WaitAllTaskFinished()76 void SharedConcurrentSweeper::WaitAllTaskFinished()
77 {
78     if (!isSweeping_) {
79         return;
80     }
81     int spaceIndex = isFullGC_ ? 1 : 0;
82     for (; spaceIndex < SHARED_SWEEPING_SPACE_NUM; spaceIndex++) {
83         if (remainingTaskNum_[spaceIndex] > 0) {
84             LockHolder holder(mutexs_[spaceIndex]);
85             while (remainingTaskNum_[spaceIndex] > 0) {
86                 cvs_[spaceIndex].Wait(&mutexs_[spaceIndex]);
87             }
88         }
89     }
90 }
91 
92 // call in suspendAll thread.
EnsureAllTaskFinished()93 void SharedConcurrentSweeper::EnsureAllTaskFinished()
94 {
95     if (!isSweeping_) {
96         return;
97     }
98     int spaceIndex = isFullGC_ ? 1 : 0;
99     for (; spaceIndex < SHARED_SWEEPING_SPACE_NUM; spaceIndex++) {
100         int type = spaceIndex + SHARED_SWEEPING_SPACE_BEGIN;
101         WaitingTaskFinish(static_cast<MemSpaceType>(type));
102     }
103     isSweeping_ = false;
104     if (IsRequestDisabled()) {
105         enableType_ = EnableConcurrentSweepType::DISABLE;
106     }
107 }
108 
109 // call in mutator thread
EnsureTaskFinished(MemSpaceType type)110 void SharedConcurrentSweeper::EnsureTaskFinished(MemSpaceType type)
111 {
112     if (!isSweeping_) {
113         return;
114     }
115     WaitingTaskFinish(type);
116 }
117 
WaitingTaskFinish(MemSpaceType type)118 void SharedConcurrentSweeper::WaitingTaskFinish(MemSpaceType type)
119 {
120     int spaceIndex = type - SHARED_SWEEPING_SPACE_BEGIN;
121     if (remainingTaskNum_[spaceIndex] > 0) {
122         {
123             LockHolder holder(mutexs_[spaceIndex]);
124             remainingTaskNum_[spaceIndex]++;
125         }
126         AsyncSweepSpace(type, true);
127         LockHolder holder(mutexs_[spaceIndex]);
128         while (remainingTaskNum_[spaceIndex] > 0) {
129             cvs_[spaceIndex].Wait(&mutexs_[spaceIndex]);
130         }
131     }
132     SharedSparseSpace *space = sHeap_->GetSpaceWithType(type);
133     space->FinishFillSweptRegion();
134 }
135 
TryFillSweptRegion()136 void SharedConcurrentSweeper::TryFillSweptRegion()
137 {
138     if (!isFullGC_) {
139         sHeap_->GetOldSpace()->TryFillSweptRegion();
140     }
141     sHeap_->GetNonMovableSpace()->TryFillSweptRegion();
142 }
143 
Run(uint32_t threadIndex)144 bool SharedConcurrentSweeper::SweeperTask::Run([[maybe_unused]] uint32_t threadIndex)
145 {
146     ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedConcurrentSweeper::Sweep");
147     if (type_ == SHARED_NON_MOVABLE) {
148         sweeper_->AsyncSweepSpace(SHARED_NON_MOVABLE, false);
149         if (!isFullGC_) {
150             sweeper_->AsyncSweepSpace(SHARED_OLD_SPACE, false);
151         }
152     } else {
153         ASSERT(type_ == SHARED_OLD_SPACE);
154         if (!isFullGC_) {
155             sweeper_->AsyncSweepSpace(SHARED_OLD_SPACE, false);
156         }
157         sweeper_->AsyncSweepSpace(SHARED_NON_MOVABLE, false);
158     }
159 
160     return true;
161 }
162 
EnableConcurrentSweep(EnableConcurrentSweepType type)163 void SharedConcurrentSweeper::EnableConcurrentSweep(EnableConcurrentSweepType type)
164 {
165     if (IsConfigDisabled()) {
166         return;
167     }
168     if (ConcurrentSweepEnabled() && isSweeping_ && type == EnableConcurrentSweepType::DISABLE) {
169         enableType_ = EnableConcurrentSweepType::REQUEST_DISABLE;
170     } else {
171         enableType_ = type;
172     }
173 }
174 }  // namespace panda::ecmascript
175