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 WORKER_NUM = 8;
48 static constexpr size_t PHY_SIZE_MULTIPLE = WORKER_NUM + 1;
49 static constexpr size_t STANDARD_POOL_SIZE = WORKER_NUM * DEFAULT_WORKER_HEAP_SIZE + DEFAULT_HEAP_SIZE;
50
51 #if defined(PANDA_TARGET_32)
52 static constexpr size_t MAX_GLOBAL_NATIVE_LIMIT = 512_MB;
53 #else
54 static constexpr size_t MAX_GLOBAL_NATIVE_LIMIT = 2048_MB;
55 #endif
56
57 static constexpr size_t MIN_OLD_SPACE_LIMIT = 2_MB;
58
59 static constexpr size_t MAX_NONMOVABLE_LIVE_OBJ_SIZE = 60_MB;
60
61 static constexpr size_t REGION_SIZE_LOG2 = 18U;
62
63 static constexpr size_t MIN_HEAP_SIZE = 5_MB;
64
65 static constexpr size_t DEFAULT_REGION_SIZE = 1U << REGION_SIZE_LOG2;
66 static constexpr size_t DEFAULT_REGION_MASK = DEFAULT_REGION_SIZE - 1;
67
68 static constexpr size_t DEFAULT_MARK_STACK_SIZE = 4_KB;
69
70 static constexpr double MIN_OBJECT_SURVIVAL_RATE = 0.75;
71 static constexpr double GROW_OBJECT_SURVIVAL_RATE = 0.8;
72 static constexpr double SHRINK_OBJECT_SURVIVAL_RATE = 0.2;
73 static constexpr double LOW_ALLOCATION_SPEED_PER_MS = 1000;
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 size_t MIN_WORKNODE_CHUNK_AREA_SIZE = 256_KB;
85 static constexpr uint32_t WORKNODE_SPACE_SIZE = 8_KB;
86 static constexpr size_t MAX_CHUNK_AREA_SIZE = 1_MB;
87 static constexpr size_t MAX_REGEXP_CACHE_SIZE = 8_KB;
88
89 // idle gc
90 static constexpr size_t IDLE_GC_YOUNG_SPACE = 3_MB;
91
92 using TaggedType = uint64_t;
93 static constexpr uint32_t TAGGED_TYPE_SIZE = sizeof(TaggedType);
94 static constexpr uint32_t TAGGED_TYPE_SIZE_LOG = base::MathHelper::GetIntLog2(TAGGED_TYPE_SIZE);
95
96 template<typename T>
IsAligned(T value,size_t alignment)97 constexpr inline bool IsAligned(T value, size_t alignment)
98 {
99 return (value & (alignment - 1U)) == 0;
100 }
101
102 template<typename T>
AlignDown(T x,size_t alignment)103 inline T AlignDown(T x, size_t alignment)
104 {
105 ASSERT(std::is_integral<T>::value);
106 // alignment must be a power of two.
107 ASSERT(alignment != 0 && ((alignment & (alignment - 1U)) == 0));
108 return x & ~(alignment - 1U);
109 }
110
111 template<typename T>
AlignUp(T x,size_t alignment)112 inline T AlignUp(T x, size_t alignment)
113 {
114 ASSERT(std::is_integral<T>::value);
115 return AlignDown<T>(static_cast<T>(x + alignment - 1U), alignment);
116 }
117 } // namespace panda::ecmascript
118
119 #endif // ECMASCRIPT_MEM_MEM_H
120