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 #include "runtime/include/gc_task.h"
17
18 #include "runtime/mem/gc/gc.h"
19
20 namespace ark {
21
22 std::atomic<uint32_t> GCTask::nextId_ = 1;
23
Run(mem::GC & gc)24 void GCTask::Run(mem::GC &gc)
25 {
26 gc.WaitForGC(*this);
27 gc.SetCanAddGCTask(true);
28 }
29
Release(mem::InternalAllocatorPtr allocator)30 void GCTask::Release(mem::InternalAllocatorPtr allocator)
31 {
32 allocator->Delete(this);
33 }
34
UpdateGCCollectionType(GCCollectionType gcCollectionType)35 void GCTask::UpdateGCCollectionType(GCCollectionType gcCollectionType)
36 {
37 ASSERT(gcCollectionType != GCCollectionType::NONE);
38 if (gcCollectionType <= collectionType) {
39 return;
40 }
41 collectionType = gcCollectionType;
42 }
43
operator <<(std::ostream & os,const GCTaskCause & cause)44 std::ostream &operator<<(std::ostream &os, const GCTaskCause &cause)
45 {
46 switch (cause) {
47 case GCTaskCause::INVALID_CAUSE:
48 os << "Invalid";
49 break;
50 case GCTaskCause::PYGOTE_FORK_CAUSE:
51 os << "PygoteFork";
52 break;
53 case GCTaskCause::STARTUP_COMPLETE_CAUSE:
54 os << "StartupComplete";
55 break;
56 case GCTaskCause::NATIVE_ALLOC_CAUSE:
57 os << "NativeAlloc";
58 break;
59 case GCTaskCause::EXPLICIT_CAUSE:
60 os << "Explicit";
61 break;
62 case GCTaskCause::HEAP_USAGE_THRESHOLD_CAUSE:
63 os << "Threshold";
64 break;
65 case GCTaskCause::MIXED:
66 os << "Mixed";
67 break;
68 case GCTaskCause::YOUNG_GC_CAUSE:
69 os << "Young";
70 break;
71 case GCTaskCause::OOM_CAUSE:
72 os << "OOM";
73 break;
74 case GCTaskCause::CROSSREF_CAUSE:
75 os << "Crossref";
76 break;
77 default:
78 LOG(FATAL, GC) << "Unknown gc cause";
79 break;
80 }
81 return os;
82 }
83
operator <<(std::ostream & os,const GCCollectionType & collectionType)84 std::ostream &operator<<(std::ostream &os, const GCCollectionType &collectionType)
85 {
86 switch (collectionType) {
87 case GCCollectionType::NONE:
88 os << "NONE";
89 break;
90 case GCCollectionType::YOUNG:
91 os << "YOUNG";
92 break;
93 case GCCollectionType::TENURED:
94 os << "TENURED";
95 break;
96 case GCCollectionType::MIXED:
97 os << "MIXED";
98 break;
99 case GCCollectionType::FULL:
100 os << "FULL";
101 break;
102 default:
103 LOG(FATAL, GC) << "Unknown collection type";
104 break;
105 }
106 return os;
107 }
108
109 } // namespace ark
110