From 73012035e59ea5caa4775853543b87a2be90bb3c Mon Sep 17 00:00:00 2001 From: leiguangyu Date: Tue, 12 Aug 2025 21:54:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=BE=93=E5=87=BA=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E9=80=82=E9=85=8Dpc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: If41af6405c0ec13558ba5fd471f4b46275508669 Signed-off-by: leiguangyu --- BUILD.gn | 13 +++++------ bundle.json | 3 +++ etc/hiperf.cfg | 2 +- hiperf.gni | 1 + include/subcommand_record.h | 12 ++++++++++ include/subcommand_stat.h | 4 ++++ include/utilities.h | 2 ++ src/subcommand_record.cpp | 2 ++ src/subcommand_stat.cpp | 8 ++++++- src/utilities.cpp | 15 +++++++++++++ test/BUILD.gn | 4 ++++ .../common/native/subcommand_record_test.cpp | 19 ++++++++++++++++ .../common/native/subcommand_stat_test.cpp | 22 ++++++++++++++++++- 13 files changed, 97 insertions(+), 10 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 677648e..5c33143 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -135,9 +135,7 @@ sources_platform_with_ts_common = [ ] if (hiperf_debug) { - sources_platform_with_ts_common += [ - "./src/debug_logger.cpp", - ] + sources_platform_with_ts_common += [ "./src/debug_logger.cpp" ] } sources_platform_common = [ @@ -157,9 +155,7 @@ if (is_ohos) { } if (hiperf_debug) { - sources_platform_common += [ - "./src/option_debug.cpp", - ] + sources_platform_common += [ "./src/option_debug.cpp" ] } sources_platform_linux = [ @@ -273,6 +269,9 @@ ohos_source_set("hiperf_platform_linux") { "init:libbegetutil", ] defines = [ "CONFIG_HAS_CCM" ] + if (hiperf_feature_pc) { + defines += [ "is_pc=${hiperf_feature_pc}" ] + } } else { external_deps = [ "faultloggerd:unwinder_host" ] } @@ -463,7 +462,7 @@ ohos_executable("hiperf") { ohos_executable("hiperf_host") { sources = [ "./src/main.cpp" ] - + deps = [ ":hiperf_platform_common" ] external_deps = [ diff --git a/bundle.json b/bundle.json index 6443937..0eb7d84 100644 --- a/bundle.json +++ b/bundle.json @@ -12,6 +12,9 @@ "component": { "name": "hiperf", "subsystem": "developtools", + "features": [ + "hiperf_feature_pc" + ], "adapted_system_type": [ "standard" ], diff --git a/etc/hiperf.cfg b/etc/hiperf.cfg index a87ab47..cdd12e5 100644 --- a/etc/hiperf.cfg +++ b/etc/hiperf.cfg @@ -2,7 +2,7 @@ "jobs": [{ "name": "post-fs-data", "cmds": [ - "mkdir /data/log/hiperflog 0770 shell shell", + "mkdir /data/log/hiperflog 0777 shell shell", "restorecon /data/log/hiperflog", "chmod 0666 /dev/lperf" ] diff --git a/hiperf.gni b/hiperf.gni index 8123bff..c066484 100644 --- a/hiperf.gni +++ b/hiperf.gni @@ -34,6 +34,7 @@ declare_args() { hiperf_independent_compilation = true bundle_framework_enable = false ability_base_enable = false + hiperf_feature_pc = false if (defined(global_parts_info) && defined(global_parts_info.bundlemanager_bundle_framework)) { bundle_framework_enable = true diff --git a/include/subcommand_record.h b/include/subcommand_record.h index 14eea9b..3a9e9d7 100644 --- a/include/subcommand_record.h +++ b/include/subcommand_record.h @@ -68,7 +68,11 @@ public: "Usage: hiperf record [options] [command [command-args]]\n" " Collect performance sampling information of running [command].\n" " The default options are: -c --cpu-limit 25 -d 10000.0 -e hw-cpu-cycles\n" +#if defined(is_pc) && is_pc + " -f 4000 -m 1024 -o /data/storage/el2/base/cache/perf.data.\n" +#else " -f 4000 -m 1024 -o /data/local/tmp/perf.data.\n" +#endif " -a\n" " Collect system-wide information.\n" " for measures all processes/threads\n" @@ -169,7 +173,11 @@ public: " --data-limit \n" " Stop recording after SIZE bytes of records. Default is unlimited.\n" " -o \n" +#if defined(is_pc) && is_pc + " Set output file name, default is specified by the TMPDIR environment variable.\n" +#else " Set output file name, default is /data/local/tmp/perf.data.\n" +#endif " -z\n" " Compress record data.\n" " --restart\n" @@ -248,7 +256,11 @@ private: int cmdlinesSize_ = DEFAULT_SAVED_CMDLINES_SIZE; int oldCmdlinesSize_ = 0; std::vector symbolDir_ = {}; +#if defined(is_pc) && is_pc + std::string outputFilename_ = "/data/storage/el2/base/cache/perf.data"; +#else std::string outputFilename_ = "/data/local/tmp/perf.data"; +#endif std::string appPackage_ = {}; int checkAppMs_ = DEFAULT_CHECK_APP_MS; std::string clockId_ = {}; diff --git a/include/subcommand_stat.h b/include/subcommand_stat.h index 6adfc88..e337a1d 100644 --- a/include/subcommand_stat.h +++ b/include/subcommand_stat.h @@ -90,7 +90,11 @@ public: " start: start counting\n" " stop: stop counting\n" " -o \n" +#if defined(is_pc) && is_pc + " Set output file name, default is specified by the TMPDIR environment variable.\n" +#else " Set output file name, default is /data/local/tmp/perf_stat.txt.\n" +#endif " Only restrain using with --control prepare.\n" // clang-format on ), diff --git a/include/utilities.h b/include/utilities.h index 8dca5e7..9d56a30 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -418,6 +418,8 @@ cJSON* ParseJson(const std::string &filePath); bool GetJsonNum(cJSON* tag, const char* key, size_t &value); bool GetCfgValue(const char* cfgPath, const char* cfgKey, size_t &value); #endif + +void GetDefaultPathByEnv(std::string& outputFilename, const std::string fileType); } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index b1f53e4..49d447a 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -589,6 +589,8 @@ bool SubCommandRecord::ParseOption(std::vector &args) #ifdef CONFIG_HAS_CCM GetMmapPagesCfg(); #endif + const std::string fileType = "perf.data"; + GetDefaultPathByEnv(outputFilename_, fileType); if (!GetOptions(args)) { return false; } diff --git a/src/subcommand_stat.cpp b/src/subcommand_stat.cpp index 41db0ee..87106ce 100644 --- a/src/subcommand_stat.cpp +++ b/src/subcommand_stat.cpp @@ -37,7 +37,11 @@ const uint16_t THOUSANDS_SEPARATOR = 3; namespace OHOS { namespace Developtools { namespace HiPerf { +#if defined(is_pc) && is_pc +const std::string DEFAULT_STAT_FILE = "/data/storage/el2/base/cache/perf_stat.txt"; +#else const std::string DEFAULT_STAT_FILE = "/data/local/tmp/perf_stat.txt"; +#endif // when there are many events, start record will take more time. const std::chrono::milliseconds CONTROL_WAITREPY_TIMEOUT = 2000ms; static std::map thread_map_; @@ -911,7 +915,7 @@ HiperfError SubCommandStat::CheckStatOption() if (!CheckRestartOption(appPackage_, targetSystemWide_, restart_, selectPids_)) { return HiperfError::CHECK_RESTART_OPTION_FAIL; } - + // check option if (!CheckSelectCpuPidOption()) { return HiperfError::CHECK_SELECT_CPU_PID_FAIL; @@ -1155,6 +1159,8 @@ bool SubCommandStat::CheckOutPutFile() } if (outputFilename_.empty()) { outputFilename_ = DEFAULT_STAT_FILE; + const std::string fileType = "perf_stat.txt"; + GetDefaultPathByEnv(outputFilename_, fileType); } if (!IsValidOutPath(outputFilename_)) { printf("Invalid output file path, permission denied\n"); diff --git a/src/utilities.cpp b/src/utilities.cpp index 696b783..04535c7 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -1074,6 +1074,21 @@ void AgeHiperflogFiles() } #endif } + +void GetDefaultPathByEnv(std::string& outputFilename, const std::string fileType) +{ +#if defined(is_pc) && is_pc + char* outputPathEnv = std::getenv("TMPDIR"); + if (outputPathEnv != nullptr && strlen(outputPathEnv) > 0) { + outputFilename = outputPathEnv; + std::string fileName = StringEndsWith(outputFilename, "/") ? fileType : "/" + fileType; + outputFilename += fileName; + HIPERF_HILOGI(MODULE_DEFAULT, "get TMPDIR env success : %{public}s", outputFilename.c_str()); + } else { + HIPERF_HILOGE(MODULE_DEFAULT, "get TMPDIR env failed"); + } +#endif +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/test/BUILD.gn b/test/BUILD.gn index aabe9d0..38e0aa9 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -113,6 +113,10 @@ ohos_unittest("hiperf_unittest") { "CONFIG_HAS_CCM", ] + if (hiperf_feature_pc) { + defines += [ "is_pc=${hiperf_feature_pc}" ] + } + public_deps = [] external_deps = [ "abseil-cpp:absl_container", diff --git a/test/unittest/common/native/subcommand_record_test.cpp b/test/unittest/common/native/subcommand_record_test.cpp index 32f6c87..648243f 100644 --- a/test/unittest/common/native/subcommand_record_test.cpp +++ b/test/unittest/common/native/subcommand_record_test.cpp @@ -2544,6 +2544,25 @@ HWTEST_F(SubCommandRecordTest, UpdateDevHostMaps4, TestSize.Level1) cmd.UpdateDevHostMaps(recordIn); EXPECT_EQ(recordIn.data_.addr, addr); } + +HWTEST_F(SubCommandRecordTest, CheckPcRecordPath, TestSize.Level1) +{ + SubCommandRecord cmd; + string defaultName = "/data/storage/el2/base/cache/perf.data"; +#if defined(is_pc) && is_pc + EXPECT_EQ(cmd.outputFilename_, defaultName); + cmd.ParseOption({}); + char* outputPathEnv = std::getenv("TMPDIR"); + if (outputPathEnv != nullptr && strlen(outputPathEnv) > 0) { + string outputFilename = outputPathEnv; + std::string fileName = StringEndsWith(outputFilename, "/") ? "perf.data" : "/perf.data"; + outputFilename += fileName; + EXPECT_EQ(cmd.outputFilename_, outputFilename); + } else { + EXPECT_EQ(cmd.outputFilename_, defaultName); + } +#endif +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/test/unittest/common/native/subcommand_stat_test.cpp b/test/unittest/common/native/subcommand_stat_test.cpp index c87c083..1648820 100644 --- a/test/unittest/common/native/subcommand_stat_test.cpp +++ b/test/unittest/common/native/subcommand_stat_test.cpp @@ -2451,7 +2451,7 @@ HWTEST_F(SubCommandStatTest, TestOnSubCommand_control05, TestSize.Level1) const std::string expectedStr = "was not stopped within 30 seconds"; std::string tempOutputFile = "/data/local/tmp/stat_test_output.tmp"; std::string cmdWithOutput = testCmd + " > " + tempOutputFile + " 2>&1"; - + int ret = system((cmdWithOutput + " &").c_str()); ASSERT_EQ(ret, 0); @@ -2518,6 +2518,26 @@ HWTEST_F(SubCommandStatTest, OutPutFileName02, TestSize.Level1) EXPECT_EQ(CheckTraceCommandOutput("hiperf stat --control prepare -a -o /data/log/hiperflog/stat.txt", {"Invalid output file path, permission denied"}), true); } + +HWTEST_F(SubCommandStatTest, CheckPcStatPath, TestSize.Level1) +{ + SubCommandStat cmdStat; + string defaultName = "/data/storage/el2/base/cache/perf_stat.txt"; +#if defined(is_pc) && is_pc + EXPECT_EQ(cmdStat.outputFilename_, defaultName); + cmdStat.controlCmd_ = CONTROL_CMD_PREPARE; + cmdStat.CheckOutPutFile(); + char* outputPathEnv = std::getenv("TMPDIR"); + if (outputPathEnv != nullptr && strlen(outputPathEnv) > 0) { + string outputFilename = outputPathEnv; + std::string fileName = StringEndsWith(outputFilename, "/") ? "perf_stat.txt" : "/perf_stat.txt"; + outputFilename += fileName; + EXPECT_EQ(cmdStat.outputFilename_, outputFilename); + } else { + EXPECT_EQ(cmdStat.outputFilename_, defaultName); + } +#endif +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS -- Gitee From b3d7a818e5502f9886974b1f99da8d886cacb731 Mon Sep 17 00:00:00 2001 From: leiguangyu Date: Tue, 12 Aug 2025 23:29:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=B8=8D=E6=89=93?= =?UTF-8?q?=E5=8D=B0path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I9f0f8db0441ed1f7f10621e612f73531c7672c78 Signed-off-by: leiguangyu --- BUILD.gn | 3 +++ src/subcommand_record.cpp | 25 ++++++++++++++++++++++++- src/utilities.cpp | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 5c33143..32931f4 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -222,6 +222,9 @@ ohos_source_set("hiperf_platform_common") { "samgr:samgr_proxy", ] defines += [ "CONFIG_HAS_CCM" ] + if (hiperf_feature_pc) { + defines += [ "is_pc=${hiperf_feature_pc}" ] + } if (bundle_framework_enable) { external_deps += [ "bundle_framework:appexecfwk_base", diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index 49d447a..f9047f1 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -1678,9 +1678,14 @@ HiperfError SubCommandRecord::OnSubCommand(std::vector& args) ChildResponseToMain(false); CloseClientThread(); } +#if defined(is_pc) && is_pc + HLOGE("Fail to create record file"); + HIPERF_HILOGE(MODULE_DEFAULT, "[OnSubCommand] Fail to create record file"); +#else HLOGE("Fail to create record file %s", outputFilename_.c_str()); HIPERF_HILOGE(MODULE_DEFAULT, "[OnSubCommand] Fail to create record file %{public}s", outputFilename_.c_str()); +#endif return HiperfError::CREATE_OUTPUT_FILE_FAIL; } HIPERF_HILOGI(MODULE_DEFAULT, "[OnSubCommand] CreateInitRecordFile finished"); @@ -1727,8 +1732,13 @@ HiperfError SubCommandRecord::OnSubCommand(std::vector& args) startSaveFileTimes_ = steady_clock::now(); if (!backtrack_) { if (!FinishWriteRecordFile()) { +#if defined(is_pc) && is_pc + HLOGE("Fail to finish record file"); + HIPERF_HILOGE(MODULE_DEFAULT, "Fail to finish record file"); +#else HLOGE("Fail to finish record file %s", outputFilename_.c_str()); HIPERF_HILOGE(MODULE_DEFAULT, "Fail to finish record file %{public}s", outputFilename_.c_str()); +#endif return HiperfError::FINISH_WRITE_RECORD_FILE_FAIL; } else if (!PostProcessRecordFile()) { HLOGE("Fail to post process record file"); @@ -2081,8 +2091,11 @@ bool SubCommandRecord::CreateInitRecordFile(bool compressData) CHECK_TRUE(fileWriter_->WriteAttrAndId(perfEvents_.GetAttrWithId(), isSpe_), false, 0, ""); CHECK_TRUE(AddFeatureRecordFile(), false, 0, ""); - +#if defined(is_pc) && is_pc + HLOGD("create new record file"); +#else HLOGD("create new record file %s", outputFilename_.c_str()); +#endif return true; } @@ -2104,14 +2117,22 @@ bool SubCommandRecord::PostProcessRecordFile() fileWriter_.reset(); if (!CreateInitRecordFile(compressData_)) { // create again +#if defined(is_pc) && is_pc + HLOGEP("Fail to open data file"); +#else HLOGEP("Fail to open data file %s ", outputFilename_.c_str()); +#endif return false; } // read temp file auto fileReader = PerfFileReader::Instance(tempFileName); if (fileReader == nullptr) { +#if defined(is_pc) && is_pc + HLOGEP("Fail to open data file"); +#else HLOGEP("Fail to open data file %s ", tempFileName.c_str()); +#endif return false; } @@ -2414,7 +2435,9 @@ bool SubCommandRecord::OnlineReportData() } std::unique_ptr reporter = std::make_unique(); +#ifndef is_pc HLOGD("report the file %s to report file %s \n", tempFileName.c_str(), outputFilename_.c_str()); +#endif std::vector args; args.emplace_back("-i"); args.emplace_back(tempFileName); diff --git a/src/utilities.cpp b/src/utilities.cpp index 04535c7..b1e4df0 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -1083,7 +1083,7 @@ void GetDefaultPathByEnv(std::string& outputFilename, const std::string fileType outputFilename = outputPathEnv; std::string fileName = StringEndsWith(outputFilename, "/") ? fileType : "/" + fileType; outputFilename += fileName; - HIPERF_HILOGI(MODULE_DEFAULT, "get TMPDIR env success : %{public}s", outputFilename.c_str()); + HIPERF_HILOGI(MODULE_DEFAULT, "get TMPDIR env success"); } else { HIPERF_HILOGE(MODULE_DEFAULT, "get TMPDIR env failed"); } -- Gitee