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 static constexpr size_t DEFAULT_SHARED_HEAP_SIZE = 778_MB; 29 static constexpr size_t MAX_HEAP_SIZE = 1024_MB; 30 31 class EcmaParamConfiguration { 32 public: 33 enum class HeapType : uint8_t { 34 DEFAULT_HEAP, 35 WORKER_HEAP, 36 SHARED_HEAP 37 }; 38 39 EcmaParamConfiguration() = default; 40 EcmaParamConfiguration(HeapType heapType, size_t poolSize, size_t heapSize = 1_MB) 41 { 42 switch (heapType) { 43 case HeapType::WORKER_HEAP: 44 if (heapSize > LOW_MEMORY && heapSize < DEFAULT_WORKER_HEAP_SIZE) { 45 maxHeapSize_ = heapSize; 46 } else { 47 maxHeapSize_ = DEFAULT_WORKER_HEAP_SIZE; 48 } 49 break; 50 case HeapType::SHARED_HEAP: 51 if (heapSize > LOW_MEMORY && heapSize < DEFAULT_SHARED_HEAP_SIZE) { 52 maxHeapSize_ = heapSize; 53 } else { 54 maxHeapSize_ = DEFAULT_SHARED_HEAP_SIZE; 55 }; 56 break; 57 default: 58 if (poolSize >= DEFAULT_HEAP_SIZE) { 59 maxHeapSize_ = DEFAULT_HEAP_SIZE; 60 } else { 61 maxHeapSize_ = poolSize; // pool is too small, no memory left for worker 62 } 63 if (heapSize > LOW_MEMORY && heapSize < DEFAULT_HEAP_SIZE) { 64 maxHeapSize_ = heapSize; 65 } 66 if (heapSize >= DEFAULT_HEAP_SIZE && heapSize <= MAX_HEAP_SIZE) { 67 maxHeapSize_ = heapSize; 68 } 69 } 70 Initialize(); 71 } 72 Initialize()73 void Initialize() 74 { 75 if (maxHeapSize_ < LOW_MEMORY) { 76 LOG_ECMA(FATAL) << "this branch is unreachable"; 77 UNREACHABLE(); 78 } 79 if (maxHeapSize_ < MEDIUM_MEMORY) { // 64_MB ~ 128_MB 80 minSemiSpaceSize_ = 2_MB; 81 maxSemiSpaceSize_ = 4_MB; 82 defaultReadOnlySpaceSize_ = 256_KB; 83 defaultNonMovableSpaceSize_ = 2_MB; 84 defaultSnapshotSpaceSize_ = 512_KB; 85 defaultMachineCodeSpaceSize_ = 2_MB; 86 defaultGlobalAllocLimit_ = 20_MB; 87 edenSpaceTriggerConcurrentMark_ = 1_MB; 88 semiSpaceTriggerConcurrentMark_ = 1_MB; 89 semiSpaceStepOvershootSize_ = 2_MB; 90 oldSpaceStepOvershootSize_ = 4_MB; 91 oldSpaceMaxOvershootSize_ = 8_MB; 92 outOfMemoryOvershootSize_ = 2_MB; 93 minAllocLimitGrowingStep_ = 2_MB; 94 minNativeLimitGrowingStep_ = 16_MB; 95 minGrowingStep_ = 4_MB; 96 maxStackSize_ = 128_KB; 97 maxJSSerializerSize_ = 8_MB; 98 sharedHeapLimitGrowingFactor_ = 2; // 2: growing factor 99 sharedHeapLimitGrowingStep_ = 20_MB; 100 incObjSizeThresholdInSensitive_ = 40_MB; 101 stepNativeSizeInc_ = 256_MB; 102 nativeSizeOvershoot_ = 80_MB; 103 maxNativeSizeInc_ = 768_MB; 104 fragmentationLimitForSharedFullGC_ = 5_MB; 105 } else if (maxHeapSize_ < HIGH_MEMORY) { // 128_MB ~ 256_MB 106 minSemiSpaceSize_ = 2_MB; 107 maxSemiSpaceSize_ = 8_MB; 108 defaultReadOnlySpaceSize_ = 256_KB; 109 defaultNonMovableSpaceSize_ = 6_MB; 110 defaultSnapshotSpaceSize_ = 512_KB; 111 defaultMachineCodeSpaceSize_ = 2_MB; 112 defaultGlobalAllocLimit_ = 20_MB; 113 edenSpaceTriggerConcurrentMark_ = 1.5_MB; 114 semiSpaceTriggerConcurrentMark_ = 1.5_MB; 115 semiSpaceStepOvershootSize_ = 2_MB; 116 oldSpaceStepOvershootSize_ = 8_MB; 117 oldSpaceMaxOvershootSize_ = 16_MB; 118 outOfMemoryOvershootSize_ = 2_MB; 119 minAllocLimitGrowingStep_ = 4_MB; 120 minNativeLimitGrowingStep_ = 32_MB; 121 minGrowingStep_ = 8_MB; 122 maxStackSize_ = 128_KB; 123 maxJSSerializerSize_ = 16_MB; 124 sharedHeapLimitGrowingFactor_ = 2; // 2: growing factor 125 sharedHeapLimitGrowingStep_ = 40_MB; 126 incObjSizeThresholdInSensitive_ = 40_MB; 127 stepNativeSizeInc_ = 256_MB; 128 nativeSizeOvershoot_ = 80_MB; 129 maxNativeSizeInc_ = 768_MB; 130 fragmentationLimitForSharedFullGC_ = 5_MB; 131 } else { // 256_MB ~ 384_MB 132 minSemiSpaceSize_ = 2_MB; 133 maxSemiSpaceSize_ = 16_MB; 134 defaultReadOnlySpaceSize_ = 256_KB; 135 defaultNonMovableSpaceSize_ = 64_MB; 136 defaultSnapshotSpaceSize_ = 4_MB; 137 defaultMachineCodeSpaceSize_ = 8_MB; 138 defaultGlobalAllocLimit_ = 20_MB; 139 edenSpaceTriggerConcurrentMark_ = 1.5_MB; 140 semiSpaceTriggerConcurrentMark_ = 1.5_MB; 141 semiSpaceStepOvershootSize_ = 2_MB; 142 oldSpaceStepOvershootSize_ = 8_MB; 143 oldSpaceMaxOvershootSize_ = 16_MB; 144 outOfMemoryOvershootSize_ = 2_MB; 145 minAllocLimitGrowingStep_ = 8_MB; 146 minNativeLimitGrowingStep_ = 64_MB; 147 minGrowingStep_ = 16_MB; 148 maxStackSize_ = 128_KB; 149 maxJSSerializerSize_ = 16_MB; 150 sharedHeapLimitGrowingFactor_ = 4; // 4: growing factor 151 sharedHeapLimitGrowingStep_ = 80_MB; 152 incObjSizeThresholdInSensitive_ = 80_MB; 153 stepNativeSizeInc_ = 300_MB; 154 nativeSizeOvershoot_ = 100_MB; 155 asyncClearNativePointerThreshold_ = 500_MB; 156 maxNativeSizeInc_ = 1_GB; 157 fragmentationLimitForSharedFullGC_ = 5_MB; 158 } 159 } 160 GetMaxHeapSize()161 size_t GetMaxHeapSize() const 162 { 163 return maxHeapSize_; 164 } 165 GetMinSemiSpaceSize()166 size_t GetMinSemiSpaceSize() const 167 { 168 return minSemiSpaceSize_; 169 } 170 GetMaxSemiSpaceSize()171 size_t GetMaxSemiSpaceSize() const 172 { 173 return maxSemiSpaceSize_; 174 } 175 GetDefaultReadOnlySpaceSize()176 size_t GetDefaultReadOnlySpaceSize() const 177 { 178 return defaultReadOnlySpaceSize_; 179 } 180 GetDefaultNonMovableSpaceSize()181 size_t GetDefaultNonMovableSpaceSize() const 182 { 183 return defaultNonMovableSpaceSize_; 184 } 185 GetDefaultSnapshotSpaceSize()186 size_t GetDefaultSnapshotSpaceSize() const 187 { 188 return defaultSnapshotSpaceSize_; 189 } 190 GetDefaultMachineCodeSpaceSize()191 size_t GetDefaultMachineCodeSpaceSize() const 192 { 193 return defaultMachineCodeSpaceSize_; 194 } 195 GetDefaultGlobalAllocLimit()196 size_t GetDefaultGlobalAllocLimit() const 197 { 198 return defaultGlobalAllocLimit_; 199 } 200 GetEdenSpaceTriggerConcurrentMark()201 size_t GetEdenSpaceTriggerConcurrentMark() const 202 { 203 return edenSpaceTriggerConcurrentMark_; 204 } 205 GetSemiSpaceTriggerConcurrentMark()206 size_t GetSemiSpaceTriggerConcurrentMark() const 207 { 208 return semiSpaceTriggerConcurrentMark_; 209 } 210 GetSemiSpaceStepOvershootSize()211 size_t GetSemiSpaceStepOvershootSize() const 212 { 213 return semiSpaceStepOvershootSize_; 214 } 215 GetOldSpaceStepOvershootSize()216 size_t GetOldSpaceStepOvershootSize() const 217 { 218 return oldSpaceStepOvershootSize_; 219 } 220 GetOldSpaceMaxOvershootSize()221 size_t GetOldSpaceMaxOvershootSize() const 222 { 223 return oldSpaceMaxOvershootSize_; 224 } 225 GetOutOfMemoryOvershootSize()226 size_t GetOutOfMemoryOvershootSize() const 227 { 228 return outOfMemoryOvershootSize_; 229 } 230 GetMinAllocLimitGrowingStep()231 size_t GetMinAllocLimitGrowingStep() const 232 { 233 return minAllocLimitGrowingStep_; 234 } 235 GetMinNativeLimitGrowingStep()236 size_t GetMinNativeLimitGrowingStep() const 237 { 238 return minNativeLimitGrowingStep_; 239 } 240 GetMinGrowingStep()241 size_t GetMinGrowingStep() const 242 { 243 return minGrowingStep_; 244 } 245 GetSharedHeapLimitGrowingFactor()246 size_t GetSharedHeapLimitGrowingFactor() const 247 { 248 return sharedHeapLimitGrowingFactor_; 249 } 250 GetSharedHeapLimitGrowingStep()251 size_t GetSharedHeapLimitGrowingStep() const 252 { 253 return sharedHeapLimitGrowingStep_; 254 } 255 GetIncObjSizeThresholdInSensitive()256 size_t GetIncObjSizeThresholdInSensitive() const 257 { 258 return incObjSizeThresholdInSensitive_; 259 } 260 GetStepNativeSizeInc()261 size_t GetStepNativeSizeInc() const 262 { 263 return stepNativeSizeInc_; 264 } 265 GetNativeSizeOvershoot()266 size_t GetNativeSizeOvershoot() const 267 { 268 return nativeSizeOvershoot_; 269 } 270 GetAsyncClearNativePointerThreshold()271 size_t GetAsyncClearNativePointerThreshold() const 272 { 273 return asyncClearNativePointerThreshold_; 274 } 275 GetMaxNativeSizeInc()276 size_t GetMaxNativeSizeInc() const 277 { 278 return maxNativeSizeInc_; 279 } 280 GetFragmentationLimitForSharedFullGC()281 size_t GetFragmentationLimitForSharedFullGC() const 282 { 283 return fragmentationLimitForSharedFullGC_; 284 } 285 GetMaxStackSize()286 uint32_t GetMaxStackSize() const 287 { 288 return maxStackSize_; 289 } 290 GetDefalutStackSize()291 static size_t GetDefalutStackSize() 292 { 293 return DEFAULT_STACK_SIZE; 294 } 295 GetDefaultReservedStackSize()296 static size_t GetDefaultReservedStackSize() 297 { 298 return DEFAULT_RESERVED_STACK_SIZE; 299 } 300 GetAllowedUpperStackDiff()301 static size_t GetAllowedUpperStackDiff() 302 { 303 return ALLOWED_UPPER_STACK_DIFF; 304 } 305 GetMaxJSSerializerSize()306 size_t GetMaxJSSerializerSize() const 307 { 308 return maxJSSerializerSize_; 309 } 310 311 private: 312 static constexpr size_t LOW_MEMORY = 64_MB; 313 static constexpr size_t MEDIUM_MEMORY = 128_MB; 314 static constexpr size_t HIGH_MEMORY = 256_MB; 315 static constexpr size_t DEFAULT_STACK_SIZE = 992_KB; 316 static constexpr size_t ALLOWED_UPPER_STACK_DIFF = 4_KB; 317 static constexpr size_t DEFAULT_RESERVED_STACK_SIZE = 16_KB; 318 319 size_t maxHeapSize_ {0}; 320 size_t minSemiSpaceSize_ {0}; 321 size_t maxSemiSpaceSize_ {0}; 322 size_t defaultReadOnlySpaceSize_ {0}; 323 size_t defaultNonMovableSpaceSize_ {0}; 324 size_t defaultSnapshotSpaceSize_ {0}; 325 size_t defaultMachineCodeSpaceSize_ {0}; 326 size_t defaultGlobalAllocLimit_ {0}; 327 size_t edenSpaceTriggerConcurrentMark_ {0}; 328 size_t semiSpaceTriggerConcurrentMark_ {0}; 329 size_t semiSpaceStepOvershootSize_ {0}; 330 size_t oldSpaceStepOvershootSize_ {0}; 331 size_t oldSpaceMaxOvershootSize_ {0}; 332 size_t outOfMemoryOvershootSize_ {0}; 333 size_t minAllocLimitGrowingStep_ {0}; 334 size_t minNativeLimitGrowingStep_ {0}; 335 size_t minGrowingStep_ {0}; 336 size_t sharedHeapLimitGrowingFactor_ {0}; 337 size_t sharedHeapLimitGrowingStep_ {0}; 338 size_t incObjSizeThresholdInSensitive_ {0}; 339 size_t stepNativeSizeInc_ {0}; 340 size_t nativeSizeOvershoot_ {0}; 341 size_t asyncClearNativePointerThreshold_ {0}; 342 size_t maxNativeSizeInc_ {0}; 343 size_t maxJSSerializerSize_ {0}; 344 size_t fragmentationLimitForSharedFullGC_ {0}; 345 uint32_t maxStackSize_ {0}; 346 }; 347 } // namespace panda::ecmascript 348 349 #endif // ECMASCRIPT_ECMA_PARAM_CONFIGURATION_H 350