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 16 #ifndef PANDA_RUNTIME_GC_TASK_H_ 17 #define PANDA_RUNTIME_GC_TASK_H_ 18 19 #include <cstdint> 20 21 #include "runtime/include/mem/allocator.h" 22 23 namespace ark { 24 25 class ManagedThread; 26 27 namespace mem { 28 class GC; 29 } // namespace mem 30 31 /// Causes are ordered by priority. Bigger index - means bigger priority in GC 32 enum class GCTaskCause : uint8_t { 33 INVALID_CAUSE = 0, 34 YOUNG_GC_CAUSE, // if young space is full 35 PYGOTE_FORK_CAUSE, 36 STARTUP_COMPLETE_CAUSE, 37 NATIVE_ALLOC_CAUSE, 38 HEAP_USAGE_THRESHOLD_CAUSE, 39 MIXED, // startGC(mixed). In this case we ignore garbage percentage for tenured regions. 40 EXPLICIT_CAUSE, // System.gc 41 OOM_CAUSE, // if all heap is full 42 CROSSREF_CAUSE, 43 }; 44 45 /** 46 * Collection types are ordered by gc collection scale 47 * 48 * @see GCTask::UpdateGCCollectionType 49 */ 50 enum class GCCollectionType : uint8_t { NONE = 0, YOUNG, MIXED, TENURED, FULL }; 51 52 struct PANDA_PUBLIC_API GCTask { GCTaskGCTask53 explicit GCTask(GCTaskCause gcReason) : GCTask(gcReason, 0U) {} 54 GCTaskGCTask55 explicit GCTask(GCTaskCause gcReason, uint64_t gcTargetTime) 56 { 57 this->reason = gcReason; 58 this->targetTime_ = gcTargetTime; 59 this->collectionType = GCCollectionType::NONE; 60 this->id_ = nextId_++; 61 } 62 GetIdGCTask63 uint64_t GetId() const 64 { 65 return id_; 66 } 67 68 /** 69 * Update collection type in the gc task if the new coolcetion type is bigger 70 * @param gc_collection_type new gc collection type 71 */ 72 void UpdateGCCollectionType(GCCollectionType gcCollectionType); 73 GetTargetTimeGCTask74 uint64_t GetTargetTime() const 75 { 76 return targetTime_; 77 } 78 79 // NOLINTNEXTLINE(google-runtime-references) 80 virtual void Run(mem::GC &gc); 81 82 virtual void Release(mem::InternalAllocatorPtr allocator); 83 84 virtual ~GCTask() = default; 85 86 GCTask(const GCTask &other) = default; 87 GCTask &operator=(const GCTask &other) = default; 88 GCTask(GCTask &&other) = default; 89 GCTask &operator=(GCTask &&other) = default; 90 91 GCTaskCause reason; // NOLINT(misc-non-private-member-variables-in-classes) 92 GCCollectionType collectionType; // NOLINT(misc-non-private-member-variables-in-classes) 93 94 private: 95 uint32_t id_ = 0; 96 uint64_t targetTime_; 97 static std::atomic<uint32_t> nextId_; 98 }; 99 100 std::ostream &operator<<(std::ostream &os, const GCTaskCause &cause); 101 std::ostream &operator<<(std::ostream &os, const GCCollectionType &collectionType); 102 103 } // namespace ark 104 105 #endif // PANDA_RUNTIME_GC_TASK_H_ 106