From ea04729231f645144ba2bd186bbb4b874b0048a4 Mon Sep 17 00:00:00 2001 From: hhl Date: Sat, 6 Sep 2025 15:42:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hhl --- .../native/appkit/dfr/appfreeze_inner.cpp | 2 +- services/appdfr/include/appfreeze_util.h | 1 + .../appdfr/src/appfreeze_cpu_freq_manager.cpp | 42 +++++++++++-------- .../appdfr/src/appfreeze_event_report.cpp | 11 +++-- services/appdfr/src/appfreeze_manager.cpp | 25 ++++------- services/appdfr/src/appfreeze_util.cpp | 10 +++++ .../appfreeze_cpu_freq_manager_test.cpp | 13 ++++++ .../appfreeze_event_report_test.cpp | 5 ++- .../appfreeze_manager_test.cpp | 15 ++----- .../cpu_sys_config_test.cpp | 17 ++++++-- 10 files changed, 86 insertions(+), 55 deletions(-) diff --git a/frameworks/native/appkit/dfr/appfreeze_inner.cpp b/frameworks/native/appkit/dfr/appfreeze_inner.cpp index 824d5a0e096..1fc49344bd2 100644 --- a/frameworks/native/appkit/dfr/appfreeze_inner.cpp +++ b/frameworks/native/appkit/dfr/appfreeze_inner.cpp @@ -166,13 +166,13 @@ void AppfreezeInner::AppfreezeHandleOverReportCount(bool isSixSecondEvent) " ret:%{public}d", pid, ret); } faultData.errorObject.name = AppFreezeType::THREAD_BLOCK_3S; - EnableFreezeSample(faultData); } if (!IsHandleAppfreeze()) { NotifyANR(faultData); return; } std::string msgContent; + EnableFreezeSample(faultData); GetMainHandlerDump(msgContent); ChangeFaultDateInfo(faultData, msgContent); return; diff --git a/services/appdfr/include/appfreeze_util.h b/services/appdfr/include/appfreeze_util.h index 2714a32b3b6..52d5a8f3a16 100644 --- a/services/appdfr/include/appfreeze_util.h +++ b/services/appdfr/include/appfreeze_util.h @@ -35,6 +35,7 @@ public: static uint64_t GetMilliseconds(); static std::string RoundToTwoDecimals(float value); static int GetCpuCount(); + static std::string FreezePathToRealPath(const std::string& filePath); }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appdfr/src/appfreeze_cpu_freq_manager.cpp b/services/appdfr/src/appfreeze_cpu_freq_manager.cpp index f6be7e91aa7..d8e3fc403b7 100644 --- a/services/appdfr/src/appfreeze_cpu_freq_manager.cpp +++ b/services/appdfr/src/appfreeze_cpu_freq_manager.cpp @@ -92,7 +92,7 @@ bool AppfreezeCpuFreqManager::RemoveOldInfo() ++it; } } - TAG_LOGI(AAFwkTag::APPDFR, "reomve old tasks count: %{public}d, " + TAG_LOGI(AAFwkTag::APPDFR, "remove old tasks count: %{public}d, " "current tasks count: %{public}zu", removeCount, cpuInfoMap_.size()); return removeCount != 0; } @@ -135,7 +135,8 @@ bool AppfreezeCpuFreqManager::GetInfoByCpuCount(int32_t cpu, std::vector& totalTimeList, const std::vector& blockTotalTimeList, TotalTime& totalTime) { - if (totalTimeList.size() <= i || totalTimeList.size() != blockTotalTimeList.size()) { + if (totalTimeList.size() <= 0 || totalTimeList.size() <= i || totalTimeList.size() != blockTotalTimeList.size()) { TAG_LOGE(AAFwkTag::APPDFR, "index:%{public}zu, halfTotal size:%{public}zu, " "blockTotal size:%{public}zu.", i, totalTimeList.size(), blockTotalTimeList.size()); return false; @@ -211,17 +212,17 @@ std::string AppfreezeCpuFreqManager::GetCpuInfoContent(const std::vector> &warnCpuDetailInfo, const std::vector &blockTotalTimeList, const std::vector> &blockCpuDetailInfo) { - if (warnTotalTimeList.size() == 0 || warnTotalTimeList.size() == 0) { - return ""; - } - if (warnTotalTimeList.size() != blockTotalTimeList.size()) { - TAG_LOGE(AAFwkTag::APPDFR, "Warning and block have different sizes, warning size:%{public}zu," - " block size:%{public}zu", warnTotalTimeList.size(), blockTotalTimeList.size()); + size_t warnTotalSize = warnTotalTimeList.size(); + size_t warnCpuSize = warnCpuDetailInfo.size(); + if (warnTotalSize == 0 || warnTotalSize != warnCpuSize || + warnTotalSize != blockTotalTimeList.size() || warnCpuSize != blockCpuDetailInfo.size()) { + TAG_LOGE(AAFwkTag::APPDFR, "warning total size:%{public}zu, warning cpi size:%{public}zu", + warnTotalSize, warnCpuSize); return ""; } std::stringstream ss; ss << "start time:" << AbilityRuntime::TimeUtil::DefaultCurrentTimeStr() << std::endl; - for (size_t i = 0; i < warnCpuDetailInfo.size(); ++i) { + for (size_t i = 0; i < warnCpuSize; ++i) { auto warnningData = warnCpuDetailInfo[i]; auto blockData = blockCpuDetailInfo[i]; if (warnningData.size() != blockData.size()) { @@ -258,7 +259,8 @@ std::string AppfreezeCpuFreqManager::GetCpuInfoContent(const std::vector strings; SplitStr(line, " ", strings); if (strings.size() <= DEFAULT_CPU_SIZE) { @@ -336,7 +338,8 @@ double AppfreezeCpuFreqManager::GetOptimalCpuTime(int32_t pid) { int maxCpuCount = cpuCount_ - AppfreezeUtil::CPU_COUNT_SUBTRACT; std::string cpuFile = CpuSysConfig::GetMaxCoreDimpsPath(maxCpuCount); - std::ifstream fin(cpuFile); + std::string realPath = AppfreezeUtil::FreezePathToRealPath(cpuFile); + std::ifstream fin(realPath); if (!fin.is_open()) { TAG_LOGE(AAFwkTag::APPDFR, "Read cpu info failed, cpuFile:%{public}s, errno:%{public}d", cpuFile.c_str(), errno); @@ -344,7 +347,7 @@ double AppfreezeCpuFreqManager::GetOptimalCpuTime(int32_t pid) } std::string content; double ret = 0; - if (getline(fin, content) && content.empty()) { + if (!getline(fin, content) || content.empty()) { TAG_LOGE(AAFwkTag::APPDFR, "Read info failed, path:%{public}s", cpuFile.c_str()); return ret; } @@ -484,12 +487,15 @@ std::string AppfreezeCpuFreqManager::GetCpuInfoPath(const std::string &type, str << "#CpuFreq Usage (usage >=1%)" << std::endl << GetCpuInfoContent( warnCpuInfo.GetTotalTimeList(), warnCpuInfo.GetCpuDetailData(), blockCpuInfo.GetTotalTimeList(), blockCpuInfo.GetCpuDetailData()) << std::endl; - OHOS::SaveStringToFile(logFile, str.str(), false); + if (!OHOS::SaveStringToFile(logFile, str.str(), false)) { + TAG_LOGE(AAFwkTag::APPDFR, "save to cpu info to file failed, logFile: %{public}s", logFile.c_str()); + } else { + TAG_LOGW(AAFwkTag::APPDFR, "write cpu info success, logFile: %{public}s", logFile.c_str()); + } { std::lock_guard lock(freezeInfoMutex_); cpuInfoMap_.erase(type); } - TAG_LOGW(AAFwkTag::APPDFR, "write cpu info success, logFile: %{public}s", logFile.c_str()); return logFile; } } // namespace AppExecFwk diff --git a/services/appdfr/src/appfreeze_event_report.cpp b/services/appdfr/src/appfreeze_event_report.cpp index 258daf0f932..4df120c4ba2 100644 --- a/services/appdfr/src/appfreeze_event_report.cpp +++ b/services/appdfr/src/appfreeze_event_report.cpp @@ -71,7 +71,9 @@ int AppfreezeEventReport::LogAppInputBlockEvent(const std::string &name, HiSysEv EVENT_APP_RUNNING_UNIQUE_ID, eventInfo.appRunningUniqueId, EVENT_INPUT_ID, eventInfo.eventId, EVENT_FREEZE_MEMORY, eventInfo.freezeMemory, - EVENT_ENABLE_MAINTHREAD_SAMPLE, eventInfo.enableFreeze); + EVENT_ENABLE_MAINTHREAD_SAMPLE, eventInfo.enableFreeze, + EVENT_FOREGROUND, eventInfo.foregroundState, + EVENT_FREEZE_INFO_PATH, eventInfo.freezeInfoFile); return ret; } @@ -95,8 +97,8 @@ int AppfreezeEventReport::LogThreadBlockEvent(const std::string &name, HiSysEven EVENT_FREEZE_MEMORY, eventInfo.freezeMemory, EVENT_TRACE_ID, eventInfo.hitraceInfo, EVENT_FREEZE_INFO_PATH, eventInfo.freezeInfoFile, - EVENT_FOREGROUND, eventInfo.foregroundState, - EVENT_ENABLE_MAINTHREAD_SAMPLE, eventInfo.enableFreeze); + EVENT_ENABLE_MAINTHREAD_SAMPLE, eventInfo.enableFreeze, + EVENT_FOREGROUND, eventInfo.foregroundState); return ret; } @@ -119,7 +121,8 @@ int AppfreezeEventReport::LogGeneralEvent(const std::string &name, HiSysEventTyp EVENT_APP_RUNNING_UNIQUE_ID, eventInfo.appRunningUniqueId, EVENT_FREEZE_MEMORY, eventInfo.freezeMemory, EVENT_FREEZE_INFO_PATH, eventInfo.freezeInfoFile, - EVENT_ENABLE_MAINTHREAD_SAMPLE, eventInfo.enableFreeze); + EVENT_ENABLE_MAINTHREAD_SAMPLE, eventInfo.enableFreeze, + EVENT_FOREGROUND, eventInfo.foregroundState); return ret; } } // namespace AppExecFwk diff --git a/services/appdfr/src/appfreeze_manager.cpp b/services/appdfr/src/appfreeze_manager.cpp index a7a2b0626df..c5d01f7fcbe 100644 --- a/services/appdfr/src/appfreeze_manager.cpp +++ b/services/appdfr/src/appfreeze_manager.cpp @@ -46,11 +46,6 @@ namespace OHOS { namespace AppExecFwk { namespace { -constexpr char EVENT_UID[] = "UID"; -#ifdef ABILITY_RUNTIME_HITRACE_ENABLE -constexpr int32_t CHARACTER_WIDTH = 2; -#endif - constexpr int MAX_LAYER = 8; constexpr int FREEZEMAP_SIZE_MAX = 20; constexpr int FREEZE_TIME_LIMIT = 60000; @@ -67,11 +62,6 @@ static bool g_betaVersion = OHOS::system::GetParameter("const.logsystem.versiont static bool g_overseaVersion = OHOS::system::GetParameter("const.global.region", "CN") != "CN"; static bool g_developMode = (OHOS::system::GetParameter("persist.hiview.leak_detector", "unknown") == "enable") || (OHOS::system::GetParameter("persist.hiview.leak_detector", "unknown") == "true"); - -static constexpr const char *const HITRACE_ID = "hitrace_id: "; -static constexpr const char *const SPAN_ID = "span_id: "; -static constexpr const char *const PARENT_SPAN_ID = "parent_span_id: "; -static constexpr const char *const TRACE_FLAG = "trace_flag: "; } static constexpr const char *const TWELVE_BIG_CPU_CUR_FREQ = "/sys/devices/system/cpu/cpufreq/policy2/scaling_cur_freq"; static constexpr const char *const TWELVE_BIG_CPU_MAX_FREQ = "/sys/devices/system/cpu/cpufreq/policy2/scaling_max_freq"; @@ -315,6 +305,8 @@ FaultData AppfreezeManager::GetFaultNotifyData(const FaultData& faultData, int p faultNotifyData.appfreezeInfo = faultData.appfreezeInfo; faultNotifyData.appRunningUniqueId = faultData.appRunningUniqueId; faultNotifyData.procStatm = faultData.procStatm; + faultNotifyData.isInForeground = faultData.isInForeground; + faultNotifyData.isEnableMainThreadSample = faultData.isEnableMainThreadSample; return faultNotifyData; } @@ -371,7 +363,7 @@ int AppfreezeManager::AcquireStack(const FaultData& faultData, std::string AppfreezeManager::ParseDecToHex(uint64_t id) { std::stringstream ss; - ss << std::hex << std::setfill('0') << std::setw(CHARACTER_WIDTH) << id; + ss << std::hex << id; return ss.str(); } @@ -384,11 +376,12 @@ std::string AppfreezeManager::GetHitraceInfo() return ""; } std::ostringstream hitraceIdStr; - hitraceIdStr << HITRACE_ID << ParseDecToHex(hitraceId.GetChainId()) << - SPAN_ID << ParseDecToHex(hitraceId.GetSpanId()) << - PARENT_SPAN_ID << ParseDecToHex(hitraceId.GetSpanId()) << - TRACE_FLAG << ParseDecToHex(hitraceId.GetParentSpanId()); - TAG_LOGW(AAFwkTag::APPDFR, "hitraceIdStr:%{public}s", hitraceIdStr.str().c_str()); + hitraceIdStr << "hitrace_id: " << ParseDecToHex(hitraceId.GetChainId()) << + "span_id: " << ParseDecToHex(hitraceId.GetSpanId()) << + "parent_span_id: " << ParseDecToHex(hitraceId.GetParentSpanId()) << + "trace_flag: " << ParseDecToHex(hitraceId.GetFlags()); + std::string hiTraceIdInfo = hitraceIdStr.str(); + TAG_LOGW(AAFwkTag::APPDFR, "hitraceIdStr:%{public}s", hiTraceIdInfo.c_str()); return hitraceIdStr.str(); #endif return ""; diff --git a/services/appdfr/src/appfreeze_util.cpp b/services/appdfr/src/appfreeze_util.cpp index a3d4c170a36..4c981a07671 100644 --- a/services/appdfr/src/appfreeze_util.cpp +++ b/services/appdfr/src/appfreeze_util.cpp @@ -117,5 +117,15 @@ int AppfreezeUtil::GetCpuCount() TAG_LOGD(AAFwkTag::APPDFR, "read: %{public}s to get cpu count:%{public}d.", procStatPath.c_str(), cpuCount); return cpuCount; } + +std::string AppfreezeUtil::FreezePathToRealPath(const std::string& filePath) +{ + std::string realPath; + if (!OHOS::PathToRealPath(filePath, realPath)) { + TAG_LOGE(AAFwkTag::APPDFR, "pathToRealPath failed:%{public}s", filePath.c_str()); + return ""; + } + return realPath; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/appfreeze_cpu_freq_manager_test.cpp b/test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/appfreeze_cpu_freq_manager_test.cpp index 36ddb1ecfed..e9b39d308eb 100644 --- a/test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/appfreeze_cpu_freq_manager_test.cpp +++ b/test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/appfreeze_cpu_freq_manager_test.cpp @@ -360,5 +360,18 @@ HWTEST_F(AppfreezeCpuFreqManagerTest, GetCpuInfoPathTest_001, TestSize.Level0) testValue, uid, pid); EXPECT_TRUE(!ret.empty()); } + +/** + * @tc.number: FreezePathToRealPathTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, FreezePathToRealPathTest_001, TestSize.Level0) +{ + std::string logFile = AppfreezeUtil::FreezePathToRealPath("../FreezePathToRealPathTest_001"); + EXPECT_EQ(logFile, ""); + logFile = AppfreezeUtil::FreezePathToRealPath("/data/log/faultlog"); + EXPECT_TRUE(!logFile.empty()); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/dfr_test/appfreeze_event_report_test/appfreeze_event_report_test.cpp b/test/unittest/dfr_test/appfreeze_event_report_test/appfreeze_event_report_test.cpp index cef39e86bd2..292e1076052 100644 --- a/test/unittest/dfr_test/appfreeze_event_report_test/appfreeze_event_report_test.cpp +++ b/test/unittest/dfr_test/appfreeze_event_report_test/appfreeze_event_report_test.cpp @@ -109,6 +109,7 @@ HWTEST_F(AppfreezeEventReportTest, SendAppfreezeEvent_Test_002, TestSize.Level1) eventInfo.freezeInfoFile = "SendAppfreezeEvent_Test_002: freezeInfoFile"; eventInfo.hitraceInfo = "hitraceInfo: 1234"; eventInfo.foregroundState = false; + eventInfo.enableFreeze = false; int ret = AppfreezeEventReport::SendAppfreezeEvent(eventName, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, eventInfo); EXPECT_EQ(ret, 0); @@ -208,6 +209,7 @@ HWTEST_F(AppfreezeEventReportTest, SendAppfreezeEvent_Test_005, TestSize.Level1) eventInfo.freezeInfoFile = "SendAppfreezeEvent_Test_005: freezeInfoFile"; eventInfo.hitraceInfo = "hitraceInfo: 1234"; eventInfo.foregroundState = false; + eventInfo.enableFreeze = false; int ret = AppfreezeEventReport::SendAppfreezeEvent(eventName, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, eventInfo); EXPECT_EQ(ret, 0); @@ -306,7 +308,8 @@ HWTEST_F(AppfreezeEventReportTest, SendAppfreezeEvent_Test_008, TestSize.Level1) eventInfo.errorMessage = "SendAppfreezeEvent_Test_008: error message"; eventInfo.freezeInfoFile = "SendAppfreezeEvent_Test_008: freezeInfoFile"; eventInfo.hitraceInfo = "hitraceInfo: 1234"; - eventInfo.foregroundState = true; + eventInfo.foregroundState = false; + eventInfo.enableFreeze = false; int ret = AppfreezeEventReport::SendAppfreezeEvent(eventName, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, eventInfo); EXPECT_EQ(ret, 0); diff --git a/test/unittest/dfr_test/appfreeze_manager_test/appfreeze_manager_test.cpp b/test/unittest/dfr_test/appfreeze_manager_test/appfreeze_manager_test.cpp index 3201801c7c5..848621e9f62 100644 --- a/test/unittest/dfr_test/appfreeze_manager_test/appfreeze_manager_test.cpp +++ b/test/unittest/dfr_test/appfreeze_manager_test/appfreeze_manager_test.cpp @@ -298,17 +298,6 @@ HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_CatchStack_001, TestSize.Lev EXPECT_TRUE(appfreezeManager->catchStackMap_.empty()); } -/** - * @tc.number: AppfreezeManagerTest_ParseDecToHex_001 - * @tc.desc: add testcase codecoverage - * @tc.type: FUNC - */ -HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_ParseDecToHex_001, TestSize.Level1) -{ - std::string ret = appfreezeManager->ParseDecToHex(1234); // test value - EXPECT_EQ(ret, "4d2"); -} - #ifdef ABILITY_RUNTIME_HITRACE_ENABLE /** * @tc.number: AppfreezeManagerTest_GetHitraceInfo_001 @@ -317,7 +306,9 @@ HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_ParseDecToHex_001, TestSize. */ HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_GetHitraceInfo_001, TestSize.Level1) { - std::string ret = appfreezeManager->GetHitraceInfo(); + std::string ret = appfreezeManager->ParseDecToHex(1234); // test value + EXPECT_EQ(ret, "4d2"); + ret = appfreezeManager->GetHitraceInfo(); EXPECT_TRUE(ret.empty()); OHOS::HiviewDFX::HiTraceChain::Begin("AppfreezeManagerTest_GetHitraceInfo_001", 0); appfreezeManager->GetHitraceInfo(); diff --git a/test/unittest/dfr_test/cpu_sys_config_test/cpu_sys_config_test.cpp b/test/unittest/dfr_test/cpu_sys_config_test/cpu_sys_config_test.cpp index 49fcc74370f..1053b20c708 100644 --- a/test/unittest/dfr_test/cpu_sys_config_test/cpu_sys_config_test.cpp +++ b/test/unittest/dfr_test/cpu_sys_config_test/cpu_sys_config_test.cpp @@ -57,6 +57,8 @@ void CpuSysConfigTest::TearDown(void) */ HWTEST_F(CpuSysConfigTest, GetFreqTimePath_Test_001, TestSize.Level1) { + std::shared_ptr cpuSysConfig = std::make_shared(); + EXPECT_TRUE(cpuSysConfig); int cpu = 0; EXPECT_TRUE(!CpuSysConfig::GetFreqTimePath(cpu).empty()); } @@ -97,7 +99,10 @@ HWTEST_F(CpuSysConfigTest, GetFreqTimePath_Test_003, TestSize.Level1) HWTEST_F(CpuSysConfigTest, GetMainThreadRunningTimePath_Test_001, TestSize.Level1) { int pid = 0; - EXPECT_TRUE(!CpuSysConfig::GetMainThreadRunningTimePath(pid).empty()); + std::string path = CpuSysConfig::GetMainThreadRunningTimePath(pid); + EXPECT_TRUE(!path.empty()); + std::ifstream fin(path); + EXPECT_TRUE(!fin.is_open()); } /** @@ -136,7 +141,10 @@ HWTEST_F(CpuSysConfigTest, GetMainThreadRunningTimePath_Test_003, TestSize.Level HWTEST_F(CpuSysConfigTest, GetProcRunningTimePath_Test_001, TestSize.Level1) { int pid = 0; - EXPECT_TRUE(!CpuSysConfig::GetProcRunningTimePath(pid).empty()); + std::string path = CpuSysConfig::GetProcRunningTimePath(pid); + EXPECT_TRUE(!path.empty()); + std::ifstream fin(path); + EXPECT_TRUE(!fin.is_open()); } /** @@ -175,7 +183,10 @@ HWTEST_F(CpuSysConfigTest, GetProcRunningTimePath_Test_003, TestSize.Level1) HWTEST_F(CpuSysConfigTest, GetMaxCoreDimpsPath_Test_001, TestSize.Level1) { int maxCpuCount = 0; - EXPECT_TRUE(!CpuSysConfig::GetMaxCoreDimpsPath(maxCpuCount).empty()); + std::string path = CpuSysConfig::GetMaxCoreDimpsPath(maxCpuCount); + EXPECT_TRUE(!path.empty()); + std::ifstream fin(path); + EXPECT_TRUE(fin.is_open()); } /** -- Gitee