1 /* 2 * Copyright (c) 2021 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_INCLUDE_GC_TASK_H_ 17 #define PANDA_RUNTIME_INCLUDE_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 struct GCTask { GCTaskGCTask46 explicit GCTask(GCTaskCause reason, uint64_t target_time = 0U) : GCTask(reason, target_time, nullptr) {} 47 GCTaskGCTask48 explicit GCTask(GCTaskCause reason, ManagedThread *caller_thread) : GCTask(reason, 0U, caller_thread) {} 49 GCTaskGCTask50 explicit GCTask(GCTaskCause reason, uint64_t target_time, ManagedThread *caller_thread) 51 { 52 this->reason_ = reason; 53 this->target_time_ = target_time; 54 this->caller_thread_ = caller_thread; 55 } 56 GetTargetTimeGCTask57 uint64_t GetTargetTime() const 58 { 59 return target_time_; 60 } 61 62 // NOLINTNEXTLINE(google-runtime-references) 63 virtual void Run(mem::GC &gc); 64 65 virtual void Release(mem::InternalAllocatorPtr allocator); 66 67 virtual ~GCTask() = default; 68 69 GCTask(const GCTask &other) = default; 70 GCTask &operator=(const GCTask &other) = default; 71 GCTask(GCTask &&other) = default; 72 GCTask &operator=(GCTask &&other) = default; 73 74 GCTaskCause reason_; // NOLINT(misc-non-private-member-variables-in-classes) 75 ManagedThread *caller_thread_; // NOLINT(misc-non-private-member-variables-in-classes) 76 77 private: 78 uint64_t target_time_; 79 }; 80 81 std::ostream &operator<<(std::ostream &os, const GCTaskCause &cause); 82 83 } // namespace panda 84 85 #endif // PANDA_RUNTIME_INCLUDE_GC_TASK_H_ 86