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/full_gc.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/mem/concurrent_marker.h" 194514f5e3Sopenharmony_ci#include "ecmascript/mem/incremental_marker.h" 204514f5e3Sopenharmony_ci#include "ecmascript/mem/parallel_marker-inl.h" 214514f5e3Sopenharmony_ci#include "ecmascript/mem/verification.h" 224514f5e3Sopenharmony_ci#include "ecmascript/runtime_call_id.h" 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_cinamespace panda::ecmascript { 254514f5e3Sopenharmony_ciFullGC::FullGC(Heap *heap) : heap_(heap), workManager_(heap->GetWorkManager()) {} 264514f5e3Sopenharmony_ci 274514f5e3Sopenharmony_civoid FullGC::RunPhases() 284514f5e3Sopenharmony_ci{ 294514f5e3Sopenharmony_ci GCStats *gcStats = heap_->GetEcmaVM()->GetEcmaGCStats(); 304514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::RunPhases;Reason" 314514f5e3Sopenharmony_ci + std::to_string(static_cast<int>(gcStats->GetGCReason())) 324514f5e3Sopenharmony_ci + ";Sensitive" + std::to_string(static_cast<int>(heap_->GetSensitiveStatus())) 334514f5e3Sopenharmony_ci + ";IsInBackground" + std::to_string(heap_->IsInBackground()) 344514f5e3Sopenharmony_ci + ";Startup" + std::to_string(heap_->OnStartupEvent()) 354514f5e3Sopenharmony_ci + ";Young" + std::to_string(heap_->GetNewSpace()->GetCommittedSize()) 364514f5e3Sopenharmony_ci + ";Old" + std::to_string(heap_->GetOldSpace()->GetCommittedSize()) 374514f5e3Sopenharmony_ci + ";huge" + std::to_string(heap_->GetHugeObjectSpace()->GetCommittedSize()) 384514f5e3Sopenharmony_ci + ";NonMov" + std::to_string(heap_->GetNonMovableSpace()->GetCommittedSize()) 394514f5e3Sopenharmony_ci + ";TotCommit" + std::to_string(heap_->GetCommittedSize())); 404514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::TotalGC, gcStats); 414514f5e3Sopenharmony_ci MEM_ALLOCATE_AND_GC_TRACE(heap_->GetEcmaVM(), FullGC_RunPhases); 424514f5e3Sopenharmony_ci 434514f5e3Sopenharmony_ci if (heap_->CheckOngoingConcurrentMarking()) { 444514f5e3Sopenharmony_ci LOG_GC(DEBUG) << "FullGC after ConcurrentMarking"; 454514f5e3Sopenharmony_ci heap_->GetConcurrentMarker()->Reset(); // HPPGC use mark result to move TaggedObject. 464514f5e3Sopenharmony_ci } 474514f5e3Sopenharmony_ci 484514f5e3Sopenharmony_ci if (heap_->GetIncrementalMarker()->IsTriggeredIncrementalMark()) { 494514f5e3Sopenharmony_ci LOG_GC(DEBUG) << "FullGC after IncrementalMarking"; 504514f5e3Sopenharmony_ci heap_->ClearIdleTask(); 514514f5e3Sopenharmony_ci heap_->DisableNotifyIdle(); 524514f5e3Sopenharmony_ci heap_->GetIncrementalMarker()->Reset(); 534514f5e3Sopenharmony_ci } 544514f5e3Sopenharmony_ci ProcessSharedGCRSetWorkList(); 554514f5e3Sopenharmony_ci Initialize(); 564514f5e3Sopenharmony_ci Mark(); 574514f5e3Sopenharmony_ci Sweep(); 584514f5e3Sopenharmony_ci Finish(); 594514f5e3Sopenharmony_ci if (UNLIKELY(heap_->ShouldVerifyHeap())) { 604514f5e3Sopenharmony_ci // verify mark 614514f5e3Sopenharmony_ci LOG_ECMA(DEBUG) << "start verify post fullgc"; 624514f5e3Sopenharmony_ci Verification(heap_, VerifyKind::VERIFY_SHARED_RSET_POST_FULL_GC).VerifyAll(); 634514f5e3Sopenharmony_ci } 644514f5e3Sopenharmony_ci} 654514f5e3Sopenharmony_ci 664514f5e3Sopenharmony_civoid FullGC::RunPhasesForAppSpawn() 674514f5e3Sopenharmony_ci{ 684514f5e3Sopenharmony_ci auto marker = reinterpret_cast<CompressGCMarker*>(heap_->GetCompressGCMarker()); 694514f5e3Sopenharmony_ci marker->SetAppSpawn(true); 704514f5e3Sopenharmony_ci RunPhases(); 714514f5e3Sopenharmony_ci marker->SetAppSpawn(false); 724514f5e3Sopenharmony_ci} 734514f5e3Sopenharmony_ci 744514f5e3Sopenharmony_civoid FullGC::Initialize() 754514f5e3Sopenharmony_ci{ 764514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Initialize"); 774514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::Initialize, heap_->GetEcmaVM()->GetEcmaGCStats()); 784514f5e3Sopenharmony_ci heap_->Prepare(); 794514f5e3Sopenharmony_ci auto callback = [](Region *current) { 804514f5e3Sopenharmony_ci current->ResetAliveObject(); 814514f5e3Sopenharmony_ci current->ClearOldToNewRSet(); 824514f5e3Sopenharmony_ci }; 834514f5e3Sopenharmony_ci heap_->EnumerateNonMovableRegions(callback); 844514f5e3Sopenharmony_ci heap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) { 854514f5e3Sopenharmony_ci current->ClearMarkGCBitset(); 864514f5e3Sopenharmony_ci current->ClearCrossRegionRSet(); 874514f5e3Sopenharmony_ci }); 884514f5e3Sopenharmony_ci youngSpaceCommitSize_ = heap_->GetNewSpace()->GetCommittedSize(); 894514f5e3Sopenharmony_ci heap_->SwapNewSpace(); 904514f5e3Sopenharmony_ci workManager_->Initialize(TriggerGCType::FULL_GC, ParallelGCTaskPhase::COMPRESS_HANDLE_GLOBAL_POOL_TASK); 914514f5e3Sopenharmony_ci heap_->GetCompressGCMarker()->Initialize(); 924514f5e3Sopenharmony_ci 934514f5e3Sopenharmony_ci youngAndOldAliveSize_ = 0; 944514f5e3Sopenharmony_ci nonMoveSpaceFreeSize_ = 0; 954514f5e3Sopenharmony_ci oldSpaceCommitSize_ = heap_->GetOldSpace()->GetCommittedSize(); 964514f5e3Sopenharmony_ci nonMoveSpaceCommitSize_ = heap_->GetNonMovableSpace()->GetCommittedSize(); 974514f5e3Sopenharmony_ci} 984514f5e3Sopenharmony_ci 994514f5e3Sopenharmony_civoid FullGC::Mark() 1004514f5e3Sopenharmony_ci{ 1014514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Mark"); 1024514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::Mark, heap_->GetEcmaVM()->GetEcmaGCStats()); 1034514f5e3Sopenharmony_ci heap_->GetCompressGCMarker()->MarkRoots(MAIN_THREAD_INDEX, VMRootVisitType::UPDATE_ROOT); 1044514f5e3Sopenharmony_ci heap_->GetCompressGCMarker()->ProcessMarkStack(MAIN_THREAD_INDEX); 1054514f5e3Sopenharmony_ci heap_->WaitRunningTaskFinished(); 1064514f5e3Sopenharmony_ci // MarkJitCodeMap must be call after other mark work finish to make sure which jserror object js alive. 1074514f5e3Sopenharmony_ci heap_->GetCompressGCMarker()->MarkJitCodeMap(MAIN_THREAD_INDEX); 1084514f5e3Sopenharmony_ci} 1094514f5e3Sopenharmony_ci 1104514f5e3Sopenharmony_civoid FullGC::Sweep() 1114514f5e3Sopenharmony_ci{ 1124514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Sweep"); 1134514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::Sweep, heap_->GetEcmaVM()->GetEcmaGCStats()); 1144514f5e3Sopenharmony_ci // process weak reference 1154514f5e3Sopenharmony_ci uint32_t totalThreadCount = 1; // 1 : mainthread 1164514f5e3Sopenharmony_ci if (heap_->IsParallelGCEnabled()) { 1174514f5e3Sopenharmony_ci totalThreadCount += Taskpool::GetCurrentTaskpool()->GetTotalThreadNum(); 1184514f5e3Sopenharmony_ci } 1194514f5e3Sopenharmony_ci for (uint32_t i = 0; i < totalThreadCount; i++) { 1204514f5e3Sopenharmony_ci ProcessQueue *queue = workManager_->GetWeakReferenceQueue(i); 1214514f5e3Sopenharmony_ci 1224514f5e3Sopenharmony_ci while (true) { 1234514f5e3Sopenharmony_ci auto obj = queue->PopBack(); 1244514f5e3Sopenharmony_ci if (UNLIKELY(obj == nullptr)) { 1254514f5e3Sopenharmony_ci break; 1264514f5e3Sopenharmony_ci } 1274514f5e3Sopenharmony_ci ObjectSlot slot(ToUintPtr(obj)); 1284514f5e3Sopenharmony_ci JSTaggedValue value(slot.GetTaggedType()); 1294514f5e3Sopenharmony_ci auto header = value.GetTaggedWeakRef(); 1304514f5e3Sopenharmony_ci 1314514f5e3Sopenharmony_ci Region *objectRegion = Region::ObjectAddressToRange(header); 1324514f5e3Sopenharmony_ci if (!HasEvacuated(objectRegion)) { 1334514f5e3Sopenharmony_ci if (!objectRegion->InSharedHeap() && !objectRegion->Test(header)) { 1344514f5e3Sopenharmony_ci slot.Clear(); 1354514f5e3Sopenharmony_ci } 1364514f5e3Sopenharmony_ci } else { 1374514f5e3Sopenharmony_ci MarkWord markWord(header); 1384514f5e3Sopenharmony_ci if (markWord.IsForwardingAddress()) { 1394514f5e3Sopenharmony_ci TaggedObject *dst = markWord.ToForwardingAddress(); 1404514f5e3Sopenharmony_ci auto weakRef = JSTaggedValue(JSTaggedValue(dst).CreateAndGetWeakRef()).GetRawTaggedObject(); 1414514f5e3Sopenharmony_ci slot.Update(weakRef); 1424514f5e3Sopenharmony_ci } else { 1434514f5e3Sopenharmony_ci slot.Update(static_cast<JSTaggedType>(JSTaggedValue::Undefined().GetRawData())); 1444514f5e3Sopenharmony_ci } 1454514f5e3Sopenharmony_ci } 1464514f5e3Sopenharmony_ci } 1474514f5e3Sopenharmony_ci } 1484514f5e3Sopenharmony_ci 1494514f5e3Sopenharmony_ci WeakRootVisitor gcUpdateWeak = [this](TaggedObject *header) -> TaggedObject* { 1504514f5e3Sopenharmony_ci Region *objectRegion = Region::ObjectAddressToRange(header); 1514514f5e3Sopenharmony_ci if (UNLIKELY(objectRegion == nullptr)) { 1524514f5e3Sopenharmony_ci LOG_GC(ERROR) << "FullGC updateWeakReference: region is nullptr, header is " << header; 1534514f5e3Sopenharmony_ci return nullptr; 1544514f5e3Sopenharmony_ci } 1554514f5e3Sopenharmony_ci if (!HasEvacuated(objectRegion)) { 1564514f5e3Sopenharmony_ci // The weak object in shared heap is always alive during fullGC. 1574514f5e3Sopenharmony_ci if (objectRegion->InSharedHeap() || objectRegion->Test(header)) { 1584514f5e3Sopenharmony_ci return header; 1594514f5e3Sopenharmony_ci } 1604514f5e3Sopenharmony_ci return nullptr; 1614514f5e3Sopenharmony_ci } 1624514f5e3Sopenharmony_ci 1634514f5e3Sopenharmony_ci MarkWord markWord(header); 1644514f5e3Sopenharmony_ci if (markWord.IsForwardingAddress()) { 1654514f5e3Sopenharmony_ci return markWord.ToForwardingAddress(); 1664514f5e3Sopenharmony_ci } 1674514f5e3Sopenharmony_ci return nullptr; 1684514f5e3Sopenharmony_ci }; 1694514f5e3Sopenharmony_ci heap_->GetEcmaVM()->GetJSThread()->IterateWeakEcmaGlobalStorage(gcUpdateWeak); 1704514f5e3Sopenharmony_ci heap_->GetEcmaVM()->ProcessReferences(gcUpdateWeak); 1714514f5e3Sopenharmony_ci 1724514f5e3Sopenharmony_ci heap_->GetSweeper()->Sweep(true); 1734514f5e3Sopenharmony_ci heap_->GetSweeper()->PostTask(true); 1744514f5e3Sopenharmony_ci} 1754514f5e3Sopenharmony_ci 1764514f5e3Sopenharmony_civoid FullGC::Finish() 1774514f5e3Sopenharmony_ci{ 1784514f5e3Sopenharmony_ci ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Finish"); 1794514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::Finish, heap_->GetEcmaVM()->GetEcmaGCStats()); 1804514f5e3Sopenharmony_ci if (!forAppSpawn_) { 1814514f5e3Sopenharmony_ci heap_->SwapOldSpace(); 1824514f5e3Sopenharmony_ci } 1834514f5e3Sopenharmony_ci youngAndOldAliveSize_ = workManager_->Finish(); 1844514f5e3Sopenharmony_ci if (forAppSpawn_) { 1854514f5e3Sopenharmony_ci heap_->ResumeForAppSpawn(); 1864514f5e3Sopenharmony_ci } else { 1874514f5e3Sopenharmony_ci heap_->Resume(FULL_GC); 1884514f5e3Sopenharmony_ci } 1894514f5e3Sopenharmony_ci heap_->GetSweeper()->TryFillSweptRegion(); 1904514f5e3Sopenharmony_ci} 1914514f5e3Sopenharmony_ci 1924514f5e3Sopenharmony_cibool FullGC::HasEvacuated(Region *region) 1934514f5e3Sopenharmony_ci{ 1944514f5e3Sopenharmony_ci auto marker = reinterpret_cast<CompressGCMarker*>(heap_->GetCompressGCMarker()); 1954514f5e3Sopenharmony_ci return marker->NeedEvacuate(region); 1964514f5e3Sopenharmony_ci} 1974514f5e3Sopenharmony_ci 1984514f5e3Sopenharmony_civoid FullGC::SetForAppSpawn(bool flag) 1994514f5e3Sopenharmony_ci{ 2004514f5e3Sopenharmony_ci forAppSpawn_ = flag; 2014514f5e3Sopenharmony_ci} 2024514f5e3Sopenharmony_ci 2034514f5e3Sopenharmony_ciARK_INLINE void FullGC::ProcessSharedGCRSetWorkList() 2044514f5e3Sopenharmony_ci{ 2054514f5e3Sopenharmony_ci TRACE_GC(GCStats::Scope::ScopeId::ProcessSharedGCRSetWorkList, heap_->GetEcmaVM()->GetEcmaGCStats()); 2064514f5e3Sopenharmony_ci heap_->ProcessSharedGCRSetWorkList(); 2074514f5e3Sopenharmony_ci} 2084514f5e3Sopenharmony_ci} // namespace panda::ecmascript 209