14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/mem/concurrent_sweeper.h" 174514f5e3Sopenharmony_ci#include "ecmascript/mem/heap.h" 184514f5e3Sopenharmony_ci#include "ecmascript/mem/region-inl.h" 194514f5e3Sopenharmony_ci#include "ecmascript/runtime_call_id.h" 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_cinamespace panda::ecmascript { 224514f5e3Sopenharmony_ciConcurrentSweeper::ConcurrentSweeper(Heap *heap, EnableConcurrentSweepType type) 234514f5e3Sopenharmony_ci : heap_(heap), 244514f5e3Sopenharmony_ci enableType_(type) 254514f5e3Sopenharmony_ci{ 264514f5e3Sopenharmony_ci} 274514f5e3Sopenharmony_ci 284514f5e3Sopenharmony_civoid ConcurrentSweeper::PostTask(bool fullGC) 294514f5e3Sopenharmony_ci{ 304514f5e3Sopenharmony_ci if (ConcurrentSweepEnabled()) { 314514f5e3Sopenharmony_ci if (!fullGC) { 324514f5e3Sopenharmony_ci Taskpool::GetCurrentTaskpool()->PostTask( 334514f5e3Sopenharmony_ci std::make_unique<SweeperTask>(heap_->GetJSThread()->GetThreadId(), this, OLD_SPACE)); 344514f5e3Sopenharmony_ci } 354514f5e3Sopenharmony_ci Taskpool::GetCurrentTaskpool()->PostTask( 364514f5e3Sopenharmony_ci std::make_unique<SweeperTask>(heap_->GetJSThread()->GetThreadId(), this, NON_MOVABLE)); 374514f5e3Sopenharmony_ci Taskpool::GetCurrentTaskpool()->PostTask( 384514f5e3Sopenharmony_ci std::make_unique<SweeperTask>(heap_->GetJSThread()->GetThreadId(), this, MACHINE_CODE_SPACE)); 394514f5e3Sopenharmony_ci } 404514f5e3Sopenharmony_ci} 414514f5e3Sopenharmony_ci 424514f5e3Sopenharmony_civoid ConcurrentSweeper::Sweep(bool fullGC) 434514f5e3Sopenharmony_ci{ 444514f5e3Sopenharmony_ci MEM_ALLOCATE_AND_GC_TRACE(heap_->GetEcmaVM(), ConcurrentSweepingInitialize); 454514f5e3Sopenharmony_ci if (ConcurrentSweepEnabled()) { 464514f5e3Sopenharmony_ci // Add all region to region list. Ensure all task finish 474514f5e3Sopenharmony_ci if (!fullGC) { 484514f5e3Sopenharmony_ci heap_->GetOldSpace()->PrepareSweeping(); 494514f5e3Sopenharmony_ci } 504514f5e3Sopenharmony_ci heap_->GetNonMovableSpace()->PrepareSweeping(); 514514f5e3Sopenharmony_ci heap_->GetMachineCodeSpace()->PrepareSweeping(); 524514f5e3Sopenharmony_ci // Prepare 534514f5e3Sopenharmony_ci isSweeping_ = true; 544514f5e3Sopenharmony_ci startSpaceType_ = fullGC ? NON_MOVABLE : OLD_SPACE; 554514f5e3Sopenharmony_ci for (int type = startSpaceType_; type < FREE_LIST_NUM; type++) { 564514f5e3Sopenharmony_ci remainingTaskNum_[type] = FREE_LIST_NUM - startSpaceType_; 574514f5e3Sopenharmony_ci } 584514f5e3Sopenharmony_ci } else { 594514f5e3Sopenharmony_ci if (!fullGC) { 604514f5e3Sopenharmony_ci heap_->GetOldSpace()->Sweep(); 614514f5e3Sopenharmony_ci } 624514f5e3Sopenharmony_ci heap_->GetNonMovableSpace()->Sweep(); 634514f5e3Sopenharmony_ci heap_->GetMachineCodeSpace()->Sweep(); 644514f5e3Sopenharmony_ci } 654514f5e3Sopenharmony_ci heap_->GetHugeObjectSpace()->Sweep(); 664514f5e3Sopenharmony_ci heap_->GetHugeMachineCodeSpace()->Sweep(); 674514f5e3Sopenharmony_ci} 684514f5e3Sopenharmony_ci 694514f5e3Sopenharmony_civoid ConcurrentSweeper::AsyncSweepSpace(MemSpaceType type, bool isMain) 704514f5e3Sopenharmony_ci{ 714514f5e3Sopenharmony_ci auto space = heap_->GetSpaceWithType(type); 724514f5e3Sopenharmony_ci space->AsyncSweep(isMain); 734514f5e3Sopenharmony_ci 744514f5e3Sopenharmony_ci LockHolder holder(mutexs_[type]); 754514f5e3Sopenharmony_ci if (--remainingTaskNum_[type] == 0) { 764514f5e3Sopenharmony_ci cvs_[type].SignalAll(); 774514f5e3Sopenharmony_ci } 784514f5e3Sopenharmony_ci} 794514f5e3Sopenharmony_ci 804514f5e3Sopenharmony_civoid ConcurrentSweeper::WaitAllTaskFinished() 814514f5e3Sopenharmony_ci{ 824514f5e3Sopenharmony_ci if (!isSweeping_) { 834514f5e3Sopenharmony_ci return; 844514f5e3Sopenharmony_ci } 854514f5e3Sopenharmony_ci for (int i = startSpaceType_; i < FREE_LIST_NUM; i++) { 864514f5e3Sopenharmony_ci if (remainingTaskNum_[i] > 0) { 874514f5e3Sopenharmony_ci LockHolder holder(mutexs_[i]); 884514f5e3Sopenharmony_ci while (remainingTaskNum_[i] > 0) { 894514f5e3Sopenharmony_ci cvs_[i].Wait(&mutexs_[i]); 904514f5e3Sopenharmony_ci } 914514f5e3Sopenharmony_ci } 924514f5e3Sopenharmony_ci } 934514f5e3Sopenharmony_ci} 944514f5e3Sopenharmony_ci 954514f5e3Sopenharmony_civoid ConcurrentSweeper::EnsureAllTaskFinished() 964514f5e3Sopenharmony_ci{ 974514f5e3Sopenharmony_ci if (!isSweeping_) { 984514f5e3Sopenharmony_ci return; 994514f5e3Sopenharmony_ci } 1004514f5e3Sopenharmony_ci for (int i = startSpaceType_; i < FREE_LIST_NUM; i++) { 1014514f5e3Sopenharmony_ci WaitingTaskFinish(static_cast<MemSpaceType>(i)); 1024514f5e3Sopenharmony_ci } 1034514f5e3Sopenharmony_ci isSweeping_ = false; 1044514f5e3Sopenharmony_ci if (IsRequestDisabled()) { 1054514f5e3Sopenharmony_ci enableType_ = EnableConcurrentSweepType::DISABLE; 1064514f5e3Sopenharmony_ci } 1074514f5e3Sopenharmony_ci} 1084514f5e3Sopenharmony_ci 1094514f5e3Sopenharmony_civoid ConcurrentSweeper::EnsureTaskFinished(MemSpaceType type) 1104514f5e3Sopenharmony_ci{ 1114514f5e3Sopenharmony_ci CHECK_JS_THREAD(heap_->GetEcmaVM()); 1124514f5e3Sopenharmony_ci EnsureTaskFinishedNoCheck(type); 1134514f5e3Sopenharmony_ci} 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_civoid ConcurrentSweeper::EnsureTaskFinishedNoCheck(MemSpaceType type) 1164514f5e3Sopenharmony_ci{ 1174514f5e3Sopenharmony_ci if (!isSweeping_) { 1184514f5e3Sopenharmony_ci return; 1194514f5e3Sopenharmony_ci } 1204514f5e3Sopenharmony_ci WaitingTaskFinish(type); 1214514f5e3Sopenharmony_ci} 1224514f5e3Sopenharmony_ci 1234514f5e3Sopenharmony_civoid ConcurrentSweeper::WaitingTaskFinish(MemSpaceType type) 1244514f5e3Sopenharmony_ci{ 1254514f5e3Sopenharmony_ci if (remainingTaskNum_[type] > 0) { 1264514f5e3Sopenharmony_ci { 1274514f5e3Sopenharmony_ci LockHolder holder(mutexs_[type]); 1284514f5e3Sopenharmony_ci remainingTaskNum_[type]++; 1294514f5e3Sopenharmony_ci } 1304514f5e3Sopenharmony_ci AsyncSweepSpace(type, true); 1314514f5e3Sopenharmony_ci LockHolder holder(mutexs_[type]); 1324514f5e3Sopenharmony_ci while (remainingTaskNum_[type] > 0) { 1334514f5e3Sopenharmony_ci cvs_[type].Wait(&mutexs_[type]); 1344514f5e3Sopenharmony_ci } 1354514f5e3Sopenharmony_ci } 1364514f5e3Sopenharmony_ci SparseSpace *space = heap_->GetSpaceWithType(type); 1374514f5e3Sopenharmony_ci space->FinishFillSweptRegion(); 1384514f5e3Sopenharmony_ci} 1394514f5e3Sopenharmony_ci 1404514f5e3Sopenharmony_civoid ConcurrentSweeper::TryFillSweptRegion() 1414514f5e3Sopenharmony_ci{ 1424514f5e3Sopenharmony_ci for (int i = startSpaceType_; i < FREE_LIST_NUM; i++) { 1434514f5e3Sopenharmony_ci SparseSpace *space = heap_->GetSpaceWithType(static_cast<MemSpaceType>(i)); 1444514f5e3Sopenharmony_ci space->TryFillSweptRegion(); 1454514f5e3Sopenharmony_ci } 1464514f5e3Sopenharmony_ci} 1474514f5e3Sopenharmony_ci 1484514f5e3Sopenharmony_civoid ConcurrentSweeper::ClearRSetInRange(Region *current, uintptr_t freeStart, uintptr_t freeEnd) 1494514f5e3Sopenharmony_ci{ 1504514f5e3Sopenharmony_ci if (ConcurrentSweepEnabled()) { 1514514f5e3Sopenharmony_ci // This clear may exist data race with array and jsobject trim, so use CAS 1524514f5e3Sopenharmony_ci current->AtomicClearSweepingOldToNewRSetInRange(freeStart, freeEnd); 1534514f5e3Sopenharmony_ci current->AtomicClearSweepingLocalToShareRSetInRange(freeStart, freeEnd); 1544514f5e3Sopenharmony_ci } else { 1554514f5e3Sopenharmony_ci current->ClearOldToNewRSetInRange(freeStart, freeEnd); 1564514f5e3Sopenharmony_ci current->ClearLocalToShareRSetInRange(freeStart, freeEnd); 1574514f5e3Sopenharmony_ci } 1584514f5e3Sopenharmony_ci current->ClearCrossRegionRSetInRange(freeStart, freeEnd); 1594514f5e3Sopenharmony_ci} 1604514f5e3Sopenharmony_ci 1614514f5e3Sopenharmony_cibool ConcurrentSweeper::SweeperTask::Run([[maybe_unused]] uint32_t threadIndex) 1624514f5e3Sopenharmony_ci{ 1634514f5e3Sopenharmony_ci uint32_t sweepTypeNum = FREE_LIST_NUM - sweeper_->startSpaceType_; 1644514f5e3Sopenharmony_ci for (size_t i = sweeper_->startSpaceType_; i < FREE_LIST_NUM; i++) { 1654514f5e3Sopenharmony_ci auto type = static_cast<MemSpaceType>(((i + type_) % sweepTypeNum) + sweeper_->startSpaceType_); 1664514f5e3Sopenharmony_ci sweeper_->AsyncSweepSpace(type, false); 1674514f5e3Sopenharmony_ci } 1684514f5e3Sopenharmony_ci return true; 1694514f5e3Sopenharmony_ci} 1704514f5e3Sopenharmony_ci 1714514f5e3Sopenharmony_civoid ConcurrentSweeper::EnableConcurrentSweep(EnableConcurrentSweepType type) 1724514f5e3Sopenharmony_ci{ 1734514f5e3Sopenharmony_ci if (IsConfigDisabled()) { 1744514f5e3Sopenharmony_ci return; 1754514f5e3Sopenharmony_ci } 1764514f5e3Sopenharmony_ci if (ConcurrentSweepEnabled() && isSweeping_ && type == EnableConcurrentSweepType::DISABLE) { 1774514f5e3Sopenharmony_ci enableType_ = EnableConcurrentSweepType::REQUEST_DISABLE; 1784514f5e3Sopenharmony_ci } else { 1794514f5e3Sopenharmony_ci enableType_ = type; 1804514f5e3Sopenharmony_ci } 1814514f5e3Sopenharmony_ci} 1824514f5e3Sopenharmony_ci} // namespace panda::ecmascript 183