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 #ifndef COMMON_COMPONENTS_HEAP_COLLECTOR_COLLECTOR_H 17 #define COMMON_COMPONENTS_HEAP_COLLECTOR_COLLECTOR_H 18 19 #include <atomic> 20 #include <cstdint> 21 #include <cstdlib> 22 #include <functional> 23 #include <set> 24 #include <vector> 25 26 #include "common_components/common/base_object.h" 27 #include "common_components/heap/collector/gc_request.h" 28 #include "common_components/heap/collector/gc_stats.h" 29 #include "common_interfaces/thread/mutator_base.h" 30 #include "common_interfaces/base/runtime_param.h" 31 32 namespace common { 33 enum CollectorType { 34 NO_COLLECTOR = 0, // No Collector 35 PROXY_COLLECTOR, // Proxy of Collector 36 COPY_COLLECTOR, // Regional-Copying GC 37 SMOOTH_COLLECTOR, // wgc 38 COLLECTOR_TYPE_COUNT, 39 }; 40 41 // Central garbage identification algorithm. 42 class Collector { 43 public: 44 Collector(); 45 virtual ~Collector() = default; 46 47 static const char* GetGCPhaseName(GCPhase phase); 48 49 // Initializer and finalizer. 50 virtual void Init(const RuntimeParam& param) = 0; Fini()51 virtual void Fini() {} 52 const char* GetCollectorName() const; 53 54 // This pure virtual function implements the trigger of GC. 55 // reason: Reason for GC. 56 // async: Trigger from unsafe context, e.g., holding a lock, in the middle of an allocation. 57 // In order to prevent deadlocks, async trigger only add one async gc task and will not block. 58 void RequestGC(GCReason reason, bool async, GCType gcType); 59 GetGCPhase()60 virtual GCPhase GetGCPhase() const { return gcPhase_.load(std::memory_order_acquire); } 61 SetGCPhase(const GCPhase phase)62 virtual void SetGCPhase(const GCPhase phase) { gcPhase_.store(phase, std::memory_order_release); } 63 FixObjectRefFields(BaseObject *)64 virtual void FixObjectRefFields(BaseObject*) const {} 65 66 virtual void RunGarbageCollection(uint64_t, GCReason, GCType) = 0; 67 GetGCStats()68 virtual GCStats& GetGCStats() 69 { 70 LOG_COMMON(FATAL) << "Unresolved fatal"; 71 UNREACHABLE_CC(); 72 } 73 74 virtual BaseObject* ForwardObject(BaseObject*) = 0; 75 76 virtual bool ShouldIgnoreRequest(GCRequest& quest) = 0; 77 virtual bool IsFromObject(BaseObject*) const = 0; 78 virtual bool IsUnmovableFromObject(BaseObject*) const = 0; 79 virtual BaseObject* FindToVersion(BaseObject* obj) const = 0; 80 81 virtual bool TryUpdateRefField(BaseObject*, RefField<>&, BaseObject*&) const = 0; 82 virtual bool TryForwardRefField(BaseObject*, RefField<>&, BaseObject*&) const = 0; 83 virtual bool TryUntagRefField(BaseObject*, RefField<>&, BaseObject*&) const = 0; 84 virtual RefField<> GetAndTryTagRefField(BaseObject*) const = 0; 85 86 virtual bool IsOldPointer(RefField<>&) const = 0; 87 virtual bool IsCurrentPointer(RefField<>&) const = 0; 88 virtual void AddRawPointerObject(BaseObject*) = 0; 89 virtual void RemoveRawPointerObject(BaseObject*) = 0; 90 FindLatestVersion(BaseObject * obj)91 BaseObject* FindLatestVersion(BaseObject* obj) const 92 { 93 if (obj == nullptr) { return nullptr; } 94 95 auto to = FindToVersion(obj); 96 if (to != nullptr) { 97 return to; 98 } 99 return obj; 100 }; 101 102 protected: RequestGCInternal(GCReason,bool,GCType)103 virtual void RequestGCInternal(GCReason, bool, GCType) 104 { 105 LOG_COMMON(FATAL) << "Unresolved fatal"; 106 UNREACHABLE_CC(); 107 } 108 109 CollectorType collectorType_ = CollectorType::NO_COLLECTOR; 110 std::atomic<GCPhase> gcPhase_ = { GCPhase::GC_PHASE_IDLE }; 111 }; 112 } // namespace common 113 114 #endif // COMMON_COMPONENTS_HEAP_COLLECTOR_COLLECTOR_H 115