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 16 #ifndef ECMASCRIPT_ECMA_PARAM_CONFIGURATION_H 17 #define ECMASCRIPT_ECMA_PARAM_CONFIGURATION_H 18 19 #include "ecmascript/mem/mem_common.h" 20 21 #include "libpandabase/macros.h" 22 23 namespace panda::ecmascript { 24 static constexpr size_t DEFAULT_HEAP_SIZE = 256_MB; // Recommended range: 128-256MB 25 static constexpr size_t DEFAULT_WORKER_HEAP_SIZE = 128_MB; // Recommended range: 128_MB 26 27 class EcmaParamConfiguration { 28 public: EcmaParamConfiguration(bool isWorker,size_t poolSize)29 EcmaParamConfiguration(bool isWorker, size_t poolSize) 30 { 31 if (isWorker) { 32 maxHeapSize_ = DEFAULT_WORKER_HEAP_SIZE; 33 } else { 34 if (poolSize >= DEFAULT_HEAP_SIZE) { 35 maxHeapSize_ = DEFAULT_HEAP_SIZE; 36 } else { 37 maxHeapSize_ = poolSize; // pool is too small, no memory left for worker 38 } 39 } 40 Initialize(); 41 } 42 Initialize()43 void Initialize() 44 { 45 if (maxHeapSize_ < LOW_MEMORY) { 46 UNREACHABLE(); 47 } 48 if (maxHeapSize_ < MEDIUM_MEMORY) { // 64_MB ~ 128_MB 49 minSemiSpaceSize_ = 2_MB; 50 maxSemiSpaceSize_ = 4_MB; 51 defaultReadOnlySpaceSize_ = 256_KB; 52 defaultNonMovableSpaceSize_ = 2_MB; 53 defaultSnapshotSpaceSize_ = 512_KB; 54 defaultMachineCodeSpaceSize_ = 2_MB; 55 semiSpaceTriggerConcurrentMark_ = 1_MB; 56 semiSpaceOvershootSize_ = 2_MB; 57 outOfMemoryOvershootSize_ = 2_MB; 58 minAllocLimitGrowingStep_ = 2_MB; 59 minGrowingStep_ = 4_MB; 60 maxStackSize_ = 128_KB; 61 } else if (maxHeapSize_ < HIGH_MEMORY) { // 128_MB ~ 256_MB 62 minSemiSpaceSize_ = 2_MB; 63 maxSemiSpaceSize_ = 8_MB; 64 defaultReadOnlySpaceSize_ = 256_KB; 65 defaultNonMovableSpaceSize_ = 6_MB; 66 defaultSnapshotSpaceSize_ = 512_KB; 67 defaultMachineCodeSpaceSize_ = 2_MB; 68 semiSpaceTriggerConcurrentMark_ = 1.5_MB; 69 semiSpaceOvershootSize_ = 2_MB; 70 outOfMemoryOvershootSize_ = 2_MB; 71 minAllocLimitGrowingStep_ = 4_MB; 72 minGrowingStep_ = 8_MB; 73 maxStackSize_ = 128_KB; 74 } else { // 256_MB 75 minSemiSpaceSize_ = 2_MB; 76 maxSemiSpaceSize_ = 16_MB; 77 defaultReadOnlySpaceSize_ = 256_KB; 78 defaultNonMovableSpaceSize_ = 6_MB; 79 defaultSnapshotSpaceSize_ = 4_MB; 80 defaultMachineCodeSpaceSize_ = 8_MB; 81 semiSpaceTriggerConcurrentMark_ = 1.5_MB; 82 semiSpaceOvershootSize_ = 2_MB; 83 outOfMemoryOvershootSize_ = 2_MB; 84 minAllocLimitGrowingStep_ = 8_MB; 85 minGrowingStep_ = 16_MB; 86 maxStackSize_ = 128_KB; 87 } 88 } 89 GetMaxHeapSize()90 size_t GetMaxHeapSize() const 91 { 92 return maxHeapSize_; 93 } 94 GetMinSemiSpaceSize()95 size_t GetMinSemiSpaceSize() const 96 { 97 return minSemiSpaceSize_; 98 } 99 GetMaxSemiSpaceSize()100 size_t GetMaxSemiSpaceSize() const 101 { 102 return maxSemiSpaceSize_; 103 } 104 GetDefaultReadOnlySpaceSize()105 size_t GetDefaultReadOnlySpaceSize() const 106 { 107 return defaultReadOnlySpaceSize_; 108 } 109 GetDefaultNonMovableSpaceSize()110 size_t GetDefaultNonMovableSpaceSize() const 111 { 112 return defaultNonMovableSpaceSize_; 113 } 114 GetDefaultSnapshotSpaceSize()115 size_t GetDefaultSnapshotSpaceSize() const 116 { 117 return defaultSnapshotSpaceSize_; 118 } 119 GetDefaultMachineCodeSpaceSize()120 size_t GetDefaultMachineCodeSpaceSize() const 121 { 122 return defaultMachineCodeSpaceSize_; 123 } 124 GetSemiSpaceTriggerConcurrentMark()125 size_t GetSemiSpaceTriggerConcurrentMark() const 126 { 127 return semiSpaceTriggerConcurrentMark_; 128 } 129 GetSemiSpaceOvershootSize()130 size_t GetSemiSpaceOvershootSize() const 131 { 132 return semiSpaceOvershootSize_; 133 } 134 GetOutOfMemoryOvershootSize()135 size_t GetOutOfMemoryOvershootSize() const 136 { 137 return outOfMemoryOvershootSize_; 138 } 139 GetMinAllocLimitGrowingStep()140 size_t GetMinAllocLimitGrowingStep() const 141 { 142 return minAllocLimitGrowingStep_; 143 } 144 GetMinGrowingStep()145 size_t GetMinGrowingStep() const 146 { 147 return minGrowingStep_; 148 } 149 GetMaxStackSize()150 uint32_t GetMaxStackSize() const 151 { 152 return maxStackSize_; 153 } 154 GetDefalutStackSize()155 static size_t GetDefalutStackSize() 156 { 157 return DEFAULT_STACK_SIZE; 158 } 159 GetDefaultReservedStackSize()160 static size_t GetDefaultReservedStackSize() 161 { 162 return DEFAULT_RESERVED_STACK_SIZE; 163 } 164 165 private: 166 static constexpr size_t LOW_MEMORY = 64_MB; 167 static constexpr size_t MEDIUM_MEMORY = 128_MB; 168 static constexpr size_t HIGH_MEMORY = 256_MB; 169 static constexpr size_t DEFAULT_STACK_SIZE = 992_KB; 170 static constexpr size_t DEFAULT_RESERVED_STACK_SIZE = 16_KB; 171 172 size_t maxHeapSize_ {0}; 173 size_t minSemiSpaceSize_ {0}; 174 size_t maxSemiSpaceSize_ {0}; 175 size_t defaultReadOnlySpaceSize_ {0}; 176 size_t defaultNonMovableSpaceSize_ {0}; 177 size_t defaultSnapshotSpaceSize_ {0}; 178 size_t defaultMachineCodeSpaceSize_ {0}; 179 size_t semiSpaceTriggerConcurrentMark_ {0}; 180 size_t semiSpaceOvershootSize_ {0}; 181 size_t outOfMemoryOvershootSize_ {0}; 182 size_t minAllocLimitGrowingStep_ {0}; 183 size_t minGrowingStep_ {0}; 184 uint32_t maxStackSize_ {0}; 185 }; 186 } // namespace panda::ecmascript 187 188 #endif // ECMASCRIPT_ECMA_PARAM_CONFIGURATION_H 189