From 8242526f0a47e7b1deb723b7a5ec0684ee8f0f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Thu, 17 Jul 2025 20:09:27 +0800 Subject: [PATCH] dump trace when ams timeout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- bundle.json | 1 + services/backup.cfg | 3 ++- services/backup_sa/BUILD.gn | 2 ++ .../backup_sa/include/module_ipc/service.h | 3 +++ .../backup_sa/src/module_ipc/sub_service.cpp | 26 +++++++++++++++++++ .../mock/module_external/bms_adapter_mock.cpp | 1 - tests/unittests/backup_sa/module_ipc/BUILD.gn | 16 ++++++++++++ .../backup_sa/module_ipc/service_test.cpp | 4 +-- .../backup_sa/module_ipc/sub_service_test.cpp | 17 ++++++++++++ 9 files changed, 69 insertions(+), 4 deletions(-) diff --git a/bundle.json b/bundle.json index a6d9c33f1..662849e8b 100644 --- a/bundle.json +++ b/bundle.json @@ -35,6 +35,7 @@ "hitrace", "hilog", "hisysevent", + "hiview", "ipc", "init", "json", diff --git a/services/backup.cfg b/services/backup.cfg index 4c96571a3..f35b49877 100644 --- a/services/backup.cfg +++ b/services/backup.cfg @@ -13,7 +13,8 @@ "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", "ohos.permission.ACCESS_EXT_SYSTEM_ABILITY", "ohos.permission.CONNECT_BACKUP_EXTENSION", - "ohos.permission.READ_MEDIA" + "ohos.permission.READ_MEDIA", + "ohos.permission.READ_HIVIEW_SYSTEM" ], "permission_acls" : ["ohos.permission.ACCESS_EXT_SYSTEM_ABILITY"] } diff --git a/services/backup_sa/BUILD.gn b/services/backup_sa/BUILD.gn index 4f0cab5c4..21ef49e04 100644 --- a/services/backup_sa/BUILD.gn +++ b/services/backup_sa/BUILD.gn @@ -211,7 +211,9 @@ ohos_shared_library("backup_sa") { "common_event_service:cesfwk_innerkits", "hilog:libhilog", "hisysevent:libhisysevent", + "hitrace:hitrace_dump", "hitrace:hitrace_meter", + "hiview:libucollection_client", "init:libbegetutil", "ipc:ipc_core", "jsoncpp:jsoncpp", diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 8bb77e1ad..deb50d87b 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -33,6 +33,7 @@ #include "svc_session_manager.h" #include "system_ability.h" #include "thread_pool.h" +#include "trace_collector_client.h" namespace OHOS::FileManagement::Backup { struct ExtensionMutexInfo { @@ -720,6 +721,7 @@ private: const vector &bundleNames, std::string &oldBackupVersion); void AppStatReportErr(const string &bundleName, const string &func, RadarError err); void SaStatReport(const string &bundleName, const string &func, RadarError err); + void DumpHitrace(); void TotalStart() { if (totalStatistic_ != nullptr) { @@ -789,6 +791,7 @@ private: std::shared_mutex statMapMutex_; std::map> saStatisticMap_; std::map> backupExtOnReleaseMap_; + std::shared_ptr traceCollector_ = nullptr; public: std::map> backupExtMutexMap_; std::map failedBundles_; diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 7c83fd08f..29c0452c0 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -73,6 +73,7 @@ namespace { const int32_t MAX_FILE_READY_REPORT_TIME = 2; const int32_t WAIT_SCANNING_INFO_SEND_TIME = 5; const int ERR_SIZE = -1; +constexpr uint32_t TRACE_PATH_LEN = 50; } // namespace void Service::AppendBundles(const std::vector &bundleNames) @@ -650,11 +651,36 @@ void Service::SaStatReport(const string &bundleName, const string &func, RadarEr saStatistic->ReportSA(func, err); } +void Service::DumpHitrace() +{ + if (traceCollector_ == nullptr) { + traceCollector_ = OHOS::HiviewDFX::UCollectClient::TraceCollector::Create(); + } + if (traceCollector_ != nullptr) { + HILOGI("start DumpSnapshot!"); + auto dumpRet = traceCollector_->DumpSnapshot(); + if (dumpRet.retCode != OHOS::HiviewDFX::UCollect::UcError::SUCCESS) { + HILOGI("DumpSnapshot fail, errorCode = %{public}d", dumpRet.retCode); + } else { + std::string dumpRetString = ""; + for (std::string item : dumpRet.data) { + if (item.size() > TRACE_PATH_LEN) { + dumpRetString += item.substr(TRACE_PATH_LEN) + "; "; + } else { + dumpRetString += item + "; "; + } + } + HILOGI("DumpSnapshot succ, output: %{public}s", dumpRetString.c_str()); + } + } +} + void Service::ExtConnectDied(const string &callName) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); try { HILOGI("Begin, bundleName: %{public}s", callName.c_str()); + DumpHitrace(); std::shared_ptr mutexPtr = GetExtensionMutex(callName); if (mutexPtr == nullptr) { HILOGE("extension mutex ptr is nullptr"); diff --git a/tests/mock/module_external/bms_adapter_mock.cpp b/tests/mock/module_external/bms_adapter_mock.cpp index 9694e08bc..1b6f41d44 100644 --- a/tests/mock/module_external/bms_adapter_mock.cpp +++ b/tests/mock/module_external/bms_adapter_mock.cpp @@ -21,7 +21,6 @@ #include "b_json/b_json_entity_extension_config.h" #include "bundle_mgr_client.h" -#include "module_ipc/service.h" #include "module_ipc/svc_session_manager.h" #include "module_sched/sched_scheduler.h" diff --git a/tests/unittests/backup_sa/module_ipc/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index 3c8a43658..dfe9ccde5 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -53,7 +53,9 @@ ohos_unittest("module_ipc_test") { "googletest:gtest_main", "hilog:libhilog", "hisysevent:libhisysevent", + "hitrace:hitrace_dump", "hitrace:hitrace_meter", + "hiview:libucollection_client", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", @@ -129,7 +131,9 @@ ohos_unittest("backup_service_test") { "googletest:gtest_main", "hilog:libhilog", "hisysevent:libhisysevent", + "hitrace:hitrace_dump", "hitrace:hitrace_meter", + "hiview:libucollection_client", "init:libbegetutil", "ipc:ipc_core", "jsoncpp:jsoncpp", @@ -213,7 +217,9 @@ ohos_unittest("backup_service_throw_test") { "googletest:gtest_main", "hilog:libhilog", "hisysevent:libhisysevent", + "hitrace:hitrace_dump", "hitrace:hitrace_meter", + "hiview:libucollection_client", "init:libbegetutil", "ipc:ipc_core", "jsoncpp:jsoncpp", @@ -283,7 +289,9 @@ ohos_unittest("backup_service_session_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hitrace:hitrace_dump", "hitrace:hitrace_meter", + "hiview:libucollection_client", "init:libbegetutil", "ipc:ipc_core", "jsoncpp:jsoncpp", @@ -340,6 +348,8 @@ ohos_unittest("backup_service_scheduler_test") { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hitrace:hitrace_dump", + "hiview:libucollection_client", "ipc:ipc_core", "jsoncpp:jsoncpp", "safwk:system_ability_fwk", @@ -410,7 +420,9 @@ ohos_unittest("backup_restore_deps_manager_test") { "googletest:gtest_main", "hilog:libhilog", "hisysevent:libhisysevent", + "hitrace:hitrace_dump", "hitrace:hitrace_meter", + "hiview:libucollection_client", "init:libbegetutil", "ipc:ipc_core", "jsoncpp:jsoncpp", @@ -483,7 +495,9 @@ ohos_unittest("backup_service_incremental_test") { "googletest:gtest_main", "hilog:libhilog", "hisysevent:libhisysevent", + "hitrace:hitrace_dump", "hitrace:hitrace_meter", + "hiview:libucollection_client", "init:libbegetutil", "ipc:ipc_core", "jsoncpp:jsoncpp", @@ -565,7 +579,9 @@ ohos_unittest("backup_service_other_test") { "googletest:gtest_main", "hilog:libhilog", "hisysevent:libhisysevent", + "hitrace:hitrace_dump", "hitrace:hitrace_meter", + "hiview:libucollection_client", "init:libbegetutil", "ipc:ipc_core", "jsoncpp:jsoncpp", diff --git a/tests/unittests/backup_sa/module_ipc/service_test.cpp b/tests/unittests/backup_sa/module_ipc/service_test.cpp index d47502656..e24a4d02e 100644 --- a/tests/unittests/backup_sa/module_ipc/service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_test.cpp @@ -1865,13 +1865,13 @@ HWTEST_F(ServiceTest, SUB_Service_AppendBundlesDetailsBackupSession_0100, testin GTEST_LOG_(INFO) << "1. AppendBundlesDetailsBackupSession fail nullptr"; servicePtr_->session_ = nullptr; int ret = servicePtr_->AppendBundlesDetailsBackupSession(bundleNames, bundleInfos); - EXPECT_EQ(ret, 13900020); + EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); GTEST_LOG_(INFO) << "2. AppendBundlesDetailsBackupSession fail false"; servicePtr_->session_ = session_; servicePtr_->isOccupyingSession_.store(true); ret = servicePtr_->AppendBundlesDetailsBackupSession(bundleNames, bundleInfos); - EXPECT_EQ(ret, 13900020); + EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); GTEST_LOG_(INFO) << "3. AppendBundlesDetailsBackupSession succ"; servicePtr_->isOccupyingSession_.store(false); diff --git a/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp b/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp index da25a5f8e..619025347 100644 --- a/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp @@ -2315,4 +2315,21 @@ HWTEST_F(ServiceTest, SUB_Service_GetCompatibilityInfo_0200, testing::ext::TestS GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by GetCompatibilityInfo."; } GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_GetCompatibilityInfo_0200"; +} + +/** + * @tc.number: SUB_Service_DumpHiTrace_0000 + * @tc.name: SUB_Service_DumpHiTrace_0000 + * @tc.desc: 测试 DumpHiTrace 的正常/异常分支 + * @tc.size: TINY + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: NA + */ +HWTEST_F(ServiceTest, SUB_Service_DumpHiTrace_0000, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_DumpHiTrace_0000"; + service->DumpHitrace; + EXPECT_NE(service->traceCollector_, nullptr); + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_DumpHiTrace_0000"; } \ No newline at end of file -- Gitee