2021-09-04 08:06:49 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 07:34:31 +00:00
|
|
|
#include "ecmascript/mem/full_gc.h"
|
2021-09-24 06:31:54 +00:00
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/ecma_vm.h"
|
|
|
|
#include "ecmascript/mem/clock_scope.h"
|
2021-12-17 09:18:10 +00:00
|
|
|
#include "ecmascript/mem/concurrent_marker.h"
|
2021-12-16 03:41:16 +00:00
|
|
|
#include "ecmascript/mem/object_xray-inl.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/mem/mark_stack.h"
|
|
|
|
#include "ecmascript/mem/mem.h"
|
2021-12-17 09:18:10 +00:00
|
|
|
#include "ecmascript/mem/parallel_marker-inl.h"
|
2021-12-16 03:41:16 +00:00
|
|
|
#include "ecmascript/mem/space-inl.h"
|
2022-02-09 08:48:39 +00:00
|
|
|
#include "ecmascript/runtime_call_id.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
namespace panda::ecmascript {
|
2022-04-26 05:59:49 +00:00
|
|
|
FullGC::FullGC(Heap *heap) : heap_(heap), workManager_(heap->GetWorkManager()) {}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-02-16 07:34:31 +00:00
|
|
|
void FullGC::RunPhases()
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2022-02-16 07:34:31 +00:00
|
|
|
ECMA_BYTRACE_NAME(BYTRACE_TAG_ARK, "FullGC::RunPhases");
|
|
|
|
MEM_ALLOCATE_AND_GC_TRACE(heap_->GetEcmaVM(), FullGC_RunPhases);
|
2021-12-17 09:18:10 +00:00
|
|
|
ClockScope clockScope;
|
|
|
|
|
2022-02-09 08:15:29 +00:00
|
|
|
bool concurrentMark = heap_->CheckConcurrentMark();
|
2021-12-17 09:18:10 +00:00
|
|
|
if (concurrentMark) {
|
2022-02-16 07:34:31 +00:00
|
|
|
ECMA_GC_LOG() << "FullGC after ConcurrentMarking";
|
2021-12-17 09:18:10 +00:00
|
|
|
heap_->GetConcurrentMarker()->Reset(); // HPPGC use mark result to move TaggedObject.
|
|
|
|
}
|
2022-04-25 11:18:14 +00:00
|
|
|
Initialize();
|
|
|
|
Mark();
|
|
|
|
Sweep();
|
|
|
|
Finish();
|
2022-02-16 07:34:31 +00:00
|
|
|
heap_->GetEcmaVM()->GetEcmaGCStats()->StatisticFullGC(clockScope.GetPauseTime(), youngAndOldAliveSize_,
|
|
|
|
youngSpaceCommitSize_, oldSpaceCommitSize_,
|
|
|
|
nonMoveSpaceFreeSize_, nonMoveSpaceCommitSize_);
|
|
|
|
ECMA_GC_LOG() << "FullGC::RunPhases " << clockScope.TotalSpentTime();
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:18:14 +00:00
|
|
|
void FullGC::Initialize()
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2022-04-25 11:18:14 +00:00
|
|
|
ECMA_BYTRACE_NAME(BYTRACE_TAG_ARK, "FullGC::Initialize");
|
2021-12-17 09:18:10 +00:00
|
|
|
heap_->Prepare();
|
2021-09-04 08:06:49 +00:00
|
|
|
auto callback = [](Region *current) {
|
2022-04-16 10:04:54 +00:00
|
|
|
current->ClearOldToNewRSet();
|
2021-09-04 08:06:49 +00:00
|
|
|
};
|
2021-12-17 09:18:10 +00:00
|
|
|
heap_->EnumerateNonMovableRegions(callback);
|
2022-04-12 03:24:42 +00:00
|
|
|
heap_->EnumerateNonNewSpaceRegions([](Region *current) {
|
2022-04-16 10:04:54 +00:00
|
|
|
current->ClearMarkGCBitset();
|
|
|
|
current->ClearCrossRegionRSet();
|
2022-04-12 03:24:42 +00:00
|
|
|
});
|
2022-04-27 10:26:06 +00:00
|
|
|
youngSpaceCommitSize_ = heap_->GetNewSpace()->GetCommittedSize();
|
2022-02-22 12:16:40 +00:00
|
|
|
heap_->SwapNewSpace();
|
2022-04-26 05:59:49 +00:00
|
|
|
workManager_->Initialize(TriggerGCType::FULL_GC, ParallelGCTaskPhase::COMPRESS_HANDLE_GLOBAL_POOL_TASK);
|
2022-04-28 03:08:15 +00:00
|
|
|
heap_->GetCompressGCMarker()->Initialize();
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
youngAndOldAliveSize_ = 0;
|
|
|
|
nonMoveSpaceFreeSize_ = 0;
|
2022-02-22 12:16:40 +00:00
|
|
|
oldSpaceCommitSize_ = heap_->GetOldSpace()->GetCommittedSize();
|
2021-09-04 08:06:49 +00:00
|
|
|
nonMoveSpaceCommitSize_ = heap_->GetNonMovableSpace()->GetCommittedSize();
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:18:14 +00:00
|
|
|
void FullGC::Mark()
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2022-04-25 11:18:14 +00:00
|
|
|
ECMA_BYTRACE_NAME(BYTRACE_TAG_ARK, "FullGC::Mark");
|
2022-04-28 03:08:15 +00:00
|
|
|
heap_->GetCompressGCMarker()->MarkRoots(MAIN_THREAD_INDEX);
|
|
|
|
heap_->GetCompressGCMarker()->ProcessMarkStack(MAIN_THREAD_INDEX);
|
2021-12-17 09:18:10 +00:00
|
|
|
heap_->WaitRunningTaskFinished();
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:18:14 +00:00
|
|
|
void FullGC::Sweep()
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2022-04-25 11:18:14 +00:00
|
|
|
ECMA_BYTRACE_NAME(BYTRACE_TAG_ARK, "FullGC::Sweep");
|
2021-09-04 08:06:49 +00:00
|
|
|
// process weak reference
|
2022-03-23 07:43:38 +00:00
|
|
|
auto totalThreadCount = Taskpool::GetCurrentTaskpool()->GetTotalThreadNum() + 1; // gc thread and main thread
|
2021-12-17 09:18:10 +00:00
|
|
|
for (uint32_t i = 0; i < totalThreadCount; i++) {
|
2022-04-26 05:59:49 +00:00
|
|
|
ProcessQueue *queue = workManager_->GetWeakReferenceQueue(i);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
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 (!objectRegion->InYoungAndOldGeneration()) {
|
2022-04-16 10:04:54 +00:00
|
|
|
if (!objectRegion->Test(header)) {
|
2021-09-04 08:06:49 +00:00
|
|
|
slot.Update(static_cast<JSTaggedType>(JSTaggedValue::Undefined().GetRawData()));
|
|
|
|
}
|
|
|
|
} 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()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto stringTable = heap_->GetEcmaVM()->GetEcmaStringTable();
|
|
|
|
WeakRootVisitor gcUpdateWeak = [](TaggedObject *header) {
|
|
|
|
Region *objectRegion = Region::ObjectAddressToRange(header);
|
|
|
|
if (!objectRegion->InYoungAndOldGeneration()) {
|
2022-04-16 10:04:54 +00:00
|
|
|
if (objectRegion->Test(header)) {
|
2021-09-04 08:06:49 +00:00
|
|
|
return header;
|
|
|
|
}
|
|
|
|
return reinterpret_cast<TaggedObject *>(ToUintPtr(nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkWord markWord(header);
|
|
|
|
if (markWord.IsForwardingAddress()) {
|
2021-12-17 09:18:10 +00:00
|
|
|
return markWord.ToForwardingAddress();
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
return reinterpret_cast<TaggedObject *>(ToUintPtr(nullptr));
|
|
|
|
};
|
|
|
|
stringTable->SweepWeakReference(gcUpdateWeak);
|
2021-09-07 14:24:16 +00:00
|
|
|
heap_->GetEcmaVM()->GetJSThread()->IterateWeakEcmaGlobalStorage(gcUpdateWeak);
|
2021-09-04 08:06:49 +00:00
|
|
|
heap_->GetEcmaVM()->ProcessReferences(gcUpdateWeak);
|
|
|
|
|
2021-12-17 09:18:10 +00:00
|
|
|
heap_->UpdateDerivedObjectInStack();
|
2022-04-25 11:18:14 +00:00
|
|
|
heap_->GetSweeper()->Sweep(true);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 11:18:14 +00:00
|
|
|
void FullGC::Finish()
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2022-04-25 11:18:14 +00:00
|
|
|
ECMA_BYTRACE_NAME(BYTRACE_TAG_ARK, "FullGC::Finish");
|
2022-03-22 06:41:42 +00:00
|
|
|
heap_->GetSweeper()->PostConcurrentSweepTasks(true);
|
2022-02-16 07:34:31 +00:00
|
|
|
heap_->Resume(FULL_GC);
|
2022-04-26 05:59:49 +00:00
|
|
|
workManager_->Finish(youngAndOldAliveSize_);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
2022-02-23 08:29:15 +00:00
|
|
|
} // namespace panda::ecmascript
|