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 MAX_HUGE_OBJECT_CAPACITY = 512_MB;
41 static constexpr size_t LARGE_POOL_SIZE = 480_MB;
42 static constexpr size_t MEDIUM_POOL_SIZE = 256_MB;
43 static constexpr size_t LOW_POOL_SIZE = 64_MB;
44 static constexpr size_t MIN_MEM_POOL_CAPACITY = 64_MB;
45 static constexpr size_t WORKER_NUM = 7;
46 static constexpr size_t PHY_SIZE_MULTIPLE = WORKER_NUM + 1;
47 static constexpr size_t STANDARD_POOL_SIZE = WORKER_NUM * DEFAULT_WORKER_HEAP_SIZE + DEFAULT_HEAP_SIZE;
48 #if defined(PANDA_TARGET_32)
49 static constexpr size_t MAX_GLOBAL_NATIVE_LIMIT = 512_MB;
50 #else
51 static constexpr size_t MAX_GLOBAL_NATIVE_LIMIT = 2048_MB;
52 #endif
53
54 static constexpr size_t MIN_OLD_SPACE_LIMIT = 2_MB;
55
56 static constexpr size_t REGION_SIZE_LOG2 = 18U;
57
58 static constexpr size_t MIN_HEAP_SIZE = 5_MB;
59
60 static constexpr size_t DEFAULT_REGION_SIZE = 1U << REGION_SIZE_LOG2;
61 static constexpr size_t DEFAULT_REGION_MASK = DEFAULT_REGION_SIZE - 1;
62
63 static constexpr size_t DEFAULT_MARK_STACK_SIZE = 4_KB;
64
65 static constexpr double MIN_OBJECT_SURVIVAL_RATE = 0.75;
66 static constexpr double GROW_OBJECT_SURVIVAL_RATE = 0.8;
67 static constexpr double SHRINK_OBJECT_SURVIVAL_RATE = 0.2;
68 static constexpr double LOW_ALLOCATION_SPEED_PER_MS = 1000;
69 // Objects which are larger than half of the region size are huge objects.
70 // Regular objects will be allocated on regular regions and migrated on spaces.
71 // They will never be moved to huge object space. So we take half of a regular
72 // region as the border of regular objects.
73 static constexpr size_t MAX_32BIT_OBJECT_SPACE_SIZE = 1_GB;
74 static constexpr size_t MAX_REGULAR_HEAP_OBJECT_SIZE = DEFAULT_REGION_SIZE * 2 / 3;
75 // internal allocator
76 static constexpr size_t CHUNK_ALIGN_SIZE = 4_KB;
77 static constexpr size_t MIN_CHUNK_AREA_SIZE = 4_KB;
78 static constexpr size_t MAX_CACHED_CHUNK_AREA_SIZE = 16_KB;
79 static constexpr size_t MAX_CHUNK_AREA_SIZE = 1_MB;
80
81 // idle gc
82 static constexpr size_t IDLE_GC_YOUNG_SPACE = 3_MB;
83
84 using TaggedType = uint64_t;
85 static constexpr uint32_t TAGGED_TYPE_SIZE = sizeof(TaggedType);
86 static constexpr uint32_t TAGGED_TYPE_SIZE_LOG = base::MathHelper::GetIntLog2(TAGGED_TYPE_SIZE);
87
88 template<typename T>
IsAligned(T value,size_t alignment)89 constexpr inline bool IsAligned(T value, size_t alignment)
90 {
91 return (value & (alignment - 1U)) == 0;
92 }
93
94 template<typename T>
AlignDown(T x,size_t alignment)95 inline T AlignDown(T x, size_t alignment)
96 {
97 ASSERT(std::is_integral<T>::value);
98 // alignment must be a power of two.
99 ASSERT(alignment != 0 && ((alignment & (alignment - 1U)) == 0));
100 return x & ~(alignment - 1U);
101 }
102
103 template<typename T>
AlignUp(T x,size_t alignment)104 inline T AlignUp(T x, size_t alignment)
105 {
106 ASSERT(std::is_integral<T>::value);
107 return AlignDown<T>(static_cast<T>(x + alignment - 1U), alignment);
108 }
109 } // namespace panda::ecmascript
110
111 #endif // ECMASCRIPT_MEM_MEM_H
112