1 /* 2 * Copyright (c) 2021 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 #ifndef ECMASCRIPT_MEM_FULL_GC_H 17 #define ECMASCRIPT_MEM_FULL_GC_H 18 19 #include "ecmascript/mem/garbage_collector.h" 20 #include "ecmascript/mem/heap.h" 21 #include "ecmascript/mem/work_manager.h" 22 23 namespace panda { 24 namespace ecmascript { 25 class FullGCRunner; 26 27 class FullGC final : public GarbageCollector { 28 public: 29 explicit FullGC(Heap *heap); 30 ~FullGC() override = default; 31 32 NO_COPY_SEMANTIC(FullGC); 33 NO_MOVE_SEMANTIC(FullGC); 34 35 void RunPhases() override; 36 void RunPhasesForAppSpawn(); 37 void SetForAppSpawn(bool flag); 38 protected: 39 void Initialize() override; 40 void Mark() override; 41 void Sweep() override; 42 void Finish() override; 43 44 private: 45 void MarkRoots(); 46 void ProcessSharedGCRSetWorkList(); 47 bool HasEvacuated(Region *region); 48 void UpdateRecordWeakReference(uint32_t threadId); 49 void UpdateRecordJSWeakMap(uint32_t threadId); 50 51 Heap *heap_; 52 size_t youngAndOldAliveSize_ = 0; 53 size_t nonMoveSpaceFreeSize_ = 0; 54 size_t youngSpaceCommitSize_ = 0; 55 size_t oldSpaceCommitSize_ = 0; 56 size_t nonMoveSpaceCommitSize_ = 0; 57 bool forAppSpawn_ {false}; 58 // obtain from heap 59 WorkManager *workManager_ {nullptr}; 60 61 friend class WorkManager; 62 friend class Heap; 63 }; 64 65 class FullGCMarkRootVisitor final : public RootVisitor { 66 public: 67 inline explicit FullGCMarkRootVisitor(FullGCRunner *runner); 68 ~FullGCMarkRootVisitor() override = default; 69 70 inline void VisitRoot([[maybe_unused]] Root type, ObjectSlot slot) override; 71 72 inline void VisitRangeRoot([[maybe_unused]] Root type, ObjectSlot start, ObjectSlot end) override; 73 74 inline void VisitBaseAndDerivedRoot([[maybe_unused]] Root type, ObjectSlot base, ObjectSlot derived, 75 uintptr_t baseOldObject) override; 76 private: 77 FullGCRunner *runner_ {nullptr}; 78 }; 79 80 class FullGCMarkObjectVisitor final : public BaseObjectVisitor<FullGCMarkObjectVisitor> { 81 public: 82 inline explicit FullGCMarkObjectVisitor(FullGCRunner *runner); 83 ~FullGCMarkObjectVisitor() override = default; 84 85 inline void VisitObjectRangeImpl(BaseObject *root, uintptr_t start, uintptr_t end, 86 VisitObjectArea area) override; 87 88 inline void VisitJSWeakMapImpl(BaseObject *rootObject) override; 89 90 inline void VisitHClassSlot(ObjectSlot slot, TaggedObject *hclass); 91 private: 92 FullGCRunner *runner_ {nullptr}; 93 }; 94 95 class FullGCUpdateLocalToShareRSetVisitor final : public BaseObjectVisitor<FullGCUpdateLocalToShareRSetVisitor> { 96 public: 97 inline explicit FullGCUpdateLocalToShareRSetVisitor(FullGCRunner *runner); 98 ~FullGCUpdateLocalToShareRSetVisitor() override = default; 99 100 inline void VisitObjectRangeImpl(BaseObject *root, uintptr_t start, uintptr_t end, 101 VisitObjectArea area) override; 102 private: 103 inline void SetLocalToShareRSet(ObjectSlot slot, Region *rootRegion); 104 105 FullGCRunner *runner_ {nullptr}; 106 }; 107 108 class FullGCRunner { 109 public: 110 inline explicit FullGCRunner(Heap *heap, WorkNodeHolder *workNodeHolder, bool isAppSpawn); 111 ~FullGCRunner() = default; 112 113 inline FullGCMarkRootVisitor &GetMarkRootVisitor(); 114 inline FullGCMarkObjectVisitor &GetMarkObjectVisitor(); 115 116 protected: 117 inline void HandleMarkingSlot(ObjectSlot slot); 118 119 inline void HandleMarkingSlotObject(ObjectSlot slot, TaggedObject *object); 120 121 template <class Callback> 122 void VisitBodyInObj(BaseObject *root, uintptr_t start, uintptr_t end, Callback &&cb); 123 124 private: 125 inline bool NeedEvacuate(Region *region); 126 127 inline void EvacuateObject(ObjectSlot slot, TaggedObject *object, const MarkWord &markWord); 128 129 inline uintptr_t AllocateForwardAddress(size_t size); 130 131 inline uintptr_t AllocateDstSpace(size_t size); 132 133 inline uintptr_t AllocateAppSpawnSpace(size_t size); 134 135 inline void RawCopyObject(uintptr_t fromAddress, uintptr_t toAddress, size_t size, const MarkWord &markWord); 136 137 inline void UpdateForwardAddressIfSuccess(ObjectSlot slot, TaggedObject *object, JSHClass *klass, size_t size, 138 TaggedObject *toAddress); 139 140 inline void UpdateForwardAddressIfFailed(ObjectSlot slot, size_t size, uintptr_t toAddress, TaggedObject *dst); 141 142 inline void PushObject(TaggedObject *object); 143 144 inline void RecordWeakReference(JSTaggedType *weak); 145 146 inline void RecordJSWeakMap(TaggedObject *object); 147 148 Heap *heap_ {nullptr}; 149 WorkNodeHolder *workNodeHolder_ {nullptr}; 150 bool isAppSpawn_ {false}; 151 FullGCMarkRootVisitor markRootVisitor_; 152 FullGCMarkObjectVisitor markObjectVisitor_; 153 FullGCUpdateLocalToShareRSetVisitor updateLocalToShareRSetVisitor_; 154 155 friend class FullGCMarkRootVisitor; 156 friend class FullGCMarkObjectVisitor; 157 friend class FullGCUpdateLocalToShareRSetVisitor; 158 friend class CompressGCMarker; 159 }; 160 } // namespace ecmascript 161 } // namespace panda 162 163 #endif // ECMASCRIPT_MEM_FULL_GC_H 164