diff --git a/ecmascript/ecma_param_configuration.h b/ecmascript/ecma_param_configuration.h index 66ff377a2b..a02b438d46 100644 --- a/ecmascript/ecma_param_configuration.h +++ b/ecmascript/ecma_param_configuration.h @@ -26,6 +26,7 @@ namespace panda::ecmascript { static constexpr size_t DEFAULT_HEAP_SIZE = 448_MB; // Recommended range: 128-448MB static constexpr size_t DEFAULT_WORKER_HEAP_SIZE = 768_MB; // Recommended range: 128_MB, LargeHeap: 768_MB static constexpr size_t DEFAULT_SHARED_HEAP_SIZE = 778_MB; +static constexpr size_t MAX_HEAP_SIZE = 1024_MB; class EcmaParamConfiguration { public: @@ -35,7 +36,7 @@ public: SHARED_HEAP }; - EcmaParamConfiguration(HeapType heapType, size_t poolSize) + EcmaParamConfiguration(HeapType heapType, size_t poolSize, size_t heapSize = 1_MB) { switch (heapType) { case HeapType::WORKER_HEAP: @@ -50,6 +51,9 @@ public: } else { maxHeapSize_ = poolSize; // pool is too small, no memory left for worker } + if (heapSize >= DEFAULT_HEAP_SIZE && heapSize <= MAX_HEAP_SIZE) { + maxHeapSize_ = heapSize; + } } Initialize(); } diff --git a/ecmascript/ecma_vm.cpp b/ecmascript/ecma_vm.cpp index a69c382fbc..37e9b6bd92 100644 --- a/ecmascript/ecma_vm.cpp +++ b/ecmascript/ecma_vm.cpp @@ -105,8 +105,10 @@ EcmaVM *EcmaVM::Create(const JSRuntimeOptions &options) Runtime::CreateIfFirstVm(options); auto heapType = options.IsWorker() ? EcmaParamConfiguration::HeapType::WORKER_HEAP : EcmaParamConfiguration::HeapType::DEFAULT_HEAP; + size_t heapSize = options.GetHeapSize(); auto config = EcmaParamConfiguration(heapType, - MemMapAllocator::GetInstance()->GetCapacity()); + MemMapAllocator::GetInstance()->GetCapacity(), + heapSize); JSRuntimeOptions newOptions = options; // only define SUPPORT_ENABLE_ASM_INTERP can enable asm-interpreter #if !defined(SUPPORT_ENABLE_ASM_INTERP) diff --git a/ecmascript/js_runtime_options.h b/ecmascript/js_runtime_options.h index 1abaaf5841..2aa23f8834 100644 --- a/ecmascript/js_runtime_options.h +++ b/ecmascript/js_runtime_options.h @@ -386,6 +386,29 @@ public: } } + void SetMemConfigProperty(std::string configProperty) + { + if (configProperty != "") { + std::string key; + std::string value; + for (char c : configProperty) { + if (isdigit(c)) { + value += c; + } else { + key += c; + } + } + if (key == "jsHeap") { + heapSize_ = stoi(value) * 1_MB; + } + } + } + + size_t GetHeapSize() const + { + return heapSize_; + } + int GetDefaultProperties() { return ArkProperties::PARALLEL_GC | ArkProperties::CONCURRENT_MARK | ArkProperties::CONCURRENT_SWEEP | @@ -1665,6 +1688,7 @@ private: uint32_t forceSharedGc_ {1}; int arkProperties_ = GetDefaultProperties(); std::string arkBundleName_ = {""}; + size_t heapSize_ = {0}; uint32_t gcThreadNum_ {7}; // 7: default thread num uint32_t longPauseTime_ {40}; // 40: default pause time std::string aotOutputFile_ {""}; diff --git a/ecmascript/napi/include/jsnapi.h b/ecmascript/napi/include/jsnapi.h index 4bd747e3da..10ab800c95 100644 --- a/ecmascript/napi/include/jsnapi.h +++ b/ecmascript/napi/include/jsnapi.h @@ -195,6 +195,11 @@ public: arkBundleName_ = bundleName; } + void SetMemConfigProperty(std::string configProperty) + { + memConfigProperty_ = configProperty; + } + void SetGcThreadNum(size_t num) { gcThreadNum_ = num; @@ -321,6 +326,11 @@ private: return arkBundleName_; } + std::string GetMemConfigProperty() const + { + return memConfigProperty_; + } + size_t GetGcThreadNum() const { return gcThreadNum_; @@ -385,6 +395,7 @@ private: bool enableCpuprofiler_ {false}; int arkProperties_ {-1}; std::string arkBundleName_ = {""}; + std::string memConfigProperty_ = {""}; size_t gcThreadNum_ {DEFAULT_GC_THREAD_NUM}; size_t longPauseTime_ {DEFAULT_LONG_PAUSE_TIME}; bool enableAsmInterpreter_ {true}; diff --git a/ecmascript/napi/jsnapi_expo.cpp b/ecmascript/napi/jsnapi_expo.cpp index ebe6beb918..86b46b80c3 100644 --- a/ecmascript/napi/jsnapi_expo.cpp +++ b/ecmascript/napi/jsnapi_expo.cpp @@ -3091,6 +3091,7 @@ EcmaVM *JSNApi::CreateJSVM(const RuntimeOption &option) { JSRuntimeOptions runtimeOptions; runtimeOptions.SetArkProperties(option.GetArkProperties()); + runtimeOptions.SetMemConfigProperty(option.GetMemConfigProperty()); runtimeOptions.SetArkBundleName(option.GetArkBundleName()); runtimeOptions.SetLongPauseTime(option.GetLongPauseTime()); runtimeOptions.SetGcThreadNum(option.GetGcThreadNum()); diff --git a/ecmascript/tests/ecma_vm_test.cpp b/ecmascript/tests/ecma_vm_test.cpp index dada7f4f38..7bb5a2e36e 100644 --- a/ecmascript/tests/ecma_vm_test.cpp +++ b/ecmascript/tests/ecma_vm_test.cpp @@ -58,6 +58,7 @@ HWTEST_F_L0(EcmaVMTest, CreateEcmaVMInTwoWays) options2.SetEnableForceGC(false); options2.SetForceFullGC(false); options2.SetArkProperties(ArkProperties::GC_STATS_PRINT); + options2.SetMemConfigProperty("jsHeap500"); // A non-production gc strategy. Prohibit stw-gc 10 times. EcmaVM *ecmaVm2 = JSNApi::CreateEcmaVM(options2); @@ -72,6 +73,7 @@ HWTEST_F_L0(EcmaVMTest, CreateEcmaVMInTwoWays) EXPECT_TRUE(options1Out.EnableForceGC() != options2Out.EnableForceGC()); EXPECT_TRUE(options1Out.ForceFullGC() != options2Out.ForceFullGC()); EXPECT_TRUE(options1Out.GetArkProperties() != options2Out.GetArkProperties()); + EXPECT_TRUE(options2Out.GetHeapSize() == 500_MB); JSNApi::DestroyJSVM(ecmaVm2); });