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/partial_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_evacuator.h"
214514f5e3Sopenharmony_ci#include "ecmascript/mem/parallel_marker-inl.h"
224514f5e3Sopenharmony_ci#include "ecmascript/runtime_call_id.h"
234514f5e3Sopenharmony_ci#include "ecmascript/mem/verification.h"
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cinamespace panda::ecmascript {
264514f5e3Sopenharmony_ciPartialGC::PartialGC(Heap *heap) : heap_(heap), workManager_(heap->GetWorkManager()) {}
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_civoid PartialGC::RunPhases()
294514f5e3Sopenharmony_ci{
304514f5e3Sopenharmony_ci    GCStats *gcStats = heap_->GetEcmaVM()->GetEcmaGCStats();
314514f5e3Sopenharmony_ci    ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "PartialGC::RunPhases" + std::to_string(heap_->IsConcurrentFullMark())
324514f5e3Sopenharmony_ci        + ";Reason" + std::to_string(static_cast<int>(gcStats->GetGCReason()))
334514f5e3Sopenharmony_ci        + ";Sensitive" + std::to_string(static_cast<int>(heap_->GetSensitiveStatus()))
344514f5e3Sopenharmony_ci        + ";IsInBackground" + std::to_string(heap_->IsInBackground())
354514f5e3Sopenharmony_ci        + ";Startup" + std::to_string(heap_->OnStartupEvent())
364514f5e3Sopenharmony_ci        + ";ConMark" + std::to_string(static_cast<int>(heap_->GetJSThread()->GetMarkStatus()))
374514f5e3Sopenharmony_ci        + ";Young" + std::to_string(heap_->GetNewSpace()->GetCommittedSize())
384514f5e3Sopenharmony_ci        + ";Old" + std::to_string(heap_->GetOldSpace()->GetCommittedSize())
394514f5e3Sopenharmony_ci        + ";TotalCommit" + std::to_string(heap_->GetCommittedSize()));
404514f5e3Sopenharmony_ci    TRACE_GC(GCStats::Scope::ScopeId::TotalGC, gcStats);
414514f5e3Sopenharmony_ci    MEM_ALLOCATE_AND_GC_TRACE(heap_->GetEcmaVM(), PartialGC_RunPhases);
424514f5e3Sopenharmony_ci    bool mainThreadInForeground = heap_->GetJSThread()->IsMainThreadFast() && !heap_->IsInBackground();
434514f5e3Sopenharmony_ci    bool needAjustGCThreadPrio = heap_->GetGCType() == TriggerGCType::OLD_GC ||
444514f5e3Sopenharmony_ci        heap_->GetNewSpace()->GetCommittedSize() >= heap_->GetNewSpace()->GetMaximumCapacity();
454514f5e3Sopenharmony_ci    if (mainThreadInForeground && needAjustGCThreadPrio) {
464514f5e3Sopenharmony_ci        Taskpool::GetCurrentTaskpool()->SetThreadPriority(PriorityMode::STW);
474514f5e3Sopenharmony_ci    }
484514f5e3Sopenharmony_ci    markingInProgress_ = heap_->CheckOngoingConcurrentMarking();
494514f5e3Sopenharmony_ci    LOG_GC(DEBUG) << "markingInProgress_" << markingInProgress_;
504514f5e3Sopenharmony_ci    Initialize();
514514f5e3Sopenharmony_ci    Mark();
524514f5e3Sopenharmony_ci    if (UNLIKELY(heap_->ShouldVerifyHeap())) {
534514f5e3Sopenharmony_ci        Verification::VerifyMark(heap_);
544514f5e3Sopenharmony_ci    }
554514f5e3Sopenharmony_ci    ProcessSharedGCRSetWorkList();
564514f5e3Sopenharmony_ci    Sweep();
574514f5e3Sopenharmony_ci    Evacuate();
584514f5e3Sopenharmony_ci    if (heap_->IsConcurrentFullMark()) {
594514f5e3Sopenharmony_ci        heap_->GetSweeper()->PostTask();
604514f5e3Sopenharmony_ci    }
614514f5e3Sopenharmony_ci    if (UNLIKELY(heap_->ShouldVerifyHeap())) {
624514f5e3Sopenharmony_ci        Verification::VerifyEvacuate(heap_);
634514f5e3Sopenharmony_ci    }
644514f5e3Sopenharmony_ci    Finish();
654514f5e3Sopenharmony_ci    if (mainThreadInForeground && needAjustGCThreadPrio) {
664514f5e3Sopenharmony_ci        Taskpool::GetCurrentTaskpool()->SetThreadPriority(PriorityMode::FOREGROUND);
674514f5e3Sopenharmony_ci    }
684514f5e3Sopenharmony_ci}
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_civoid PartialGC::Initialize()
714514f5e3Sopenharmony_ci{
724514f5e3Sopenharmony_ci    ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "PartialGC::Initialize");
734514f5e3Sopenharmony_ci    TRACE_GC(GCStats::Scope::ScopeId::Initialize, heap_->GetEcmaVM()->GetEcmaGCStats());
744514f5e3Sopenharmony_ci    if (!markingInProgress_ && !heap_->GetIncrementalMarker()->IsTriggeredIncrementalMark()) {
754514f5e3Sopenharmony_ci        LOG_GC(DEBUG) << "No ongoing Concurrent marking. Initializing...";
764514f5e3Sopenharmony_ci        heap_->Prepare();
774514f5e3Sopenharmony_ci        if (heap_->IsConcurrentFullMark()) {
784514f5e3Sopenharmony_ci            heap_->GetOldSpace()->SelectCSet();
794514f5e3Sopenharmony_ci            heap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) {
804514f5e3Sopenharmony_ci                current->ClearMarkGCBitset();
814514f5e3Sopenharmony_ci                current->ClearCrossRegionRSet();
824514f5e3Sopenharmony_ci            });
834514f5e3Sopenharmony_ci            heap_->EnumerateNonNewSpaceRegions([](Region *current) {
844514f5e3Sopenharmony_ci                current->ResetAliveObject();
854514f5e3Sopenharmony_ci            });
864514f5e3Sopenharmony_ci        }
874514f5e3Sopenharmony_ci        workManager_->Initialize(TriggerGCType::OLD_GC, ParallelGCTaskPhase::OLD_HANDLE_GLOBAL_POOL_TASK);
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ci        freeSize_ = 0;
904514f5e3Sopenharmony_ci        hugeSpaceFreeSize_ = 0;
914514f5e3Sopenharmony_ci        oldSpaceCommitSize_ = heap_->GetOldSpace()->GetCommittedSize();
924514f5e3Sopenharmony_ci        nonMoveSpaceCommitSize_ = heap_->GetNonMovableSpace()->GetCommittedSize();
934514f5e3Sopenharmony_ci    }
944514f5e3Sopenharmony_ci}
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_civoid PartialGC::Finish()
974514f5e3Sopenharmony_ci{
984514f5e3Sopenharmony_ci    ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "PartialGC::Finish");
994514f5e3Sopenharmony_ci    TRACE_GC(GCStats::Scope::ScopeId::Finish, heap_->GetEcmaVM()->GetEcmaGCStats());
1004514f5e3Sopenharmony_ci    heap_->Resume(OLD_GC);
1014514f5e3Sopenharmony_ci    if (heap_->GetIncrementalMarker()->IsTriggeredIncrementalMark()) {
1024514f5e3Sopenharmony_ci        heap_->GetIncrementalMarker()->Reset();
1034514f5e3Sopenharmony_ci    } else if (markingInProgress_) {
1044514f5e3Sopenharmony_ci        auto marker = heap_->GetConcurrentMarker();
1054514f5e3Sopenharmony_ci        marker->Reset(false);
1064514f5e3Sopenharmony_ci    } else {
1074514f5e3Sopenharmony_ci        workManager_->Finish();
1084514f5e3Sopenharmony_ci    }
1094514f5e3Sopenharmony_ci    if (heap_->IsConcurrentFullMark()) {
1104514f5e3Sopenharmony_ci        heap_->GetSweeper()->TryFillSweptRegion();
1114514f5e3Sopenharmony_ci        heap_->SetFullMarkRequestedState(false);
1124514f5e3Sopenharmony_ci    }
1134514f5e3Sopenharmony_ci}
1144514f5e3Sopenharmony_ci
1154514f5e3Sopenharmony_civoid PartialGC::Mark()
1164514f5e3Sopenharmony_ci{
1174514f5e3Sopenharmony_ci    ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "PartialGC::Mark");
1184514f5e3Sopenharmony_ci    TRACE_GC(GCStats::Scope::ScopeId::Mark, heap_->GetEcmaVM()->GetEcmaGCStats());
1194514f5e3Sopenharmony_ci    if (markingInProgress_) {
1204514f5e3Sopenharmony_ci        heap_->GetConcurrentMarker()->ReMark();
1214514f5e3Sopenharmony_ci        return;
1224514f5e3Sopenharmony_ci    }
1234514f5e3Sopenharmony_ci    heap_->GetNonMovableMarker()->MarkRoots(MAIN_THREAD_INDEX);
1244514f5e3Sopenharmony_ci    if (heap_->IsConcurrentFullMark()) {
1254514f5e3Sopenharmony_ci        heap_->GetNonMovableMarker()->ProcessMarkStack(MAIN_THREAD_INDEX);
1264514f5e3Sopenharmony_ci    } else if (heap_->IsEdenMark()) {
1274514f5e3Sopenharmony_ci        {
1284514f5e3Sopenharmony_ci            ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "GC::ProcessOldToNew");
1294514f5e3Sopenharmony_ci            heap_->GetNonMovableMarker()->ProcessOldToNew(MAIN_THREAD_INDEX);
1304514f5e3Sopenharmony_ci        }
1314514f5e3Sopenharmony_ci        {
1324514f5e3Sopenharmony_ci            ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "GC::ProcessNewToEden");
1334514f5e3Sopenharmony_ci            heap_->GetNonMovableMarker()->ProcessNewToEden(MAIN_THREAD_INDEX);
1344514f5e3Sopenharmony_ci        }
1354514f5e3Sopenharmony_ci        heap_->GetNonMovableMarker()->ProcessSnapshotRSet(MAIN_THREAD_INDEX);
1364514f5e3Sopenharmony_ci    } else if (heap_->IsYoungMark()) {
1374514f5e3Sopenharmony_ci        {
1384514f5e3Sopenharmony_ci            ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "GC::ProcessOldToNew");
1394514f5e3Sopenharmony_ci            heap_->GetNonMovableMarker()->ProcessOldToNew(MAIN_THREAD_INDEX);
1404514f5e3Sopenharmony_ci        }
1414514f5e3Sopenharmony_ci        heap_->GetNonMovableMarker()->ProcessSnapshotRSet(MAIN_THREAD_INDEX);
1424514f5e3Sopenharmony_ci    }
1434514f5e3Sopenharmony_ci    heap_->WaitRunningTaskFinished();
1444514f5e3Sopenharmony_ci    // MarkJitCodeMap must be call after other mark work finish to make sure which jserror object js alive.
1454514f5e3Sopenharmony_ci    heap_->GetNonMovableMarker()->MarkJitCodeMap(MAIN_THREAD_INDEX);
1464514f5e3Sopenharmony_ci}
1474514f5e3Sopenharmony_ci
1484514f5e3Sopenharmony_civoid PartialGC::Sweep()
1494514f5e3Sopenharmony_ci{
1504514f5e3Sopenharmony_ci    ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "PartialGC::Sweep");
1514514f5e3Sopenharmony_ci    ProcessNativeDelete();
1524514f5e3Sopenharmony_ci    if (heap_->IsConcurrentFullMark()) {
1534514f5e3Sopenharmony_ci        heap_->GetOldSpace()->EnumerateRegions([](Region *current) {
1544514f5e3Sopenharmony_ci            current->SetRegionAliveSize();
1554514f5e3Sopenharmony_ci        });
1564514f5e3Sopenharmony_ci        TRACE_GC(GCStats::Scope::ScopeId::Sweep, heap_->GetEcmaVM()->GetEcmaGCStats());
1574514f5e3Sopenharmony_ci        heap_->GetSweeper()->Sweep();
1584514f5e3Sopenharmony_ci    }
1594514f5e3Sopenharmony_ci}
1604514f5e3Sopenharmony_ci
1614514f5e3Sopenharmony_civoid PartialGC::ProcessNativeDelete()
1624514f5e3Sopenharmony_ci{
1634514f5e3Sopenharmony_ci    ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "GC::ProcessNativeDelete");
1644514f5e3Sopenharmony_ci    TRACE_GC(GCStats::Scope::ScopeId::ClearNativeObject, heap_->GetEcmaVM()->GetEcmaGCStats());
1654514f5e3Sopenharmony_ci    WeakRootVisitor gcUpdateWeak = [this](TaggedObject *header) -> TaggedObject* {
1664514f5e3Sopenharmony_ci        Region *objectRegion = Region::ObjectAddressToRange(reinterpret_cast<TaggedObject *>(header));
1674514f5e3Sopenharmony_ci        ASSERT(!objectRegion->InSharedHeap());
1684514f5e3Sopenharmony_ci        if (heap_->IsEdenMark() && !objectRegion->InEdenSpace()) {
1694514f5e3Sopenharmony_ci            return header;
1704514f5e3Sopenharmony_ci        }
1714514f5e3Sopenharmony_ci        if (!objectRegion->InGeneralNewSpaceOrCSet() && heap_->IsYoungMark()) {
1724514f5e3Sopenharmony_ci            return header;
1734514f5e3Sopenharmony_ci        }
1744514f5e3Sopenharmony_ci        if (!objectRegion->Test(header)) {
1754514f5e3Sopenharmony_ci            return nullptr;
1764514f5e3Sopenharmony_ci        }
1774514f5e3Sopenharmony_ci        return header;
1784514f5e3Sopenharmony_ci    };
1794514f5e3Sopenharmony_ci    heap_->GetEcmaVM()->ProcessNativeDelete(gcUpdateWeak);
1804514f5e3Sopenharmony_ci}
1814514f5e3Sopenharmony_ci
1824514f5e3Sopenharmony_civoid PartialGC::Evacuate()
1834514f5e3Sopenharmony_ci{
1844514f5e3Sopenharmony_ci    ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "PartialGC::Evacuate");
1854514f5e3Sopenharmony_ci    TRACE_GC(GCStats::Scope::ScopeId::Evacuate, heap_->GetEcmaVM()->GetEcmaGCStats());
1864514f5e3Sopenharmony_ci    heap_->GetEvacuator()->Evacuate();
1874514f5e3Sopenharmony_ci}
1884514f5e3Sopenharmony_ci
1894514f5e3Sopenharmony_ciARK_INLINE void PartialGC::ProcessSharedGCRSetWorkList()
1904514f5e3Sopenharmony_ci{
1914514f5e3Sopenharmony_ci    TRACE_GC(GCStats::Scope::ScopeId::ProcessSharedGCRSetWorkList, heap_->GetEcmaVM()->GetEcmaGCStats());
1924514f5e3Sopenharmony_ci    heap_->ProcessSharedGCRSetWorkList();
1934514f5e3Sopenharmony_ci}
1944514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
195