From bfd14ca2b06f4c33d97d8e7608b79ea949734bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Fri, 12 Sep 2025 10:05:30 +0800 Subject: [PATCH 1/2] optimize total stat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- .../backup_sa/include/module_ipc/service.h | 41 ++-------- services/backup_sa/src/module_ipc/service.cpp | 23 ++---- .../src/module_ipc/service_incremental.cpp | 18 ++--- .../backup_sa/src/module_ipc/sub_service.cpp | 66 ++++++++++++---- .../module_ipc/service_incremental_test.cpp | 30 +++++++ .../backup_sa/module_ipc/service_test.cpp | 78 ++++++++++++++++--- .../backup_utils/b_radar/b_radar_test.cpp | 3 +- utils/include/b_radar/radar_total_statistic.h | 9 +-- utils/src/b_radar/radar_total_statistic.cpp | 10 +-- 9 files changed, 169 insertions(+), 109 deletions(-) diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index a161014eb..001b83d91 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -401,11 +401,6 @@ private: */ void ClearSessionAndSchedInfo(const std::string &bundleName); - /** - * @brief 上报总体统计打点 - */ - void TotalStatReport(ErrCode errCode); - /** * @brief 整个备份恢复流程结束 * @@ -724,37 +719,10 @@ private: void ClearIncrementalStatFile(int32_t userId, const string &bundleName); BJsonCachedEntity CreateJsonEntity(UniqueFd &fd, vector &bundleInfos, const std::vector &bundleNames); - void TotalStart() - { - if (totalStatistic_ != nullptr) { - totalStatistic_->totalSpendTime_.Start(); - } - } - - void GetBundleInfoStart() - { - if (totalStatistic_ != nullptr) { - totalStatistic_->getBundleInfoSpend_.Start(); - } - } - - void GetBundleInfoEnd() - { - if (totalStatistic_ != nullptr) { - totalStatistic_->getBundleInfoSpend_.End(); - } - } - - void UpdateHandleCnt(ErrCode errCode) - { - if (totalStatistic_ != nullptr) { - if (errCode == ERR_OK) { - totalStatistic_->succBundleCount_.fetch_add(1); - } else { - totalStatistic_->failBundleCount_.fetch_add(1); - } - } - } + void TotalStatStart(BizScene bizScene, std::string caller, uint64_t startTime, Mode mode = Mode::FULL); + void TotalStatEnd(ErrCode errCode); + void UpdateHandleCnt(ErrCode errCode); + void TotalStatReport(); private: static sptr instance_; static std::mutex instanceLock_; @@ -790,6 +758,7 @@ private: std::condition_variable getDataSizeCon_; std::atomic isScannedEnd_ {false}; std::atomic onScanning_ {false}; + std::shared_mutex totalStatMutex_; std::shared_ptr totalStatistic_ = nullptr; std::shared_mutex statMapMutex_; std::map> saStatisticMap_; diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 53a258108..d7405aef4 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -470,11 +470,11 @@ ErrCode Service::VerifyCaller(IServiceReverseType::Scenario scenario) ErrCode Service::InitRestoreSession(const sptr &remote) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - totalStatistic_ = std::make_shared(BizScene::RESTORE, GetCallerName()); + Duration totalSpend; + totalSpend.Start(); ErrCode ret = VerifyCaller(); if (ret != ERR_OK) { HILOGE("Init restore session failed, verify caller failed"); - totalStatistic_->Report("InitRestoreSession", MODULE_INIT, ret); return ret; } ret = session_->Active({ @@ -487,13 +487,13 @@ ErrCode Service::InitRestoreSession(const sptr &remote) }); if (ret == ERR_OK) { HILOGE("Success to init a new restore session"); + TotalStatStart(BizScene::RESTORE, GetCallerName(), totalSpend.startMilli_); ClearFailedBundles(); successBundlesNum_ = 0; ClearBundleRadarReport(); ClearFileReadyRadarReport(); return ret; } - totalStatistic_->Report("InitRestoreSession", MODULE_INIT, ret); if (ret == BError(BError::Codes::SA_SESSION_CONFLICT)) { HILOGE("Active restore session error, Already have a session"); return ret; @@ -506,11 +506,11 @@ ErrCode Service::InitRestoreSession(const sptr &remote) ErrCode Service::InitBackupSession(const sptr &remote) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - totalStatistic_ = std::make_shared(BizScene::BACKUP, GetCallerName()); + Duration totalSpend; + totalSpend.Start(); ErrCode ret = VerifyCaller(); if (ret != ERR_OK) { HILOGE("Init full backup session fail, verify caller failed"); - totalStatistic_->Report("InitBackupSession", MODULE_INIT, ret); return ret; } int32_t oldSize = StorageMgrAdapter::UpdateMemPara(BConstants::BACKUP_VFS_CACHE_PRESSURE); @@ -526,13 +526,13 @@ ErrCode Service::InitBackupSession(const sptr &remote) }); if (ret == ERR_OK) { HILOGE("Success to init a new backup session"); + TotalStatStart(BizScene::BACKUP, GetCallerName(), totalSpend.startMilli_); ClearFailedBundles(); successBundlesNum_ = 0; ClearBundleRadarReport(); ClearFileReadyRadarReport(); return ret; } - totalStatistic_->Report("InitBackupSession", MODULE_INIT, ret); if (ret == BError(BError::Codes::SA_SESSION_CONFLICT)) { HILOGE("Active backup session error, Already have a session"); return ret; @@ -586,9 +586,7 @@ vector Service::GetRestoreBundleNames(UniqueFd fd, const vector &bundleNames, std::string &oldBackupVersion) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - GetBundleInfoStart(); auto restoreInfos = BundleMgrAdapter::GetBundleInfos(bundleNames, session->GetSessionUserId()); - GetBundleInfoEnd(); BJsonCachedEntity cachedEntity(move(fd)); auto cache = cachedEntity.Structuralize(); oldBackupVersion = cache.GetBackupVersion(); @@ -669,7 +667,6 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, int32_t userId) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - TotalStart(); HILOGI("Begin"); try { if (session_ == nullptr || isOccupyingSession_.load()) { @@ -770,7 +767,6 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, int32_t userId) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - TotalStart(); try { if (session_ == nullptr || isOccupyingSession_.load()) { HILOGE("AppendBundles restore session error, session is empty"); @@ -869,7 +865,6 @@ void Service::SetCurrentSessProperties( ErrCode Service::AppendBundlesBackupSession(const vector &bundleNames) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - TotalStart(); try { if (session_ == nullptr || isOccupyingSession_.load()) { HILOGE("AppendBundles backup session error, session is empty"); @@ -884,10 +879,8 @@ ErrCode Service::AppendBundlesBackupSession(const vector &bundleName return ret; } auto bundleDetails = MakeDetailList(bundleNames); - GetBundleInfoStart(); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppendBundles(bundleDetails, session_->GetSessionUserId()); - GetBundleInfoEnd(); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false, bundleNames); AppendBundles(supportBackupNames); SetCurrentBackupSessProperties(supportBackupNames, session_->GetSessionUserId(), backupInfos, false); @@ -911,7 +904,6 @@ ErrCode Service::AppendBundlesDetailsBackupSession(const vector &bun const vector &bundleInfos) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - TotalStart(); try { if (session_ == nullptr || isOccupyingSession_.load()) { HILOGE("AppendBundles backup session with infos error, session is empty"); @@ -931,10 +923,8 @@ ErrCode Service::AppendBundlesDetailsBackupSession(const vector &bun BJsonUtil::BuildBundleInfos(bundleNames, bundleInfos, bundleNamesOnly, session_->GetSessionUserId(), isClearDataFlags); auto bundleDetails = MakeDetailList(bundleNames); - GetBundleInfoStart(); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppendBundles(bundleDetails, session_->GetSessionUserId()); - GetBundleInfoEnd(); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false, bundleNames); AppendBundles(supportBackupNames); HandleCurGroupBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); @@ -1504,6 +1494,7 @@ void Service::SessionDeactive() HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); try { HILOGI("Begin"); + TotalStatReport(); isInRelease_.store(true); //清理处置状态 if (session_ == nullptr) { diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index b3b33ebc0..172187090 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -321,11 +321,11 @@ ErrCode Service::GetAppLocalListAndDoIncrementalBackup() ErrCode Service::InitIncrementalBackupSession(const sptr& remote) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - totalStatistic_ = std::make_shared(BizScene::BACKUP, GetCallerName(), Mode::INCREMENTAL); + Duration totalSpend; + totalSpend.Start(); ErrCode errCode = VerifyCaller(); if (errCode != ERR_OK) { HILOGE("Init incremental backup session fail, Verify caller failed, errCode:%{public}d", errCode); - totalStatistic_->Report("InitIncrementalBackupSession", MODULE_INIT, errCode); return errCode; } if (session_ == nullptr) { @@ -341,13 +341,13 @@ ErrCode Service::InitIncrementalBackupSession(const sptr& remot .activeTime = TimeUtils::GetCurrentTime()}); if (errCode == ERR_OK) { HILOGE("Success to init a new incremental backup session"); + TotalStatStart(BizScene::BACKUP, GetCallerName(), totalSpend.startMilli_, Mode::INCREMENTAL); ClearFailedBundles(); successBundlesNum_ = 0; ClearBundleRadarReport(); ClearFileReadyRadarReport(); return errCode; } - totalStatistic_->Report("InitIncrementalBackupSession", MODULE_INIT, errCode); if (errCode == BError(BError::Codes::SA_SESSION_CONFLICT)) { HILOGE("Active incremental backup error, Already have a session"); return errCode; @@ -370,11 +370,11 @@ ErrCode Service::InitIncrementalBackupSessionWithErrMsg(const sptr& remote, std::string &errMsg) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - totalStatistic_ = std::make_shared(BizScene::BACKUP, GetCallerName(), Mode::INCREMENTAL); + Duration totalSpend; + totalSpend.Start(); ErrCode errCode = VerifyCaller(); if (errCode != ERR_OK) { HILOGE("Init incremental backup session fail, Verify caller failed, errCode:%{public}d", errCode); - totalStatistic_->Report("InitIncrementalBackupSessionWithErrMsg", MODULE_INIT, errCode); return errCode; } if (session_ == nullptr) { @@ -389,13 +389,13 @@ ErrCode Service::InitIncrementalBackupSession(const sptr& remot .callerName = GetCallerName(), .activeTime = TimeUtils::GetCurrentTime()}); if (errCode == ERR_OK) { + TotalStatStart(BizScene::BACKUP, GetCallerName(), totalSpend.startMilli_, Mode::INCREMENTAL); ClearFailedBundles(); successBundlesNum_ = 0; ClearBundleRadarReport(); ClearFileReadyRadarReport(); return errCode; } - totalStatistic_->Report("InitIncrementalBackupSessionWithErrMsg", MODULE_INIT, errCode); if (errCode == BError(BError::Codes::SA_SESSION_CONFLICT)) { errMsg = BJsonUtil::BuildInitSessionErrInfo(session_->GetSessionUserId(), session_->GetSessionCallerName(), @@ -420,7 +420,6 @@ vector Service::GetBundleNameByDetails(const std::vector &bundlesToBackup) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - TotalStart(); vector bundleNames; try { if (session_ == nullptr || isOccupyingSession_.load()) { @@ -436,10 +435,8 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vectorDecreaseSessionCnt(__PRETTY_FUNCTION__); return ret; } - GetBundleInfoStart(); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppendBundles(bundlesToBackup, session_->GetSessionUserId()); - GetBundleInfoEnd(); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true, bundleNames); AppendBundles(supportBackupNames); SetBundleIncDataInfo(bundlesToBackup, supportBackupNames); @@ -470,7 +467,6 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vector &infos) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - TotalStart(); vector bundleNames; try { if (session_ == nullptr || isOccupyingSession_.load()) { @@ -491,10 +487,8 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vector> bundleNameDetailMap = BJsonUtil::BuildBundleInfos(bundleNames, infos, bundleNamesOnly, session_->GetSessionUserId(), isClearDataFlags); - GetBundleInfoStart(); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppendBundles(bundlesToBackup, session_->GetSessionUserId()); - GetBundleInfoEnd(); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true, bundleNames); AppendBundles(supportBackupNames); SetBundleIncDataInfo(bundlesToBackup, supportBackupNames); diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index de1d44c84..18a8c5978 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -758,23 +758,13 @@ void Service::NoticeClientFinish(const string &bundleName, ErrCode errCode) } } -void Service::TotalStatReport(ErrCode errCode) -{ - if (totalStatistic_ == nullptr) { - HILOGE("totalStat is null"); - return; - } - totalStatistic_->totalSpendTime_.End(); - totalStatistic_->Report("OnAllBundlesFinished", errCode); -} - void Service::OnAllBundlesFinished(ErrCode errCode) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); HILOGI("called begin."); if (session_->IsOnAllBundlesFinished()) { IServiceReverseType::Scenario scenario = session_->GetScenario(); - TotalStatReport(errCode); + TotalStatEnd(errCode); if (isInRelease_.load() && (scenario == IServiceReverseType::Scenario::RESTORE)) { HILOGI("Will destory session info"); SessionDeactive(); @@ -942,11 +932,11 @@ ErrCode Service::InitRestoreSessionWithErrMsg(const sptr &remot ErrCode Service::InitRestoreSession(const sptr& remote, std::string &errMsg) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - totalStatistic_ = std::make_shared(BizScene::RESTORE, GetCallerName()); + Duration totalSpend; + totalSpend.Start(); ErrCode ret = VerifyCaller(); if (ret != ERR_OK) { HILOGE("Init restore session failed, verify caller failed"); - totalStatistic_->Report("InitRestoreSession", MODULE_INIT, ret); return ret; } ret = session_->Active({ @@ -958,13 +948,13 @@ ErrCode Service::InitRestoreSession(const sptr& remote, std::st .activeTime = TimeUtils::GetCurrentTime(), }); if (ret == ERR_OK) { + TotalStatStart(BizScene::RESTORE, GetCallerName(), totalSpend.startMilli_); ClearFailedBundles(); successBundlesNum_ = 0; ClearBundleRadarReport(); ClearFileReadyRadarReport(); return ret; } - totalStatistic_->Report("InitRestoreSession", MODULE_INIT, ret); if (ret == BError(BError::Codes::SA_SESSION_CONFLICT)) { errMsg = BJsonUtil::BuildInitSessionErrInfo(session_->GetSessionUserId(), session_->GetSessionCallerName(), @@ -990,11 +980,11 @@ ErrCode Service::InitBackupSessionWithErrMsg(const sptr& remote ErrCode Service::InitBackupSession(const sptr& remote, std::string &errMsg) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - totalStatistic_ = std::make_shared(BizScene::BACKUP, GetCallerName()); + Duration totalSpend; + totalSpend.Start(); ErrCode ret = VerifyCaller(); if (ret != ERR_OK) { HILOGE("Init full backup session fail, verify caller failed"); - totalStatistic_->Report("InitBackupSessionWithErrMsg", MODULE_INIT, ret); return ret; } int32_t oldSize = StorageMgrAdapter::UpdateMemPara(BConstants::BACKUP_VFS_CACHE_PRESSURE); @@ -1009,13 +999,13 @@ ErrCode Service::InitBackupSession(const sptr& remote, std::str .activeTime = TimeUtils::GetCurrentTime(), }); if (ret == ERR_OK) { + TotalStatStart(BizScene::BACKUP, GetCallerName(), totalSpend.startMilli_, Mode::INCREMENTAL); ClearFailedBundles(); successBundlesNum_ = 0; ClearBundleRadarReport(); ClearFileReadyRadarReport(); return ret; } - totalStatistic_->Report("InitBackupSessionWithErrMsg", MODULE_INIT, ret); if (ret == BError(BError::Codes::SA_SESSION_CONFLICT)) { errMsg = BJsonUtil::BuildInitSessionErrInfo(session_->GetSessionUserId(), session_->GetSessionCallerName(), @@ -1609,4 +1599,46 @@ void Service::DoNoticeClientFinish(const std::string &bundleName, ErrCode errCod NoticeClientFinish(bundleName, errCode); } } + +void Service::TotalStatStart(BizScene bizScene, std::string caller, uint64_t startTime, Mode mode) +{ + std::unique_lock lock(totalStatMutex_); + totalStatistic_ = std::make_shared(bizScene, caller, mode); + totalStatistic_->totalSpendTime_.startMilli_ = startTime; +} + +void Service::TotalStatEnd(ErrCode errCode) +{ + std::unique_lock lock(totalStatMutex_); + if (totalStatistic_ != nullptr) { + totalStatistic_->totalSpendTime_.End(); + if (errCode != ERROR_OK) { + totalStatistic_->innerErr_ = errCode; + } + } +} + +void Service::UpdateHandleCnt(ErrCode errCode) +{ + std::unique_lock lock(totalStatMutex_); + if (totalStatistic_ != nullptr) { + if (errCode == ERROR_OK) { + totalStatistic_->succBundleCount_++; + } else { + totalStatistic_->failBundleCount_++; + } + } +} + +void Service::TotalStatReport() +{ + std::shared_lock lock(totalStatMutex_); + if (totalStatistic_ == nullptr) { + HILOGE("totalStat is null"); + return; + } + if (totalStatistic_->succBundleCount_ > 0 || totalStatistic_->failBundleCount_ > 0) { + totalStatistic_->Report("OnAllBundlesFinished", totalStatistic_->innerErr_); + } +} } \ No newline at end of file diff --git a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp index 9111b9b55..f1e9c3a0e 100644 --- a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp @@ -488,6 +488,36 @@ ErrCode Service::GetCompatibilityInfo(const std::string &bundleName, const std:: { return BService::serviceMock->GetCompatibilityInfo(bundleName, extInfo, compatInfo); } + +void Service::TotalStatStart(BizScene bizScene, std::string caller, uint64_t startTime, Mode mode) +{ + std::unique_lock lock(totalStatMutex_); + totalStatistic_ = std::make_shared(bizScene, caller, mode); + totalStatistic_->totalSpendTime_.startMilli_ = startTime; +} + +void Service::TotalStatEnd(ErrCode errCode) +{ + std::unique_lock lock(totalStatMutex_); + if (totalStatistic_ != nullptr) { + totalStatistic_->totalSpendTime_.End(); + if (errCode != ERROR_OK) { + totalStatistic_->innerErr_ = errCode; + } + } +} + +void Service::UpdateHandleCnt(ErrCode errCode) +{ + std::unique_lock lock(totalStatMutex_); + if (totalStatistic_ != nullptr) { + if (errCode == ERROR_OK) { + totalStatistic_->succBundleCount_++; + } else { + totalStatistic_->failBundleCount_++; + } + } +} } // namespace OHOS::FileManagement::Backup namespace OHOS::FileManagement::Backup { diff --git a/tests/unittests/backup_sa/module_ipc/service_test.cpp b/tests/unittests/backup_sa/module_ipc/service_test.cpp index faeb4852c..1057e0070 100644 --- a/tests/unittests/backup_sa/module_ipc/service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_test.cpp @@ -2573,26 +2573,80 @@ HWTEST_F(ServiceTest, SUB_Service_AppFileReady_0104, testing::ext::TestSize.Leve GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_AppFileReady_0104"; } -HWTEST_F(ServiceTest, Service_Total_Start, testing::ext::TestSize.Level1) +/** + * @tc.number: Service_Total_Start_Test + * @tc.name: Service_Total_Start_Test + * @tc.desc: 测试 TotalStatStart 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ServiceTest, Service_Total_Start_Test, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin Service_Total_Start_Test"; + servicePtr_->TotalStatStart(BizScene::BACKUP, BUNDLE_NAME, 1); + EXPECT_GT(servicePtr_->totalStatistic_->totalSpendTime_.startMilli_, 0); + GTEST_LOG_(INFO) << "ServiceTest-end Service_Total_Start_Test"; +} + +/** + * @tc.number: Service_Total_Stat_Report_Test + * @tc.name: Service_Total_Stat_Report_Test + * @tc.desc: 测试 TotalStatReport 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ServiceTest, Service_Total_Stat_Report_Test, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "ServiceTest-begin Service_Total_Start"; + GTEST_LOG_(INFO) << "ServiceTest-begin Service_Total_Stat_Report_Test"; servicePtr_->totalStatistic_ = nullptr; - servicePtr_->TotalStart(); + servicePtr_->TotalStatReport(); servicePtr_->totalStatistic_ = totalStat_; - servicePtr_->TotalStart(); - EXPECT_GT(totalStat_->totalSpendTime_.startMilli_, 0); - GTEST_LOG_(INFO) << "ServiceTest-end Service_Total_Start"; + servicePtr_->TotalStatReport(); + EXPECT_GT(totalStat_->uniqId_, 0); + GTEST_LOG_(INFO) << "ServiceTest-end Service_Total_Stat_Report_Test"; } -HWTEST_F(ServiceTest, Service_Total_Stat_Report, testing::ext::TestSize.Level1) +/** + * @tc.number: Service_Total_Stat_End_Test + * @tc.name: Service_Total_Stat_End_Test + * @tc.desc: 测试 TotalStatEnd 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ServiceTest, Service_Total_Stat_End_Test, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "ServiceTest-begin Service_Total_Stat_Report"; + GTEST_LOG_(INFO) << "ServiceTest-begin Service_Total_Stat_End_Test"; servicePtr_->totalStatistic_ = nullptr; - servicePtr_->TotalStatReport(ERR_OK); + servicePtr_->TotalStatEnd(0); servicePtr_->totalStatistic_ = totalStat_; - servicePtr_->TotalStatReport(ERR_OK); - EXPECT_GT(totalStat_->totalSpendTime_.endMilli_, 0); - GTEST_LOG_(INFO) << "ServiceTest-end Service_Total_Stat_Report"; + servicePtr_->TotalStatEnd(0); + EXPECT_EQ(totalStat_->innerErr_, 0); + servicePtr_->TotalStatEnd(1); + EXPECT_EQ(totalStat_->innerErr_, 1); + GTEST_LOG_(INFO) << "ServiceTest-end Service_Total_Stat_End_Test"; } +/** + * @tc.number: Service_Update_Handle_Count_Test + * @tc.name: Service_Update_Handle_Count_Test + * @tc.desc: 测试 UpdateHandleCnt 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ServiceTest, Service_Update_Handle_Count_Test, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin Service_Update_Handle_Count_Test"; + servicePtr_->totalStatistic_ = nullptr; + servicePtr_->UpdateHandleCnt(0); + servicePtr_->totalStatistic_ = totalStat_; + servicePtr_->UpdateHandleCnt(0); + EXPECT_EQ(totalStat_->succBundleCount_, 1); + servicePtr_->UpdateHandleCnt(1); + EXPECT_EQ(totalStat_->failBundleCount_, 1); + GTEST_LOG_(INFO) << "ServiceTest-end Service_Update_Handle_Count_Test"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_utils/b_radar/b_radar_test.cpp b/tests/unittests/backup_utils/b_radar/b_radar_test.cpp index 17947b793..4156cdc8d 100644 --- a/tests/unittests/backup_utils/b_radar/b_radar_test.cpp +++ b/tests/unittests/backup_utils/b_radar/b_radar_test.cpp @@ -104,11 +104,10 @@ HWTEST_F(BRadarTest, TOTAL_CONSTRUCTOR_0100, testing::ext::TestSize.Level1) EXPECT_EQ(totalStatistic1.hostPkg_, caller); EXPECT_GT(totalStatistic1.uniqId_, 0); EXPECT_EQ(totalStatistic1.uniqId_, totalStatistic1.GetUniqId()); - int64_t uniqId1 = totalStatistic1.uniqId_; RadarTotalStatistic totalStatistic2(BizScene::RESTORE, caller); EXPECT_EQ(totalStatistic2.bizScene_, BizScene::RESTORE); EXPECT_EQ(totalStatistic2.mode_, Mode::FULL); - EXPECT_NE(uniqId1, totalStatistic2.uniqId_); + EXPECT_GT(totalStatistic2.uniqId_, 0); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "BRadarTest-an exception occurred."; diff --git a/utils/include/b_radar/radar_total_statistic.h b/utils/include/b_radar/radar_total_statistic.h index 22d4824d7..9f0ce3138 100644 --- a/utils/include/b_radar/radar_total_statistic.h +++ b/utils/include/b_radar/radar_total_statistic.h @@ -25,10 +25,10 @@ namespace OHOS::FileManagement::Backup { class RadarTotalStatistic { public: - std::atomic succBundleCount_ = 0; - std::atomic failBundleCount_ = 0; - Duration getBundleInfoSpend_ = {0, 0}; + uint32_t succBundleCount_ = 0; + uint32_t failBundleCount_ = 0; Duration totalSpendTime_ = {0, 0}; + int innerErr_ = 0; RadarTotalStatistic(BizScene bizScene, std::string caller, Mode mode = Mode::FULL); RadarTotalStatistic(const RadarTotalStatistic &) = delete; @@ -48,9 +48,6 @@ private: std::string hostPkg_ = ""; Mode mode_ = Mode::FULL; int64_t uniqId_ = 0; - std::mutex lastCntMutex_; - uint32_t lastSuccCnt_ = 0; - uint32_t lastFailCnt_ = 0; }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_RADAR_TOTAL_STATISTIC_H diff --git a/utils/src/b_radar/radar_total_statistic.cpp b/utils/src/b_radar/radar_total_statistic.cpp index 87035f4a5..d89db4c1c 100644 --- a/utils/src/b_radar/radar_total_statistic.cpp +++ b/utils/src/b_radar/radar_total_statistic.cpp @@ -29,17 +29,13 @@ RadarTotalStatistic::RadarTotalStatistic(BizScene bizScene, std::string callerNa void RadarTotalStatistic::Report(const std::string &func, int32_t error, std::string errMsg) { - std::lock_guard lastCntLock(lastCntMutex_); - uint32_t succCount = succBundleCount_.load(); - uint32_t failCount = failBundleCount_.load(); HiSysEventWrite( DOMAIN, BACKUP_RESTORE_STATISTIC, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, - GET_BUNDLE_INFO_SPEND, getBundleInfoSpend_.GetSpan(), TOTAL_SPEND, totalSpendTime_.GetSpan(), - SUCC_BUNDLE_CNT, succCount - lastSuccCnt_, - FAIL_BUNDLE_CNT, failCount - lastFailCnt_, + SUCC_BUNDLE_CNT, succBundleCount_, + FAIL_BUNDLE_CNT, failBundleCount_, ORG_PKG, DOMAIN_NAME, FUNC, func, CONCURRENT_ID, uniqId_, @@ -50,8 +46,6 @@ void RadarTotalStatistic::Report(const std::string &func, int32_t error, std::st ERROR_CODE, error, BIZ_STAGE, DEFAULT_STAGE, STAGE_RES, error == 0 ? STAGE_RES_SUCCESS : STAGE_RES_FAIL); - lastSuccCnt_ = succCount; - lastFailCnt_ = failCount; } void RadarTotalStatistic::Report(const std::string &func, uint32_t moduleId, uint32_t moduleErr) -- Gitee From d52ae3a8ec37f68a021887ffa020f5b60de7240c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B8=8A?= Date: Fri, 12 Sep 2025 10:39:06 +0800 Subject: [PATCH 2/2] fix code sytle problem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘渊 --- services/backup_sa/src/module_ipc/service.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index d7405aef4..dec015f50 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -1542,7 +1542,6 @@ void Service::SessionDeactive() } } catch (...) { HILOGE("Unexpected exception"); - return; } } -- Gitee