From 16ce9e161dff6aa31eb596ce5ecfa692a516df6f Mon Sep 17 00:00:00 2001 From: peterhuangyu Date: Mon, 9 Jun 2025 20:15:15 +0800 Subject: [PATCH] add testcase and fix issues Signed-off-by: peterhuangyu --- .../include/appfreeze_cpu_freq_manager.h | 12 +- services/appdfr/include/appfreeze_util.h | 2 + .../appdfr/src/appfreeze_cpu_freq_manager.cpp | 82 +++-- services/appdfr/src/appfreeze_manager.cpp | 2 +- services/appdfr/src/appfreeze_util.cpp | 36 ++- test/unittest/dfr_test/BUILD.gn | 1 + .../appfreeze_cpu_freq_manager_test/BUILD.gn | 56 ++++ .../appfreeze_cpu_freq_manager_test.cpp | 298 ++++++++++++++++++ 8 files changed, 447 insertions(+), 42 deletions(-) create mode 100644 test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/BUILD.gn create mode 100644 test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/appfreeze_cpu_freq_manager_test.cpp diff --git a/services/appdfr/include/appfreeze_cpu_freq_manager.h b/services/appdfr/include/appfreeze_cpu_freq_manager.h index 596f69ade08..779e2e214a7 100644 --- a/services/appdfr/include/appfreeze_cpu_freq_manager.h +++ b/services/appdfr/include/appfreeze_cpu_freq_manager.h @@ -34,16 +34,17 @@ public: static std::shared_ptr GetInstance(); void SetHalfStackPath(const std::string& stackpath); void InitHalfCpuInfo(int32_t pid); - std::string WriteCpuInfoToFile(const std::string& bundleName, int32_t uid, int32_t pid); + std::string WriteCpuInfoToFile(const std::string &bundleName, int32_t uid, int32_t pid, + const std::string &eventName); private: bool ReadCpuDataByNum(int32_t num, std::vector& parseDatas, TotalTime& totalTime); void ParseCpuData(std::vector>& datas, std::vector& totalTimeLists); std::string GetCpuStr(int code, std::vector& freqPairs, float percentage); - bool GetCpuTotalValue(size_t i, std::vector totalTimeList_, + bool GetCpuTotalValue(size_t i, std::vector totalTimeList, std::vector blockTotalTimeList, TotalTime& totalTime); - uint32_t GetProcessCpuTime(int32_t pid); - uint32_t GetDeviceRuntime(); + uint64_t GetProcessCpuTime(int32_t pid); + uint64_t GetDeviceRuntime(); std::string GetCpuInfoContent(); std::string GetStartTime(uint64_t start); uint64_t GetAppCpuTime(int32_t pid); @@ -58,9 +59,10 @@ private: static ffrt::mutex freezeInfoMutex_; static std::vector> handlingHalfCpuData_; static std::vector totalTimeList_; - static int64_t halfTime_; + static uint64_t halfTime_; static uint64_t optimalCpuTime_; static std::string stackPath_; + static int cpuCount_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appdfr/include/appfreeze_util.h b/services/appdfr/include/appfreeze_util.h index 8cd49857ee2..18763755640 100644 --- a/services/appdfr/include/appfreeze_util.h +++ b/services/appdfr/include/appfreeze_util.h @@ -25,6 +25,7 @@ public: static constexpr const char* const LOG_FILE_PATH = "/data/log/eventlog/freeze"; static constexpr const char* const EVENTLOG_PATH = "/data/log/eventlog"; static constexpr int64_t SEC_TO_MILLISEC = 1000; + static constexpr int32_t CPU_COUNT_SUBTRACT = 1; AppfreezeUtil(); ~AppfreezeUtil(); @@ -32,6 +33,7 @@ public: static std::string TimestampFormatToDate(time_t timeStamp, const std::string& format); static uint64_t GetMilliseconds(); static std::string RoundToTwoDecimals(float value); + static int GetCpuCount(); }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appdfr/src/appfreeze_cpu_freq_manager.cpp b/services/appdfr/src/appfreeze_cpu_freq_manager.cpp index 65ebc53d2cf..ced26f0e671 100644 --- a/services/appdfr/src/appfreeze_cpu_freq_manager.cpp +++ b/services/appdfr/src/appfreeze_cpu_freq_manager.cpp @@ -31,16 +31,14 @@ #include "time_util.h" #include "appfreeze_util.h" #include "dfx_define.h" +#include "fault_data.h" namespace OHOS { namespace AppExecFwk { namespace { constexpr int64_t HZ_TO_MHZ = 1000; - constexpr int32_t CPU_NUM = 11; - constexpr uint32_t CPU_FREQ_AND_TIME_NUM = 5; - constexpr uint32_t CPU_FREQ_NUM = 0; + constexpr size_t CPU_FREQ_AND_TIME_NUM = 5; constexpr int CPU_FREQ_DECIMAL_BASE = 10; - constexpr int CPU_HANDING_MIN_SIZE = 1; constexpr float CPU_PERCENTAGE = 100.0f; constexpr float CPU_MIN_FREQ_USAGE_VALUE = 1.0f; constexpr uint32_t TIME_IN_STATE_FIRST_INDEX = 0; @@ -56,14 +54,16 @@ std::shared_ptr AppfreezeCpuFreqManager::instance_ = nu ffrt::mutex AppfreezeCpuFreqManager::singletonMutex_; ffrt::mutex AppfreezeCpuFreqManager::freezeInfoMutex_; std::vector> AppfreezeCpuFreqManager::handlingHalfCpuData_; -int64_t AppfreezeCpuFreqManager::halfTime_ = 0; +uint64_t AppfreezeCpuFreqManager::halfTime_ = 0; uint64_t AppfreezeCpuFreqManager::optimalCpuTime_ = 0; std::string AppfreezeCpuFreqManager::stackPath_ = ""; std::vector AppfreezeCpuFreqManager::totalTimeList_; +int AppfreezeCpuFreqManager::cpuCount_ = 0; typedef double (*GetThreadCpuload)(int); AppfreezeCpuFreqManager::AppfreezeCpuFreqManager() { + cpuCount_ = AppfreezeUtil::GetCpuCount(); } AppfreezeCpuFreqManager::~AppfreezeCpuFreqManager() @@ -105,11 +105,11 @@ void AppfreezeCpuFreqManager::InitHalfCpuInfo(int32_t pid) void AppfreezeCpuFreqManager::ParseCpuData(std::vector>& datas, std::vector& totalTimeLists) { - TAG_LOGW(AAFwkTag::APPDFR, "ParseCpuData start time: %{public}s", + TAG_LOGD(AAFwkTag::APPDFR, "ParseCpuData start time: %{public}s", AbilityRuntime::TimeUtil::DefaultCurrentTimeStr().c_str()); std::string tmp = "start time: " + AbilityRuntime::TimeUtil::DefaultCurrentTimeStr(); TAG_LOGW(AAFwkTag::APPDFR, "ParseCpuData called, %{public}s", tmp.c_str()); - for (int32_t i = 0; i <= CPU_NUM; ++i) { + for (int32_t i = 0; i <= cpuCount_; ++i) { std::vector parseDatas; TotalTime totalTime; if (ReadCpuDataByNum(i, parseDatas, totalTime)) { @@ -117,7 +117,7 @@ void AppfreezeCpuFreqManager::ParseCpuData(std::vector> totalTimeLists.push_back(totalTime); } } - TAG_LOGW(AAFwkTag::APPDFR, "ParseCpuData end time: %{public}s", + TAG_LOGD(AAFwkTag::APPDFR, "ParseCpuData end time: %{public}s", AbilityRuntime::TimeUtil::DefaultCurrentTimeStr().c_str()); } @@ -126,7 +126,7 @@ bool AppfreezeCpuFreqManager::ReadCpuDataByNum(int32_t num, std::vector CPU_NUM) { + if (num > cpuCount_) { TAG_LOGE(AAFwkTag::APPDFR, "Read cpu info failed, num:%{public}d", num); return false; } @@ -182,19 +182,22 @@ std::string AppfreezeCpuFreqManager::GetCpuStr(int code, std::vector totalTimeList_, +bool AppfreezeCpuFreqManager::GetCpuTotalValue(size_t i, std::vector totalTimeList, std::vector blockTotalTimeList, TotalTime& totalTime) { - totalTime.totalCpuTime = totalTimeList_[i].totalCpuTime > blockTotalTimeList[i].totalCpuTime ? - (totalTimeList_[i].totalCpuTime - blockTotalTimeList[i].totalCpuTime) : - (blockTotalTimeList[i].totalCpuTime - totalTimeList_[i].totalCpuTime); + if (totalTimeList.size() <= i || totalTimeList.size() != blockTotalTimeList.size()) { + return false; + } + totalTime.totalCpuTime = totalTimeList[i].totalCpuTime > blockTotalTimeList[i].totalCpuTime ? + (totalTimeList[i].totalCpuTime - blockTotalTimeList[i].totalCpuTime) : + (blockTotalTimeList[i].totalCpuTime - totalTimeList[i].totalCpuTime); if (totalTime.totalCpuTime <= 0) { TAG_LOGE(AAFwkTag::APPDFR, "totalCpuTime:%{public}" PRIu64"less than zero.", totalTime.totalCpuTime); return false; } - totalTime.totalRunningTime = totalTimeList_[i].totalRunningTime > blockTotalTimeList[i].totalRunningTime ? - (totalTimeList_[i].totalRunningTime - blockTotalTimeList[i].totalRunningTime) : - (blockTotalTimeList[i].totalRunningTime - totalTimeList_[i].totalRunningTime); + totalTime.totalRunningTime = totalTimeList[i].totalRunningTime > blockTotalTimeList[i].totalRunningTime ? + (totalTimeList[i].totalRunningTime - blockTotalTimeList[i].totalRunningTime) : + (blockTotalTimeList[i].totalRunningTime - totalTimeList[i].totalRunningTime); return true; } @@ -250,6 +253,9 @@ std::string AppfreezeCpuFreqManager::GetCpuInfoContent() uint64_t AppfreezeCpuFreqManager::GetAppCpuTime(int32_t pid) { int32_t cpuTime = 0; + if (pid < 0) { + return cpuTime; + } std::string filePath = "/proc/" + std::to_string(pid) + "/task/" + std::to_string(pid) + "/stat"; std::string content; if (!LoadStringFromFile(filePath, content)) { @@ -271,7 +277,7 @@ uint64_t AppfreezeCpuFreqManager::GetAppCpuTime(int32_t pid) return cpuTime; } -uint32_t AppfreezeCpuFreqManager::GetProcessCpuTime(int32_t pid) +uint64_t AppfreezeCpuFreqManager::GetProcessCpuTime(int32_t pid) { if (pid < 0) { return 0; @@ -282,7 +288,7 @@ uint32_t AppfreezeCpuFreqManager::GetProcessCpuTime(int32_t pid) TAG_LOGE(AAFwkTag::APPDFR, "Read cpu stat failed, path:%{public}s", statPath.c_str()); return 0; } - uint32_t processCpuTime = 0; + uint64_t processCpuTime = 0; if (!content.empty()) { std::vector tokens; SplitStr(content, " ", tokens); @@ -290,15 +296,15 @@ uint32_t AppfreezeCpuFreqManager::GetProcessCpuTime(int32_t pid) TAG_LOGE(AAFwkTag::APPDFR, "GetAppCpuTime failed, content size: %{public}zu.", tokens.size()); return processCpuTime; } - processCpuTime = static_cast(strtoull(tokens[START_TIME_FIRST_INDEX].c_str(), + processCpuTime = static_cast(strtoull(tokens[START_TIME_FIRST_INDEX].c_str(), nullptr, CPU_FREQ_DECIMAL_BASE)); - processCpuTime += static_cast(strtoull(tokens[START_TIME_SECOND_INDEX].c_str(), + processCpuTime += static_cast(strtoull(tokens[START_TIME_SECOND_INDEX].c_str(), nullptr, CPU_FREQ_DECIMAL_BASE)); } return processCpuTime; } -uint32_t AppfreezeCpuFreqManager::GetDeviceRuntime() +uint64_t AppfreezeCpuFreqManager::GetDeviceRuntime() { std::string statPath = "/proc/stat"; std::string content; @@ -306,18 +312,18 @@ uint32_t AppfreezeCpuFreqManager::GetDeviceRuntime() TAG_LOGE(AAFwkTag::APPDFR, "Read device run time failed, path:%{public}s", statPath.c_str()); return 0; } - uint32_t deviceRuntime = 0; + uint64_t deviceRuntime = 0; std::string line; std::istringstream iss(content); - if (std::getline(iss, line) && !content.empty()) { + if (std::getline(iss, line) && !line.empty()) { std::vector strings; - SplitStr(content, " ", strings); - if (strings.size() <= 0) { - TAG_LOGE(AAFwkTag::APPDFR, "GetDeviceRuntime failed, content size: %{public}zu.", strings.size()); + SplitStr(line, " ", strings); + if (strings.size() <= 1) { + TAG_LOGE(AAFwkTag::APPDFR, "GetDeviceRuntime failed, string size: %{public}zu.", strings.size()); return deviceRuntime; } for (size_t i = 1; i < strings.size(); ++i) { - deviceRuntime += static_cast(strtoull(strings[i].c_str(), nullptr, + deviceRuntime += static_cast(strtoull(strings[i].c_str(), nullptr, CPU_FREQ_DECIMAL_BASE)); } } @@ -326,18 +332,24 @@ uint32_t AppfreezeCpuFreqManager::GetDeviceRuntime() double AppfreezeCpuFreqManager::GetOptimalCpuTime(int32_t pid) { + int maxCount = cpuCount_ - AppfreezeUtil::CPU_COUNT_SUBTRACT; + if (maxCount <= 0) { + TAG_LOGE(AAFwkTag::APPDFR, "GetOptimalCpuTime failed, maxCount:%{public}d", maxCount); + return -1; + } std::string statPath = "/sys/devices/system/cpu/cpu11/cpu_capacity"; std::string content; int ret = -1; if (!LoadStringFromFile(statPath, content)) { - TAG_LOGE(AAFwkTag::APPDFR, "GetOptimalCpuTime failed, path:%{public}s", statPath.c_str()); + TAG_LOGE(AAFwkTag::APPDFR, "GetOptimalCpuTime failed, path:%{public}s, errno:%{public}d", + statPath.c_str(), errno); return ret; } if (content.empty()) { TAG_LOGE(AAFwkTag::APPDFR, "Read info failed, path:%{public}s", statPath.c_str()); return ret; } - int32_t dmips = static_cast(strtoull(content.c_str(), nullptr, CPU_FREQ_DECIMAL_BASE)); + int32_t dmips = static_cast(strtoull(content.c_str(), nullptr, CPU_FREQ_DECIMAL_BASE)); void* threadFuncHandler = dlopen(LIB_THREAD_CPU_LOAD_PATH, RTLD_LAZY); if (threadFuncHandler == nullptr) { TAG_LOGE(AAFwkTag::APPDFR, "dlopen failed, funcHandler is nullptr."); @@ -355,7 +367,7 @@ double AppfreezeCpuFreqManager::GetOptimalCpuTime(int32_t pid) threadFuncHandler = nullptr; getThreadCpuload = nullptr; dlclose(threadFuncHandler); - if (dmips <= 0) { + if (dmips <= 0 || optimalCpuTime <= 0) { return ret; } TAG_LOGW(AAFwkTag::APPDFR, "dmips %{public}d optimalCpuTime %{public}lf", dmips, optimalCpuTime); @@ -423,7 +435,7 @@ void AppfreezeCpuFreqManager::WriteDfxLogToFile(const std::string& filePath, con ss << LOG_FILE_SEP << std::endl; ss << "TimeStamp: " << AbilityRuntime::TimeUtil::DefaultCurrentTimeStr() << std::endl; ss << "Module name: " << bundleName << std::endl; - OHOS::SaveStringToFile(filePath, ss.str(), false); + OHOS::SaveStringToFile(filePath, ss.str()); } void AppfreezeCpuFreqManager::Clear() @@ -435,10 +447,10 @@ void AppfreezeCpuFreqManager::Clear() handlingHalfCpuData_.clear(); } -std::string AppfreezeCpuFreqManager::WriteCpuInfoToFile(const std::string& bundleName, - int32_t uid, int32_t pid) +std::string AppfreezeCpuFreqManager::WriteCpuInfoToFile(const std::string &bundleName, + int32_t uid, int32_t pid, const std::string &eventName) { - std::string fileName = "cpu_info"; + std::string fileName = "cpu-info-" + AbilityRuntime::TimeUtil::FormatTime("%Y%m%d%H%M%S"); std::string filePath = AppfreezeUtil::CreateFile(AppfreezeUtil::EVENTLOG_PATH, fileName); WriteDfxLogToFile(filePath, bundleName); std::ostringstream str; @@ -447,7 +459,7 @@ std::string AppfreezeCpuFreqManager::WriteCpuInfoToFile(const std::string& bundl std::lock_guard lock(freezeInfoMutex_); str << std::endl << GetStaticInfo(pid); str << std::endl << "#CpuFreq Usage (usage >=1%)" << std::endl << GetCpuInfoContent() << std::endl; - path = filePath + "," + stackPath_; + path = (eventName == AppFreezeType::LIFECYCLE_TIMEOUT) ? filePath : (filePath + "," + stackPath_); Clear(); } OHOS::SaveStringToFile(filePath, str.str(), false); diff --git a/services/appdfr/src/appfreeze_manager.cpp b/services/appdfr/src/appfreeze_manager.cpp index 959355a6628..818ab823e73 100644 --- a/services/appdfr/src/appfreeze_manager.cpp +++ b/services/appdfr/src/appfreeze_manager.cpp @@ -420,7 +420,7 @@ std::string AppfreezeManager::ReportAppfreezeCpuInfo(const FaultData& faultData, } else if (faultData.errorObject.name == AppFreezeType::THREAD_BLOCK_6S || faultData.errorObject.name == AppFreezeType::LIFECYCLE_TIMEOUT) { filePath = AppExecFwk::AppfreezeCpuFreqManager::GetInstance()->WriteCpuInfoToFile(appInfo.bundleName, - appInfo.uid, appInfo.pid); + appInfo.uid, appInfo.pid, faultData.errorObject.name); } TAG_LOGI(AAFwkTag::APPDFR, "appfreezeInfo:%{public}s, filePath:%{public}s", faultData.appfreezeInfo.c_str(), filePath.c_str()); diff --git a/services/appdfr/src/appfreeze_util.cpp b/services/appdfr/src/appfreeze_util.cpp index 283e9162bb7..6afe51973b2 100644 --- a/services/appdfr/src/appfreeze_util.cpp +++ b/services/appdfr/src/appfreeze_util.cpp @@ -20,8 +20,9 @@ #include #include -#include "file_ex.h" #include "directory_ex.h" +#include "file_ex.h" +#include "string_ex.h" #include "hilog_tag_wrapper.h" namespace OHOS { @@ -31,6 +32,7 @@ namespace { constexpr mode_t DEFAULT_LOG_DIR_MODE = 0770; constexpr mode_t DEFAULT_LOG_FILE_MODE = 0644; constexpr uint32_t TWO_DECIMALS = 2; + constexpr size_t CPU_INFO_SIZE = 11; } AppfreezeUtil::AppfreezeUtil() @@ -83,5 +85,37 @@ std::string AppfreezeUtil::RoundToTwoDecimals(float value) ss<< std::fixed << std::setprecision(TWO_DECIMALS) << value; return ss.str(); } + +int AppfreezeUtil::GetCpuCount() +{ + std::string procStatPath = "/proc/stat"; + std::string content; + if (!LoadStringFromFile(procStatPath, content) || content.empty()) { + TAG_LOGW(AAFwkTag::APPDFR, "failed to read path:%{public}s, errno:%{public}d", + procStatPath.c_str(), errno); + return 0; + } + int cpuCount = 0; + std::istringstream iss(content); + std::string line; + while (std::getline(iss, line)) { + if (line.empty()) { + continue; + } + std::vector splitStrs; + SplitStr(line, " ", splitStrs); + if (splitStrs.size() != CPU_INFO_SIZE) { + break; + } + if (splitStrs[0].find("cpu") != 0) { + TAG_LOGW(AAFwkTag::APPDFR, "not find cpu prefix, head: %{public}s.", splitStrs[0].c_str()); + break; + } + cpuCount++; + } + cpuCount -= CPU_COUNT_SUBTRACT; + TAG_LOGD(AAFwkTag::APPDFR, "read: %{public}s to get cpu count:%{public}d.", procStatPath.c_str(), cpuCount); + return cpuCount; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/test/unittest/dfr_test/BUILD.gn b/test/unittest/dfr_test/BUILD.gn index 47e40e97143..ab67464aedd 100644 --- a/test/unittest/dfr_test/BUILD.gn +++ b/test/unittest/dfr_test/BUILD.gn @@ -16,6 +16,7 @@ group("unittest") { deps = [] deps += [ + "appfreeze_cpu_freq_manager_test:unittest", "appfreeze_inner_test:unittest", "appfreeze_manager_test:unittest", "appfreeze_state_test:unittest", diff --git a/test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/BUILD.gn b/test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/BUILD.gn new file mode 100644 index 00000000000..5fd8e2bcc8d --- /dev/null +++ b/test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/BUILD.gn @@ -0,0 +1,56 @@ +# Copyright (c) 2025 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. + +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/freeze_checker" + +############################################################################### + +ohos_unittest("appfreeze_cpu_freq_manager_test") { + module_out_path = module_output_path + + include_dirs = [ + "${ability_runtime_services_path}/appdfr/include", + ] + + sources = [ "appfreeze_cpu_freq_manager_test.cpp" ] + + deps = [ + "${ability_runtime_path}/utils/global/freeze:freeze_util", + "${ability_runtime_services_path}/abilitymgr:abilityms", + ] + + external_deps = [ + "ability_runtime:app_manager", + "ffrt:libffrt", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + ] + + defines = [] + if (ability_runtime_hitrace_enable) { + external_deps += [ "hitrace:libhitracechain" ] + defines += [ "ABILITY_RUNTIME_HITRACE_ENABLE" ] + } +} + +############################################################################### + +group("unittest") { + testonly = true + deps = [ ":appfreeze_cpu_freq_manager_test" ] +} diff --git a/test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/appfreeze_cpu_freq_manager_test.cpp b/test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/appfreeze_cpu_freq_manager_test.cpp new file mode 100644 index 00000000000..d6845cbf6a1 --- /dev/null +++ b/test/unittest/dfr_test/appfreeze_cpu_freq_manager_test/appfreeze_cpu_freq_manager_test.cpp @@ -0,0 +1,298 @@ +/* + * Copyright (c) 2025 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 + +#define private public +#include "appfreeze_cpu_freq_manager.h" +#undef private + +#include "appfreeze_data.h" +#include "appfreeze_util.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::AppExecFwk; + +namespace OHOS { +namespace AppExecFwk { +class AppfreezeCpuFreqManagerTest : public testing::Test { +public: + AppfreezeCpuFreqManagerTest() + {} + ~AppfreezeCpuFreqManagerTest() + {} + std::shared_ptr appfreezeCpuFreqManager_ = nullptr; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void AppfreezeCpuFreqManagerTest::SetUpTestCase(void) +{} + +void AppfreezeCpuFreqManagerTest::TearDownTestCase(void) +{} + +void AppfreezeCpuFreqManagerTest::SetUp(void) +{ + appfreezeCpuFreqManager_ = AppfreezeCpuFreqManager::GetInstance(); +} + +void AppfreezeCpuFreqManagerTest::TearDown(void) +{ + AppfreezeCpuFreqManager::DestroyInstance(); +} + +/** + * @tc.number: SetHalfStackPathTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, SetHalfStackPathTest_001, TestSize.Level0) +{ + appfreezeCpuFreqManager_->SetHalfStackPath(""); + EXPECT_TRUE(appfreezeCpuFreqManager_->stackPath_.empty()); + appfreezeCpuFreqManager_->SetHalfStackPath("AppfreezeCpuFreqManagerTest_001"); + EXPECT_TRUE(!appfreezeCpuFreqManager_->stackPath_.empty()); +} + +/** + * @tc.number: InitHalfCpuInfoTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, InitHalfCpuInfoTest_001, TestSize.Level1) +{ + appfreezeCpuFreqManager_->InitHalfCpuInfo(getpid()); + appfreezeCpuFreqManager_->InitHalfCpuInfo(getpid()); + EXPECT_TRUE(appfreezeCpuFreqManager_->handlingHalfCpuData_.size() != 0); + appfreezeCpuFreqManager_->Clear(); +} + +/** + * @tc.number: InitHalfCpuInfoTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, ReadCpuDataByNumTest_001, TestSize.Level1) +{ + std::vector parseDatas; + TotalTime totalTime; + int32_t num = 100; + appfreezeCpuFreqManager_->ReadCpuDataByNum(num, parseDatas, totalTime); + EXPECT_TRUE(parseDatas.size() == 0); + num = 0; + appfreezeCpuFreqManager_->ReadCpuDataByNum(num, parseDatas, totalTime); + EXPECT_TRUE(parseDatas.size() != 0); +} + +/** + * @tc.number: GetCpuStrTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, GetCpuStrTest_001, TestSize.Level1) +{ + int code = 0; + std::vector freqPairs; + float percentage = 44.1000f; + std::string str = appfreezeCpuFreqManager_->GetCpuStr(code, freqPairs, percentage); + EXPECT_TRUE(!str.empty()); + FrequencyPair freqPair1 = { + .frequency = 100, + .percentage = 10.000f, + }; + FrequencyPair freqPair2 = { + .frequency = 120, + .percentage = 33.000f, + }; + FrequencyPair freqPair3 = { + .frequency = 50, + .percentage = 1.000f, + }; + freqPairs.push_back(freqPair1); + freqPairs.push_back(freqPair2); + freqPairs.push_back(freqPair3); + str = appfreezeCpuFreqManager_->GetCpuStr(code, freqPairs, percentage); + EXPECT_TRUE(!str.empty()); +} + +/** + * @tc.number: GetCpuTotalValueTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, GetCpuTotalValueTest_001, TestSize.Level1) +{ + TotalTime time1 = { + .totalRunningTime = 50, + .totalCpuTime = 1000, + }; + std::vector totalTimeList; + totalTimeList.push_back(time1); + std::vector blockTotalTimeList; + TotalTime totalTime; + bool ret = appfreezeCpuFreqManager_->GetCpuTotalValue(0, totalTimeList, blockTotalTimeList, totalTime); + EXPECT_TRUE(!ret); + TotalTime time2 = { + .totalRunningTime = 100, + .totalCpuTime = 1000, + }; + blockTotalTimeList.push_back(time2); + ret = appfreezeCpuFreqManager_->GetCpuTotalValue(0, totalTimeList, blockTotalTimeList, totalTime); + EXPECT_TRUE(!ret); + blockTotalTimeList.clear(); + TotalTime time3 = { + .totalRunningTime = 10, + .totalCpuTime = 20, + }; + blockTotalTimeList.push_back(time3); + ret = appfreezeCpuFreqManager_->GetCpuTotalValue(0, totalTimeList, blockTotalTimeList, totalTime); + EXPECT_TRUE(ret); +} + +/** + * @tc.number: GetCpuInfoContentTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, GetCpuInfoContentTest_001, TestSize.Level1) +{ + std::string ret = appfreezeCpuFreqManager_->GetCpuInfoContent(); + appfreezeCpuFreqManager_->Clear(); + EXPECT_TRUE(ret.empty()); + appfreezeCpuFreqManager_->InitHalfCpuInfo(getpid()); + EXPECT_TRUE(appfreezeCpuFreqManager_->handlingHalfCpuData_.size() > 0); + ret = appfreezeCpuFreqManager_->GetCpuInfoContent(); + EXPECT_TRUE(!ret.empty()); + appfreezeCpuFreqManager_->Clear(); +} + +/** + * @tc.number: GetAppCpuTimeTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, GetAppCpuTimeTest_001, TestSize.Level1) +{ + uint64_t ret = appfreezeCpuFreqManager_->GetAppCpuTime(-1); + EXPECT_TRUE(ret == 0); + ret = appfreezeCpuFreqManager_->GetAppCpuTime(getpid()); + EXPECT_TRUE(ret >= 0); + ret = appfreezeCpuFreqManager_->GetAppCpuTime(12345670); + EXPECT_TRUE(ret == 0); +} + +/** + * @tc.number: GetProcessCpuTimeTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, GetProcessCpuTimeTest_001, TestSize.Level0) +{ + uint64_t ret = appfreezeCpuFreqManager_->GetProcessCpuTime(-1); + EXPECT_TRUE(ret == 0); + ret = appfreezeCpuFreqManager_->GetProcessCpuTime(getpid()); + EXPECT_TRUE(ret >= 0); + ret = appfreezeCpuFreqManager_->GetProcessCpuTime(12345670); + EXPECT_TRUE(ret == 0); +} + +/** + * @tc.number: GetDeviceRuntimeTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, GetDeviceRuntimeTest_001, TestSize.Level0) +{ + uint64_t ret = appfreezeCpuFreqManager_->GetDeviceRuntime(); + EXPECT_TRUE(ret >= 0); +} + +/** + * @tc.number: GetOptimalCpuTimeTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, GetOptimalCpuTimeTest_001, TestSize.Level0) +{ + uint64_t ret = appfreezeCpuFreqManager_->GetOptimalCpuTime(getpid()); + EXPECT_TRUE(ret >= 0); +} + +/** + * @tc.number: GetStartTimeTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, GetStartTimeTest_001, TestSize.Level0) +{ + uint64_t start = AppfreezeUtil::GetMilliseconds(); + std::string ret = appfreezeCpuFreqManager_->GetStartTime(start); + EXPECT_TRUE(!ret.empty()); +} + +/** + * @tc.number: GetStaticInfoTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, GetStaticInfoTest_001, TestSize.Level0) +{ + std::string ret = appfreezeCpuFreqManager_->GetStaticInfo(getpid()); + EXPECT_TRUE(!ret.empty()); +} + +/** + * @tc.number: GetStaticInfoHeadTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, GetStaticInfoHeadTest_001, TestSize.Level0) +{ + std::string ret = appfreezeCpuFreqManager_->GetStaticInfoHead(); + EXPECT_TRUE(!ret.empty()); +} + +/** + * @tc.number: WriteDfxLogToFileTest_001 + * @tc.desc: add testcase + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, WriteDfxLogToFileTest_001, TestSize.Level0) +{ + appfreezeCpuFreqManager_->WriteDfxLogToFile("filePath", "bundleName"); + EXPECT_TRUE(appfreezeCpuFreqManager_); +} + +/** + * @tc.number: WriteCpuInfoToFileTest_001 + * @tc.desc: add testcase codecoverage + * @tc.type: FUNC + */ +HWTEST_F(AppfreezeCpuFreqManagerTest, WriteCpuInfoToFileTest_001, TestSize.Level0) +{ + std::string ret = appfreezeCpuFreqManager_->WriteCpuInfoToFile("AppfreezeCpuFreqManagerTest", + getuid(), getpid(), "AppfreezeCpuFreqManagerTest"); + EXPECT_TRUE(!ret.empty()); + ret = appfreezeCpuFreqManager_->WriteCpuInfoToFile("AppfreezeCpuFreqManagerTest", + getuid(), getpid(), "LIFECYCLE_TIMEOUT"); + EXPECT_TRUE(!ret.empty()); +} +} // namespace AppExecFwk +} // namespace OHOS -- Gitee