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 ECMASCRIPT_MEM_MEM_H
17 #define ECMASCRIPT_MEM_MEM_H
18
19 #include <cstdint>
20
21 #include "ecmascript/base/math_helper.h"
22 #include "ecmascript/ecma_param_configuration.h"
23 #include "ecmascript/mem/mem_common.h"
24 #include "ecmascript/mem/tagged_object.h"
25
26 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage, bugprone-lambda-function-name)
27 #define LOG_ECMA_MEM(level) LOG_GC(level) << __func__ << ":" << __LINE__ << " "
28
29 namespace panda::ecmascript {
30 enum class MemAlignment : uint8_t {
31 MEM_ALIGN_OBJECT = 8,
32 MEM_ALIGN_REGION = 16,
33 };
34
35 enum class MemAlignmentLog2 : uint8_t {
36 MEM_ALIGN_OBJECT_LOG2 = 3,
37 MEM_ALIGN_REGION_LOG2 = 4,
38 };
39
40 static constexpr size_t INITIAL_REGULAR_OBJECT_CAPACITY = 1024_MB;
41 static constexpr size_t INITIAL_HUGE_OBJECT_CAPACITY = 1024_MB;
42 static constexpr size_t INCREMENT_HUGE_OBJECT_CAPACITY = 128_MB;
43 static constexpr size_t LARGE_POOL_SIZE = 480_MB;
44 static constexpr size_t MEDIUM_POOL_SIZE = 256_MB;
45 static constexpr size_t LOW_POOL_SIZE = 64_MB;
46 static constexpr size_t MIN_MEM_POOL_CAPACITY = 64_MB;
47 static constexpr size_t MAX_MEM_POOL_CAPACITY = 1536_MB;
48
49 #if defined(PANDA_TARGET_32)
50 static constexpr size_t MAX_GLOBAL_NATIVE_LIMIT = 512_MB;
51 #else
52 static constexpr size_t MAX_GLOBAL_NATIVE_LIMIT = 2048_MB;
53 #endif
54
55 static constexpr size_t MIN_OLD_SPACE_LIMIT = 2_MB;
56 static constexpr size_t MIN_BACKGROUNG_GC_LIMIT = 30_MB;
57
58 static constexpr size_t MAX_NONMOVABLE_LIVE_OBJ_SIZE = 60_MB;
59
60 static constexpr size_t REGION_SIZE_LOG2 = 18U;
61
62 static constexpr size_t MIN_HEAP_SIZE = 5_MB;
63
64 static constexpr size_t DEFAULT_REGION_SIZE = 1U << REGION_SIZE_LOG2;
65 static constexpr size_t DEFAULT_REGION_MASK = DEFAULT_REGION_SIZE - 1;
66
67 static constexpr size_t DEFAULT_MARK_STACK_SIZE = 4_KB;
68
69 static constexpr double MIN_OBJECT_SURVIVAL_RATE = 0.75;
70 static constexpr double GROW_OBJECT_SURVIVAL_RATE = 0.8;
71 static constexpr double SHRINK_OBJECT_SURVIVAL_RATE = 0.2;
72 static constexpr double LOW_ALLOCATION_SPEED_PER_MS = 1000;
73 static constexpr double DEFAULT_CAPACITY_RATE = 0.6;
74 // Objects which are larger than half of the region size are huge objects.
75 // Regular objects will be allocated on regular regions and migrated on spaces.
76 // They will never be moved to huge object space. So we take half of a regular
77 // region as the border of regular objects.
78 static constexpr size_t MAX_32BIT_OBJECT_SPACE_SIZE = 1_GB;
79 static constexpr size_t MAX_REGULAR_HEAP_OBJECT_SIZE = DEFAULT_REGION_SIZE * 2 / 3;
80 // internal allocator
81 static constexpr size_t CHUNK_ALIGN_SIZE = 4_KB;
82 static constexpr size_t MIN_CHUNK_AREA_SIZE = 4_KB;
83 static constexpr size_t MAX_CACHED_CHUNK_AREA_SIZE = 16_KB;
84 static constexpr uint32_t WORKNODE_SPACE_SIZE = 8_KB;
85 static constexpr size_t MAX_CHUNK_AREA_SIZE = 1_MB;
86 static constexpr size_t MAX_REGEXP_CACHE_SIZE = 8_KB;
87 // taskpool
88 static constexpr double TRIGGER_OLDGC_OBJECT_LIMIT_RATE = 0.1;
89 static constexpr double TRIGGER_OLDGC_OBJECT_SIZE_LIMIT = 20_MB;
90 static constexpr double TRIGGER_OLDGC_NATIVE_LIMIT_RATE = 0.1;
91 static constexpr double TRIGGER_OLDGC_NATIVE_SIZE_LIMIT = 20_MB;
92 // idle gc
93 static constexpr size_t IDLE_GC_YOUNG_SPACE = 3_MB;
94
95 static constexpr double LOW_ALLOCATION_RATE_PER_MS = 10;
96 static constexpr double IDLE_SPACE_SIZE_LIMIT_RATE = 0.8f;
97 static constexpr double IDLE_SPACE_SIZE_MIN_INC_RATIO = 1.1f;
98 static constexpr double IDLE_FRAGMENT_SIZE_RATIO = 0.1f;
99 static constexpr size_t IDLE_SPACE_SIZE_MIN_INC_STEP = 5_MB;
100 static constexpr size_t IDLE_SPACE_SIZE_MIN_INC_STEP_FULL = 1_MB;
101 static constexpr size_t IDLE_MIN_EXPECT_RECLAIM_SIZE = 1_MB;
102
103 using TaggedType = uint64_t;
104 static constexpr uint32_t TAGGED_TYPE_SIZE = sizeof(TaggedType);
105 static constexpr uint32_t TAGGED_TYPE_SIZE_LOG = base::MathHelper::GetIntLog2(TAGGED_TYPE_SIZE);
106 constexpr size_t HEAD_SIZE = TaggedObject::TaggedObjectSize();
107
108 template<typename T>
IsAligned(T value, size_t alignment)109 constexpr inline bool IsAligned(T value, size_t alignment)
110 {
111 return (value & (alignment - 1U)) == 0;
112 }
113
114 template<typename T>
AlignDown(T x, size_t alignment)115 inline T AlignDown(T x, size_t alignment)
116 {
117 ASSERT(std::is_integral<T>::value);
118 // alignment must be a power of two.
119 ASSERT(alignment != 0 && ((alignment & (alignment - 1U)) == 0));
120 return x & ~(alignment - 1U);
121 }
122
123 template<typename T>
AlignUp(T x, size_t alignment)124 inline T AlignUp(T x, size_t alignment)
125 {
126 ASSERT(std::is_integral<T>::value && (x + alignment) > 0);
127 return AlignDown<T>(static_cast<T>(x + alignment - 1U), alignment);
128 }
129 } // namespace panda::ecmascript
130
131 #endif // ECMASCRIPT_MEM_MEM_H
132