From 76f97fc013b25236c632eafd16a25f3e904f1ae9 Mon Sep 17 00:00:00 2001 From: EurusHomles-zH Date: Mon, 11 Dec 2023 09:24:44 +0800 Subject: [PATCH] Description:1.The id is not triggered when the signal carries parameters. 2.Trigger GC Signed-off-by: EurusHomles-zH --- frameworks/native/appkit/app/main_thread.cpp | 65 +++++++++++++++++-- frameworks/native/runtime/js_runtime.cpp | 13 ++++ .../inner_api/runtime/include/js_runtime.h | 2 + .../inner_api/runtime/include/runtime.h | 2 + .../kits/native/appkit/app/main_thread.h | 2 + .../js_environment/src/js_environment.cpp | 9 +++ .../interfaces/inner_api/js_environment.h | 2 + .../mock_runtime.h | 8 +++ 8 files changed, 97 insertions(+), 6 deletions(-) diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index 2f5ff2bfc7..76be8ee516 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -114,8 +114,9 @@ enum class SignalType { SIGNAL_JSHEAP_OLD, SIGNAL_JSHEAP, SIGNAL_JSHEAP_PRIV, - SIGNAL_START_SAMPLE, - SIGNAL_STOP_SAMPLE, + SIGNAL_NO_TRIGGERID, + SIGNAL_NO_TRIGGERID_PRIV, + SIGNAL_FORCE_FULLGC, }; constexpr char EVENT_KEY_PACKAGE_NAME[] = "PACKAGE_NAME"; @@ -2115,6 +2116,7 @@ void MainThread::HandleSignal(int signal, [[maybe_unused]] siginfo_t *siginfo, v { if (signal != MUSL_SIGNAL_JSHEAP) { HILOG_ERROR("HandleSignal failed, signal is %{public}d", signal); + return; } HILOG_INFO("HandleSignal sival_int is %{public}d", siginfo->si_value.sival_int); switch (static_cast(siginfo->si_value.sival_int)) { @@ -2133,12 +2135,25 @@ void MainThread::HandleSignal(int signal, [[maybe_unused]] siginfo_t *siginfo, v signalHandler_->PostTask(privateHeapFunc, "MainThread:SIGNAL_JSHEAP_PRIV"); break; } - case SignalType::SIGNAL_START_SAMPLE: { - HILOG_ERROR("HandleSignal failed, SIGNAL_START_SAMPLE is retained"); + case SignalType::SIGNAL_NO_TRIGGERID: { + auto heapFunc = std::bind(&MainThread::HandleDumpHeap, false); + signalHandler_->PostTask(heapFunc, "MainThread::SIGNAL_JSHEAP"); + + auto noTriggerIdFunc = std::bind(&MainThread::DestroyHeapProfiler); + signalHandler_->PostTask(noTriggerIdFunc, "MainThread::SIGNAL_NO_TRIGGERID"); break; } - case SignalType::SIGNAL_STOP_SAMPLE: { - HILOG_ERROR("HandleSignal failed, SIGNAL_STOP_SAMPLE is retained"); + case SignalType::SIGNAL_NO_TRIGGERID_PRIV: { + auto privateHeapFunc = std::bind(&MainThread::HandleDumpHeap, true); + signalHandler_->PostTask(privateHeapFunc, "MainThread:SIGNAL_JSHEAP_PRIV"); + + auto noTriggerIdFunc = std::bind(&MainThread::DestroyHeapProfiler); + signalHandler_->PostTask(noTriggerIdFunc, "MainThread::SIGNAL_NO_TRIGGERID_PRIV"); + break; + } + case SignalType::SIGNAL_FORCE_FULLGC: { + auto forceFullGCFunc = std::bind(&MainThread::ForceFullGC); + signalHandler_->PostTask(forceFullGCFunc, "MainThread:SIGNAL_FORCE_FULLGC"); break; } default: @@ -2165,6 +2180,44 @@ void MainThread::HandleDumpHeap(bool isPrivate) mainHandler_->PostTask(task, "MainThread:DumpHeap"); } +void MainThread::DestroyHeapProfiler() +{ + HILOG_DEBUG("Destory heap profiler."); + if (mainHandler_ == nullptr) { + HILOG_ERROR("DestroyHeapProfiler failed, mainHandler is nullptr"); + return; + } + + auto task = [] { + auto app = applicationForDump_.lock(); + if (app == nullptr || app->GetRuntime() == nullptr) { + HILOG_ERROR("runtime is nullptr."); + return; + } + app->GetRuntime()->DestroyHeapProfiler(); + }; + mainHandler_->PostTask(task, "MainThread:DestroyHeapProfiler"); +} + +void MainThread::ForceFullGC() +{ + HILOG_DEBUG("Force fullGC."); + if (mainHandler_ == nullptr) { + HILOG_ERROR("ForceFullGC failed, mainHandler is nullptr"); + return; + } + + auto task = [] { + auto app = applicationForDump_.lock(); + if (app == nullptr || app->GetRuntime() == nullptr) { + HILOG_ERROR("runtime is nullptr."); + return; + } + app->GetRuntime()->ForceFullGC(); + }; + mainHandler_->PostTask(task, "MainThread:ForceFullGC"); +} + void MainThread::Start() { HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 425863014d..0531b22c45 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -1056,6 +1056,19 @@ void JsRuntime::DumpHeapSnapshot(bool isPrivate) nativeEngine->DumpHeapSnapshot(true, DumpFormat::JSON, isPrivate); } +void JsRuntime::DestroyHeapProfiler() +{ + CHECK_POINTER(jsEnv_); + jsEnv_->DestroyHeapProfiler(); +} + +void JsRuntime::ForceFullGC() +{ + auto vm = GetEcmaVm(); + CHECK_POINTER(vm); + panda::JSNApi::TriggerGC(vm, panda::JSNApi::TRIGGER_GC_TYPE::FULL_GC); +} + bool JsRuntime::BuildJsStackInfoList(uint32_t tid, std::vector& jsFrames) { auto nativeEngine = GetNativeEnginePointer(); diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index 74eacbf211..38ee111899 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -77,6 +77,8 @@ public: void PostSyncTask(const std::function& task, const std::string& name); void RemoveTask(const std::string& name); void DumpHeapSnapshot(bool isPrivate) override; + void DestroyHeapProfiler() override; + void ForceFullGC() override; bool BuildJsStackInfoList(uint32_t tid, std::vector& jsFrames) override; void NotifyApplicationState(bool isBackground) override; bool SuspendVM(uint32_t tid) override; diff --git a/interfaces/inner_api/runtime/include/runtime.h b/interfaces/inner_api/runtime/include/runtime.h index d41e52e8d6..95c80440f5 100644 --- a/interfaces/inner_api/runtime/include/runtime.h +++ b/interfaces/inner_api/runtime/include/runtime.h @@ -78,6 +78,8 @@ public: virtual void StartDebugMode(bool needBreakPoint, const std::string &processName, bool isDebug = true) = 0; virtual bool BuildJsStackInfoList(uint32_t tid, std::vector& jsFrames) = 0; virtual void DumpHeapSnapshot(bool isPrivate) = 0; + virtual void DestroyHeapProfiler() = 0; + virtual void ForceFullGC() = 0; virtual void NotifyApplicationState(bool isBackground) = 0; virtual bool SuspendVM(uint32_t tid) = 0; virtual void ResumeVM(uint32_t tid) = 0; diff --git a/interfaces/kits/native/appkit/app/main_thread.h b/interfaces/kits/native/appkit/app/main_thread.h index 1d4a55b594..f3c41b1649 100644 --- a/interfaces/kits/native/appkit/app/main_thread.h +++ b/interfaces/kits/native/appkit/app/main_thread.h @@ -521,6 +521,8 @@ private: void UpdateRuntimeModuleChecker(const std::unique_ptr &runtime); static void HandleDumpHeap(bool isPrivate); + static void DestroyHeapProfiler(); + static void ForceFullGC(); static void HandleSignal(int signal, siginfo_t *siginfo, void *context); void NotifyAppFault(const FaultData &faultData); diff --git a/js_environment/frameworks/js_environment/src/js_environment.cpp b/js_environment/frameworks/js_environment/src/js_environment.cpp index 3d855ae35f..1acf7b9329 100644 --- a/js_environment/frameworks/js_environment/src/js_environment.cpp +++ b/js_environment/frameworks/js_environment/src/js_environment.cpp @@ -284,6 +284,15 @@ void JsEnvironment::StartProfiler(const char* libraryPath, uint32_t instanceId, panda::DFXJSNApi::StartProfiler(vm_, option, tid, instanceId, debuggerPostTask); } +void JsEnvironment::DestroyHeapProfiler() +{ + if (vm_ == nullptr) { + JSENV_LOG_E("Invalid vm."); + return; + } + panda::DFXJSNApi::DestroyHeapProfiler(vm_); +} + void JsEnvironment::ReInitJsEnvImpl(std::unique_ptr impl) { JSENV_LOG_I("ReInit jsenv impl."); diff --git a/js_environment/interfaces/inner_api/js_environment.h b/js_environment/interfaces/inner_api/js_environment.h index 768966d5cc..e4cafb8869 100644 --- a/js_environment/interfaces/inner_api/js_environment.h +++ b/js_environment/interfaces/inner_api/js_environment.h @@ -88,6 +88,8 @@ public: void StartProfiler( const char* libraryPath, uint32_t instanceId, PROFILERTYPE profiler, int32_t interval, uint32_t tid); + void DestroyHeapProfiler(); + void ReInitJsEnvImpl(std::unique_ptr impl); void SetModuleLoadChecker(const std::shared_ptr& moduleCheckerDelegate); diff --git a/test/mock/frameworks_kits_runtime_test/mock_runtime.h b/test/mock/frameworks_kits_runtime_test/mock_runtime.h index 8897901a8c..d3ef1047f4 100644 --- a/test/mock/frameworks_kits_runtime_test/mock_runtime.h +++ b/test/mock/frameworks_kits_runtime_test/mock_runtime.h @@ -62,6 +62,14 @@ public: { return; } + void DestroyHeapProfiler() override + { + return; + } + void ForceFullGC() override + { + return; + } void NotifyApplicationState(bool isBackground) override { return;