• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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_GEN_GC_GEN_GC_H
16 #define PANDA_RUNTIME_MEM_GC_GEN_GC_GEN_GC_H
17 
18 #include "runtime/include/mem/panda_smart_pointers.h"
19 #include "runtime/mem/gc/gc_marker.h"
20 #include "runtime/mem/gc/card_table.h"
21 #include "runtime/mem/gc/generational-gc-base.h"
22 #include "runtime/mem/heap_verifier.h"
23 
24 namespace panda {
25 class ManagedThread;
26 }  // namespace panda
27 namespace panda::mem {
28 
29 /**
30  * \brief Generational GC
31  */
32 template <class LanguageConfig>
33 class GenGC : public GenerationalGC<LanguageConfig> {
34 public:
35     using ReferenceCheckPredicateT = typename GC::ReferenceCheckPredicateT;
36 
37     explicit GenGC(ObjectAllocatorBase *object_allocator, const GCSettings &settings);
38 
39     NO_COPY_SEMANTIC(GenGC);
40     NO_MOVE_SEMANTIC(GenGC);
41     ~GenGC() override = default;
42 
43     void InitGCBits(panda::ObjectHeader *obj_header) override;
44 
45     void InitGCBitsForAllocationInTLAB(panda::ObjectHeader *obj_header) override;
46 
47     void Trigger() override;
48 
49     void MarkReferences(GCMarkingStackType *references, GCPhase gc_phase) override;
50 
51     void MarkObject(ObjectHeader *object) override;
52 
53     void UnMarkObject(ObjectHeader *object_header);
54 
55     bool InGCSweepRange(const ObjectHeader *obj) const override;
56 
57 private:
58     void InitializeImpl() override;
59 
60     void RunPhasesImpl(GCTask &task) override;
61 
62     void PreStartupImp() override;
63 
64     /**
65      * GC for young generation. Runs with STW.
66      * @param task gc task for current GC
67      */
68     void RunYoungGC(GCTask &task);
69 
70     /**
71      * GC for tenured generation.
72      * @param task gc task for current GC
73      */
74     void RunTenuredGC(GCTask &task);
75 
76     /**
77      * Full GC for both generations (tenured and young)
78      * @param task gc task for current GC
79      */
80     void RunFullGC(GCTask &task);
81 
82     /**
83      * Marks objects in young generation
84      * @param task gc task for current GC
85      */
86     void MarkYoung(const GCTask &task);
87 
88     void MarkYoungStack(GCMarkingStackType *objects_stack);
89 
90     /**
91      * Mark roots and add them to the stack
92      * @param objects_stack
93      * @param visit_class_roots
94      * @param visit_card_table_roots
95      */
96     void MarkRoots(GCMarkingStackType *objects_stack, CardTableVisitFlag visit_card_table_roots,
97                    const ReferenceCheckPredicateT &pred, VisitGCRootFlags flags = VisitGCRootFlags::ACCESS_ROOT_ALL);
98 
99     /**
100      * Initial marks roots and fill in 1st level from roots into stack.
101      * STW
102      * @param objects_stack
103      */
104     void InitialMark(GCMarkingStackType *objects_stack);
105 
106     /**
107      * ReMarks objects after Concurrent marking
108      * @param objects_stack
109      * @param task gc task for current GC
110      */
111     void ReMark(GCMarkingStackType *objects_stack, const GCTask &task);
112 
113     /**
114      * Mark objects for the whole heap on pause
115      * @param task gc task for current GC
116      */
117     void FullMark(const GCTask &task);
118 
119     /**
120      * Collect dead objects in young generation and move survivors
121      */
122     void CollectYoungAndMove();
123 
124     /**
125      * Collect verification info for CollectAndMove phase
126      * @param young_mem_range young memory region
127      * @return instance of verifier to be used to verify for updated references
128      */
129     [[nodiscard]] HeapVerifierIntoGC<LanguageConfig> CollectVerificationInfo(const MemRange &young_mem_range);
130 
131     /**
132      * Verify updted references
133      * @param collect_verifier instance of the verifier that was obtained before references were updated
134      *
135      * @see CollectVerificationInfo
136      * @see UpdateRefsToMovedObjects
137      */
138     void VerifyCollectAndMove(HeapVerifierIntoGC<LanguageConfig> &&young_verifier);
139 
140     /**
141      * Remove dead strings from string table in tenured space
142      */
143     void SweepStringTable();
144 
145     /**
146      * Update all refs to moved objects
147      */
148     void UpdateRefsToMovedObjects(PandaVector<ObjectHeader *> *moved_objects);
149 
150     /**
151      * Sweep dead objects in tenured space on pause
152      */
153     void Sweep();
154 
155     /**
156      * Concurrent sweep dead objects in tenured space
157      */
158     void ConcurrentSweep();
159 
160     bool IsMarked(const ObjectHeader *object) const override;
161 
162     bool ShouldRunTenuredGC(const GCTask &task) override;
163 
164     /**
165      * @param task gc task for current GC
166      * @param have_enough_space_for_young boolean value that determines whether we have enough memory to move from
167      * young space to tenured space
168      * @return true if need run full gc or flase - otherwise
169      */
170     bool ShouldRunFullGC(const GCTask &task, bool have_enough_space_for_young) const;
171 
172     bool HaveEnoughSpaceToMove() const;
173 
174     DefaultGCMarkerImpl<LanguageConfig::LANG_TYPE, LanguageConfig::HAS_VALUE_OBJECT_TYPES> marker_;
175     bool concurrent_marking_flag_ {false};  //! flag indicates if we currently in concurrent marking phase
176     PandaUniquePtr<CardTable> card_table_ {nullptr};
177 };
178 
179 }  // namespace panda::mem
180 
181 #endif  // PANDA_RUNTIME_MEM_GC_GEN_GC_GEN_GC_H
182