1 /* 2 * Copyright (c) 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 16 #ifndef ECMASCRIPT_MEM_SHARED_HEAP_SHARED_CONCURRENT_MARKER_H 17 #define ECMASCRIPT_MEM_SHARED_HEAP_SHARED_CONCURRENT_MARKER_H 18 19 #include "ecmascript/daemon/daemon_thread.h" 20 #include "ecmascript/mem/concurrent_marker.h" 21 #include "ecmascript/mem/shared_heap/shared_gc_marker.h" 22 23 namespace panda::ecmascript { 24 class EcmaVM; 25 class SharedHeap; 26 27 class SharedConcurrentMarker { 28 public: 29 explicit SharedConcurrentMarker(EnableConcurrentMarkType type); 30 ~SharedConcurrentMarker() = default; 31 32 /* 33 * Concurrent marking related configurations and utilities. 34 */ 35 void EnableConcurrentMarking(EnableConcurrentMarkType type); 36 IsEnabled()37 bool IsEnabled() const 38 { 39 return !IsDisabled(); 40 } 41 IsDisabled()42 bool IsDisabled() const 43 { 44 return enableMarkType_ == EnableConcurrentMarkType::DISABLE || 45 enableMarkType_ == EnableConcurrentMarkType::CONFIG_DISABLE; 46 } 47 ConfigConcurrentMark(bool enabled)48 void ConfigConcurrentMark(bool enabled) 49 { 50 enableMarkType_ = enabled ? EnableConcurrentMarkType::ENABLE : 51 EnableConcurrentMarkType::CONFIG_DISABLE; 52 } 53 IsRequestDisabled()54 bool IsRequestDisabled() const 55 { 56 return enableMarkType_ == EnableConcurrentMarkType::REQUEST_DISABLE; 57 } 58 IsConfigDisabled()59 bool IsConfigDisabled() const 60 { 61 return enableMarkType_ == EnableConcurrentMarkType::CONFIG_DISABLE; 62 } 63 IsTriggeredConcurrentMark()64 bool IsTriggeredConcurrentMark() const 65 { 66 return isConcurrentMarking_; 67 } 68 void Mark(TriggerGCType gcType, GCReason gcReason); // In daemon thread 69 void ReMark(); // In daemon thread 70 71 void Reset(bool clearGCBits); // In daemon thread 72 73 void ResetWorkManager(SharedGCWorkManager *sWorkManager); // In js thread 74 GetDuration()75 double GetDuration() const 76 { 77 return duration_; 78 } 79 GetHeapObjectSize()80 double GetHeapObjectSize() const 81 { 82 return sHeapObjectSize_; 83 } 84 85 private: 86 NO_COPY_SEMANTIC(SharedConcurrentMarker); 87 NO_MOVE_SEMANTIC(SharedConcurrentMarker); 88 89 class RecursionScope { 90 public: RecursionScope(SharedConcurrentMarker * sMarker)91 explicit RecursionScope(SharedConcurrentMarker* sMarker) : sMarker_(sMarker) 92 { 93 if (sMarker_->recursionDepth_++ != 0) { 94 LOG_GC(FATAL) << "Recursion in SharedConcurrentMarker Constructor, depth: " 95 << sMarker_->recursionDepth_; 96 } 97 } ~RecursionScope()98 ~RecursionScope() 99 { 100 if (--sMarker_->recursionDepth_ != 0) { 101 LOG_GC(FATAL) << "Recursion in SharedConcurrentMarker Destructor, depth: " 102 << sMarker_->recursionDepth_; 103 } 104 } 105 private: 106 SharedConcurrentMarker* sMarker_ {nullptr}; 107 }; 108 SetDuration(double duration)109 void SetDuration(double duration) 110 { 111 duration_ = duration; 112 } 113 114 void MarkRoots(SharedMarkType markType); // In daemon thread 115 void InitializeMarking(); // In daemon thread 116 void DoMarking(); // In daemon thread 117 void FinishMarking(float spendTime); // In daemon thread 118 void HandleMarkingFinished(); // In daemon thread 119 120 void Finish(); // In daemon thread 121 122 SharedHeap *sHeap_ {nullptr}; 123 DaemonThread *dThread_ {nullptr}; 124 125 // obtained from the shared heap instance. 126 SharedGCWorkManager *sWorkManager_ {nullptr}; 127 size_t sHeapObjectSize_ {0}; 128 double duration_ {0.0}; 129 EnableConcurrentMarkType enableMarkType_ {EnableConcurrentMarkType::CONFIG_DISABLE}; 130 131 bool isConcurrentMarking_ {false}; 132 int32_t recursionDepth_ {0}; 133 TriggerGCType gcType_ {TriggerGCType::SHARED_GC}; 134 GCReason gcReason_ {GCReason::OTHER}; 135 }; 136 } // namespace panda::ecmascript 137 #endif // ECMASCRIPT_MEM_SHARED_HEAP_SHARED_CONCURRENT_MARKER_H 138