arkcompiler_ets_runtime/ecmascript/mem/full_gc.cpp
chentianyu 5fd527c8a1 Add trace info for FullGC
Signed-off-by: chentianyu <chentianyu31@huawei.com>
Change-Id: Iac95cc284deda7af61036d2617da6bb36c730970
2024-03-30 16:28:16 +08:00

210 lines
7.8 KiB
C++

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/mem/full_gc.h"
#include "ecmascript/ecma_vm.h"
#include "ecmascript/mem/clock_scope.h"
#include "ecmascript/mem/concurrent_marker.h"
#include "ecmascript/mem/concurrent_sweeper.h"
#include "ecmascript/mem/heap-inl.h"
#include "ecmascript/mem/incremental_marker.h"
#include "ecmascript/mem/mark_stack.h"
#include "ecmascript/mem/mem.h"
#include "ecmascript/mem/parallel_marker-inl.h"
#include "ecmascript/mem/space-inl.h"
#include "ecmascript/mem/verification.h"
#include "ecmascript/mem/visitor.h"
#include "ecmascript/mem/gc_stats.h"
#include "ecmascript/ecma_string_table.h"
#include "ecmascript/runtime_call_id.h"
namespace panda::ecmascript {
FullGC::FullGC(Heap *heap) : heap_(heap), workManager_(heap->GetWorkManager()) {}
void FullGC::RunPhases()
{
GCStats *gcStats = heap_->GetEcmaVM()->GetEcmaGCStats();
ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::RunPhases;Reason"
+ std::to_string(static_cast<int>(gcStats->GetGCReason()))
+ ";Sensitive" + std::to_string(static_cast<int>(heap_->GetSensitiveStatus()))
+ ";Startup" + std::to_string(heap_->onStartUpEvent())
+ ";Young" + std::to_string(heap_->GetNewSpace()->GetCommittedSize())
+ ";Old" + std::to_string(heap_->GetOldSpace()->GetCommittedSize())
+ ";huge" + std::to_string(heap_->GetHugeObjectSpace()->GetCommittedSize())
+ ";NonMov" + std::to_string(heap_->GetNonMovableSpace()->GetCommittedSize())
+ ";TotCommit" + std::to_string(heap_->GetCommittedSize()));
TRACE_GC(GCStats::Scope::ScopeId::TotalGC, gcStats);
MEM_ALLOCATE_AND_GC_TRACE(heap_->GetEcmaVM(), FullGC_RunPhases);
if (heap_->CheckOngoingConcurrentMarking()) {
LOG_GC(DEBUG) << "FullGC after ConcurrentMarking";
heap_->GetConcurrentMarker()->Reset(); // HPPGC use mark result to move TaggedObject.
}
if (heap_->GetIncrementalMarker()->IsTriggeredIncrementalMark()) {
LOG_GC(DEBUG) << "FullGC after IncrementalMarking";
heap_->ClearIdleTask();
heap_->DisableNotifyIdle();
heap_->GetIncrementalMarker()->Reset();
}
Initialize();
Mark();
Sweep();
Finish();
if (UNLIKELY(heap_->ShouldVerifyHeap())) {
// verify mark
LOG_ECMA(DEBUG) << "start verify post fullgc";
Verification(heap_, VerifyKind::VERIFY_SHARED_RSET_POST_FULL_GC).VerifyAll();
}
heap_->NotifyHeapAliveSizeAfterGC(heap_->GetHeapObjectSize());
}
void FullGC::RunPhasesForAppSpawn()
{
auto marker = reinterpret_cast<CompressGCMarker*>(heap_->GetCompressGCMarker());
marker->SetAppSpawn(true);
RunPhases();
marker->SetAppSpawn(false);
}
void FullGC::Initialize()
{
ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Initialize");
TRACE_GC(GCStats::Scope::ScopeId::Initialize, heap_->GetEcmaVM()->GetEcmaGCStats());
heap_->Prepare();
auto callback = [](Region *current) {
current->ResetAliveObject();
current->ClearOldToNewRSet();
};
heap_->EnumerateNonMovableRegions(callback);
heap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) {
current->ClearMarkGCBitset();
current->ClearCrossRegionRSet();
});
youngSpaceCommitSize_ = heap_->GetNewSpace()->GetCommittedSize();
heap_->SwapNewSpace();
workManager_->Initialize(TriggerGCType::FULL_GC, ParallelGCTaskPhase::COMPRESS_HANDLE_GLOBAL_POOL_TASK);
heap_->GetCompressGCMarker()->Initialize();
youngAndOldAliveSize_ = 0;
nonMoveSpaceFreeSize_ = 0;
oldSpaceCommitSize_ = heap_->GetOldSpace()->GetCommittedSize();
nonMoveSpaceCommitSize_ = heap_->GetNonMovableSpace()->GetCommittedSize();
}
void FullGC::Mark()
{
ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Mark");
TRACE_GC(GCStats::Scope::ScopeId::Mark, heap_->GetEcmaVM()->GetEcmaGCStats());
heap_->GetCompressGCMarker()->MarkRoots(MAIN_THREAD_INDEX);
heap_->GetCompressGCMarker()->ProcessMarkStack(MAIN_THREAD_INDEX);
heap_->WaitRunningTaskFinished();
}
void FullGC::Sweep()
{
ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Sweep");
TRACE_GC(GCStats::Scope::ScopeId::Sweep, heap_->GetEcmaVM()->GetEcmaGCStats());
// process weak reference
uint32_t totalThreadCount = 1; // 1 : mainthread
if (heap_->IsParallelGCEnabled()) {
totalThreadCount += Taskpool::GetCurrentTaskpool()->GetTotalThreadNum();
}
for (uint32_t i = 0; i < totalThreadCount; i++) {
ProcessQueue *queue = workManager_->GetWeakReferenceQueue(i);
while (true) {
auto obj = queue->PopBack();
if (UNLIKELY(obj == nullptr)) {
break;
}
ObjectSlot slot(ToUintPtr(obj));
JSTaggedValue value(slot.GetTaggedType());
auto header = value.GetTaggedWeakRef();
Region *objectRegion = Region::ObjectAddressToRange(header);
if (!HasEvacuated(objectRegion)) {
if (!objectRegion->InSharedHeap() && !objectRegion->Test(header)) {
slot.Clear();
}
} else {
MarkWord markWord(header);
if (markWord.IsForwardingAddress()) {
TaggedObject *dst = markWord.ToForwardingAddress();
auto weakRef = JSTaggedValue(JSTaggedValue(dst).CreateAndGetWeakRef()).GetRawTaggedObject();
slot.Update(weakRef);
} else {
slot.Update(static_cast<JSTaggedType>(JSTaggedValue::Undefined().GetRawData()));
}
}
}
}
WeakRootVisitor gcUpdateWeak = [this](TaggedObject *header) {
Region *objectRegion = Region::ObjectAddressToRange(header);
if (!objectRegion) {
LOG_GC(ERROR) << "FullGC updateWeakReference: region is nullptr, header is " << header;
return reinterpret_cast<TaggedObject *>(ToUintPtr(nullptr));
}
if (!HasEvacuated(objectRegion)) {
// The weak object in shared heap is always alive during fullGC.
if (objectRegion->InSharedHeap() || objectRegion->Test(header)) {
return header;
}
return reinterpret_cast<TaggedObject *>(ToUintPtr(nullptr));
}
MarkWord markWord(header);
if (markWord.IsForwardingAddress()) {
return markWord.ToForwardingAddress();
}
return reinterpret_cast<TaggedObject *>(ToUintPtr(nullptr));
};
heap_->GetEcmaVM()->GetJSThread()->IterateWeakEcmaGlobalStorage(gcUpdateWeak);
heap_->GetEcmaVM()->ProcessReferences(gcUpdateWeak);
heap_->GetSweeper()->Sweep(true);
heap_->GetSweeper()->PostTask(true);
}
void FullGC::Finish()
{
ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "FullGC::Finish");
TRACE_GC(GCStats::Scope::ScopeId::Finish, heap_->GetEcmaVM()->GetEcmaGCStats());
if (!forAppSpawn_) {
heap_->SwapOldSpace();
}
youngAndOldAliveSize_ = workManager_->Finish();
if (forAppSpawn_) {
heap_->ResumeForAppSpawn();
} else {
heap_->Resume(FULL_GC);
}
heap_->GetSweeper()->TryFillSweptRegion();
}
bool FullGC::HasEvacuated(Region *region)
{
auto marker = reinterpret_cast<CompressGCMarker*>(heap_->GetCompressGCMarker());
return marker->NeedEvacuate(region);
}
void FullGC::SetForAppSpawn(bool flag)
{
forAppSpawn_ = flag;
}
} // namespace panda::ecmascript