From 649b8884c8954656b24f3003256acf05bd2fddca Mon Sep 17 00:00:00 2001 From: Rtangyu Date: Mon, 24 Jul 2023 19:59:31 +0800 Subject: [PATCH 1/2] add SuspendVM and ResumeVM issues:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I79K2R Signed-off-by: Rtangyu --- frameworks/native/runtime/js_runtime.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index a681c532d5..d302bfb873 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -934,7 +934,13 @@ bool JsRuntime::BuildJsStackInfoList(uint32_t tid, std::vector& jsFram auto nativeEngine = GetNativeEnginePointer(); CHECK_POINTER_AND_RETURN(nativeEngine, false); std::vector jsFrameInfo; + auto arkNativeEngine = static_cast(nativeEngine->GetWorkerVm(nativeEngine, tid)); + if (arkNativeEngine == nullptr){ + return false; + } + arkNativeEngine->SuspendVM(); bool ret = nativeEngine->BuildJsStackInfoList(tid, jsFrameInfo); + arkNativeEngine->ResumeVM(); if (!ret) { return ret; } From 3f3be1f8f900ffd936554eba2c06d7c01148e785 Mon Sep 17 00:00:00 2001 From: Rtangyu Date: Tue, 25 Jul 2023 20:31:51 +0800 Subject: [PATCH 2/2] Redundant code optimization issues:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I7NXZJ Signed-off-by: Rtangyu --- .../native/appkit/app/mix_stack_dumper.cpp | 11 +++++-- frameworks/native/runtime/js_runtime.cpp | 30 +++++++++++++++---- .../inner_api/runtime/include/js_runtime.h | 2 ++ .../inner_api/runtime/include/runtime.h | 2 ++ .../mock_runtime.h | 5 ++++ .../mock_native_engine.h | 4 +-- .../runtime_test/mock_js_native_engine.h | 4 +-- 7 files changed, 46 insertions(+), 12 deletions(-) diff --git a/frameworks/native/appkit/app/mix_stack_dumper.cpp b/frameworks/native/appkit/app/mix_stack_dumper.cpp index 353dd4f7fa..7a59ddd152 100644 --- a/frameworks/native/appkit/app/mix_stack_dumper.cpp +++ b/frameworks/native/appkit/app/mix_stack_dumper.cpp @@ -207,18 +207,25 @@ bool MixStackDumper::DumpMixFrame(int fd, pid_t nstid, pid_t tid) std::vector nativeFrames; bool hasNativeFrame = true; + bool isVmSuspended = false; + auto application = application_.lock(); + if (application != nullptr && application->GetRuntime() != nullptr) { + isVmSuspended = application->GetRuntime()->SuspendVM(nstid); + } if (!catcher_->CatchFrame(nstid, nativeFrames)) { hasNativeFrame = false; } bool hasJsFrame = true; std::vector jsFrames; - auto application = application_.lock(); // if we failed to get native frame, target thread may not be seized - if (application != nullptr && application->GetRuntime() != nullptr && hasNativeFrame) { + if (isVmSuspended) { hasJsFrame = application->GetRuntime()->BuildJsStackInfoList(nstid, jsFrames); } catcher_->ReleaseThread(nstid); + if (isVmSuspended) { + application->GetRuntime()->ResumeVM(nstid); + } if (jsFrames.size() == 0) { hasJsFrame = false; diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index d302bfb873..f65d5ddaf6 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -934,13 +934,7 @@ bool JsRuntime::BuildJsStackInfoList(uint32_t tid, std::vector& jsFram auto nativeEngine = GetNativeEnginePointer(); CHECK_POINTER_AND_RETURN(nativeEngine, false); std::vector jsFrameInfo; - auto arkNativeEngine = static_cast(nativeEngine->GetWorkerVm(nativeEngine, tid)); - if (arkNativeEngine == nullptr){ - return false; - } - arkNativeEngine->SuspendVM(); bool ret = nativeEngine->BuildJsStackInfoList(tid, jsFrameInfo); - arkNativeEngine->ResumeVM(); if (!ret) { return ret; } @@ -963,6 +957,30 @@ void JsRuntime::NotifyApplicationState(bool isBackground) HILOG_INFO("NotifyApplicationState, isBackground %{public}d.", isBackground); } +bool JsRuntime::SuspendVM(uint32_t tid) +{ + auto nativeEngine = GetNativeEnginePointer(); + CHECK_POINTER_AND_RETURN(nativeEngine, false); + auto arkNativeEngine = nativeEngine->GetWorkerEngine(tid); + if (arkNativeEngine == nullptr) { + HILOG_ERROR("SuspendVM arkNativeEngine is nullptr"); + return false; + } + return arkNativeEngine->SuspendVM(); +} + +void JsRuntime::ResumeVM(uint32_t tid) +{ + auto nativeEngine = GetNativeEnginePointer(); + CHECK_POINTER(nativeEngine); + auto arkNativeEngine = nativeEngine->GetWorkerEngine(tid); + if (arkNativeEngine == nullptr) { + HILOG_ERROR("ResumeVM arkNativeEngine is nullptr"); + return; + } + arkNativeEngine->ResumeVM(); +} + void JsRuntime::PreloadSystemModule(const std::string& moduleName) { HandleScope handleScope(*this); diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index 0a0a6f5bd7..1558c309d6 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -85,6 +85,8 @@ public: void DumpHeapSnapshot(bool isPrivate) override; bool BuildJsStackInfoList(uint32_t tid, std::vector& jsFrames) override; void NotifyApplicationState(bool isBackground) override; + bool SuspendVM(uint32_t tid) override; + void ResumeVM(uint32_t tid) override; bool RunSandboxScript(const std::string& path, const std::string& hapPath); bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false); diff --git a/interfaces/inner_api/runtime/include/runtime.h b/interfaces/inner_api/runtime/include/runtime.h index 1a758ee246..b19d89bfde 100644 --- a/interfaces/inner_api/runtime/include/runtime.h +++ b/interfaces/inner_api/runtime/include/runtime.h @@ -78,6 +78,8 @@ public: virtual bool BuildJsStackInfoList(uint32_t tid, std::vector& jsFrames) = 0; virtual void DumpHeapSnapshot(bool isPrivate) = 0; virtual void NotifyApplicationState(bool isBackground) = 0; + virtual bool SuspendVM(uint32_t tid) = 0; + virtual void ResumeVM(uint32_t tid) = 0; virtual void PreloadSystemModule(const std::string& moduleName) = 0; virtual void FinishPreload() = 0; virtual bool LoadRepairPatch(const std::string& patchFile, const std::string& baseFile) = 0; diff --git a/test/mock/frameworks_kits_runtime_test/mock_runtime.h b/test/mock/frameworks_kits_runtime_test/mock_runtime.h index bafd1d77cb..d6eceaf6ac 100644 --- a/test/mock/frameworks_kits_runtime_test/mock_runtime.h +++ b/test/mock/frameworks_kits_runtime_test/mock_runtime.h @@ -47,6 +47,11 @@ public: { return true; } + bool SuspendVM(uint32_t tid) override + { + return true; + } + void ResumeVM(uint32_t tid) override {} bool UnLoadRepairPatch(const std::string& patchFile) override { return true; diff --git a/test/unittest/ability_runtime_error_util_test/mock_native_engine.h b/test/unittest/ability_runtime_error_util_test/mock_native_engine.h index ccfa90acec..6c84f2cbde 100644 --- a/test/unittest/ability_runtime_error_util_test/mock_native_engine.h +++ b/test/unittest/ability_runtime_error_util_test/mock_native_engine.h @@ -310,12 +310,12 @@ public: return true; } - bool DeleteWorker(NativeEngine* hostEngine, NativeEngine* workerEngine) override + bool DeleteWorker(NativeEngine* workerEngine) override { return true; } - NativeEngine* GetWorkerVm(NativeEngine* hostEngine, uint32_t tid) override + NativeEngine* GetWorkerEngine(uint32_t tid) override { return nullptr; } diff --git a/test/unittest/runtime_test/mock_js_native_engine.h b/test/unittest/runtime_test/mock_js_native_engine.h index 680b17d9dd..14bafa8f67 100644 --- a/test/unittest/runtime_test/mock_js_native_engine.h +++ b/test/unittest/runtime_test/mock_js_native_engine.h @@ -309,12 +309,12 @@ public: return true; } - bool DeleteWorker(NativeEngine* hostEngine, NativeEngine* workerEngine) override + bool DeleteWorker(NativeEngine* workerEngine) override { return true; } - NativeEngine* GetWorkerVm(NativeEngine* hostEngine, uint32_t tid) override + NativeEngine* GetWorkerEngine(uint32_t tid) override { return nullptr; }