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 }; 43 44 /** 45 * Collection types are ordered by gc collection scale 46 * 47 * @see GCTask::UpdateGCCollectionType 48 */ 49 enum class GCCollectionType : uint8_t { NONE = 0, YOUNG, MIXED, TENURED, FULL }; 50 51 struct PANDA_PUBLIC_API GCTask { GCTaskGCTask52 explicit GCTask(GCTaskCause gcReason) : GCTask(gcReason, 0U) {} 53 GCTaskGCTask54 explicit GCTask(GCTaskCause gcReason, uint64_t gcTargetTime) 55 { 56 this->reason = gcReason; 57 this->targetTime_ = gcTargetTime; 58 this->collectionType = GCCollectionType::NONE; 59 this->id_ = nextId_++; 60 } 61 GetIdGCTask62 uint64_t GetId() const 63 { 64 return id_; 65 } 66 67 /** 68 * Update collection type in the gc task if the new coolcetion type is bigger 69 * @param gc_collection_type new gc collection type 70 */ 71 void UpdateGCCollectionType(GCCollectionType gcCollectionType); 72 GetTargetTimeGCTask73 uint64_t GetTargetTime() const 74 { 75 return targetTime_; 76 } 77 78 // NOLINTNEXTLINE(google-runtime-references) 79 virtual void Run(mem::GC &gc); 80 81 virtual void Release(mem::InternalAllocatorPtr allocator); 82 83 virtual ~GCTask() = default; 84 85 GCTask(const GCTask &other) = default; 86 GCTask &operator=(const GCTask &other) = default; 87 GCTask(GCTask &&other) = default; 88 GCTask &operator=(GCTask &&other) = default; 89 90 GCTaskCause reason; // NOLINT(misc-non-private-member-variables-in-classes) 91 GCCollectionType collectionType; // NOLINT(misc-non-private-member-variables-in-classes) 92 93 private: 94 uint32_t id_ = 0; 95 uint64_t targetTime_; 96 static std::atomic<uint32_t> nextId_; 97 }; 98 99 std::ostream &operator<<(std::ostream &os, const GCTaskCause &cause); 100 std::ostream &operator<<(std::ostream &os, const GCCollectionType &collectionType); 101 102 } // namespace ark 103 104 #endif // PANDA_RUNTIME_GC_TASK_H_ 105