• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/cross_vm/unified_gc/unified_gc.h"
17 
18 #include "ecmascript/cross_vm/unified_gc/unified_gc_marker.h"
19 #include "ecmascript/mem/concurrent_marker.h"
20 #include "ecmascript/mem/verification.h"
21 #include "ecmascript/mem/heap.h"
22 
23 namespace panda::ecmascript {
24 #ifdef PANDA_JS_ETS_HYBRID_MODE
25 std::atomic<bool> UnifiedGC::isInterruptUnifiedGC_ {false};
26 #endif // PANDA_JS_ETS_HYBRID_MODE
27 
RunPhases()28 void UnifiedGC::RunPhases()
29 {
30     Runtime::GetInstance()->GCIterateThreadList([](JSThread *thread) {
31         Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
32         if (heap->DaemonCheckOngoingConcurrentMarking()) {
33             LOG_GC(DEBUG) << "UnifiedGC after ConcurrentMarking";
34             heap->GetConcurrentMarker()->Reset();
35         }
36     });
37     Initialize();
38     Mark();
39     Finish();
40 }
41 
Initialize()42 void UnifiedGC::Initialize()
43 {
44     ECMA_BYTRACE_NAME(HITRACE_LEVEL_COMMERCIAL, HITRACE_TAG_ARK, "UnifiedGC::Initialize", "");
45     Runtime::GetInstance()->GCIterateThreadList([](JSThread *thread) {
46         Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
47         TRACE_GC(GCStats::Scope::ScopeId::Initialize, heap->GetEcmaGCStats());
48         heap->GetUnifiedGCMarker()->Initialize();
49     });
50 }
51 
Mark()52 void UnifiedGC::Mark()
53 {
54     ECMA_BYTRACE_NAME(HITRACE_LEVEL_COMMERCIAL, HITRACE_TAG_ARK, "UnifiedGC::Mark", "");
55     Runtime::GetInstance()->GCIterateThreadList([](JSThread *thread) {
56         Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
57         TRACE_GC(GCStats::Scope::ScopeId::Mark, heap->GetEcmaGCStats());
58         heap->GetUnifiedGCMarker()->InitialMark(DAEMON_THREAD_INDEX);
59     });
60     Runtime::GetInstance()->GCIterateThreadList([](JSThread *thread) {
61         Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
62         heap->GetUnifiedGCMarker()->ProcessMarkStack(DAEMON_THREAD_INDEX);
63     });
64 #ifdef PANDA_JS_ETS_HYBRID_MODE
65     static const auto noMarkTaskCheck = []() -> bool {
66         bool noMarkTask = true;
67         Runtime::GetInstance()->GCIterateThreadList([&noMarkTask](JSThread *thread) {
68             Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
69             if (heap->GetRunningTaskCount() != 0) {
70                 noMarkTask = false;
71                 return;
72             }
73         });
74         return noMarkTask;
75     };
76     while (!stsVMInterface_->WaitForConcurrentMark(noMarkTaskCheck)) {
77         Runtime::GetInstance()->GCIterateThreadList([](JSThread *thread) {
78             Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
79             heap->WaitRunningTaskFinished();
80         });
81     }
82     stsVMInterface_->RemarkStartBarrier();
83     while (!stsVMInterface_->WaitForRemark(noMarkTaskCheck)) {
84         Runtime::GetInstance()->GCIterateThreadList([](JSThread *thread) {
85             Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
86             heap->WaitRunningTaskFinished();
87         });
88     }
89 #endif // PANDA_JS_ETS_HYBRID_MODE
90 }
91 
Finish()92 void UnifiedGC::Finish()
93 {
94     ECMA_BYTRACE_NAME(HITRACE_LEVEL_COMMERCIAL, HITRACE_TAG_ARK, "UnifiedGC::Finish", "");
95     DaemonThread::GetInstance()->FinishRunningTask();
96     Runtime::GetInstance()->GCIterateThreadList([](JSThread *thread) {
97         Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
98         TRACE_GC(GCStats::Scope::ScopeId::Finish, heap->GetEcmaGCStats());
99         heap->GetUnifiedGCMarker()->Finish();
100     });
101 }
102 }  // namespace panda::ecmascript