diff --git a/services/common/include/storage_service_errno.h b/services/common/include/storage_service_errno.h index 8be036c034b4158029d18ceb7f9bea6639c011f3..9a7dc6218bf105ac07b90260609e9f095842f53a 100644 --- a/services/common/include/storage_service_errno.h +++ b/services/common/include/storage_service_errno.h @@ -77,6 +77,8 @@ enum ErrNo { E_EL5_ADD_CLASS_ERROR, E_EL5_ENCRYPT_CLASS_ERROR, E_ELX_KEY_INACTIVE_ERROR, + + E_STORAGE_USAGE_NOT_ENOUGH = 250, }; enum JsErrCode { diff --git a/services/storage_daemon/include/utils/storage_radar.h b/services/storage_daemon/include/utils/storage_radar.h index 5fca18078f48577cb54975b3765cb1f789089979..c02eed8a718d303506587586460787f5c2c41ce8 100644 --- a/services/storage_daemon/include/utils/storage_radar.h +++ b/services/storage_daemon/include/utils/storage_radar.h @@ -32,6 +32,7 @@ enum class BizScene : int32_t { USER_KEY_ENCRYPTION, SPACE_STATISTICS, EXTERNAL_VOLUME_MANAGER, + STORAGE_USAGE_MANAGER, }; enum class StageRes : int32_t { @@ -81,6 +82,10 @@ enum class BizStage : int32_t { BIZ_STAGE_FORMAT, BIZ_STAGE_SET_VOLUME_DESCRIPTION, BIZ_STAGE_GET_ALL_VOLUMES, + + BIZ_STAGE_THRESHOLD_TEN_PERCENT = 51, + BIZ_STAGE_THRESHOLD_FIVE_PERCENT, + BIZ_STAGE_THRESHOLD_MINIMAL, }; struct RadarParameter { @@ -119,6 +124,7 @@ public: const std::string &extraData); static void ReportIamResult(const std::string &funcName, uint32_t userId, int ret); static void ReportHuksResult(const std::string &funcName, int ret); + static void ReportStorageUsage(enum BizStage stage, const std::string &extraData); private: StorageRadar() = default; diff --git a/services/storage_daemon/utils/storage_radar.cpp b/services/storage_daemon/utils/storage_radar.cpp index 451f845a6096276060358052e3ac166cabd3eb2f..0e774c544d6a0005e964726217f82d982ec26487 100644 --- a/services/storage_daemon/utils/storage_radar.cpp +++ b/services/storage_daemon/utils/storage_radar.cpp @@ -155,7 +155,22 @@ void StorageRadar::ReportHuksResult(const std::string &funcName, int ret) }; StorageRadar::GetInstance().RecordFuctionResult(param); } - + +void StorageRadar::ReportStorageUsage(enum BizStage stage, const std::string &extraData) +{ + RadarParameter param = { + .orgPkg = DEFAULT_ORGPKGNAME, + .userId = DEFAULT_USERID, + .funcName = "CheckAndCleanCache", + .bizScene = BizScene::STORAGE_USAGE_MANAGER, + .bizStage = stage, + .keyElxLevel = "NA", + .errorCode = E_STORAGE_USAGE_NOT_ENOUGH, + .extraData = extraData + }; + StorageRadar::GetInstance().RecordFuctionResult(param); +} + bool StorageRadar::RecordKillProcessResult(std::string processName, int32_t errcode) { int32_t res = E_OK; diff --git a/services/storage_manager/include/storage/storage_monitor_service.h b/services/storage_manager/include/storage/storage_monitor_service.h index 04974ef075f0d5a6c583d5a6c7d5a8bf3cab794b..12f31d64b451b0b1eff4f555012eae4d88de0295 100644 --- a/services/storage_manager/include/storage/storage_monitor_service.h +++ b/services/storage_manager/include/storage/storage_monitor_service.h @@ -22,6 +22,7 @@ #include #include #include "event_handler.h" +#include "utils/storage_radar.h" namespace OHOS { namespace StorageManager { @@ -45,6 +46,8 @@ private: void EventNotifyHighFreqHandler(); void CleanBundleCacheByInterval(const std::string ×tamp, int64_t lowThreshold, int32_t checkInterval); + void ReportRadarStorageUsage(enum StorageService::BizStage stage, const std::string &extraData); + std::mutex eventMutex_; std::thread eventThread_; std::condition_variable eventCon_; @@ -55,6 +58,9 @@ private: std::chrono::steady_clock::time_point lastNotificationTimeHighFreq_ = std::chrono::time_point_cast( std::chrono::steady_clock::now()) - std::chrono::minutes(SMART_EVENT_INTERVAL_HIGH_FREQ); + std::chrono::steady_clock::time_point lastReportRadarTime_ = + std::chrono::time_point_cast( + std::chrono::steady_clock::now()) - std::chrono::hours(SMART_EVENT_INTERVAL); }; } // StorageManager } // OHOS diff --git a/services/storage_manager/storage/src/storage_monitor_service.cpp b/services/storage_manager/storage/src/storage_monitor_service.cpp index ad01c17a054da432d73db366ad1d06669d4da58f..597ac3331168a9e5249463a7e1afd2c8f06c0465 100644 --- a/services/storage_manager/storage/src/storage_monitor_service.cpp +++ b/services/storage_manager/storage/src/storage_monitor_service.cpp @@ -32,6 +32,7 @@ #include "storage_service_log.h" #include "storage/bundle_manager_connector.h" #include "storage/storage_total_status_service.h" +#include "utils/storage_radar.h" #include "want.h" namespace OHOS { @@ -58,6 +59,7 @@ const std::string FAULT_ID_ONE = "845010021"; const std::string FAULT_ID_TWO = "845010022"; const std::string FAULT_ID_THREE = "845010023"; const std::string FAULT_SUGGEST_THREE = "545010023"; +constexpr int RETRY_MAX_TIMES = 3; StorageMonitorService::StorageMonitorService() {} @@ -149,15 +151,23 @@ void StorageMonitorService::CheckAndCleanCache(int64_t freeSize, int64_t totalSi LOGI("Device storage freeSize=%{public}lld, threshold=%{public}lld", static_cast(freeSize), static_cast(lowThreshold)); + std::string freeSizeStr = std::to_string(freeSize); + std::string totalSizeStr = std::to_string(totalSize); + std::string lowThresholdStr = std::to_string(lowThreshold); + std::string storageUsage = "storage usage not enough:freeSize = " + freeSizeStr + ", totalSize = " + totalSizeStr + + ", lowThreshold = " + lowThresholdStr; if (freeSize < (lowThreshold * CONST_NUM_THREE) / CONST_NUM_TWO) { CleanBundleCache(lowThreshold); + ReportRadarStorageUsage(StorageService::BizStage::BIZ_STAGE_THRESHOLD_MINIMAL, storageUsage); return; } if (freeSize > (totalSize * STORAGE_THRESHOLD_PERCENTAGE) / CONST_NUM_ONE_HUNDRED) { CleanBundleCacheByInterval(TIMESTAMP_WEEK, lowThreshold, CLEAN_CACHE_WEEK); + ReportRadarStorageUsage(StorageService::BizStage::BIZ_STAGE_THRESHOLD_TEN_PERCENT, storageUsage); } else { CleanBundleCacheByInterval(TIMESTAMP_DAY, lowThreshold, SEND_EVENT_INTERVAL); + ReportRadarStorageUsage(StorageService::BizStage::BIZ_STAGE_THRESHOLD_FIVE_PERCENT, storageUsage); } } @@ -182,6 +192,17 @@ void StorageMonitorService::CleanBundleCacheByInterval(const std::string ×t } } +void StorageMonitorService::ReportRadarStorageUsage(enum StorageService::BizStage stage, const std::string &extraData) +{ + auto currentTime = std::chrono::steady_clock::now(); + int32_t duration = static_cast(std::chrono::duration_cast + (currentTime - lastReportRadarTime_).count()); + if (duration >= SEND_EVENT_INTERVAL) { + StorageService::StorageRadar::ReportStorageUsage(stage, extraData); + lastReportRadarTime_ = currentTime; + } +} + void StorageMonitorService::CleanBundleCache(int64_t lowThreshold) { auto bundleMgr = DelayedSingleton::GetInstance()->GetBundleMgrProxy(); @@ -190,10 +211,16 @@ void StorageMonitorService::CleanBundleCache(int64_t lowThreshold) return; } LOGI("Device storage free size not enough, start clean bundle cache files automatic."); - auto ret = bundleMgr->CleanBundleCacheFilesAutomatic(lowThreshold * CONST_NUM_TWO); - if (ret != ERR_OK) { - LOGE("Invoke bundleMgr interface to clean bundle cache files automatic failed."); - } + int retryCount = 0; + do { + auto ret = bundleMgr->CleanBundleCacheFilesAutomatic(lowThreshold * CONST_NUM_TWO); + if (ret == ERR_OK) { + LOGI("Invoke bundleMgr interface to clean bundle cache files automatic success."); + break; + } + retryCount ++; + LOGE("Invoke bundleMgr interface to clean bundle cache files automatic failed. Retry."); + } while (retryCount < RETRY_MAX_TIMES); } int64_t StorageMonitorService::GetLowerThreshold(int64_t totalSize)