14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2024 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/shared_heap/shared_gc.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/mem/shared_heap/shared_concurrent_marker.h" 194514f5e3Sopenharmony_ci#include "ecmascript/mem/shared_heap/shared_concurrent_sweeper.h" 204514f5e3Sopenharmony_ci#include "ecmascript/mem/shared_heap/shared_gc_marker-inl.h" 214514f5e3Sopenharmony_ci#include "ecmascript/mem/verification.h" 224514f5e3Sopenharmony_ci#include "ecmascript/runtime.h" 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_cinamespace panda::ecmascript { 254514f5e3Sopenharmony_civoid SharedGC::RunPhases() 264514f5e3Sopenharmony_ci{ 274514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedGC::RunPhases" 284514f5e3Sopenharmony_ci + std::to_string(static_cast<int>(sHeap_->GetEcmaGCStats()->GetGCReason())) 294514f5e3Sopenharmony_ci + ";Sensitive" + std::to_string(static_cast<int>(sHeap_->GetSensitiveStatus())) 304514f5e3Sopenharmony_ci + ";IsInBackground" + std::to_string(sHeap_->IsInBackground()) 314514f5e3Sopenharmony_ci + ";Startup" + std::to_string(sHeap_->OnStartupEvent()) 324514f5e3Sopenharmony_ci + ";Old" + std::to_string(sHeap_->GetOldSpace()->GetCommittedSize()) 334514f5e3Sopenharmony_ci + ";huge" + std::to_string(sHeap_->GetHugeObjectSpace()->GetCommittedSize()) 344514f5e3Sopenharmony_ci + ";NonMov" + std::to_string(sHeap_->GetNonMovableSpace()->GetCommittedSize()) 354514f5e3Sopenharmony_ci + ";TotCommit" + std::to_string(sHeap_->GetCommittedSize())); 364514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::TotalGC, sHeap_->GetEcmaGCStats()); 374514f5e3Sopenharmony_ci markingInProgress_ = sHeap_->CheckOngoingConcurrentMarking(); 384514f5e3Sopenharmony_ci Initialize(); 394514f5e3Sopenharmony_ci Mark(); 404514f5e3Sopenharmony_ci if (UNLIKELY(sHeap_->ShouldVerifyHeap())) { 414514f5e3Sopenharmony_ci // verify mark 424514f5e3Sopenharmony_ci LOG_ECMA(DEBUG) << "start verify mark"; 434514f5e3Sopenharmony_ci SharedHeapVerification(sHeap_, VerifyKind::VERIFY_SHARED_GC_MARK).VerifyMark(markingInProgress_); 444514f5e3Sopenharmony_ci } 454514f5e3Sopenharmony_ci Sweep(); 464514f5e3Sopenharmony_ci if (UNLIKELY(sHeap_->ShouldVerifyHeap())) { 474514f5e3Sopenharmony_ci // verify sweep 484514f5e3Sopenharmony_ci LOG_ECMA(DEBUG) << "start verify sweep"; 494514f5e3Sopenharmony_ci SharedHeapVerification(sHeap_, VerifyKind::VERIFY_SHARED_GC_SWEEP).VerifySweep(markingInProgress_); 504514f5e3Sopenharmony_ci } 514514f5e3Sopenharmony_ci Finish(); 524514f5e3Sopenharmony_ci sHeap_->ResetNativeSizeAfterLastGC(); 534514f5e3Sopenharmony_ci} 544514f5e3Sopenharmony_ci 554514f5e3Sopenharmony_civoid SharedGC::Initialize() 564514f5e3Sopenharmony_ci{ 574514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedGC::Initialize"); 584514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::Initialize, sHeap_->GetEcmaGCStats()); 594514f5e3Sopenharmony_ci if (!markingInProgress_) { 604514f5e3Sopenharmony_ci sHeap_->Prepare(true); 614514f5e3Sopenharmony_ci sHeap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) { 624514f5e3Sopenharmony_ci current->ClearMarkGCBitset(); 634514f5e3Sopenharmony_ci }); 644514f5e3Sopenharmony_ci sHeap_->EnumerateOldSpaceRegions([](Region *current) { 654514f5e3Sopenharmony_ci ASSERT(current->InSharedSweepableSpace()); 664514f5e3Sopenharmony_ci current->ResetAliveObject(); 674514f5e3Sopenharmony_ci }); 684514f5e3Sopenharmony_ci sWorkManager_->Initialize(TriggerGCType::SHARED_GC, SharedParallelMarkPhase::SHARED_MARK_TASK); 694514f5e3Sopenharmony_ci } 704514f5e3Sopenharmony_ci} 714514f5e3Sopenharmony_civoid SharedGC::Mark() 724514f5e3Sopenharmony_ci{ 734514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedGC::Mark"); 744514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::Mark, sHeap_->GetEcmaGCStats()); 754514f5e3Sopenharmony_ci if (markingInProgress_) { 764514f5e3Sopenharmony_ci sHeap_->GetConcurrentMarker()->ReMark(); 774514f5e3Sopenharmony_ci return; 784514f5e3Sopenharmony_ci } 794514f5e3Sopenharmony_ci SharedGCMarker *marker = sHeap_->GetSharedGCMarker(); 804514f5e3Sopenharmony_ci marker->MarkRoots(DAEMON_THREAD_INDEX, SharedMarkType::NOT_CONCURRENT_MARK); 814514f5e3Sopenharmony_ci marker->DoMark<SharedMarkType::NOT_CONCURRENT_MARK>(DAEMON_THREAD_INDEX); 824514f5e3Sopenharmony_ci marker->MergeBackAndResetRSetWorkListHandler(); 834514f5e3Sopenharmony_ci sHeap_->WaitRunningTaskFinished(); 844514f5e3Sopenharmony_ci} 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_civoid SharedGC::Sweep() 874514f5e3Sopenharmony_ci{ 884514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedGC::Sweep"); 894514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::Sweep, sHeap_->GetEcmaGCStats()); 904514f5e3Sopenharmony_ci UpdateRecordWeakReference(); 914514f5e3Sopenharmony_ci WeakRootVisitor gcUpdateWeak = [](TaggedObject *header) -> TaggedObject* { 924514f5e3Sopenharmony_ci Region *objectRegion = Region::ObjectAddressToRange(header); 934514f5e3Sopenharmony_ci if (UNLIKELY(objectRegion == nullptr)) { 944514f5e3Sopenharmony_ci LOG_GC(ERROR) << "SharedGC updateWeakReference: region is nullptr, header is " << header; 954514f5e3Sopenharmony_ci return nullptr; 964514f5e3Sopenharmony_ci } 974514f5e3Sopenharmony_ci if (!objectRegion->InSharedSweepableSpace() || objectRegion->Test(header)) { 984514f5e3Sopenharmony_ci return header; 994514f5e3Sopenharmony_ci } 1004514f5e3Sopenharmony_ci return nullptr; 1014514f5e3Sopenharmony_ci }; 1024514f5e3Sopenharmony_ci auto stringTableCleaner = Runtime::GetInstance()->GetEcmaStringTable()->GetCleaner(); 1034514f5e3Sopenharmony_ci stringTableCleaner->PostSweepWeakRefTask(gcUpdateWeak); 1044514f5e3Sopenharmony_ci Runtime::GetInstance()->ProcessNativeDeleteInSharedGC(gcUpdateWeak); 1054514f5e3Sopenharmony_ci Runtime::GetInstance()->ProcessSharedNativeDelete(gcUpdateWeak); 1064514f5e3Sopenharmony_ci 1074514f5e3Sopenharmony_ci Runtime::GetInstance()->GCIterateThreadList([&](JSThread *thread) { 1084514f5e3Sopenharmony_ci ASSERT(!thread->IsInRunningState()); 1094514f5e3Sopenharmony_ci thread->IterateWeakEcmaGlobalStorage(gcUpdateWeak, GCKind::SHARED_GC); 1104514f5e3Sopenharmony_ci const_cast<Heap*>(thread->GetEcmaVM()->GetHeap())->ResetTlab(); 1114514f5e3Sopenharmony_ci }); 1124514f5e3Sopenharmony_ci 1134514f5e3Sopenharmony_ci stringTableCleaner->JoinAndWaitSweepWeakRefTask(gcUpdateWeak); 1144514f5e3Sopenharmony_ci sHeap_->GetSweeper()->Sweep(false); 1154514f5e3Sopenharmony_ci sHeap_->GetSweeper()->PostTask(false); 1164514f5e3Sopenharmony_ci} 1174514f5e3Sopenharmony_ci 1184514f5e3Sopenharmony_civoid SharedGC::Finish() 1194514f5e3Sopenharmony_ci{ 1204514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "SharedGC::Finish"); 1214514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::Finish, sHeap_->GetEcmaGCStats()); 1224514f5e3Sopenharmony_ci sHeap_->Reclaim(TriggerGCType::SHARED_GC); 1234514f5e3Sopenharmony_ci if (markingInProgress_) { 1244514f5e3Sopenharmony_ci sHeap_->GetConcurrentMarker()->Reset(false); 1254514f5e3Sopenharmony_ci } else { 1264514f5e3Sopenharmony_ci sWorkManager_->Finish(); 1274514f5e3Sopenharmony_ci } 1284514f5e3Sopenharmony_ci sHeap_->GetSweeper()->TryFillSweptRegion(); 1294514f5e3Sopenharmony_ci} 1304514f5e3Sopenharmony_ci 1314514f5e3Sopenharmony_civoid SharedGC::UpdateRecordWeakReference() 1324514f5e3Sopenharmony_ci{ 1334514f5e3Sopenharmony_ci auto totalThreadCount = Taskpool::GetCurrentTaskpool()->GetTotalThreadNum() + 1; 1344514f5e3Sopenharmony_ci for (uint32_t i = 0; i < totalThreadCount; i++) { 1354514f5e3Sopenharmony_ci ProcessQueue *queue = sHeap_->GetWorkManager()->GetWeakReferenceQueue(i); 1364514f5e3Sopenharmony_ci 1374514f5e3Sopenharmony_ci while (true) { 1384514f5e3Sopenharmony_ci auto obj = queue->PopBack(); 1394514f5e3Sopenharmony_ci if (UNLIKELY(obj == nullptr)) { 1404514f5e3Sopenharmony_ci break; 1414514f5e3Sopenharmony_ci } 1424514f5e3Sopenharmony_ci ObjectSlot slot(ToUintPtr(obj)); 1434514f5e3Sopenharmony_ci JSTaggedValue value(slot.GetTaggedType()); 1444514f5e3Sopenharmony_ci if (value.IsWeak()) { 1454514f5e3Sopenharmony_ci auto header = value.GetTaggedWeakRef(); 1464514f5e3Sopenharmony_ci Region *objectRegion = Region::ObjectAddressToRange(header); 1474514f5e3Sopenharmony_ci if (!objectRegion->Test(header)) { 1484514f5e3Sopenharmony_ci slot.Clear(); 1494514f5e3Sopenharmony_ci } 1504514f5e3Sopenharmony_ci } 1514514f5e3Sopenharmony_ci } 1524514f5e3Sopenharmony_ci } 1534514f5e3Sopenharmony_ci} 1544514f5e3Sopenharmony_ci 1554514f5e3Sopenharmony_civoid SharedGC::ResetWorkManager(SharedGCWorkManager *sWorkManager) 1564514f5e3Sopenharmony_ci{ 1574514f5e3Sopenharmony_ci sWorkManager_ = sWorkManager; 1584514f5e3Sopenharmony_ci} 1594514f5e3Sopenharmony_ci} // namespace panda::ecmascript 160