• 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 = 384_MB;                 // Recommended range: 128-384MB
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             semiSpaceOvershootSize_ = 2_MB;
60             outOfMemoryOvershootSize_ = 2_MB;
61             minAllocLimitGrowingStep_ = 2_MB;
62             minGrowingStep_ = 4_MB;
63             maxStackSize_ = 128_KB;
64             maxJSSerializerSize_ = 16_MB;
65         } else if (maxHeapSize_ < HIGH_MEMORY) { // 128_MB ~ 256_MB
66             minSemiSpaceSize_ = 2_MB;
67             maxSemiSpaceSize_ = 8_MB;
68             defaultReadOnlySpaceSize_ = 256_KB;
69             defaultNonMovableSpaceSize_ = 6_MB;
70             defaultSnapshotSpaceSize_ = 512_KB;
71             defaultMachineCodeSpaceSize_ = 2_MB;
72             semiSpaceTriggerConcurrentMark_ = 1.5_MB;
73             semiSpaceOvershootSize_ = 2_MB;
74             outOfMemoryOvershootSize_ = 2_MB;
75             minAllocLimitGrowingStep_ = 4_MB;
76             minGrowingStep_ = 8_MB;
77             maxStackSize_ = 128_KB;
78             maxJSSerializerSize_ = 32_MB;
79         }  else { // 256_MB ~ 384_MB
80             minSemiSpaceSize_ = 2_MB;
81             maxSemiSpaceSize_ = 16_MB;
82             defaultReadOnlySpaceSize_ = 256_KB;
83             defaultNonMovableSpaceSize_ = 16_MB;
84             defaultSnapshotSpaceSize_ = 4_MB;
85             defaultMachineCodeSpaceSize_ = 8_MB;
86             semiSpaceTriggerConcurrentMark_ = 1.5_MB;
87             semiSpaceOvershootSize_ = 2_MB;
88             outOfMemoryOvershootSize_ = 2_MB;
89             minAllocLimitGrowingStep_ = 8_MB;
90             minGrowingStep_ = 16_MB;
91             maxStackSize_ = 128_KB;
92             maxJSSerializerSize_ = 32_MB;
93         }
94     }
95 
GetMaxHeapSize()96     size_t GetMaxHeapSize() const
97     {
98         return maxHeapSize_;
99     }
100 
GetMinSemiSpaceSize()101     size_t GetMinSemiSpaceSize() const
102     {
103         return minSemiSpaceSize_;
104     }
105 
GetMaxSemiSpaceSize()106     size_t GetMaxSemiSpaceSize() const
107     {
108         return maxSemiSpaceSize_;
109     }
110 
GetDefaultReadOnlySpaceSize()111     size_t GetDefaultReadOnlySpaceSize() const
112     {
113         return defaultReadOnlySpaceSize_;
114     }
115 
GetDefaultNonMovableSpaceSize()116     size_t GetDefaultNonMovableSpaceSize() const
117     {
118         return defaultNonMovableSpaceSize_;
119     }
120 
GetDefaultSnapshotSpaceSize()121     size_t GetDefaultSnapshotSpaceSize() const
122     {
123         return defaultSnapshotSpaceSize_;
124     }
125 
GetDefaultMachineCodeSpaceSize()126     size_t GetDefaultMachineCodeSpaceSize() const
127     {
128         return defaultMachineCodeSpaceSize_;
129     }
130 
GetSemiSpaceTriggerConcurrentMark()131     size_t GetSemiSpaceTriggerConcurrentMark() const
132     {
133         return semiSpaceTriggerConcurrentMark_;
134     }
135 
GetSemiSpaceOvershootSize()136     size_t GetSemiSpaceOvershootSize() const
137     {
138         return semiSpaceOvershootSize_;
139     }
140 
GetOutOfMemoryOvershootSize()141     size_t GetOutOfMemoryOvershootSize() const
142     {
143         return outOfMemoryOvershootSize_;
144     }
145 
GetMinAllocLimitGrowingStep()146     size_t GetMinAllocLimitGrowingStep() const
147     {
148         return minAllocLimitGrowingStep_;
149     }
150 
GetMinGrowingStep()151     size_t GetMinGrowingStep() const
152     {
153         return minGrowingStep_;
154     }
155 
GetMaxStackSize()156     uint32_t GetMaxStackSize() const
157     {
158         return maxStackSize_;
159     }
160 
GetDefalutStackSize()161     static size_t GetDefalutStackSize()
162     {
163         return DEFAULT_STACK_SIZE;
164     }
165 
GetDefaultReservedStackSize()166     static size_t GetDefaultReservedStackSize()
167     {
168         return DEFAULT_RESERVED_STACK_SIZE;
169     }
170 
GetMaxJSSerializerSize()171     size_t GetMaxJSSerializerSize() const
172     {
173         return maxJSSerializerSize_;
174     }
175 
176 private:
177     static constexpr size_t LOW_MEMORY = 64_MB;
178     static constexpr size_t MEDIUM_MEMORY = 128_MB;
179     static constexpr size_t HIGH_MEMORY = 256_MB;
180     static constexpr size_t DEFAULT_STACK_SIZE = 992_KB;
181     static constexpr size_t DEFAULT_RESERVED_STACK_SIZE = 16_KB;
182 
183     size_t maxHeapSize_ {0};
184     size_t minSemiSpaceSize_ {0};
185     size_t maxSemiSpaceSize_ {0};
186     size_t defaultReadOnlySpaceSize_ {0};
187     size_t defaultNonMovableSpaceSize_ {0};
188     size_t defaultSnapshotSpaceSize_ {0};
189     size_t defaultMachineCodeSpaceSize_ {0};
190     size_t semiSpaceTriggerConcurrentMark_ {0};
191     size_t semiSpaceOvershootSize_ {0};
192     size_t outOfMemoryOvershootSize_ {0};
193     size_t minAllocLimitGrowingStep_ {0};
194     size_t minGrowingStep_ {0};
195     size_t maxJSSerializerSize_ {0};
196     uint32_t maxStackSize_ {0};
197 };
198 } // namespace panda::ecmascript
199 
200 #endif // ECMASCRIPT_ECMA_PARAM_CONFIGURATION_H
201