1 /** 2 * Copyright (c) 2021-2022 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 panda { 24 25 class ManagedThread; 26 27 namespace mem { 28 class GC; 29 } // namespace mem 30 31 /** 32 * Causes are ordered by priority. Bigger index - means bigger priority in GC 33 */ 34 enum class GCTaskCause : uint8_t { 35 INVALID_CAUSE = 0, 36 YOUNG_GC_CAUSE, // if young space is full 37 PYGOTE_FORK_CAUSE, 38 STARTUP_COMPLETE_CAUSE, 39 NATIVE_ALLOC_CAUSE, 40 HEAP_USAGE_THRESHOLD_CAUSE, 41 EXPLICIT_CAUSE, // System.gc 42 OOM_CAUSE, // if all heap is full 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 GCTask { GCTaskGCTask53 explicit GCTask(GCTaskCause reason, uint64_t target_time = 0U) : GCTask(reason, target_time, nullptr) {} 54 GCTaskGCTask55 explicit GCTask(GCTaskCause reason, ManagedThread *caller_thread) : GCTask(reason, 0U, caller_thread) {} 56 GCTaskGCTask57 explicit GCTask(GCTaskCause reason, uint64_t target_time, ManagedThread *caller_thread) 58 { 59 this->reason_ = reason; 60 this->target_time_ = target_time; 61 this->caller_thread_ = caller_thread; 62 this->collection_type_ = GCCollectionType::NONE; 63 } 64 65 /** 66 * Update collection type in the gc task if the new coolcetion type is bigger 67 * @param collection_type new gc collection type 68 */ 69 void UpdateGCCollectionType(GCCollectionType collection_type); 70 GetTargetTimeGCTask71 uint64_t GetTargetTime() const 72 { 73 return target_time_; 74 } 75 76 // NOLINTNEXTLINE(google-runtime-references) 77 virtual void Run(mem::GC &gc); 78 79 virtual void Release(mem::InternalAllocatorPtr allocator); 80 81 virtual ~GCTask() = default; 82 83 GCTask(const GCTask &other) = default; 84 GCTask &operator=(const GCTask &other) = default; 85 GCTask(GCTask &&other) = default; 86 GCTask &operator=(GCTask &&other) = default; 87 88 GCTaskCause reason_; // NOLINT(misc-non-private-member-variables-in-classes) 89 GCCollectionType collection_type_; // NOLINT(misc-non-private-member-variables-in-classes) 90 ManagedThread *caller_thread_; // NOLINT(misc-non-private-member-variables-in-classes) 91 92 private: 93 uint64_t target_time_; 94 }; 95 96 std::ostream &operator<<(std::ostream &os, const GCTaskCause &cause); 97 std::ostream &operator<<(std::ostream &os, const GCCollectionType &collection_type); 98 99 } // namespace panda 100 101 #endif // PANDA_RUNTIME_GC_TASK_H_ 102