diff --git a/include/utilities.h b/include/utilities.h index f822878074bf71cb8f960588a5416d9f02e740e2..be195b88e795bf3b495187b7f6cd21951e3f1523 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -343,6 +343,7 @@ bool IsStringToIntSuccess(const std::string &str, int &num); bool IsDirectoryExists(const std::string& fileName); bool CreateDirectory(const std::string& path, mode_t mode); bool IsValidOutPath(const std::string& path); +void AgeHiperflogFiles(); const std::string HMKERNEL = "HongMeng"; diff --git a/src/subcommand_dump.cpp b/src/subcommand_dump.cpp index 2b770038e7cdfb0b7b43d17499d7ea504324fbb8..f86fdc876ca4a19c2aa00113031746b3446a45e5 100644 --- a/src/subcommand_dump.cpp +++ b/src/subcommand_dump.cpp @@ -207,7 +207,7 @@ HiperfError SubCommandDump::OnSubCommand(std::vector& args) if (dumpFeatures_ || dumpAll_) { DumpFeaturePortion(indent_); } - + AgeHiperflogFiles(); return HiperfError::NO_ERR; } diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index 9959d19710476dcbeaf03f7bdc7c41b5b1727912..3f52413e3e2cb6cf8008b0681d34a2f0936aea05 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -1555,6 +1555,7 @@ HiperfError SubCommandRecord::OnSubCommand(std::vector& args) OnlineReportData(); CloseClientThread(); RemoveVdsoTmpFile(); + AgeHiperflogFiles(); HIPERF_HILOGI(MODULE_DEFAULT, "SubCommandRecord finish"); return HiperfError::NO_ERR; } diff --git a/src/subcommand_report.cpp b/src/subcommand_report.cpp index ed5984e05b591eb74f0bdad7b3815caf0525e480..325c872cb5d198e0dc8e59061890839bac3c3eed 100644 --- a/src/subcommand_report.cpp +++ b/src/subcommand_report.cpp @@ -618,6 +618,7 @@ HiperfError SubCommandReport::OnSubCommand(std::vector& args) #endif printf("report done\n"); + AgeHiperflogFiles(); return HiperfError::NO_ERR; } diff --git a/src/utilities.cpp b/src/utilities.cpp index 692d8212a2b1bdf2e2117407a0647bb88e8d3c3a..f0623bdc7d57ff8efd96c6fb1c17d3440e320120 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -27,6 +27,7 @@ #ifdef CONFIG_HAS_CCM #include "config_policy_utils.h" #endif +#include "directory_ex.h" #include "hiperf_hilog.h" #include "ipc_utilities.h" @@ -896,45 +897,6 @@ bool IsNumeric(const std::string& str) return true; } -bool IsDirectoryExists(const std::string& fileName) -{ - struct stat fileInfo; - if (stat(fileName.c_str(), &fileInfo) == 0) { - return S_ISDIR(fileInfo.st_mode); - } - return false; -} - -bool CreateDirectory(const std::string& path, mode_t mode) -{ -#if defined(is_ohos) && is_ohos - std::string::size_type pos = 0; - do { - pos = path.find('/', pos + 1); - std::string subPath = (pos == std::string::npos) ? path : path.substr(0, pos); - if (access(subPath.c_str(), F_OK) != 0) { - if (mkdir(subPath.c_str(), mode) != 0) { - return false; - } - } - } while (pos != std::string::npos); - return access(path.c_str(), F_OK) == 0; -#else - return false; -#endif -} - -bool IsValidOutPath(const std::string& path) -{ - std::vector fineName = {"/data/log/hiperflog/", "data/log/hiperflog/"}; - for (auto name : fineName) { - if (StringStartsWith(path, name)) { - return false; - } - } - return true; -} - #ifdef CONFIG_HAS_CCM cJSON* GetProductCfgRoot(const char* cfgPath) { @@ -989,6 +951,69 @@ bool GetCfgValue(const char* cfgPath, const char* cfgKey, size_t &value) return ret; } #endif + +bool IsDirectoryExists(const std::string& fileName) +{ + struct stat fileInfo; + if (stat(fileName.c_str(), &fileInfo) == 0) { + return S_ISDIR(fileInfo.st_mode); + } + return false; +} + +bool CreateDirectory(const std::string& path, mode_t mode) +{ +#if defined(is_ohos) && is_ohos + std::string::size_type pos = 0; + do { + pos = path.find('/', pos + 1); + std::string subPath = (pos == std::string::npos) ? path : path.substr(0, pos); + if (access(subPath.c_str(), F_OK) != 0) { + if (mkdir(subPath.c_str(), mode) != 0) { + return false; + } + } + } while (pos != std::string::npos); + return access(path.c_str(), F_OK) == 0; +#else + return false; +#endif +} + +bool IsValidOutPath(const std::string& path) +{ + std::vector fileName = {"/data/log/hiperflog/", "data/log/hiperflog/"}; + for (auto name : fileName) { + if (StringStartsWith(path, name)) { + return false; + } + } + return true; +} + +void AgeHiperflogFiles() +{ +#if defined(is_ohos) && is_ohos + std::vector allFiles; + OHOS::GetDirFiles("/data/log/hiperflog/", allFiles); + std::set fileNames = {"/data/log/hiperflog/[shmm]", "/data/log/hiperflog/[vdso]", + "/data/log/hiperflog/.hiperf_record_control_c2s", + "/data/log/hiperflog/.hiperf_record_control_s2c", + "/data/log/hiperflog/.hiperf_stat_control_c2s", + "/data/log/hiperflog/.hiperf_stat_control_s2c"}; + for (std::string file : allFiles) { + if (fileNames.count(file)) { + HLOGD("the file is %s,not need to delete", file.c_str()); + continue; + } + if (!OHOS::RemoveFile(file)) { + HIPERF_HILOGI(MODULE_DEFAULT, "remove hiperflog file : %{public}s failed", file.c_str()); + continue; + } + HIPERF_HILOGI(MODULE_DEFAULT, "remove hiperflog file : %{public}s", file.c_str()); + } +#endif +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/test/unittest/common/native/utilities_test.cpp b/test/unittest/common/native/utilities_test.cpp index ff8f18662700df09441bd6a5a701a3e8be338a2c..0d20993012869a4e392f9b24ac0a2e6438d76b26 100644 --- a/test/unittest/common/native/utilities_test.cpp +++ b/test/unittest/common/native/utilities_test.cpp @@ -869,7 +869,7 @@ HWTEST_F(UtilitiesTest, IsDirectoryExists, TestSize.Level1) HWTEST_F(UtilitiesTest, CreateDirectory, TestSize.Level1) { std::string file = "/data/local/tmp/hiperf_test"; - EXPECT_EQ(CreateDirectory(file, HIPERF_FILE_PERM_770), true); + EXPECT_TRUE(CreateDirectory(file, HIPERF_FILE_PERM_770)); rmdir(file.c_str()); } @@ -881,7 +881,7 @@ HWTEST_F(UtilitiesTest, CreateDirectory, TestSize.Level1) HWTEST_F(UtilitiesTest, IsValidOutPath, TestSize.Level1) { std::string file = "/data/local/tmp/perf.data"; - EXPECT_EQ(IsValidOutPath(file), true); + EXPECT_TRUE(IsValidOutPath(file)); } /** @@ -892,7 +892,7 @@ HWTEST_F(UtilitiesTest, IsValidOutPath, TestSize.Level1) HWTEST_F(UtilitiesTest, IsValidOutPathErr, TestSize.Level2) { std::string file = "/data/log/hiperflog/perf.data"; - EXPECT_EQ(IsValidOutPath(file), false); + EXPECT_FALSE(IsValidOutPath(file)); } } // namespace HiPerf } // namespace Developtools