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 #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 panda::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 STW_GC,
34 HYBRID_GC,
35 GEN_GC,
36 G1_GC,
37 GCTYPE_LAST = G1_GC,
38 };
39
IsGenerationalGCType(const GCType gc_type)40 constexpr bool IsGenerationalGCType(const GCType gc_type)
41 {
42 bool ret = false;
43 ASSERT(gc_type != GCType::INVALID_GC);
44 switch (gc_type) {
45 case GCType::GEN_GC:
46 case GCType::G1_GC:
47 ret = true;
48 break;
49 case GCType::INVALID_GC:
50 case GCType::EPSILON_GC:
51 case GCType::STW_GC:
52 case GCType::HYBRID_GC:
53 break;
54 default:
55 UNREACHABLE();
56 break;
57 }
58 return ret;
59 }
60
ToIndex(GCType type)61 constexpr size_t ToIndex(GCType type)
62 {
63 return static_cast<size_t>(type);
64 }
65
66 constexpr size_t GC_TYPE_SIZE = ToIndex(GCType::GCTYPE_LAST) + 1;
67 constexpr std::array<char const *, GC_TYPE_SIZE> GC_NAMES = {"Invalid GC", "Epsilon GC", "Stop-The-World GC",
68 "Hybrid GC", "Generation GC", "G1 GC"};
69
StringsEqual(char const * a,char const * b)70 constexpr bool StringsEqual(char const *a, char const *b)
71 {
72 return std::string_view(a) == b;
73 }
74
75 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::INVALID_GC)], "Invalid GC"));
76 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::EPSILON_GC)], "Epsilon GC"));
77 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::STW_GC)], "Stop-The-World GC"));
78 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::HYBRID_GC)], "Hybrid GC"));
79 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::GEN_GC)], "Generation GC"));
80 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::G1_GC)], "G1 GC"));
81
GCTypeFromString(std::string_view gc_type_str)82 inline GCType GCTypeFromString(std::string_view gc_type_str)
83 {
84 if (gc_type_str == "epsilon") {
85 return GCType::EPSILON_GC;
86 }
87 if (gc_type_str == "stw") {
88 return GCType::STW_GC;
89 }
90 if (gc_type_str == "gen-gc") {
91 return GCType::GEN_GC;
92 }
93 if (gc_type_str == "hybrid-gc") {
94 return GCType::HYBRID_GC;
95 }
96 if (gc_type_str == "g1-gc") {
97 return GCType::G1_GC;
98 }
99 return GCType::INVALID_GC;
100 }
101
GCStringFromType(GCType gc_type)102 inline std::string_view GCStringFromType(GCType gc_type)
103 {
104 if (gc_type == GCType::EPSILON_GC) {
105 return "epsilon";
106 }
107 if (gc_type == GCType::STW_GC) {
108 return "stw";
109 }
110 if (gc_type == GCType::GEN_GC) {
111 return "gen-gc";
112 }
113 if (gc_type == GCType::HYBRID_GC) {
114 return "hybrid-gc";
115 }
116 if (gc_type == GCType::G1_GC) {
117 return "g1-gc";
118 }
119 return "invalid-gc";
120 }
121
122 enum GCCollectMode : uint8_t {
123 GC_NONE = 0, // Non collected objects
124 GC_MINOR = 1U, // Objects collected at the minor GC
125 GC_MAJOR = 1U << 1U, // Objects collected at the major GC (MAJOR usually includes MINOR)
126 GC_FULL = 1U << 2U, // Can collect objects from some spaces which very rare contains GC candidates
127 // NOLINTNEXTLINE(hicpp-signed-bitwise)
128 GC_ALL = GC_MINOR | GC_MAJOR | GC_FULL, // Can collect objects at any phase
129 };
130
131 } // namespace panda::mem
132
133 #endif // RUNTIME_MEM_GC_GC_TYPES_H
134