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