From ff436f7ccacf8678c2254aaca52f35f65cf60563 Mon Sep 17 00:00:00 2001 From: zhangyaomaggie Date: Fri, 23 Aug 2024 20:05:32 +0800 Subject: [PATCH 1/2] add radar v1 Signed-off-by: zhangyaomaggie --- .../native/backup_ext/src/ext_extension.cpp | 42 ++++ .../backup_ext/src/sub_ext_extension.cpp | 61 +++++ .../src/b_incremental_backup_session.cpp | 25 +- .../src/b_incremental_restore_session.cpp | 4 + .../b_incremental_session_restore_async.cpp | 4 + .../backup_kit_inner/src/b_session_backup.cpp | 25 +- .../src/b_session_restore.cpp | 3 + .../src/b_session_restore_async.cpp | 4 + .../backup_kit_inner/src/service_proxy.cpp | 14 ++ .../native/backup_kit_inner/BUILD.gn | 1 + .../backup_sa/include/module_ipc/service.h | 3 + .../app_gallery_dispose_proxy.cpp | 31 +++ services/backup_sa/src/module_ipc/service.cpp | 91 +++++++ .../src/module_ipc/service_incremental.cpp | 6 + .../src/module_ipc/svc_session_manager.cpp | 11 + utils/BUILD.gn | 3 + utils/include/b_radar/b_radar.h | 105 ++++++++ utils/src/b_radar/b_radar.cpp | 228 ++++++++++++++++++ 18 files changed, 657 insertions(+), 4 deletions(-) create mode 100644 utils/include/b_radar/b_radar.h create mode 100644 utils/src/b_radar/b_radar.cpp diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 2d836e2b9..f00b12abf 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -47,6 +47,7 @@ #include "b_json/b_json_cached_entity.h" #include "b_jsonutil/b_jsonutil.h" #include "b_ohos/startup/backup_para.h" +#include "b_radar/b_radar.h" #include "b_tarball/b_tarball_factory.h" #include "filemgmt_libhilog.h" #include "hitrace_meter.h" @@ -126,10 +127,18 @@ void BackupExtExtension::VerifyCaller() uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID(); int tokenType = Security::AccessToken::AccessTokenKit::GetTokenType(tokenCaller); if (tokenType != Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) { + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("BackupExtExtension::VerifyCaller", + AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), + "{\"reason\":\"Calling tokenType error\"}"); throw BError(BError::Codes::EXT_BROKEN_IPC, string("Calling tokenType is error, token type is ").append(to_string(tokenType))); } if (IPCSkeleton::GetCallingUid() != BConstants::BACKUP_UID) { + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("BackupExtExtension::VerifyCaller", + AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), + "{\"reason\":\"Calling uid invalid\"}"); throw BError(BError::Codes::EXT_BROKEN_IPC, string("Calling uid is invalid, calling uid is ").append(to_string(IPCSkeleton::GetCallingUid()))); } @@ -826,7 +835,13 @@ void BackupExtExtension::AsyncTaskBackup(const string config) HILOGI("Do backup, start fwk timer end."); BJsonCachedEntity cachedEntity(config); auto cache = cachedEntity.Structuralize(); + auto start = std::chrono::system_clock::now(); auto ret = ptr->DoBackup(cache); + auto end = std::chrono::system_clock::now(); + auto exportDuration = std::chrono::duration_cast(end - start); + Backup::AppRadar::GetInstance().RecordDoBackUpRes("BackupExtExtension::AsyncTaskBackup", + AppRadar::GetInstance().GetUserId(), static_cast(ret), + static_cast(exportDuration.count())); // REM: 处理返回结果 ret ptr->AppDone(ret); HILOGI("backup app done %{public}d", ret); @@ -1150,6 +1165,16 @@ void BackupExtExtension::HandleSpecialVersionRestore() } } +void RecordDoRestoreRes(const std::string &bundleName, const std::string &func, int32_t ms, int32_t fileNum, + int32_t fileSize) +{ + std::stringstream ss; + ss << "\"file_count\": \"" << fileNum << "\", \"file_size\": \"" << fileSize + << "\", \"spend_time\": \"" << ms << "ms\""; + AppRadar::GetInstance().RecordRestoreFuncResWithStatus(bundleName, AppRadar::GetInstance().GetUserId(), + func, BizStageRestore::BIZ_STAGE_DO_RESTORE, ss.str(), static_cast(BError::Codes::OK)); +} + void BackupExtExtension::AsyncTaskRestore(std::set fileSet, const std::vector extManageInfo) { @@ -1159,6 +1184,7 @@ void BackupExtExtension::AsyncTaskRestore(std::set fileSet, BExcepUltils::BAssert(ptr->extension_, BError::Codes::EXT_INVAL_ARG, "Extension handle have been released"); try { int ret = ERR_OK; + auto startTime = std::chrono::system_clock::now(); if (ptr->extension_->SpecialVersionForCloneAndCloud()) { ptr->HandleSpecialVersionRestore(); return; @@ -1176,6 +1202,11 @@ void BackupExtExtension::AsyncTaskRestore(std::set fileSet, ptr->extension_->UseFullBackupOnly() && !ptr->extension_->SpecialVersionForCloneAndCloud(); ptr->RestoreBigFiles(appendTargetPath); ptr->DeleteBackupTars(); + + auto endTime = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(endTime-startTime).count(); + RecordDoRestoreRes("bundlename", "AsyncTaskRestore", static_cast(ms), + static_cast(fileSet.size()), 0); if (ret == ERR_OK) { ptr->AsyncTaskRestoreForUpgrade(); } else { @@ -1215,6 +1246,9 @@ void BackupExtExtension::AsyncTaskIncrementalRestore() ptr->CheckTmpDirFileInfos(); } // 解压 + auto startTime = std::chrono::system_clock::now(); + int32_t fileSize = 0; + int32_t fileNum = 0; int ret = ptr->DoIncrementalRestore(); // 恢复用户tar包以及大文件 // 目的地址是否需要拼接path(临时目录),FullBackupOnly为true并且非特殊场景 @@ -1224,6 +1258,10 @@ void BackupExtExtension::AsyncTaskIncrementalRestore() // delete 1.tar/manage.json ptr->DeleteBackupIncrementalTars(); + auto endTime = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(endTime-startTime).count(); + RecordDoRestoreRes("bundlename", "AsyncTaskIncrementalRestore", + static_cast(ms), fileNum, fileSize); if (ptr != nullptr && ptr->isDebug_) { ptr->CheckRestoreFileInfos(); } @@ -1317,6 +1355,7 @@ void BackupExtExtension::AsyncTaskRestoreForUpgrade() ptr->StartOnProcessTaskThread(obj, BackupRestoreScenario::FULL_RESTORE); auto callBackup = ptr->OnRestoreCallback(obj); auto callBackupEx = ptr->OnRestoreExCallback(obj); + g_onRestoreStart = std::chrono::system_clock::now(); ErrCode err = ptr->extension_->OnRestore(callBackup, callBackupEx); if (err != ERR_OK) { ptr->AppDone(BError::GetCodeByErrno(err)); @@ -1367,6 +1406,7 @@ void BackupExtExtension::AsyncTaskIncrementalRestoreForUpgrade() ptr->StartOnProcessTaskThread(obj, BackupRestoreScenario::INCREMENTAL_RESTORE); auto callBackup = ptr->IncreOnRestoreCallback(obj); auto callBackupEx = ptr->IncreOnRestoreExCallback(obj); + g_onRestoreStart = std::chrono::system_clock::now(); ErrCode err = ptr->extension_->OnRestore(callBackup, callBackupEx); if (err != ERR_OK) { HILOGE("OnRestore done, err = %{pubilc}d", err); @@ -1494,6 +1534,7 @@ void BackupExtExtension::AsyncTaskOnBackup() ptr->StartOnProcessTaskThread(obj, BackupRestoreScenario::FULL_BACKUP); auto callBackup = ptr->OnBackupCallback(obj); auto callBackupEx = ptr->OnBackupExCallback(obj); + g_onBackUpStart = std::chrono::system_clock::now(); ErrCode err = ptr->extension_->OnBackup(callBackup, callBackupEx); if (err != ERR_OK) { HILOGE("OnBackup done, err = %{pubilc}d", err); @@ -1879,6 +1920,7 @@ void BackupExtExtension::AsyncTaskOnIncrementalBackup() ptr->StartOnProcessTaskThread(obj, BackupRestoreScenario::INCREMENTAL_BACKUP); auto callBackup = ptr->IncOnBackupCallback(obj); auto callBackupEx = ptr->IncOnBackupExCallback(obj); + g_onBackUpStart = std::chrono::system_clock::now(); ErrCode err = ptr->extension_->OnBackup(callBackup, callBackupEx); if (err != ERR_OK) { HILOGE("OnBackup done, err = %{pubilc}d", err); diff --git a/frameworks/native/backup_ext/src/sub_ext_extension.cpp b/frameworks/native/backup_ext/src/sub_ext_extension.cpp index 55ed437d3..b4f63b222 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -47,6 +47,7 @@ #include "b_json/b_json_cached_entity.h" #include "b_jsonutil/b_jsonutil.h" #include "b_ohos/startup/backup_para.h" +#include "b_radar/b_radar.h" #include "b_tarball/b_tarball_factory.h" #include "filemgmt_libhilog.h" #include "hitrace_meter.h" @@ -249,6 +250,14 @@ std::function BackupExtExtension::OnRestoreCallback( return; } HILOGI("Current bundle will execute app done"); + if (errCode == ERR_OK) { + auto endTime = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(endTime - g_onRestoreStart).count(); + std::stringstream ss; + ss << "\"spend_time\": \"" << ms << "ms\""; + AppRadar::GetInstance().RecordOnRestoreRes("bundlename", AppRadar::GetInstance().GetUserId(), + "BackupExtExtension::OnRestoreCallback", ss.str(), ERR_OK); + } extensionPtr->FinishOnProcessTask(); if (errMsg.empty()) { extensionPtr->AppDone(errCode); @@ -274,6 +283,14 @@ std::function BackupExtExtension::OnRestoreExCallbac HILOGE("Extension handle have been released"); return; } + if (errCode == ERR_OK && !restoreRetInfo.empty()) { + auto endTime = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(endTime - g_onRestoreStart).count(); + std::stringstream ss; + ss << "\"spend_time\": \"" << ms << "ms\""; + AppRadar::GetInstance().RecordOnRestoreRes("bundlename", AppRadar::GetInstance().GetUserId(), + "OnRestoreExCallback", ss.str(), ERR_OK); + } extensionPtr->FinishOnProcessTask(); extensionPtr->extension_->InvokeAppExtMethod(errCode, restoreRetInfo); if (errCode == ERR_OK) { @@ -324,6 +341,14 @@ std::function BackupExtExtension::IncreOnRestoreExCa HILOGE("Extension handle have been released"); return; } + if (errCode == ERR_OK && !restoreRetInfo.empty()) { + auto endTime = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(endTime - g_onRestoreStart).count(); + std::stringstream ss; + ss << "\"spend_time\": \"" << ms << "ms\""; + AppRadar::GetInstance().RecordOnRestoreRes("bundlename", AppRadar::GetInstance().GetUserId(), + "IncreOnRestoreExCallback", ss.str(), ERR_OK); + } extensionPtr->FinishOnProcessTask(); extensionPtr->extension_->InvokeAppExtMethod(errCode, restoreRetInfo); if (errCode == ERR_OK) { @@ -353,6 +378,14 @@ std::function BackupExtExtension::IncreOnRestoreCall return; } HILOGI("Current bundle will execute app done"); + if (errCode == ERR_OK) { + auto endTime = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(endTime - g_onRestoreStart).count(); + std::stringstream ss; + ss << "\"spend_time\": \"" << ms << "ms\""; + AppRadar::GetInstance().RecordOnRestoreRes("bundlename", AppRadar::GetInstance().GetUserId(), + "IncreOnRestoreCallback", ss.str(), ERR_OK); + } extensionPtr->FinishOnProcessTask(); if (errMsg.empty()) { extensionPtr->AppIncrementalDone(errCode); @@ -379,6 +412,13 @@ std::function BackupExtExtension::OnBackupCall HILOGE("Extension handle have been released"); return; } + if (errCode == ERR_OK) { + auto onBackUpEnd = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(onBackUpEnd - g_onBackUpStart).count(); + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::OnBackupCallback", + "bundleName", AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_ON_BACKUP, 0, + string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); + } extensionPtr->FinishOnProcessTask(); extensionPtr->AsyncTaskBackup(extensionPtr->extension_->GetUsrConfig()); }; @@ -398,6 +438,13 @@ std::function BackupExtExtension::OnBackupExCa HILOGE("Extension handle have been released"); return; } + if (errCode == ERR_OK && !backupExRetInfo.empty()) { + auto onBackUpEnd = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(onBackUpEnd - g_onBackUpStart).count(); + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::OnBackupExCallback", + "bundleName", AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_ON_BACKUP, 0, + string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); + } extensionPtr->extension_->InvokeAppExtMethod(errCode, backupExRetInfo); if (backupExRetInfo.size()) { HILOGI("Will notify backup result report"); @@ -423,6 +470,13 @@ std::function BackupExtExtension::IncOnBackupC return; } HILOGI("Start GetAppLocalListAndDoIncrementalBackup"); + if (errCode == ERR_OK) { + auto onBackUpEnd = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(onBackUpEnd - g_onBackUpStart).count(); + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::IncOnBackupCallback", + "bundleName", AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_ON_BACKUP, 0, + string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); + } extPtr->FinishOnProcessTask(); proxy->GetAppLocalListAndDoIncrementalBackup(); }; @@ -445,6 +499,13 @@ std::function BackupExtExtension::IncOnBackupE HILOGE("Extension handle have been released"); return; } + if (errCode == ERR_OK && !backupExRetInfo.empty()) { + auto onBackUpEnd = std::chrono::system_clock::now(); + auto ms = std::chrono::duration_cast(onBackUpEnd - g_onBackUpStart).count(); + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::IncOnBackupExCallback", + "bundleName", AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_ON_BACKUP, 0, + string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); + } extensionPtr->extension_->InvokeAppExtMethod(errCode, backupExRetInfo); if (backupExRetInfo.size()) { HILOGI("Start GetAppLocalListAndDoIncrementalBackup"); diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp index f747d28c7..ade0f0864 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp @@ -16,6 +16,7 @@ #include "b_incremental_backup_session.h" #include "b_error/b_error.h" +#include "b_radar/b_radar.h" #include "filemgmt_libhilog.h" #include "service_proxy.h" #include "service_reverse.h" @@ -55,6 +56,8 @@ unique_ptr BIncrementalBackupSession::Init(Callbacks int32_t res = proxy->InitIncrementalBackupSession(sptr(new ServiceReverse(callbacks))); if (res != 0) { HILOGE("Failed to Backup because of %{public}d", res); + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("BIncrementalBackupSession::Init", + AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_CREATE_SESSION_BACKUP, res); return nullptr; } @@ -93,7 +96,16 @@ ErrCode BIncrementalBackupSession::AppendBundles(vector bundle return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - return proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup); + int32_t res = proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup); + if (res != 0) { + std::string ss; + for (const auto &bundle:bundlesToBackup) { + ss += bundle.bundleName + ", "; + } + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BIncrementalBackupSession::AppendBundles", + ss.c_str(), AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_APPEND_BUNDLES, res); + } + return res; } ErrCode BIncrementalBackupSession::AppendBundles(vector bundlesToBackup, @@ -104,7 +116,16 @@ ErrCode BIncrementalBackupSession::AppendBundles(vector bundle return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - return proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup, infos); + int32_t res = proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup, infos); + if (res != 0) { + std::string ss; + for (const auto &bundle:bundlesToBackup) { + ss += bundle.bundleName + ", "; + } + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BIncrementalBackupSession::AppendBundles", + ss.c_str(), AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_APPEND_BUNDLES, res); + } + return res; } ErrCode BIncrementalBackupSession::Release() diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp index 000241ad0..b2a7041f5 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp @@ -16,6 +16,7 @@ #include "b_incremental_restore_session.h" #include "b_error/b_error.h" +#include "b_radar/b_radar.h" #include "filemgmt_libhilog.h" #include "service_proxy.h" #include "service_reverse.h" @@ -54,6 +55,9 @@ unique_ptr BIncrementalRestoreSession::Init(Callback int32_t res = proxy->InitRestoreSession(sptr(new ServiceReverse(callbacks))); if (res != 0) { HILOGE("Failed to Restore because of %{public}d", res); + AppRadar::GetInstance().RecordRestoreFuncRes(AppRadar::GetInstance().GetUserId(), + "BIncrementalRestoreSession::Init", + BizStageRestore::BIZ_STAGE_CREATE_SESSION_RESTORE, res); return nullptr; } diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp index 84f75d66e..f6008298e 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp @@ -16,6 +16,7 @@ #include "b_incremental_session_restore_async.h" #include "b_error/b_error.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "filemgmt_libhilog.h" #include "service_proxy.h" @@ -62,6 +63,9 @@ shared_ptr BIncrementalSessionRestoreAsync::Ini int32_t res = proxy->InitRestoreSession(sptr(new ServiceReverse(callbacksTmp))); if (res != 0) { HILOGE("Failed to Restore because of %{public}d", res); + AppRadar::GetInstance().RecordRestoreFuncRes(AppRadar::GetInstance().GetUserId(), + "BIncrementalSessionRestoreAsync::Init", + BizStageRestore::BIZ_STAGE_CREATE_SESSION_RESTORE, res); return nullptr; } diff --git a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp index 04176686b..b0f55c78b 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp @@ -16,6 +16,7 @@ #include "b_session_backup.h" #include "b_error/b_error.h" +#include "b_radar/b_radar.h" #include "filemgmt_libhilog.h" #include "service_proxy.h" #include "service_reverse.h" @@ -55,6 +56,8 @@ unique_ptr BSessionBackup::Init(Callbacks callbacks) int32_t res = proxy->InitBackupSession(sptr(new ServiceReverse(callbacks))); if (res != 0) { HILOGE("Failed to Backup because of %{public}d", res); + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("BSessionBackup::Init", + AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_CREATE_SESSION_BACKUP, res); return nullptr; } @@ -103,7 +106,16 @@ ErrCode BSessionBackup::AppendBundles(vector bundlesToBackup) return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - return proxy->AppendBundlesBackupSession(bundlesToBackup); + int32_t res = proxy->AppendBundlesBackupSession(bundlesToBackup); + if (res != 0) { + std::string ss; + for (const auto &bundleName:bundlesToBackup) { + ss += bundleName + ", "; + } + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BSessionBackup::AppendBundles", + ss.c_str(), AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_APPEND_BUNDLES, res); + } + return res; } ErrCode BSessionBackup::AppendBundles(vector bundlesToBackup, vector detailInfos) @@ -113,7 +125,16 @@ ErrCode BSessionBackup::AppendBundles(vector bundlesToBackup, vector return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); } - return proxy->AppendBundlesDetailsBackupSession(bundlesToBackup, detailInfos); + int32_t res = proxy->AppendBundlesDetailsBackupSession(bundlesToBackup, detailInfos); + if (res != 0) { + std::string ss; + for (const auto &bundleName:bundlesToBackup) { + ss += bundleName + ", "; + } + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BSessionBackup::AppendBundles", + ss.c_str(), AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_APPEND_BUNDLES, res); + } + return res; } ErrCode BSessionBackup::Finish() diff --git a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp index 250d68bc7..ec2c95bb7 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp @@ -16,6 +16,7 @@ #include "b_session_restore.h" #include "b_error/b_error.h" +#include "b_radar/b_radar.h" #include "filemgmt_libhilog.h" #include "service_proxy.h" #include "service_reverse.h" @@ -54,6 +55,8 @@ unique_ptr BSessionRestore::Init(Callbacks callbacks) int32_t res = proxy->InitRestoreSession(new ServiceReverse(callbacks)); if (res != 0) { HILOGE("Failed to Restore because of %{public}d", res); + AppRadar::GetInstance().RecordRestoreFuncRes(AppRadar::GetInstance().GetUserId(), "BSessionRestore::Init", + BizStageRestore::BIZ_STAGE_CREATE_SESSION_RESTORE, res); return nullptr; } diff --git a/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp b/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp index 0e86b3df7..52177d113 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp @@ -16,6 +16,7 @@ #include "b_session_restore_async.h" #include "b_error/b_error.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "b_session_restore.h" #include "filemgmt_libhilog.h" @@ -63,6 +64,9 @@ shared_ptr BSessionRestoreAsync::Init(Callbacks callbacks) int32_t res = proxy->InitRestoreSession(sptr(new ServiceReverse(callbacksTmp))); if (res != 0) { HILOGE("Failed to Restore because of %{public}d", res); + AppRadar::GetInstance().RecordRestoreFuncRes(AppRadar::GetInstance().GetUserId(), + "BSessionRestoreAsync::Init", + BizStageRestore::BIZ_STAGE_CREATE_SESSION_RESTORE, res); return nullptr; } diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index 1870448e3..cf818b94e 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -15,11 +15,14 @@ #include "service_proxy.h" +#include + #include "iservice_registry.h" #include "system_ability_definition.h" #include "b_error/b_error.h" #include "b_error/b_excep_utils.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "filemgmt_libhilog.h" #include "svc_death_recipient.h" @@ -446,6 +449,9 @@ sptr ServiceProxy::GetInstance() [loadCallback]() { return loadCallback->isLoadSuccess_.load(); }); if (!waitStatus) { HILOGE("Load backup sa timeout"); + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("ServiceProxy::GetInstance", + AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_BACKUP_SA, + static_cast(BError::Codes::SA_INVAL_ARG), "{\"reason\":\"Load backup sa timeout\"}"); return nullptr; } return serviceProxy_; @@ -481,6 +487,10 @@ void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(int32_t HILOGE("Failed to get remote object"); serviceProxy_ = nullptr; isLoadSuccess_.store(false); + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle( + "ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess", + AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_BACKUP_SA, + static_cast(BError::Codes::SA_INVAL_ARG), "{\"reason\":\"Load backup sa fail\"}"); proxyConVar_.notify_one(); return; } @@ -499,6 +509,10 @@ void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t sys { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); HILOGE("Load backup sa failed, systemAbilityId:%{private}d", systemAbilityId); + std::stringstream ss; + ss << "\"reason\": \"Load backup sa failed\", \"system_ability_id\": \"" << systemAbilityId << "\""; + AppRadar::GetInstance().RecordBackupSARes(AppRadar::GetInstance().GetUserId(), "ServiceProxy::GetInstance", + static_cast(BError::Codes::EXT_BROKEN_IPC), ss.str()); unique_lock lock(proxyMutex_); serviceProxy_ = nullptr; isLoadSuccess_.store(false); diff --git a/interfaces/inner_api/native/backup_kit_inner/BUILD.gn b/interfaces/inner_api/native/backup_kit_inner/BUILD.gn index c84d227f2..2ee1a876d 100644 --- a/interfaces/inner_api/native/backup_kit_inner/BUILD.gn +++ b/interfaces/inner_api/native/backup_kit_inner/BUILD.gn @@ -27,6 +27,7 @@ config("private_config") { "${path_backup}/frameworks/native/backup_kit_inner/include", "${path_backup}/interfaces/inner_api/native/backup_kit_inner", "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + "${path_backup}/utils/include", ] } diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index fa1da2e93..78ed2c935 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -91,6 +91,9 @@ public: void OnStop() override; void StopAll(const wptr &obj, bool force = false); int Dump(int fd, const std::vector &args) override; + void PermissionCheckFailRadar(const std::string &info); + void GetLocalCapabilitiesRadar(const std::string &func, int32_t resultCode, + const std::string &info); /** * @brief 执行启动 backup extension diff --git a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp index f49ed5bd3..c60236c02 100644 --- a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp +++ b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp @@ -17,6 +17,7 @@ #include +#include "b_radar/b_radar.h" #include "message_parcel.h" #include "want.h" @@ -85,6 +86,35 @@ DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName) return DoDispose(bundleName, DisposeOperation::END_RESTORE); } +void RecordDoDisposeRes(const std::string &bundleName, + AppGalleryDisposeProxy::DisposeOperation disposeOperation, int32_t err) +{ + switch (disposeOperation) { + case AppGalleryDisposeProxy::DisposeOperation::START_BACKUP: + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("AppGalleryDisposeProxy::StartBackup", + bundleName, AppRadar::GetInstance().GetUserId(), + BizStage::BIZ_STAGE_APPLICATION, err); + break; + case AppGalleryDisposeProxy::DisposeOperation::END_BACKUP: + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("AppGalleryDisposeProxy::EndBackup", + bundleName, AppRadar::GetInstance().GetUserId(), + BizStage::BIZ_STAGE_APPLICATION, err); + break; + case AppGalleryDisposeProxy::DisposeOperation::START_RESTORE: + AppRadar::GetInstance().RecordRestoreFuncResWithBundle(bundleName, AppRadar::GetInstance().GetUserId(), + "AppGalleryDisposeProxy::StartRestore", + BizStageRestore::BIZ_STAGE_STATUS_CLEAR, err); + break; + case AppGalleryDisposeProxy::DisposeOperation::END_RESTORE: + AppRadar::GetInstance().RecordRestoreFuncResWithBundle(bundleName, AppRadar::GetInstance().GetUserId(), + "AppGalleryDisposeProxy::EndRestore", + BizStageRestore::BIZ_STAGE_STATUS_CLEAR, err); + break; + default: + break; + } +} + DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation) { HILOGI("DoDispose, app %{public}s, operation %{public}d", bundleName.c_str(), disposeOperation); @@ -113,6 +143,7 @@ DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, Disp MessageOption option(MessageOption::TF_ASYNC); int32_t ret = appRemoteObj_->SendRequest(static_cast(disposeOperation), data, reply, option); if (ret != ERR_NONE) { + RecordDoDisposeRes(bundleName, disposeOperation, ret); HILOGI("SendRequest error, code=%{public}d, bundleName=%{public}s", ret, bundleName.c_str()); return DisposeErr::REQUEST_FAIL; } diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 6e834db31..fab10321c 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -47,6 +47,7 @@ #include "b_jsonutil/b_jsonutil.h" #include "b_ohos/startup/backup_para.h" #include "b_process/b_multiuser.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "b_sa/b_sa_utils.h" #include "bundle_mgr_client.h" @@ -138,6 +139,14 @@ void Service::OnStop() HILOGI("SA OnStop End."); } +void Service::GetLocalCapabilitiesRadar(const std::string &func, int32_t resultCode, + const std::string &info) +{ + std::string funcPos = "Service::"; + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle(funcPos.append(func), + GetUserIdDefault(), BizStage::BIZ_STAGE_GET_LOCAL_CAPABILITIES, resultCode, info); +} + UniqueFd Service::GetLocalCapabilities() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -149,6 +158,7 @@ UniqueFd Service::GetLocalCapabilities() HILOGI("Begin"); if (session_ == nullptr) { HILOGE("GetLocalCapabilities error, session is empty."); + GetLocalCapabilitiesRadar("GetLocalCapabilities", EPERM, "{\"reason\":\"session empty\"}"); return UniqueFd(-EPERM); } session_->IncreaseSessionCnt(); @@ -160,6 +170,8 @@ UniqueFd Service::GetLocalCapabilities() if (fd < 0) { HILOGE("Failed to open config file = %{private}s, err = %{public}d", path.c_str(), errno); session_->DecreaseSessionCnt(); + GetLocalCapabilitiesRadar("GetLocalCapabilities", EPERM, + "{\"reason\":\"failed to open config file\"}"); return UniqueFd(-EPERM); } BJsonCachedEntity cachedEntity(std::move(fd)); @@ -177,14 +189,17 @@ UniqueFd Service::GetLocalCapabilities() } catch (const BError &e) { session_->DecreaseSessionCnt(); HILOGE("GetLocalCapabilities failed, errCode = %{public}d", e.GetCode()); + GetLocalCapabilitiesRadar("GetLocalCapabilities", e.GetCode(), "{\"reason\":\"failed\"}"); return UniqueFd(-e.GetCode()); } catch (const exception &e) { session_->DecreaseSessionCnt(); HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); + GetLocalCapabilitiesRadar("GetLocalCapabilities", EPERM, "{\"reason\":\"exception\"}"); return UniqueFd(-EPERM); } catch (...) { session_->DecreaseSessionCnt(); HILOGI("Unexpected exception"); + GetLocalCapabilitiesRadar("GetLocalCapabilities", EPERM, "{\"reason\":\"exception\"}"); return UniqueFd(-EPERM); } } @@ -203,6 +218,10 @@ string Service::VerifyCallerAndGetCallerName() if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) { Security::AccessToken::HapTokenInfo hapTokenInfo; if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenCaller, hapTokenInfo) != 0) { + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("Service::VerifyCallerAndGetCallerName", + GetUserIdDefault(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), + "{\"reason\":\"Get hap token info failed\"}"); throw BError(BError::Codes::SA_INVAL_ARG, "Get hap token info failed"); } std::string bundleNameIndexInfo = BJsonUtil::BuildBundleNameIndexInfo(hapTokenInfo.bundleName, @@ -212,10 +231,21 @@ string Service::VerifyCallerAndGetCallerName() } else { string str = to_string(tokenCaller); HILOGE("tokenID = %{private}s", GetAnonyString(str).c_str()); + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("Service::VerifyCallerAndGetCallerName", + GetUserIdDefault(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), + string("{\"reason\":\"Invalid token type ").append(to_string(tokenType)).append(string("\"}"))); throw BError(BError::Codes::SA_INVAL_ARG, string("Invalid token type ").append(to_string(tokenType))); } } +void Service::PermissionCheckFailRadar(const std::string &info) +{ + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("Service::VerifyCaller", + GetUserIdDefault(), BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), info); +} + void Service::VerifyCaller() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -225,6 +255,8 @@ void Service::VerifyCaller() case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE: { /* Update Service */ if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, BACKUP_PERMISSION) != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + std::string info = "{\"reason\":\"Permission denied, token type is " + to_string(tokenType) + "\"}"; + PermissionCheckFailRadar(info); throw BError(BError::Codes::SA_REFUSED_ACT, string("Permission denied, token type is ").append(to_string(tokenType))); } @@ -233,11 +265,15 @@ void Service::VerifyCaller() case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: { if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, BACKUP_PERMISSION) != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + std::string info = "{\"reason\":\"Permission denied, token type is " + to_string(tokenType) + "\"}"; + PermissionCheckFailRadar(info); throw BError(BError::Codes::SA_REFUSED_ACT, string("Permission denied, token type is ").append(to_string(tokenType))); } uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId)) { + std::string info = "{\"reason\":\"Permission denied, token type is " + to_string(tokenType) + "\"}"; + PermissionCheckFailRadar(info); throw BError(BError::Codes::SA_REFUSED_ACT, string("Permission denied, token type is ").append(to_string(tokenType))); } @@ -245,10 +281,14 @@ void Service::VerifyCaller() } case Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL: if (IPCSkeleton::GetCallingUid() != BConstants::SYSTEM_UID) { + std::string info = "{\"reason\":\"invalid calling uid\"}"; + PermissionCheckFailRadar(info); throw BError(BError::Codes::SA_REFUSED_ACT, "Calling uid is invalid"); } break; default: + std::string info = "{\"reason\":\"Permission denied, token type is " + to_string(tokenType) + "\"}"; + PermissionCheckFailRadar(info); throw BError(BError::Codes::SA_REFUSED_ACT, string("Invalid token type ").append(to_string(tokenType))); break; } @@ -446,6 +486,9 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const vectorDecreaseSessionCnt(); return e.GetCode(); @@ -511,6 +554,9 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, return BError(BError::Codes::OK); } catch (const BError &e) { HILOGE("Catch exception"); + AppRadar::GetInstance().RecordRestoreFuncResWithBundle("bundlename", GetUserIdDefault(), + "Service::AppendBundlesRestoreSession", + BizStageRestore::BIZ_STAGE_APPEND_BUNDLES, e.GetCode()); HandleExceptionOnAppendBundles(session_, bundleNames, {}); session_->DecreaseSessionCnt(); return e.GetCode(); @@ -930,6 +976,9 @@ ErrCode Service::LaunchBackupExtension(const BundleName &bundleName) } return BError(BError::Codes::OK); } catch (const BError &e) { + AppRadar::GetInstance().RecordGetFileHandleRes(bundleName, GetUserIdDefault(), + static_cast(e.GetCode()), + "{\"reason\":\"catch error\"}"); return e.GetCode(); } catch (const exception &e) { HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); @@ -975,11 +1024,17 @@ ErrCode Service::GetFileHandle(const string &bundleName, const string &fileName) auto backUpConnection = session_->GetExtConnection(bundleName); if (backUpConnection == nullptr) { HILOGE("GetFileHandle error, backUpConnection is empty"); + AppRadar::GetInstance().RecordGetFileHandleRes(bundleName, GetUserIdDefault(), + static_cast(BError::Codes::SA_INVAL_ARG), + "{\"reason\":\"backUpConnection is empty\"}"); return BError(BError::Codes::SA_INVAL_ARG); } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { HILOGE("GetFileHandle error, Extension backup Proxy is empty"); + AppRadar::GetInstance().RecordGetFileHandleRes(bundleName, GetUserIdDefault(), + static_cast(BError::Codes::SA_INVAL_ARG), + "{\"reason\":\"Extension backup Proxy is empty\"}"); return BError(BError::Codes::SA_INVAL_ARG); } int32_t errCode = 0; @@ -1003,6 +1058,14 @@ ErrCode Service::GetFileHandle(const string &bundleName, const string &fileName) void Service::OnBackupExtensionDied(const string &&bundleName) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + int32_t errCode = static_cast(BError::Codes::EXT_ABILITY_DIED); + if (session_->GetScenario() == IServiceReverse::Scenario::BACKUP) { + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("Service::OnBackupExtensionDied", + bundleName, GetUserIdDefault(), BizStage::BIZ_STAGE_EXTENSION_STATUS, errCode); + } else if (session_->GetScenario() == IServiceReverse::Scenario::RESTORE) { + AppRadar::GetInstance().RecordRestoreFuncResWithBundle(bundleName, GetUserIdDefault(), "OnBackupExtensionDied", + BizStageRestore::BIZ_STAGE_EXTENSION_STATUS, errCode); + } try { string callName = move(bundleName); HILOGE("Backup <%{public}s> Extension Process Died", callName.c_str()); @@ -1136,6 +1199,13 @@ void Service::ExtConnectFailed(const string &bundleName, ErrCode ret) try { HILOGE("begin %{public}s", bundleName.data()); IServiceReverse::Scenario scenario = session_->GetScenario(); + if (scenario == IServiceReverse::Scenario::BACKUP) { + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("Service::ExtConnectFailed", + bundleName, GetUserIdDefault(), BizStage::BIZ_STAGE_APPLICATION, ret); + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + AppRadar::GetInstance().RecordRestoreFuncResWithBundle(bundleName, GetUserIdDefault(), + "Service::ExtConnectFailed", BizStageRestore::BIZ_STAGE_CONNECT_BACKUP_EXTENSION, ret); + } if (scenario == IServiceReverse::Scenario::BACKUP && session_->GetIsIncrementalBackup()) { session_->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted(ret, bundleName); } else if (scenario == IServiceReverse::Scenario::RESTORE && @@ -1425,6 +1495,16 @@ void Service::UnloadService() void Service::SessionDeactive() { + IServiceReverse::Scenario scenario = session_->GetScenario(); + if (scenario == IServiceReverse::Scenario::BACKUP) { + AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle("Service::SessionDeactive", + AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_CLIENT_STATUS, + static_cast(BError::Codes::SA_BROKEN_IPC), "cloud_clone"); + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + AppRadar::GetInstance().RecordRestoreFuncRes(AppRadar::GetInstance().GetUserId(), + "Service::SessionDeactive", BizStageRestore::BIZ_STAGE_CLIENT_STATUS, + static_cast(BError::Codes::SA_BROKEN_IPC)); + } HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); try { HILOGI("Begin"); @@ -1520,6 +1600,8 @@ ErrCode Service::GetBackupInfoCmdHandle(BundleName &bundleName, std::string &res backupConnection->DisconnectBackupExtAbility(); if (ret != ERR_OK) { HILOGE("Call Ext GetBackupInfo faild."); + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithBundle("Service::GetBackupInfoCmdHandle", + bundleName, GetUserIdDefault(), BizStage::BIZ_STAGE_GET_BACKUP_INFO, ret); return BError(BError::Codes::SA_INVAL_ARG); } @@ -1828,6 +1910,15 @@ std::function Service::TimeOutCallback(wptr ptr, std::string bu HILOGE("SessionPtr is nullptr."); return; } + IServiceReverse::Scenario scenario = sessionPtr->GetScenario(); + int32_t errCode = static_cast(BError::Codes::EXT_ABILITY_TIMEOUT); + if (scenario == IServiceReverse::Scenario::BACKUP) { + AppRadar::GetInstance().RecordBackUpFuncResWithBundle("TimeOutCallback", bundleName, GetUserIdDefault(), + BizStage::BIZ_STAGE_ON_BACKUP, errCode); + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + AppRadar::GetInstance().RecordRestoreFuncResWithBundle(bundleName, GetUserIdDefault(), "TimeOutCallback", + BizStageRestore::BIZ_STAGE_EXEC_ON_RESTORE, errCode); + } try { if (SAUtils::IsSABundleName(bundleName)) { auto sessionConnection = sessionPtr->GetSAExtConnection(bundleName); diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index deb63ff3c..0f5f29345 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -38,6 +38,7 @@ #include "b_json/b_json_entity_caps.h" #include "b_ohos/startup/backup_para.h" #include "b_process/b_multiuser.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "b_sa/b_sa_utils.h" #include "filemgmt_libhilog.h" @@ -95,6 +96,7 @@ UniqueFd Service::GetLocalCapabilitiesIncremental(const std::vectorIncreaseSessionCnt(); @@ -106,6 +108,7 @@ UniqueFd Service::GetLocalCapabilitiesIncremental(const std::vectorDecreaseSessionCnt(); + GetLocalCapabilitiesRadar("GetLocalCapabilitiesIncremental", ENOENT, "{\"reason\":\"open file failed\"}"); return UniqueFd(-ENOENT); } BJsonCachedEntity cachedEntity(move(fd)); @@ -123,14 +126,17 @@ UniqueFd Service::GetLocalCapabilitiesIncremental(const std::vectorDecreaseSessionCnt(); HILOGE("GetLocalCapabilitiesIncremental failed, errCode = %{public}d", e.GetCode()); + GetLocalCapabilitiesRadar("GetLocalCapabilitiesIncremental", e.GetCode(), "{\"reason\":\"fail\"}"); return UniqueFd(-e.GetCode()); } catch (const exception &e) { session_->DecreaseSessionCnt(); HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); + GetLocalCapabilitiesRadar("GetLocalCapabilitiesIncremental", EPERM, "{\"reason\":\"exception\"}"); return UniqueFd(-EPERM); } catch (...) { session_->DecreaseSessionCnt(); HILOGI("Unexpected exception"); + GetLocalCapabilitiesRadar("GetLocalCapabilitiesIncremental", EPERM, "{\"reason\":\"exception\"}"); return UniqueFd(-EPERM); } } diff --git a/services/backup_sa/src/module_ipc/svc_session_manager.cpp b/services/backup_sa/src/module_ipc/svc_session_manager.cpp index 4ad667a9f..d22b1a5e8 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -26,6 +26,7 @@ #include "b_file_info.h" #include "b_json/b_json_entity_caps.h" #include "b_json/b_json_entity_ext_manage.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "b_sa/b_sa_utils.h" #include "b_utils/b_time.h" @@ -42,9 +43,19 @@ void SvcSessionManager::VerifyCallerAndScenario(uint32_t clientToken, IServiceRe shared_lock lock(lock_); if (impl_.scenario != scenario) { HILOGE("Inconsistent scenario, impl scenario:%{public}d", impl_.scenario); + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle( + "SvcSessionManager::VerifyCallerAndScenario", + impl_.userId, BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SDK_MIXED_SCENARIO), + "{\"reason\":\"Inconsistent scenario\"}"); throw BError(BError::Codes::SDK_MIXED_SCENARIO); } if (impl_.clientToken != clientToken) { + Backup::AppRadar::GetInstance().RecordBackUpFuncResWithoutBundle( + "SvcSessionManager::VerifyCallerAndScenario", + impl_.userId, BizStage::BIZ_STAGE_PERMISSION_CHECK, + static_cast(BError::Codes::SA_REFUSED_ACT), + "{\"reason\":\"Caller mismatched\"}"); throw BError(BError::Codes::SA_REFUSED_ACT, "Caller mismatched"); } HILOGD("Succeed to verify the caller"); diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 1ff282364..90126bd99 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -74,6 +74,7 @@ ohos_shared_library("backup_utils") { "src/b_process/b_guard_cwd.cpp", "src/b_process/b_guard_signal.cpp", "src/b_process/b_process.cpp", + "src/b_radar/b_radar.cpp", "src/b_sa/b_sa_utils.cpp", "src/b_tarball/b_tarball_cmdline.cpp", "src/b_tarball/b_tarball_factory.cpp", @@ -90,6 +91,7 @@ ohos_shared_library("backup_utils") { "c_utils:utils", "faultloggerd:libdfx_dumpcatcher", "hilog:libhilog", + "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", "ipc:ipc_core", @@ -98,6 +100,7 @@ ohos_shared_library("backup_utils") { include_dirs = [ "${path_init}/interfaces/innerkits/include/syspara", "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + "${path_backup}/utils/include", ] deps = [ diff --git a/utils/include/b_radar/b_radar.h b/utils/include/b_radar/b_radar.h new file mode 100644 index 000000000..c6eb1c168 --- /dev/null +++ b/utils/include/b_radar/b_radar.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_B_RADAR_H +#define OHOS_FILEMGMT_BACKUP_B_RADAR_H +#include +#include + +namespace OHOS::FileManagement::Backup { +static std::chrono::time_point g_onBackUpStart; +static std::chrono::time_point g_onRestoreStart; + +enum class BizScene : int32_t { + BACKUP = 1, + RESTORE = 2 +}; + +enum class BizStage : int32_t { + BIZ_STAGE_GET_LOCAL_CAPABILITIES = 1, + BIZ_STAGE_GET_BACKUP_INFO, + BIZ_STAGE_CREATE_SESSION_BACKUP, + BIZ_STAGE_BACKUP_SA, + BIZ_STAGE_APPEND_BUNDLES, + BIZ_STAGE_BACKUP_EXTENSION, + BIZ_STAGE_APPLICATION, + BIZ_STAGE_EXTENSION_STATUS, + BIZ_STAGE_ON_BACKUP, + BIZ_STAGE_DO_BACKUP, + BIZ_STAGE_DATA_CONSISTENCY, + BIZ_STAGE_STATUS_CLEAR, + BIZ_STAGE_CLIENT_STATUS, + BIZ_STAGE_PERMISSION_CHECK +}; + +enum class BizStageRestore : int32_t { + BIZ_STAGE_GET_LOCAL_CAPABILITIES = 1, + BIZ_STAGE_CREATE_SESSION_RESTORE, + BIZ_STAGE_BACKUP_SA, + BIZ_STAGE_APPEND_BUNDLES, + BIZ_STAGE_CONNECT_BACKUP_EXTENSION, + BIZ_STAGE_APPLICATION, + BIZ_STAGE_EXTENSION_STATUS, + BIZ_STAGE_GET_FILE_HANDLE, + BIZ_STAGE_DO_RESTORE, + BIZ_STAGE_DATA_CONSISTENCY, + BIZ_STAGE_EXEC_ON_RESTORE, + BIZ_STAGE_STATUS_CLEAR, + BIZ_STAGE_CLIENT_STATUS, + BIZ_STAGE_PERMISSION_CHECK +}; + +class AppRadar { +public: + static AppRadar &GetInstance() + { + static AppRadar instance; + return instance; + } + +public: + int32_t GetUserId(); + void RecordBackUpFuncResWithBundle(const std::string &func, const std::string &bundleName, int32_t userId, + enum BizStage bizStage, int32_t resultCode, + const std::string &resultInfo = "{\"reason\":\"failed\"}"); + void RecordBackUpFuncResWithoutBundle(const std::string &func, int32_t userId, + enum BizStage bizStage, int32_t resultCode, + const std::string &resultInfo = "{\"reason\":\"failed\"}"); + void RecordDoBackUpRes(const std::string &func, int32_t userId, int32_t resultCode, int32_t exportDuration); + void RecordRestoreFuncRes(int32_t userId, const std::string &func, enum BizStageRestore bizStage, + int32_t resultCode); + void RecordRestoreFuncResWithBundle(const std::string &bundleName, int32_t userId, const std::string &func, + enum BizStageRestore bizStage, int32_t resultCode); + void RecordRestoreFuncResWithResult(const std::string &bundleName, int32_t userId, const std::string &func, + enum BizStageRestore bizStage, int32_t resultCode, + const std::string &resultInfo); + void RecordRestoreFuncResWithStatus(const std::string &bundleName, int32_t userId, const std::string &func, + enum BizStageRestore bizStage, const std::string &execStatus, + int32_t resultCode); + void RecordGetFileHandleRes(const std::string &bundleName, int32_t userId, int32_t resultCode, + const std::string &resultInfo); + void RecordOnRestoreRes(const std::string &bundleName, int32_t userId, const std::string &func, + const std::string &execStatus, int32_t resultCode); + void RecordBackupSARes(int32_t userId, const std::string &func, int32_t resultCode, const std::string &resultInfo); +private: + AppRadar() = default; + ~AppRadar() = default; + AppRadar(const AppRadar &) = delete; + AppRadar &operator=(const AppRadar &) = delete; + AppRadar(AppRadar &&) = delete; + AppRadar &operator=(AppRadar &&) = delete; +}; +} // namespace OHOS::FileManagement::AppRadar +#endif // OHOS_FILEMGMT_BACKUP_B_RADAR_H diff --git a/utils/src/b_radar/b_radar.cpp b/utils/src/b_radar/b_radar.cpp new file mode 100644 index 000000000..0f4e314d2 --- /dev/null +++ b/utils/src/b_radar/b_radar.cpp @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "b_radar/b_radar.h" + +#include +#include +#include + +#include "b_process/b_multiuser.h" +#include "b_resources/b_constants.h" +#include "hisysevent.h" + +namespace { +constexpr uint8_t INDEX = 3; +constexpr int32_t MS_1000 = 1000; +const std::string FILE_BACKUP_EVENTS = "FILE_BACKUP_EVENTS"; +} +namespace OHOS::FileManagement::Backup { +static std::string GetCurrentTime() +{ + auto now = std::chrono::system_clock::now(); + auto time = std::chrono::system_clock::to_time_t(now); + auto ms = std::chrono::duration_cast(now.time_since_epoch()); + std::stringstream strTime; + strTime << (std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S:")) << (std::setfill('0')) + << (std::setw(INDEX)) << (ms.count() % MS_1000); + return strTime.str(); +} + +int32_t AppRadar::GetUserId() +{ + auto multiuser = BMultiuser::ParseUid(getuid()); + if ((multiuser.userId == BConstants::SYSTEM_UID) || (multiuser.userId == BConstants::XTS_UID)) { + return BConstants::DEFAULT_USER_ID; + } + return multiuser.userId; +} + +void AppRadar::RecordBackUpFuncResWithBundle(const std::string &func, const std::string &bundleName, + int32_t userId, enum BizStage bizStage, int32_t resultCode, + const std::string &resultInfo) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "BUNDLE_NAME", bundleName, + "USER_ID", userId, + "PID", getpid(), + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::BACKUP), + "BIZ_STAGE", static_cast(bizStage), + "RESULT_CODE", resultCode, + "RESULT_INFO", resultInfo); +} + +void AppRadar::RecordBackUpFuncResWithoutBundle(const std::string &func, int32_t userId, enum BizStage bizStage, + int32_t resultCode, const std::string &resultInfo) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "USER_ID", userId, + "PID", getpid(), + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::BACKUP), + "BIZ_STAGE", static_cast(bizStage), + "RESULT_CODE", resultCode, + "RESULT_INFO", resultInfo); +} + +void AppRadar::RecordDoBackUpRes(const std::string &func, int32_t userId, int32_t resultCode, + int32_t exportDuration) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "USER_ID", userId, + "PID", getpid(), + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::BACKUP), + "BIZ_STAGE", static_cast(BizStage::BIZ_STAGE_DO_BACKUP), + "EXEC_STATUS", "{\"spend_time\":" + std::to_string(exportDuration) + "ms\"}", + "RESULT_CODE", resultCode); +} + +void AppRadar::RecordRestoreFuncRes(int32_t userId, const std::string &func, enum BizStageRestore bizStage, + int32_t resultCode) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "USER_ID", userId, + "PID", getpid(), + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::RESTORE), + "BIZ_STAGE", static_cast(bizStage), + "RESULT_CODE", resultCode); +} + +void AppRadar::RecordRestoreFuncResWithBundle(const std::string &bundleName, int32_t userId, const std::string &func, + enum BizStageRestore bizStage, int32_t resultCode) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "BUNDLE_NAME", bundleName, + "USER_ID", userId, + "PID", getpid(), + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::RESTORE), + "BIZ_STAGE", static_cast(bizStage), + "RESULT_CODE", resultCode); +} + +void AppRadar::RecordRestoreFuncResWithResult(const std::string &bundleName, int32_t userId, const std::string &func, + enum BizStageRestore bizStage, int32_t resultCode, + const std::string &resultInfo) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "BUNDLE_NAME", bundleName, + "USER_ID", userId, + "PID", getpid(), + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::RESTORE), + "BIZ_STAGE", static_cast(bizStage), + "RESULT_CODE", resultCode, + "RESULT_INFO", resultInfo); +} + +void AppRadar::RecordRestoreFuncResWithStatus(const std::string &bundleName, int32_t userId, const std::string &func, + enum BizStageRestore bizStage, const std::string &execStatus, + int32_t resultCode) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "BUNDLE_NAME", bundleName, + "USER_ID", userId, + "PID", getpid(), + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::RESTORE), + "BIZ_STAGE", static_cast(bizStage), + "EXEC_STATUS", execStatus, + "RESULT_CODE", resultCode); +} + +void AppRadar::RecordGetFileHandleRes(const std::string &bundleName, int32_t userId, int32_t resultCode, + const std::string &resultInfo) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "BUNDLE_NAME", bundleName, + "USER_ID", userId, + "PID", getpid(), + "FUNC", "GetFileHandle", + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::RESTORE), + "BIZ_STAGE", static_cast(BizStageRestore::BIZ_STAGE_GET_FILE_HANDLE), + "RESULT_CODE", resultCode, + "RESULT_INFO", resultInfo); +} + +void AppRadar::RecordOnRestoreRes(const std::string &bundleName, int32_t userId, const std::string &func, + const std::string &execStatus, int32_t resultCode) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "BUNDLE_NAME", bundleName, + "USER_ID", userId, + "PID", getpid(), + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::RESTORE), + "BIZ_STAGE", static_cast(BizStageRestore::BIZ_STAGE_EXEC_ON_RESTORE), + "EXEC_STATUS", execStatus, + "RESULT_CODE", resultCode); +} + +void AppRadar::RecordBackupSARes(int32_t userId, const std::string &func, int32_t resultCode, + const std::string &resultInfo) +{ + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "USER_ID", userId, + "PID", getpid(), + "FUNC", func, + "TIME", GetCurrentTime(), + "BIZ_SCENE", static_cast(BizScene::RESTORE), + "BIZ_STAGE", static_cast(BizStageRestore::BIZ_STAGE_BACKUP_SA), + "RESULT_CODE", resultCode, + "RESULT_INFO", resultInfo); +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file -- Gitee From 04b6112cad5b080add3059e5e30f29f6a9c8558b Mon Sep 17 00:00:00 2001 From: zhangyaomaggie Date: Mon, 26 Aug 2024 11:16:50 +0800 Subject: [PATCH 2/2] add lock for radar Signed-off-by: zhangyaomaggie --- .../native/backup_ext/include/ext_extension.h | 8 ++++ .../native/backup_ext/src/ext_extension.cpp | 20 ++++++++-- .../backup_ext/src/sub_ext_extension.cpp | 40 +++++++++++-------- utils/include/b_radar/b_radar.h | 3 -- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index 9556c8c29..b9907171b 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -256,6 +256,10 @@ private: void SyncCallJsOnProcessTask(wptr obj, BackupRestoreScenario scenario); void StartOnProcessTimeOutTimer(wptr obj, BackupRestoreScenario scenario); void CloseOnProcessTimeOutTimer(); + void UpdateOnRestoreTime(); + void UpdateOnBackUpTime(); + int32_t GetOnBackupTimeCost(); + int32_t GetOnRestoreTimeCost(); private: std::shared_mutex lock_; @@ -283,6 +287,10 @@ private: std::condition_variable execOnProcessCon_; std::mutex onProcessLock_; std::atomic onProcessTimeout_ {false}; + std::chrono::time_point g_onBackUpStart; + std::chrono::time_point g_onRestoreStart; + std::mutex onBackupTimeLock_; + std::mutex onRestoreTimeLock_; }; } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index f00b12abf..f0e0386a3 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -1337,6 +1337,12 @@ void BackupExtExtension::AsyncTaskIncreRestoreSpecialVersion() }); } +void BackupExtExtension::UpdateOnRestoreTime() +{ + std::lock_guard lock(onRestoreTimeLock_); + g_onRestoreStart = std::chrono::system_clock::now(); +} + void BackupExtExtension::AsyncTaskRestoreForUpgrade() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -1355,7 +1361,7 @@ void BackupExtExtension::AsyncTaskRestoreForUpgrade() ptr->StartOnProcessTaskThread(obj, BackupRestoreScenario::FULL_RESTORE); auto callBackup = ptr->OnRestoreCallback(obj); auto callBackupEx = ptr->OnRestoreExCallback(obj); - g_onRestoreStart = std::chrono::system_clock::now(); + ptr->UpdateOnRestoreTime(); ErrCode err = ptr->extension_->OnRestore(callBackup, callBackupEx); if (err != ERR_OK) { ptr->AppDone(BError::GetCodeByErrno(err)); @@ -1406,7 +1412,7 @@ void BackupExtExtension::AsyncTaskIncrementalRestoreForUpgrade() ptr->StartOnProcessTaskThread(obj, BackupRestoreScenario::INCREMENTAL_RESTORE); auto callBackup = ptr->IncreOnRestoreCallback(obj); auto callBackupEx = ptr->IncreOnRestoreExCallback(obj); - g_onRestoreStart = std::chrono::system_clock::now(); + ptr->UpdateOnRestoreTime(); ErrCode err = ptr->extension_->OnRestore(callBackup, callBackupEx); if (err != ERR_OK) { HILOGE("OnRestore done, err = %{pubilc}d", err); @@ -1523,6 +1529,12 @@ void BackupExtExtension::StartFwkTimer(bool &isFwkStart) } } +void BackupExtExtension::UpdateOnBackUpTime() +{ + std::lock_guard lock(onBackupTimeLock_); + g_onBackUpStart = std::chrono::system_clock::now(); +} + void BackupExtExtension::AsyncTaskOnBackup() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -1534,7 +1546,7 @@ void BackupExtExtension::AsyncTaskOnBackup() ptr->StartOnProcessTaskThread(obj, BackupRestoreScenario::FULL_BACKUP); auto callBackup = ptr->OnBackupCallback(obj); auto callBackupEx = ptr->OnBackupExCallback(obj); - g_onBackUpStart = std::chrono::system_clock::now(); + ptr->UpdateOnBackUpTime(); ErrCode err = ptr->extension_->OnBackup(callBackup, callBackupEx); if (err != ERR_OK) { HILOGE("OnBackup done, err = %{pubilc}d", err); @@ -1920,7 +1932,7 @@ void BackupExtExtension::AsyncTaskOnIncrementalBackup() ptr->StartOnProcessTaskThread(obj, BackupRestoreScenario::INCREMENTAL_BACKUP); auto callBackup = ptr->IncOnBackupCallback(obj); auto callBackupEx = ptr->IncOnBackupExCallback(obj); - g_onBackUpStart = std::chrono::system_clock::now(); + ptr->UpdateOnBackUpTime(); ErrCode err = ptr->extension_->OnBackup(callBackup, callBackupEx); if (err != ERR_OK) { HILOGE("OnBackup done, err = %{pubilc}d", err); diff --git a/frameworks/native/backup_ext/src/sub_ext_extension.cpp b/frameworks/native/backup_ext/src/sub_ext_extension.cpp index b4f63b222..349bd40fc 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -240,6 +240,14 @@ ErrCode BackupExtExtension::UpdateFdSendRate(std::string &bundleName, int32_t se } } +int32_t BackupExtExtension::GetOnRestoreTimeCost() +{ + auto endTime = std::chrono::system_clock::now(); + std::lock_guard lock(onRestoreTimeLock_); + auto ms = std::chrono::duration_cast(endTime - g_onRestoreStart).count(); + return ms; +} + std::function BackupExtExtension::OnRestoreCallback(wptr obj) { HILOGI("Begin get full restore onRestore callback"); @@ -251,8 +259,7 @@ std::function BackupExtExtension::OnRestoreCallback( } HILOGI("Current bundle will execute app done"); if (errCode == ERR_OK) { - auto endTime = std::chrono::system_clock::now(); - auto ms = std::chrono::duration_cast(endTime - g_onRestoreStart).count(); + auto ms = extensionPtr->GetOnRestoreTimeCost(); std::stringstream ss; ss << "\"spend_time\": \"" << ms << "ms\""; AppRadar::GetInstance().RecordOnRestoreRes("bundlename", AppRadar::GetInstance().GetUserId(), @@ -284,8 +291,7 @@ std::function BackupExtExtension::OnRestoreExCallbac return; } if (errCode == ERR_OK && !restoreRetInfo.empty()) { - auto endTime = std::chrono::system_clock::now(); - auto ms = std::chrono::duration_cast(endTime - g_onRestoreStart).count(); + auto ms = extensionPtr->GetOnRestoreTimeCost(); std::stringstream ss; ss << "\"spend_time\": \"" << ms << "ms\""; AppRadar::GetInstance().RecordOnRestoreRes("bundlename", AppRadar::GetInstance().GetUserId(), @@ -342,8 +348,7 @@ std::function BackupExtExtension::IncreOnRestoreExCa return; } if (errCode == ERR_OK && !restoreRetInfo.empty()) { - auto endTime = std::chrono::system_clock::now(); - auto ms = std::chrono::duration_cast(endTime - g_onRestoreStart).count(); + auto ms = extensionPtr->GetOnRestoreTimeCost(); std::stringstream ss; ss << "\"spend_time\": \"" << ms << "ms\""; AppRadar::GetInstance().RecordOnRestoreRes("bundlename", AppRadar::GetInstance().GetUserId(), @@ -379,8 +384,7 @@ std::function BackupExtExtension::IncreOnRestoreCall } HILOGI("Current bundle will execute app done"); if (errCode == ERR_OK) { - auto endTime = std::chrono::system_clock::now(); - auto ms = std::chrono::duration_cast(endTime - g_onRestoreStart).count(); + auto ms = extensionPtr->GetOnRestoreTimeCost(); std::stringstream ss; ss << "\"spend_time\": \"" << ms << "ms\""; AppRadar::GetInstance().RecordOnRestoreRes("bundlename", AppRadar::GetInstance().GetUserId(), @@ -398,6 +402,14 @@ std::function BackupExtExtension::IncreOnRestoreCall }; } +int32_t BackupExtExtension::GetOnBackupTimeCost() +{ + auto onBackUpEnd = std::chrono::system_clock::now(); + std::lock_guard lock(onBackupTimeLock_); + auto ms = std::chrono::duration_cast(onBackUpEnd - g_onBackUpStart).count(); + return ms; +} + std::function BackupExtExtension::OnBackupCallback(wptr obj) { HILOGI("Begin get HandleFullBackupCallbackEx"); @@ -413,8 +425,7 @@ std::function BackupExtExtension::OnBackupCall return; } if (errCode == ERR_OK) { - auto onBackUpEnd = std::chrono::system_clock::now(); - auto ms = std::chrono::duration_cast(onBackUpEnd - g_onBackUpStart).count(); + auto ms = extensionPtr->GetOnBackupTimeCost(); AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::OnBackupCallback", "bundleName", AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_ON_BACKUP, 0, string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); @@ -439,8 +450,7 @@ std::function BackupExtExtension::OnBackupExCa return; } if (errCode == ERR_OK && !backupExRetInfo.empty()) { - auto onBackUpEnd = std::chrono::system_clock::now(); - auto ms = std::chrono::duration_cast(onBackUpEnd - g_onBackUpStart).count(); + auto ms = extensionPtr->GetOnBackupTimeCost(); AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::OnBackupExCallback", "bundleName", AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_ON_BACKUP, 0, string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); @@ -471,8 +481,7 @@ std::function BackupExtExtension::IncOnBackupC } HILOGI("Start GetAppLocalListAndDoIncrementalBackup"); if (errCode == ERR_OK) { - auto onBackUpEnd = std::chrono::system_clock::now(); - auto ms = std::chrono::duration_cast(onBackUpEnd - g_onBackUpStart).count(); + auto ms = extPtr->GetOnBackupTimeCost(); AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::IncOnBackupCallback", "bundleName", AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_ON_BACKUP, 0, string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); @@ -500,8 +509,7 @@ std::function BackupExtExtension::IncOnBackupE return; } if (errCode == ERR_OK && !backupExRetInfo.empty()) { - auto onBackUpEnd = std::chrono::system_clock::now(); - auto ms = std::chrono::duration_cast(onBackUpEnd - g_onBackUpStart).count(); + auto ms = extensionPtr->GetOnBackupTimeCost(); AppRadar::GetInstance().RecordBackUpFuncResWithBundle("BackupExtExtension::IncOnBackupExCallback", "bundleName", AppRadar::GetInstance().GetUserId(), BizStage::BIZ_STAGE_ON_BACKUP, 0, string("{\"spend_time\":\" ").append(to_string(ms)).append(string("ms\"}"))); diff --git a/utils/include/b_radar/b_radar.h b/utils/include/b_radar/b_radar.h index c6eb1c168..ac7f4549f 100644 --- a/utils/include/b_radar/b_radar.h +++ b/utils/include/b_radar/b_radar.h @@ -19,9 +19,6 @@ #include namespace OHOS::FileManagement::Backup { -static std::chrono::time_point g_onBackUpStart; -static std::chrono::time_point g_onRestoreStart; - enum class BizScene : int32_t { BACKUP = 1, RESTORE = 2 -- Gitee