diff --git a/bundle.json b/bundle.json index a6d9c33f1c3bdc4d04017163d1cb0533944af2c4..662849e8b6d8c38854dc3ea3b2a2a8d704253bef 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 4c96571a37d175924f65a23ba4f728e6a0bd85e4..f35b49877594688bdaa302220b6f01307421ca8d 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 4f0cab5c413c9bc394c0ebd2e2d7a733ecf8199c..21ef49e04ef9a49e2546499054e49bf083fca55d 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 8bb77e1ad7bc90d84418f36f7841c4515fb328e1..deb50d87ba211b90e2f52ea2b34d60c3f5515559 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 7c83fd08f98e657d274e451d35012217fcf85103..29c0452c03f862aa805a7823b89ce8a0951132ef 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 9694e08bc5ed34dfe9de0bdc76e1296a2fed9345..1b6f41d445d403f1ddd8795ac0b4381610cc33ae 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 3c8a43658c1d1cd7a7df72676949584e1cc23588..dfe9ccde54ff077960b579e2dbc8410b3a535414 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 d47502656b17d255318dd7e9011fa2de2a9ebb81..e24a4d02eeeddfb3dd7270e6c5aec762620ea2ed 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 da25a5f8e50f2a00c391e3bce3a43d02774ac7d4..619025347b12f3597373caf9c66aea918def2787 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