1 /**
2 * Copyright (c) 2021-2025 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 #ifndef RUNTIME_MEM_GC_GC_TYPES_H
16 #define RUNTIME_MEM_GC_GC_TYPES_H
17
18 #include <array>
19 #include <string_view>
20
21 namespace ark::mem {
22
23 enum class GCExecutionMode {
24 GC_STW_NO_MT, // Stop-the-world, single thread
25 GC_EXECUTION_MODE_LAST = GC_STW_NO_MT
26 };
27
28 constexpr GCExecutionMode GC_EXECUTION_MODE = GCExecutionMode::GC_STW_NO_MT;
29
30 enum class GCType {
31 INVALID_GC = 0,
32 EPSILON_GC,
33 EPSILON_G1_GC,
34 STW_GC,
35 GEN_GC,
36 G1_GC,
37 CMC_GC,
38 GCTYPE_LAST = CMC_GC,
39 };
40
IsGenerationalGCType(const GCType gcType)41 constexpr bool IsGenerationalGCType(const GCType gcType)
42 {
43 bool ret = false;
44 ASSERT(gcType != GCType::INVALID_GC);
45 switch (gcType) {
46 case GCType::GEN_GC:
47 case GCType::G1_GC:
48 case GCType::EPSILON_G1_GC:
49 ret = true;
50 break;
51 case GCType::INVALID_GC:
52 case GCType::EPSILON_GC:
53 case GCType::STW_GC:
54 case GCType::CMC_GC:
55 break;
56 default:
57 UNREACHABLE();
58 break;
59 }
60 return ret;
61 }
62
ToIndex(GCType type)63 constexpr size_t ToIndex(GCType type)
64 {
65 return static_cast<size_t>(type);
66 }
67
68 constexpr size_t GC_TYPE_SIZE = ToIndex(GCType::GCTYPE_LAST) + 1;
69 constexpr std::array<char const *, GC_TYPE_SIZE> GC_NAMES = {
70 "Invalid GC", "Epsilon GC", "Epsilon-G1 GC", "Stop-The-World GC", "Generation GC", "G1 GC", "CMC GC"};
71
StringsEqual(char const * a,char const * b)72 constexpr bool StringsEqual(char const *a, char const *b)
73 {
74 return std::string_view(a) == b;
75 }
76
77 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::INVALID_GC)], "Invalid GC"));
78 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::EPSILON_GC)], "Epsilon GC"));
79 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::EPSILON_G1_GC)], "Epsilon-G1 GC"));
80 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::STW_GC)], "Stop-The-World GC"));
81 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::GEN_GC)], "Generation GC"));
82 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::G1_GC)], "G1 GC"));
83 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::CMC_GC)], "CMC GC"));
84
GCTypeFromString(std::string_view gcTypeStr)85 constexpr GCType GCTypeFromString(std::string_view gcTypeStr)
86 {
87 if (gcTypeStr == "epsilon") {
88 return GCType::EPSILON_GC;
89 }
90 if (gcTypeStr == "epsilon-g1") {
91 return GCType::EPSILON_G1_GC;
92 }
93 if (gcTypeStr == "stw") {
94 return GCType::STW_GC;
95 }
96 if (gcTypeStr == "gen-gc") {
97 return GCType::GEN_GC;
98 }
99 if (gcTypeStr == "g1-gc") {
100 return GCType::G1_GC;
101 }
102 if (gcTypeStr == "cmc-gc") {
103 return GCType::CMC_GC;
104 }
105 return GCType::INVALID_GC;
106 }
107
GCStringFromType(GCType gcType)108 constexpr std::string_view GCStringFromType(GCType gcType)
109 {
110 if (gcType == GCType::EPSILON_GC) {
111 return "epsilon";
112 }
113 if (gcType == GCType::EPSILON_G1_GC) {
114 return "epsilon-g1";
115 }
116 if (gcType == GCType::STW_GC) {
117 return "stw";
118 }
119 if (gcType == GCType::GEN_GC) {
120 return "gen-gc";
121 }
122 if (gcType == GCType::G1_GC) {
123 return "g1-gc";
124 }
125 if (gcType == GCType::CMC_GC) {
126 return "cmc-gc";
127 }
128 return "invalid-gc";
129 }
130
131 enum GCCollectMode : uint8_t {
132 GC_NONE = 0, // Non collected objects
133 GC_MINOR = 1U, // Objects collected at the minor GC
134 GC_MAJOR = 1U << 1U, // Objects collected at the major GC (MAJOR usually includes MINOR)
135 GC_FULL = 1U << 2U, // Can collect objects from some spaces which very rare contains GC candidates
136 // NOLINTNEXTLINE(hicpp-signed-bitwise)
137 GC_ALL = GC_MINOR | GC_MAJOR | GC_FULL, // Can collect objects at any phase
138 };
139
140 } // namespace ark::mem
141
142 #endif // RUNTIME_MEM_GC_GC_TYPES_H
143