diff --git a/etc/hiperf.cfg b/etc/hiperf.cfg index 34a1b659ca9ae497bd7a22979099a24eca96e4d8..b744ca84fae07aa6ba7cb308ddad494ea7e5c0fa 100644 --- a/etc/hiperf.cfg +++ b/etc/hiperf.cfg @@ -1,5 +1,11 @@ { "jobs": [{ + "name": "post-fs-data", + "cmds": [ + "mkdir /data/log/hiperflog 0770 shell shell", + "restorecon /data/log/hiperflog" + ] + }, { "name": "param:security.perf_harden=0", "condition": "security.perf_harden=0", "cmds": [ diff --git a/include/utilities.h b/include/utilities.h index b8923343f9ec08e4f58425b157b42ec1bcb14991..8f21a7786d51498ca228d4c5a62a74816fa6cd54 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -101,7 +101,7 @@ const std::set ALLOW_UIDS = {1201}; const std::string SAVED_CMDLINES = "/sys/kernel/tracing/saved_cmdlines"; static FILE *g_outputDump = nullptr; const uint64_t waitAppRunCheckTimeOut = 20; - +constexpr mode_t HIPERF_FILE_PERM_770 = S_IRWXU | S_IRWXG; struct ThreadInfos { pid_t tid; pid_t pid; @@ -335,6 +335,8 @@ bool PowerOfTwo(uint64_t n); bool IsNumeric(const std::string& str); bool IscontainDigits(const std::string& str); bool IsStringToIntSuccess(const std::string &str, int &num); +bool IsFileExists(const std::string& fileName); +bool CreateDirectory(const std::string& path, mode_t mode); const std::string HMKERNEL = "HongMeng"; diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index 330aa753fd7f8bf7127c62d46690a08dfe95fdfb..a58bb8463f6c3f36a61e7fa23ffbcd048b1f4c62 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -51,8 +51,8 @@ const std::string CONTROL_CMD_PAUSE = "pause"; const std::string CONTROL_CMD_RESUME = "resume"; const std::string CONTROL_CMD_OUTPUT = "output"; const std::string CONTROL_CMD_STOP = "stop"; -const std::string CONTROL_FIFO_FILE_C2S = "/data/local/tmp/.hiperf_record_control_c2s"; -const std::string CONTROL_FIFO_FILE_S2C = "/data/local/tmp/.hiperf_record_control_s2c"; +const std::string CONTROL_FIFO_FILE_C2S = "/data/log/hiperflog/.hiperf_record_control_c2s"; +const std::string CONTROL_FIFO_FILE_S2C = "/data/log/hiperflog/.hiperf_record_control_s2c"; const std::string PERF_CPU_TIME_MAX_PERCENT = "/proc/sys/kernel/perf_cpu_time_max_percent"; const std::string PERF_EVENT_MAX_SAMPLE_RATE = "/proc/sys/kernel/perf_event_max_sample_rate"; @@ -1320,6 +1320,11 @@ bool SubCommandRecord::CreateFifoServer() { char errInfo[ERRINFOLEN] = { 0 }; const mode_t fifoMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; + std::string tempPath("/data/log/hiperflog/"); + if (!IsFileExists(tempPath)) { + HIPERF_HILOGI(MODULE_DEFAULT, "%{public}s not exist.", tempPath.c_str()); + CreateDirectory(tempPath, HIPERF_FILE_PERM_770); + } if (mkfifo(CONTROL_FIFO_FILE_S2C.c_str(), fifoMode) != 0 || mkfifo(CONTROL_FIFO_FILE_C2S.c_str(), fifoMode) != 0) { if (errno == EEXIST) { @@ -1542,7 +1547,7 @@ void SubCommandRecord::CloseClientThread() void SubCommandRecord::RemoveVdsoTmpFile() { - std::vector fileName = {"/data/local/tmp/[shmm]", "/data/local/tmp/[vdso]"}; + std::vector fileName = {"/data/log/hiperflog/[shmm]", "/data/log/hiperflog/[vdso]"}; for (auto name : fileName) { if (access(name.c_str(), F_OK) == 0) { if (remove(name.c_str()) != 0) { diff --git a/src/utilities.cpp b/src/utilities.cpp index 821870ee8f14576579df22ee11e8909f93b89cf9..28d61a330da08df58525032c14cf27482a8e19b7 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -892,6 +892,26 @@ bool IsNumeric(const std::string& str) } return true; } + +bool IsFileExists(const std::string& fileName) +{ + return OHOS::FileExists(fileName); +} + +bool CreateDirectory(const std::string& path, mode_t mode) +{ + 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; +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/src/virtual_runtime.cpp b/src/virtual_runtime.cpp index 62a0ab94505d38f0c5cd250a500b5ebfc710acfe..dd42669861f46e690ecf26211de825a054e49451 100644 --- a/src/virtual_runtime.cpp +++ b/src/virtual_runtime.cpp @@ -1275,7 +1275,11 @@ void VirtualRuntime::LoadVdso() std::string memory(map->end - map->begin, '\0'); std::copy(reinterpret_cast((map->begin)), reinterpret_cast((map->end)), &memory[0]); - std::string tempPath("/data/local/tmp/"); + std::string tempPath("/data/log/hiperflog/"); + if (!IsFileExists(tempPath)) { + HIPERF_HILOGI(MODULE_DEFAULT, "%{public}s not exist.", tempPath.c_str()); + CreateDirectory(tempPath, HIPERF_FILE_PERM_770); + } std::string tempFileName = tempPath + map->name; if (!WriteStringToFile(tempFileName, memory)) { printf("vdso temp file create fail at %s\n", tempFileName.c_str()); diff --git a/test/unittest/common/native/utilities_test.cpp b/test/unittest/common/native/utilities_test.cpp index ac152e4039311a9956d7907fc0b3109364c5575c..6a243dbe04b63bb626917091b5fa9559d4c5ae92 100644 --- a/test/unittest/common/native/utilities_test.cpp +++ b/test/unittest/common/native/utilities_test.cpp @@ -853,6 +853,28 @@ HWTEST_F(UtilitiesTest, IsArkJsFile, TestSize.Level1) EXPECT_EQ(IsArkJsFile("test.hqf"), true); EXPECT_EQ(IsArkJsFile("test.so"), false); } + +/** + * @tc.name: IsFileExists + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(UtilitiesTest, IsFileExists, TestSize.Level1) +{ + EXPECT_EQ(IsFileExists("/data/local/tmp"), true); +} + +/** + * @tc.name: CreateDirectory + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(UtilitiesTest, CreateDirectory, TestSize.Level1) +{ + std::string file = "/data/local/tmp/hiperf_test"; + EXPECT_EQ(CreateDirectory(file, HIPERF_FILE_PERM_770), true); + rmdir(file.c_str()); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS