From e5f574bb746063a901e92e127b4868fe251b13ff Mon Sep 17 00:00:00 2001 From: cs1111 Date: Mon, 8 Sep 2025 20:14:46 +0800 Subject: [PATCH] \feature:async stack open normally Signed-off-by: cs1111 Change-Id: Id60eb354653f811cd75f4bf02886fc787af4ec2f --- .../common/watchdog_interface_test.cpp | 2 +- frameworks/native/watchdog.cpp | 4 +-- frameworks/native/watchdog_inner.cpp | 35 ++++++++++++++++++- frameworks/native/watchdog_inner.h | 4 ++- frameworks/native/xcollie_utils.cpp | 15 ++++++++ frameworks/native/xcollie_utils.h | 4 +++ .../innerkits/include/xcollie/watchdog.h | 2 +- interfaces/native/innerkits/libhicollie.map | 2 +- 8 files changed, 61 insertions(+), 7 deletions(-) diff --git a/frameworks/native/test/unittest/common/watchdog_interface_test.cpp b/frameworks/native/test/unittest/common/watchdog_interface_test.cpp index c30214c..71b9cf8 100644 --- a/frameworks/native/test/unittest/common/watchdog_interface_test.cpp +++ b/frameworks/native/test/unittest/common/watchdog_interface_test.cpp @@ -357,7 +357,7 @@ HWTEST_F(WatchdogInterfaceTest, WatchdogAppDebugTest_001, TestSize.Level1) HWTEST_F(WatchdogInterfaceTest, Watchdog_AddTreadTest_001, TestSize.Level1) { Watchdog::GetInstance().SetForeground(true); - Watchdog::GetInstance().SetBundleInfo("test", "1.1.0"); + Watchdog::GetInstance().SetBundleInfo("test", "1.1.0", true); constexpr int blockTime = 2; constexpr int checkPeriod = 1000; auto blockFunc = []() { diff --git a/frameworks/native/watchdog.cpp b/frameworks/native/watchdog.cpp index 7de9434..e374168 100644 --- a/frameworks/native/watchdog.cpp +++ b/frameworks/native/watchdog.cpp @@ -58,9 +58,9 @@ void Watchdog::InitFfrtWatchdog() return WatchdogInner::GetInstance().InitFfrtWatchdog(); } -void Watchdog::SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion) +void Watchdog::SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion, bool isSystemApp) { - return WatchdogInner::GetInstance().SetBundleInfo(bundleName, bundleVersion); + return WatchdogInner::GetInstance().SetBundleInfo(bundleName, bundleVersion, isSystemApp); } void Watchdog::SetForeground(const bool& isForeground) diff --git a/frameworks/native/watchdog_inner.cpp b/frameworks/native/watchdog_inner.cpp index 23da4f9..ad81c44 100644 --- a/frameworks/native/watchdog_inner.cpp +++ b/frameworks/native/watchdog_inner.cpp @@ -115,6 +115,7 @@ constexpr int CPU_FREQ_DECIMAL_BASE = 10; constexpr const char* const SCROLL_JANK = "SCROLL_JANK"; constexpr const char* const MAIN_THREAD_JANK = "MAIN_THREAD_JANK"; constexpr const char* const BUSSINESS_THREAD_JANK = "BUSSINESS_THREAD_JANK"; +using InitAsyncStackFn = void(*)(); } std::mutex WatchdogInner::lockFfrt_; @@ -199,10 +200,11 @@ static bool IsInAppspwan() return false; } -void WatchdogInner::SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion) +void WatchdogInner::SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion, bool isSystemApp) { bundleName_ = bundleName; bundleVersion_ = bundleVersion; + isSystemApp_ = isSystemApp; } void WatchdogInner::SetForeground(const bool& isForeground) @@ -492,6 +494,36 @@ bool WatchdogInner::EnableAppStartSample(AppStartContent& startContent, int64_t return AppStartSample(isScroll, startContent); } +void WatchdogInner::InitAsyncStackIfNeed() +{ + if (bundleName_.empty()) { + return; + } + static std::once_flag onceFlag; + std::call_once(onceFlag, [this] { + static const char* debuggableEnv = getenv("HAP_DEBUGGABLE"); + if (!(debuggableEnv != NULL && strcmp(debuggableEnv, "true") == 0) && isSystemApp_) { + return; + } + if (!IsAsyncStackEnable()) { + XCOLLIE_LOGI("Current app start detection task is being executed."); + return; + } + if (IsAsyncStackBlockBundle(bundleName_)) { + return; + } + auto asyncStackLibHandle = dlopen("libasync_stack.z.so", RTLD_NOW); + if (asyncStackLibHandle != nullptr) { + auto initAsyncStack = reinterpret_cast(dlsym(asyncStackLibHandle, "DfxInitAsyncStack")); + if (initAsyncStack != nullptr) { + initAsyncStack(); + } else { + dlclose(asyncStackLibHandle); + } + } + }); +} + bool WatchdogInner::CheckSample(const TimePoint& endTime, int64_t durationTime) { bool isScroll = isScroll_; @@ -1329,6 +1361,7 @@ bool WatchdogInner::Start() if (__get_global_hook_flag() && __get_hook_flag()) { __set_hook_flag(false); } + InitAsyncStackIfNeed(); uint64_t now = GetCurrentTickMillseconds(); WatchdogTask task; uint64_t leftTimeMill; diff --git a/frameworks/native/watchdog_inner.h b/frameworks/native/watchdog_inner.h index a352055..3becd50 100644 --- a/frameworks/native/watchdog_inner.h +++ b/frameworks/native/watchdog_inner.h @@ -66,7 +66,7 @@ public: void StartProfileMainThread(const TimePoint& endTime, int64_t durationTime, int sampleInterval); bool CollectStack(std::string& stack, std::string& heaviestStack, int treeFormat = ENABLE_TREE_FORMAT); bool Deinit(); - void SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion); + void SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion, bool isSystemApp); void SetForeground(const bool& isForeground); bool GetForeground(); void RemoveInnerTask(const std::string& name); @@ -130,6 +130,7 @@ private: void ParseAppStartParams(const std::string& line, const std::string& eventName); void ReadAppStartConfig(const std::string& filePath); bool EnableAppStartSample(AppStartContent& startContent, int64_t durationTime, bool isScroll); + void InitAsyncStackIfNeed(); static void ThreadSamplerSigHandler(int sig, siginfo_t* si, void* context); bool InstallThreadSamplerSignal(); @@ -162,6 +163,7 @@ private: bool isMainThreadTraceEnabled_ {false}; std::string bundleName_; std::string bundleVersion_; + bool isSystemApp_ { true }; bool isForeground_ {false}; bool isAppDebug_ {false}; std::shared_ptr traceCollector_; diff --git a/frameworks/native/xcollie_utils.cpp b/frameworks/native/xcollie_utils.cpp index d80bf51..1a2697f 100644 --- a/frameworks/native/xcollie_utils.cpp +++ b/frameworks/native/xcollie_utils.cpp @@ -217,6 +217,21 @@ bool IsBetaVersion() return (isBetaVersion.find(ENABLE_BETA_VAULE) != std::string::npos); } +bool IsAsyncStackEnable() +{ + const char *const asyncStackSwich = "persist.hiview.async_stack.switch"; + static bool isAsyncStackEnable = OHOS::system::GetParameter(asyncStackSwich, "enable") == "enable"; + return isAsyncStackEnable; +} + +bool IsAsyncStackBlockBundle(const std::string& bundleName) +{ + const char *const asyncStackBlockBundles = "persist.hiview.async_stack.block_bundles"; + static std::string blockBundlesName = OHOS::system::GetParameter(asyncStackBlockBundles, ""); + return blockBundlesName.find(bundleName) != std::string::npos; +} + + std::string GetProcessNameFromProcCmdline(int32_t pid) { if (pid > 0) { diff --git a/frameworks/native/xcollie_utils.h b/frameworks/native/xcollie_utils.h index 06875de..44d6051 100644 --- a/frameworks/native/xcollie_utils.h +++ b/frameworks/native/xcollie_utils.h @@ -109,6 +109,10 @@ bool IsDeveloperOpen(); bool IsBetaVersion(); +bool IsAsyncStackEnable(); + +bool IsAsyncStackBlockBundle(const std::string& bundleName); + std::string GetFormatDate(); std::string FormatTime(const std::string &format); diff --git a/interfaces/native/innerkits/include/xcollie/watchdog.h b/interfaces/native/innerkits/include/xcollie/watchdog.h index 27990f9..06988b1 100644 --- a/interfaces/native/innerkits/include/xcollie/watchdog.h +++ b/interfaces/native/innerkits/include/xcollie/watchdog.h @@ -99,7 +99,7 @@ public: * @brief Set bundle info. * */ - void SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion); + void SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion, bool isSystemApp); /** * diff --git a/interfaces/native/innerkits/libhicollie.map b/interfaces/native/innerkits/libhicollie.map index 41fb852..78abd90 100644 --- a/interfaces/native/innerkits/libhicollie.map +++ b/interfaces/native/innerkits/libhicollie.map @@ -27,7 +27,7 @@ "OHOS::HiviewDFX::Watchdog::RunPeriodicalTask(std::__h::basic_string, std::__h::allocator> const&, std::__h::function&&, unsigned long long, unsigned long long)"; "OHOS::HiviewDFX::Watchdog::StopWatchdog()"; "OHOS::HiviewDFX::Watchdog::InitFfrtWatchdog()"; - "OHOS::HiviewDFX::Watchdog::SetBundleInfo(std::__h::basic_string, std::__h::allocator> const&, std::__h::basic_string, std::__h::allocator> const&)"; + "OHOS::HiviewDFX::Watchdog::SetBundleInfo(std::__h::basic_string, std::__h::allocator> const&, std::__h::basic_string, std::__h::allocator> const&, bool)"; "OHOS::HiviewDFX::Watchdog::SetForeground(bool const&)"; "OHOS::HiviewDFX::Watchdog::RemovePeriodicalTask(std::__h::basic_string, std::__h::allocator> const&)"; "OHOS::HiviewDFX::Watchdog::InitMainLooperWatcher(void (**)(char const*), void (**)(char const*))"; -- Gitee