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