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_MEM_GC_GC_TYPES_H_
17 #define PANDA_RUNTIME_MEM_GC_GC_TYPES_H_
18
19 #include <array>
20 #include <string_view>
21
22 namespace panda::mem {
23
24 enum class GCExecutionMode {
25 GC_STW_NO_MT, // Stop-the-world, single thread
26 GC_EXECUTION_MODE_LAST = GC_STW_NO_MT
27 };
28
29 constexpr GCExecutionMode GC_EXECUTION_MODE = GCExecutionMode::GC_STW_NO_MT;
30
31 enum class GCType {
32 INVALID_GC = 0,
33 EPSILON_GC,
34 STW_GC,
35 HYBRID_GC,
36 GEN_GC,
37 G1_GC,
38 GCTYPE_LAST = G1_GC,
39 };
40
IsGenerationalGCType(const GCType gc_type)41 constexpr bool IsGenerationalGCType(const GCType gc_type)
42 {
43 bool ret = false;
44 ASSERT(gc_type != GCType::INVALID_GC);
45 switch (gc_type) {
46 case GCType::GEN_GC:
47 case GCType::G1_GC:
48 ret = true;
49 break;
50 case GCType::INVALID_GC:
51 case GCType::EPSILON_GC:
52 case GCType::STW_GC:
53 case GCType::HYBRID_GC:
54 break;
55 default:
56 UNREACHABLE();
57 break;
58 }
59 return ret;
60 }
61
ToIndex(GCType type)62 constexpr size_t ToIndex(GCType type)
63 {
64 return static_cast<size_t>(type);
65 }
66
67 constexpr size_t GC_TYPE_SIZE = ToIndex(GCType::GCTYPE_LAST) + 1;
68 constexpr std::array<char const *, GC_TYPE_SIZE> GC_NAMES = {"Invalid GC", "Epsilon GC", "Stop-The-World GC",
69 "Hybrid GC", "Generation GC"};
70
StringsEqual(char const * a,char const * b)71 constexpr bool StringsEqual(char const *a, char const *b)
72 {
73 return std::string_view(a) == b;
74 }
75
76 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::INVALID_GC)], "Invalid GC"));
77 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::EPSILON_GC)], "Epsilon GC"));
78 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::STW_GC)], "Stop-The-World GC"));
79 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::HYBRID_GC)], "Hybrid GC"));
80 static_assert(StringsEqual(GC_NAMES[ToIndex(GCType::GEN_GC)], "Generation 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 return "invalid-gc";
117 }
118
119 enum GCCollectMode : uint8_t {
120 GC_NONE = 0, // Non collected objects
121 GC_MINOR = 1U, // Objects collected at the minor GC
122 GC_MAJOR = 1U << 1U, // Objects collected at the major GC (MAJOR usually includes MINOR)
123 GC_FULL = 1U << 2U, // Can collect objects from some spaces which very rarely contains GC candidates
124 // NOLINTNEXTLINE(hicpp-signed-bitwise)
125 GC_ALL = GC_MINOR | GC_MAJOR | GC_FULL, // Can collect objects at any phase
126 };
127
128 } // namespace panda::mem
129
130 #endif // PANDA_RUNTIME_MEM_GC_GC_TYPES_H_
131