• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 #ifndef PANDA_RUNTIME_MEM_GC_STW_GC_STW_GC_H
16 #define PANDA_RUNTIME_MEM_GC_STW_GC_STW_GC_H
17 
18 #include "runtime/mem/gc/lang/gc_lang.h"
19 #include "runtime/mem/gc/gc_marker.h"
20 #include "runtime/mem/gc/gc_root.h"
21 
22 namespace ark::mem {
23 
24 template <MTModeT MT_MODE>
25 class AllocConfig<GCType::STW_GC, MT_MODE> {
26 public:
27     using ObjectAllocatorType = ObjectAllocatorNoGen<MT_MODE>;
28     using CodeAllocatorType = CodeAllocator;
29 };
30 
31 template <class LanguageConfig>
32 class StwGCMarker : public DefaultGCMarker<StwGCMarker<LanguageConfig>, LanguageConfig> {
33     using Base = DefaultGCMarker<StwGCMarker<LanguageConfig>, LanguageConfig>;
34 
35 public:
StwGCMarker(GC * gc)36     explicit StwGCMarker(GC *gc) : Base(gc) {}
37 
MarkIfNotMarked(ObjectHeader * object)38     bool MarkIfNotMarked(ObjectHeader *object) const
39     {
40         if (!reverseMark_) {
41             return Base::template MarkIfNotMarked<false>(object);
42         }
43         return Base::template MarkIfNotMarked<true>(object);
44     }
45 
Mark(ObjectHeader * object)46     void Mark(ObjectHeader *object) const
47     {
48         if (!reverseMark_) {
49             LOG(DEBUG, GC) << "Set mark for GC " << GetDebugInfoAboutObject(object);
50             Base::template Mark<false>(object);
51         } else {
52             LOG(DEBUG, GC) << "Set unmark for GC " << GetDebugInfoAboutObject(object);
53             Base::template Mark<true>(object);
54         }
55     }
56 
IsMarked(const ObjectHeader * object)57     bool IsMarked(const ObjectHeader *object) const
58     {
59         if (!reverseMark_) {
60             LOG(DEBUG, GC) << "Get marked for GC " << GetDebugInfoAboutObject(object);
61             return Base::template IsMarked<false>(object);
62         }
63         LOG(DEBUG, GC) << "Get unmarked for GC " << GetDebugInfoAboutObject(object);
64         return Base::template IsMarked<true>(object);
65     }
66 
ReverseMark()67     void ReverseMark()
68     {
69         reverseMark_ = !reverseMark_;
70     }
71 
IsReverseMark()72     bool IsReverseMark() const
73     {
74         return reverseMark_;
75     }
76 
77 private:
78     bool reverseMark_ = false;
79 };
80 
81 /// @brief Stop the world, non-concurrent GC
82 template <class LanguageConfig>
83 class StwGC final : public GCLang<LanguageConfig> {
84 public:
85     explicit StwGC(ObjectAllocatorBase *objectAllocator, const GCSettings &settings);
86 
87     NO_COPY_SEMANTIC(StwGC);
88     NO_MOVE_SEMANTIC(StwGC);
89     ~StwGC() override = default;
90 
91     void InitGCBits(ark::ObjectHeader *object) override;
92 
93     void InitGCBitsForAllocationInTLAB(ark::ObjectHeader *objHeader) override;
94 
IsPinningSupported()95     bool IsPinningSupported() const final
96     {
97         // STW GC does not move object
98         return true;
99     }
100 
101     bool CheckGCCause(GCTaskCause cause) const override;
102 
103     bool Trigger(PandaUniquePtr<GCTask> task) override;
104 
105     void WorkerTaskProcessing(GCWorkersTask *task, void *workerData) override;
106 
107     bool IsPostponeGCSupported() const override;
108 
109 private:
110     enum MarkType : bool { DIRECT_MARK = false, REVERSE_MARK = true };
111 
112     void InitializeImpl() override;
113     void RunPhasesImpl(GCTask &task) override;
114     void Mark(const GCTask &task);
115     void MarkStack(GCMarkingStackType *stack);
116     void Sweep();
117     template <MarkType MARK_TYPE>
118     void SweepImpl();
119 
120     void MarkObject(ObjectHeader *object) override;
121     bool IsMarked(const ObjectHeader *object) const override;
122     void UnMarkObject(ObjectHeader *objectHeader);
123     void MarkReferences(GCMarkingStackType *references, GCPhase gcPhase) override;
124 
125     StwGCMarker<LanguageConfig> marker_;
126 };
127 
128 }  // namespace ark::mem
129 
130 #endif  // PANDA_RUNTIME_MEM_GC_STW_GC_STW_GC_H
131