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_ = 4_MB; 84 defaultSnapshotSpaceSize_ = 512_KB; 85 defaultMachineCodeSpaceSize_ = 2_MB; 86 defaultGlobalAllocLimit_ = 20_MB; 87 semiSpaceTriggerConcurrentMark_ = 1_MB; 88 semiSpaceStepOvershootSize_ = 2_MB; 89 oldSpaceStepOvershootSize_ = 4_MB; 90 oldSpaceMaxOvershootSize_ = 8_MB; 91 outOfMemoryOvershootSize_ = 2_MB; 92 minAllocLimitGrowingStep_ = 2_MB; 93 minNativeLimitGrowingStep_ = 16_MB; 94 minGrowingStep_ = 4_MB; 95 maxStackSize_ = 64_KB; 96 maxJSSerializerSize_ = 8_MB; 97 sharedHeapLimitGrowingFactor_ = 2; // 2: growing factor 98 sharedHeapLimitGrowingStep_ = 20_MB; 99 incObjSizeThresholdInSensitive_ = 40_MB; 100 stepNativeSizeInc_ = 256_MB; 101 nativeSizeOvershoot_ = 80_MB; 102 maxNativeSizeInc_ = 768_MB; 103 fragmentationLimitForSharedFullGC_ = 5_MB; 104 } else if (maxHeapSize_ < HIGH_MEMORY) { // 128_MB ~ 256_MB 105 minSemiSpaceSize_ = 2_MB; 106 maxSemiSpaceSize_ = 8_MB; 107 defaultReadOnlySpaceSize_ = 256_KB; 108 defaultNonMovableSpaceSize_ = 6_MB; 109 defaultSnapshotSpaceSize_ = 512_KB; 110 defaultMachineCodeSpaceSize_ = 2_MB; 111 defaultGlobalAllocLimit_ = 20_MB; 112 semiSpaceTriggerConcurrentMark_ = 1.5_MB; 113 semiSpaceStepOvershootSize_ = 2_MB; 114 oldSpaceStepOvershootSize_ = 8_MB; 115 oldSpaceMaxOvershootSize_ = 16_MB; 116 outOfMemoryOvershootSize_ = 2_MB; 117 minAllocLimitGrowingStep_ = 4_MB; 118 minNativeLimitGrowingStep_ = 32_MB; 119 minGrowingStep_ = 8_MB; 120 maxStackSize_ = 128_KB; 121 maxJSSerializerSize_ = 16_MB; 122 sharedHeapLimitGrowingFactor_ = 2; // 2: growing factor 123 sharedHeapLimitGrowingStep_ = 40_MB; 124 incObjSizeThresholdInSensitive_ = 40_MB; 125 stepNativeSizeInc_ = 256_MB; 126 nativeSizeOvershoot_ = 80_MB; 127 maxNativeSizeInc_ = 768_MB; 128 fragmentationLimitForSharedFullGC_ = 5_MB; 129 } else { // 256_MB ~ 384_MB 130 minSemiSpaceSize_ = 2_MB; 131 maxSemiSpaceSize_ = 16_MB; 132 defaultReadOnlySpaceSize_ = 256_KB; 133 defaultNonMovableSpaceSize_ = 64_MB; 134 defaultSnapshotSpaceSize_ = 4_MB; 135 defaultMachineCodeSpaceSize_ = 8_MB; 136 defaultGlobalAllocLimit_ = 20_MB; 137 semiSpaceTriggerConcurrentMark_ = 1.5_MB; 138 semiSpaceStepOvershootSize_ = 2_MB; 139 oldSpaceStepOvershootSize_ = 8_MB; 140 oldSpaceMaxOvershootSize_ = 16_MB; 141 outOfMemoryOvershootSize_ = 2_MB; 142 minAllocLimitGrowingStep_ = 8_MB; 143 minNativeLimitGrowingStep_ = 64_MB; 144 minGrowingStep_ = 16_MB; 145 maxStackSize_ = 128_KB; 146 maxJSSerializerSize_ = 16_MB; 147 sharedHeapLimitGrowingFactor_ = 4; // 4: growing factor 148 sharedHeapLimitGrowingStep_ = 80_MB; 149 incObjSizeThresholdInSensitive_ = 80_MB; 150 stepNativeSizeInc_ = 300_MB; 151 nativeSizeOvershoot_ = 100_MB; 152 asyncClearNativePointerThreshold_ = 500_MB; 153 maxNativeSizeInc_ = 1_GB; 154 fragmentationLimitForSharedFullGC_ = 5_MB; 155 } 156 } 157 GetMaxHeapSize()158 size_t GetMaxHeapSize() const 159 { 160 return maxHeapSize_; 161 } 162 GetMinSemiSpaceSize()163 size_t GetMinSemiSpaceSize() const 164 { 165 return minSemiSpaceSize_; 166 } 167 GetMaxSemiSpaceSize()168 size_t GetMaxSemiSpaceSize() const 169 { 170 return maxSemiSpaceSize_; 171 } 172 GetDefaultReadOnlySpaceSize()173 size_t GetDefaultReadOnlySpaceSize() const 174 { 175 return defaultReadOnlySpaceSize_; 176 } 177 GetDefaultNonMovableSpaceSize()178 size_t GetDefaultNonMovableSpaceSize() const 179 { 180 return defaultNonMovableSpaceSize_; 181 } 182 GetDefaultSnapshotSpaceSize()183 size_t GetDefaultSnapshotSpaceSize() const 184 { 185 return defaultSnapshotSpaceSize_; 186 } 187 GetDefaultMachineCodeSpaceSize()188 size_t GetDefaultMachineCodeSpaceSize() const 189 { 190 return defaultMachineCodeSpaceSize_; 191 } 192 GetDefaultGlobalAllocLimit()193 size_t GetDefaultGlobalAllocLimit() const 194 { 195 return defaultGlobalAllocLimit_; 196 } 197 GetSemiSpaceTriggerConcurrentMark()198 size_t GetSemiSpaceTriggerConcurrentMark() const 199 { 200 return semiSpaceTriggerConcurrentMark_; 201 } 202 GetSemiSpaceStepOvershootSize()203 size_t GetSemiSpaceStepOvershootSize() const 204 { 205 return semiSpaceStepOvershootSize_; 206 } 207 GetOldSpaceStepOvershootSize()208 size_t GetOldSpaceStepOvershootSize() const 209 { 210 return oldSpaceStepOvershootSize_; 211 } 212 GetOldSpaceMaxOvershootSize()213 size_t GetOldSpaceMaxOvershootSize() const 214 { 215 return oldSpaceMaxOvershootSize_; 216 } 217 GetOutOfMemoryOvershootSize()218 size_t GetOutOfMemoryOvershootSize() const 219 { 220 return outOfMemoryOvershootSize_; 221 } 222 GetMinAllocLimitGrowingStep()223 size_t GetMinAllocLimitGrowingStep() const 224 { 225 return minAllocLimitGrowingStep_; 226 } 227 GetMinNativeLimitGrowingStep()228 size_t GetMinNativeLimitGrowingStep() const 229 { 230 return minNativeLimitGrowingStep_; 231 } 232 GetMinGrowingStep()233 size_t GetMinGrowingStep() const 234 { 235 return minGrowingStep_; 236 } 237 GetSharedHeapLimitGrowingFactor()238 size_t GetSharedHeapLimitGrowingFactor() const 239 { 240 return sharedHeapLimitGrowingFactor_; 241 } 242 GetSharedHeapLimitGrowingStep()243 size_t GetSharedHeapLimitGrowingStep() const 244 { 245 return sharedHeapLimitGrowingStep_; 246 } 247 GetIncObjSizeThresholdInSensitive()248 size_t GetIncObjSizeThresholdInSensitive() const 249 { 250 return incObjSizeThresholdInSensitive_; 251 } 252 GetStepNativeSizeInc()253 size_t GetStepNativeSizeInc() const 254 { 255 return stepNativeSizeInc_; 256 } 257 GetNativeSizeOvershoot()258 size_t GetNativeSizeOvershoot() const 259 { 260 return nativeSizeOvershoot_; 261 } 262 GetAsyncClearNativePointerThreshold()263 size_t GetAsyncClearNativePointerThreshold() const 264 { 265 return asyncClearNativePointerThreshold_; 266 } 267 GetMaxNativeSizeInc()268 size_t GetMaxNativeSizeInc() const 269 { 270 return maxNativeSizeInc_; 271 } 272 GetFragmentationLimitForSharedFullGC()273 size_t GetFragmentationLimitForSharedFullGC() const 274 { 275 return fragmentationLimitForSharedFullGC_; 276 } 277 GetMaxStackSize()278 uint32_t GetMaxStackSize() const 279 { 280 return maxStackSize_; 281 } 282 GetDefalutStackSize()283 static size_t GetDefalutStackSize() 284 { 285 return DEFAULT_STACK_SIZE; 286 } 287 GetDefaultReservedStackSize()288 static size_t GetDefaultReservedStackSize() 289 { 290 return DEFAULT_RESERVED_STACK_SIZE; 291 } 292 GetAllowedUpperStackDiff()293 static size_t GetAllowedUpperStackDiff() 294 { 295 return ALLOWED_UPPER_STACK_DIFF; 296 } 297 GetMaxJSSerializerSize()298 size_t GetMaxJSSerializerSize() const 299 { 300 return maxJSSerializerSize_; 301 } 302 303 private: 304 static constexpr size_t LOW_MEMORY = 64_MB; 305 static constexpr size_t MEDIUM_MEMORY = 128_MB; 306 static constexpr size_t HIGH_MEMORY = 256_MB; 307 static constexpr size_t DEFAULT_STACK_SIZE = 992_KB; 308 static constexpr size_t ALLOWED_UPPER_STACK_DIFF = 4_KB; 309 static constexpr size_t DEFAULT_RESERVED_STACK_SIZE = 16_KB; 310 311 size_t maxHeapSize_ {0}; 312 size_t minSemiSpaceSize_ {0}; 313 size_t maxSemiSpaceSize_ {0}; 314 size_t defaultReadOnlySpaceSize_ {0}; 315 size_t defaultNonMovableSpaceSize_ {0}; 316 size_t defaultSnapshotSpaceSize_ {0}; 317 size_t defaultMachineCodeSpaceSize_ {0}; 318 size_t defaultGlobalAllocLimit_ {0}; 319 size_t semiSpaceTriggerConcurrentMark_ {0}; 320 size_t semiSpaceStepOvershootSize_ {0}; 321 size_t oldSpaceStepOvershootSize_ {0}; 322 size_t oldSpaceMaxOvershootSize_ {0}; 323 size_t outOfMemoryOvershootSize_ {0}; 324 size_t minAllocLimitGrowingStep_ {0}; 325 size_t minNativeLimitGrowingStep_ {0}; 326 size_t minGrowingStep_ {0}; 327 size_t sharedHeapLimitGrowingFactor_ {0}; 328 size_t sharedHeapLimitGrowingStep_ {0}; 329 size_t incObjSizeThresholdInSensitive_ {0}; 330 size_t stepNativeSizeInc_ {0}; 331 size_t nativeSizeOvershoot_ {0}; 332 size_t asyncClearNativePointerThreshold_ {0}; 333 size_t maxNativeSizeInc_ {0}; 334 size_t maxJSSerializerSize_ {0}; 335 size_t fragmentationLimitForSharedFullGC_ {0}; 336 uint32_t maxStackSize_ {0}; 337 }; 338 } // namespace panda::ecmascript 339 340 #endif // ECMASCRIPT_ECMA_PARAM_CONFIGURATION_H 341