From 04a789f0e00ddb235b698ac69992347bf4395e7d Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Thu, 30 May 2024 10:22:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=97=B6=E6=8E=A7=E5=88=B6=E9=80=9F=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yaoruozi1@huawei.com --- .../native/backup_ext/src/ext_extension.cpp | 59 ++++++++++++++----- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index d2893b56f..d7efe80a8 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -68,6 +69,8 @@ using namespace std; namespace { const int64_t DEFAULT_SLICE_SIZE = 100 * 1024 * 1024; // 分片文件大小为100M const uint32_t MAX_FILE_COUNT = 6000; // 单个tar包最多包含6000个文件 +const int32_t SINGLE_FD_GROUP_MAX_COUNT = 150; // 每组处理的FD最大数量 +const int32_t SINGLE_FD_GROUP_MAX_TIME = 1000; // 每组FD最大时间给1秒,剩余时间休眠 } // namespace static std::set GetIdxFileData() @@ -343,30 +346,42 @@ static ErrCode BigFileReady(sptr proxy) BJsonCachedEntity cachedEntity(move(fd)); auto cache = cachedEntity.Structuralize(); auto pkgInfo = cache.GetExtManageInfo(); - + HILOGI("Start handle big files ready, pkgInfo count is:%{public}zu", pkgInfo.size()); ErrCode ret {ERR_OK}; + int32_t fdCount = 0; + auto startTime = std::chrono::system_clock::now(); for (auto &item : pkgInfo) { if (item.hashName.empty() || item.fileName.empty()) { continue; } - + if (fdCount == SINGLE_FD_GROUP_MAX_COUNT) { + auto curTime = std::chrono::system_clock::now(); + auto useTimeMs = std::chrono::duration_cast(curTime - startTime).count(); + if (useTimeMs < SINGLE_FD_GROUP_MAX_TIME) { + int32_t sleepMs = SINGLE_FD_GROUP_MAX_TIME - useTimeMs; + std::this_thread::sleep_for(std::chrono::microseconds(sleepMs)); + } else { + HILOGW("The time for processing the file of the current group exceeds one second"); + } + fdCount = 0; + startTime = std::chrono::system_clock::now(); + } int32_t errCode = ERR_OK; UniqueFd fd(open(item.fileName.data(), O_RDONLY)); if (fd < 0) { - HILOGE("open file failed, file name is %{public}s, err = %{public}d", item.fileName.c_str(), errno); + HILOGE("Open file failed, file name is %{public}s, err = %{public}d", item.fileName.c_str(), errno); errCode = BError::GetCodeByErrno(errno); } - ret = proxy->AppFileReady(item.hashName, std::move(fd), errCode); if (SUCCEEDED(ret)) { - HILOGI("The application is packaged successfully, package name is %{public}s", item.hashName.c_str()); + HILOGI("Current package file ready successfully, package name is %{public}s", item.hashName.c_str()); } else { HILOGI( - "The application is packaged successfully but the AppFileReady interface fails to be invoked: " - "%{public}d", - ret); + "Current package file ready ipc error: result is:%{public}d", ret); } + fdCount++; } + HILOGI("End handle big files ready"); return ret; } @@ -1441,36 +1456,48 @@ static ErrCode IncrementalBigFileReady(const TarMap &pkgInfo, const map &bigInfos, sptr proxy) { + HILOGI("Start handle big files, pkgInfo num:%{public}zu", pkgInfo.size()); ErrCode ret {ERR_OK}; + int32_t fdCount = 0; + auto startTime = std::chrono::system_clock::now(); for (auto &item : pkgInfo) { if (item.first.empty()) { continue; } + if (fdCount == SINGLE_FD_GROUP_MAX_COUNT) { + auto curTime = std::chrono::system_clock::now(); + auto useTimeMs = std::chrono::duration_cast(curTime - startTime).count(); + if (useTimeMs < SINGLE_FD_GROUP_MAX_TIME) { + int32_t sleepMs = SINGLE_FD_GROUP_MAX_TIME - useTimeMs; + std::this_thread::sleep_for(std::chrono::microseconds(sleepMs)); + } else { + HILOGW("The time for processing the file of the current group exceeds one second"); + } + fdCount = 0; + startTime = std::chrono::system_clock::now(); + } auto [path, sta, isBeforeTar] = item.second; - int32_t errCode = ERR_OK; UniqueFd fd(open(path.data(), O_RDONLY)); if (fd < 0) { - HILOGE("IncrementalBigFileReady open file failed, file name is %{public}s, err = %{public}d", path.c_str(), - errno); + HILOGE("open file failed, file name is %{public}s, err = %{public}d", path.c_str(), errno); errCode = BError::GetCodeByErrno(errno); } - struct ReportFileInfo info = bigInfos.find(path)->second; string file = GetReportFileName(string(INDEX_FILE_INCREMENTAL_BACKUP).append(item.first)); map bigInfo; bigInfo.try_emplace(path, info); WriteFile(file, bigInfo); - ret = proxy->AppIncrementalFileReady(item.first, std::move(fd), UniqueFd(open(file.data(), O_RDONLY)), errCode); if (SUCCEEDED(ret)) { - HILOGI("IncrementalBigFileReady: The application is packaged successfully, package name is %{public}s", - item.first.c_str()); + HILOGI("The application is packaged successfully, package name is %{public}s",item.first.c_str()); RemoveFile(file); } else { - HILOGE("IncrementalBigFileReady interface fails to be invoked: %{public}d", ret); + HILOGE("Incremental file ready ipc failed: result is: %{public}d", ret); } + fdCount++; } + HILOGI("End handle big files ready"); return ret; } -- Gitee From 41075bf42c6642454856720dc94ae60b3f3225f5 Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Thu, 30 May 2024 10:38:13 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=97=B6=E6=8E=A7=E5=88=B6=E9=80=9F=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yaoruozi1@huawei.com --- frameworks/native/backup_ext/src/ext_extension.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index d7efe80a8..b23ec23e1 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -1490,7 +1490,7 @@ static ErrCode IncrementalBigFileReady(const TarMap &pkgInfo, WriteFile(file, bigInfo); ret = proxy->AppIncrementalFileReady(item.first, std::move(fd), UniqueFd(open(file.data(), O_RDONLY)), errCode); if (SUCCEEDED(ret)) { - HILOGI("The application is packaged successfully, package name is %{public}s",item.first.c_str()); + HILOGI("The application is packaged successfully, package name is %{public}s", item.first.c_str()); RemoveFile(file); } else { HILOGE("Incremental file ready ipc failed: result is: %{public}d", ret); -- Gitee