From 0c40f391bd87c14ed5721c119701b4d776963436 Mon Sep 17 00:00:00 2001 From: yuanye Date: Sat, 2 Nov 2024 15:41:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=BB=9F=E8=AE=A1=E6=89=93?= =?UTF-8?q?=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuanye --- BUILD.gn | 33 ++++++++++- bundle.json | 17 +++--- include/command_reporter.h | 41 ++++++++++++++ include/subcommand.h | 5 ++ include/subcommand_dump.h | 0 include/subcommand_record.h | 7 +++ include/subcommand_report.h | 0 include/subcommand_stat.h | 7 +++ src/command.cpp | 4 ++ src/command_reporter.cpp | 55 +++++++++++++++++++ src/subcommand_dump.cpp | 0 src/subcommand_record.cpp | 12 ++++ src/subcommand_stat.cpp | 12 ++++ test/BUILD.gn | 2 + .../common/native/subcommand_record_test.cpp | 46 ++++++++++++++++ .../common/native/subcommand_stat_test.cpp | 45 +++++++++++++++ 16 files changed, 276 insertions(+), 10 deletions(-) mode change 100644 => 100755 BUILD.gn mode change 100644 => 100755 bundle.json create mode 100755 include/command_reporter.h mode change 100644 => 100755 include/subcommand.h mode change 100644 => 100755 include/subcommand_dump.h mode change 100644 => 100755 include/subcommand_record.h mode change 100644 => 100755 include/subcommand_report.h mode change 100644 => 100755 include/subcommand_stat.h mode change 100644 => 100755 src/command.cpp create mode 100755 src/command_reporter.cpp mode change 100644 => 100755 src/subcommand_dump.cpp mode change 100644 => 100755 src/subcommand_record.cpp mode change 100644 => 100755 src/subcommand_stat.cpp mode change 100644 => 100755 test/BUILD.gn mode change 100644 => 100755 test/unittest/common/native/subcommand_record_test.cpp mode change 100644 => 100755 test/unittest/common/native/subcommand_stat_test.cpp diff --git a/BUILD.gn b/BUILD.gn old mode 100644 new mode 100755 index b7cecba..9eeff83 --- a/BUILD.gn +++ b/BUILD.gn @@ -189,6 +189,28 @@ if (is_ohos && hiperf_use_syspara) { common_configs += [ ":hiperf_syspara_config" ] } +ohos_source_set("hiperf_enable_hisysevent") { + part_name = "hiperf" + subsystem_name = "developtools" + use_exceptions = true + public_deps = common_deps + public_configs = common_configs + + sources = [ "./src/command_reporter.cpp" ] + external_deps = [ "hisysevent:libhisysevent" ] + defines = [ "ENABLE_HISYSEVENT" ] +} + +ohos_source_set("hiperf_disable_hisysevent") { + part_name = "hiperf" + subsystem_name = "developtools" + use_exceptions = true + public_deps = common_deps + public_configs = common_configs + + sources = [ "./src/command_reporter.cpp" ] +} + ohos_source_set("hiperf_platform_common") { part_name = "hiperf" subsystem_name = "developtools" @@ -384,6 +406,7 @@ ohos_executable("hiperf") { ":hiperf_etc", ":hiperf_platform_common", ":hiperf_platform_linux", + ":hiperf_enable_hisysevent", ] if (hiperf_target_static) { @@ -407,7 +430,10 @@ ohos_executable("hiperf") { ohos_executable("hiperf_host") { sources = [ "./src/main.cpp" ] - deps = [ ":hiperf_platform_common" ] + deps = [ + ":hiperf_platform_common", + ":hiperf_disable_hisysevent" + ] external_deps = [ "bounds_checking_function:libsec_shared", @@ -467,7 +493,10 @@ ohos_source_set("hiperf_platform_host") { } ohos_shared_library("hiperf_host_lib") { - public_deps = [ ":hiperf_platform_host" ] + public_deps = [ + ":hiperf_platform_host", + ":hiperf_disable_hisysevent" + ] output_name = "hiperf_report" ldflags = [ "-static-libstdc++" ] diff --git a/bundle.json b/bundle.json old mode 100644 new mode 100755 index 338d878..a22ad67 --- a/bundle.json +++ b/bundle.json @@ -19,16 +19,17 @@ "ram": "2000KB", "deps": { "components": [ - "hilog", - "napi", - "samgr", - "ipc", - "c_utils", - "bundle_framework", - "faultloggerd", - "init", "ability_base", "bounds_checking_function", + "bundle_framework", + "c_utils", + "faultloggerd", + "hilog", + "hisysevent", + "init", + "ipc", + "napi", + "samgr", "zlib" ], "third_party": [ diff --git a/include/command_reporter.h b/include/command_reporter.h new file mode 100755 index 0000000..44c1bc7 --- /dev/null +++ b/include/command_reporter.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024-2024 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. + */ +#ifndef COMMAND_REPORTER_H_ +#define COMMAND_REPORTER_H_ + +#include + +namespace OHOS::Developtools::HiPerf { + +class CommandReporter { +public: + explicit CommandReporter(const std::string& fullArgument); + ~CommandReporter(); + + void ReportCommand(); + + std::string mainCommand_ = ""; + std::string subCommand_ = ""; + std::string targetProcess_ = ""; + int32_t errorCode_ = 0; + std::string errorMessage_ = ""; + +private: + bool isReported = false; +}; + +} // namespace OHOS::Developtools::HiPerf + +#endif // COMMAND_REPORTER_H_ \ No newline at end of file diff --git a/include/subcommand.h b/include/subcommand.h old mode 100644 new mode 100755 index 15bf483..1f2b194 --- a/include/subcommand.h +++ b/include/subcommand.h @@ -15,6 +15,8 @@ #ifndef HIPERF_SUBCOMMAND_H_ #define HIPERF_SUBCOMMAND_H_ #include + +#include "command_reporter.h" #include "utilities.h" #include "virtual_runtime.h" @@ -65,6 +67,9 @@ public: return true; } + // add args for hisysevent + virtual void AddReportArgs(CommandReporter& reporter) {}; + // return false means cmd failed virtual bool OnSubCommand(std::vector &args) = 0; // some test code will use this for simple diff --git a/include/subcommand_dump.h b/include/subcommand_dump.h old mode 100644 new mode 100755 diff --git a/include/subcommand_record.h b/include/subcommand_record.h old mode 100644 new mode 100755 index 7ec6aa6..56779de --- a/include/subcommand_record.h +++ b/include/subcommand_record.h @@ -197,6 +197,9 @@ public: bool ParseOption(std::vector &args) override; void DumpOptions(void) const override; + // add args for hisysevent + void AddReportArgs(CommandReporter& reporter) override; + static bool RegisterSubCommandRecord(void); std::map speOptMap_ = { {"branch_filter", 0}, {"load_filter", 0}, @@ -363,6 +366,10 @@ private: void SetSavedCmdlinesSize(); void RecoverSavedCmdlinesSize(); bool OnlineReportData(); + + FRIEND_TEST(SubCommandRecordTest, ReportSampleAll); + FRIEND_TEST(SubCommandRecordTest, ReportSamplePid); + FRIEND_TEST(SubCommandRecordTest, ReportSampleApp); }; } // namespace HiPerf } // namespace Developtools diff --git a/include/subcommand_report.h b/include/subcommand_report.h old mode 100644 new mode 100755 diff --git a/include/subcommand_stat.h b/include/subcommand_stat.h old mode 100644 new mode 100755 index c6fd88f..18c0c10 --- a/include/subcommand_stat.h +++ b/include/subcommand_stat.h @@ -93,6 +93,9 @@ public: bool ParseSpecialOption(std::vector &args); void DumpOptions(void) const override; + // add args for hisysevent + void AddReportArgs(CommandReporter& reporter) override; + private: PerfEvents perfEvents_; bool targetSystemWide_ {false}; @@ -154,6 +157,10 @@ private: bool CheckSelectCpuPidOption(); void SetReportFlags(bool cpuFlag, bool threadFlag); void SetPerfEvent(); + + FRIEND_TEST(SubCommandStatTest, ReportSampleAll); + FRIEND_TEST(SubCommandStatTest, ReportSamplePid); + FRIEND_TEST(SubCommandStatTest, ReportSampleApp); }; bool RegisterSubCommandStat(void); diff --git a/src/command.cpp b/src/command.cpp old mode 100644 new mode 100755 index 4330e8a..879ec66 --- a/src/command.cpp +++ b/src/command.cpp @@ -28,6 +28,7 @@ bool Command::DispatchCommands(std::vector arguments) fullArgument.append(" "); fullArgument.append(arg); } + CommandReporter reporter(fullArgument); HLOGD("args:%s", VectorToString(arguments).c_str()); while (!arguments.empty()) { // we need found main command args first @@ -48,6 +49,7 @@ bool Command::DispatchCommands(std::vector arguments) // if it is an sub command auto subCommand = SubCommand::FindSubCommand(arguments.front()); if (subCommand != nullptr) { + reporter.mainCommand_ = arguments.front(); // this is an sub command which we support // remove the subcmd name @@ -56,6 +58,8 @@ bool Command::DispatchCommands(std::vector arguments) // if we found the sub command , after it processed , we will exit HLOGD("OnSubCommandOptions -> %s", subCommand->Name().c_str()); if (subCommand->OnSubCommandOptions(arguments)) { + subCommand->AddReportArgs(reporter); + reporter.ReportCommand(); // if some help cmd ? if (subCommand->OnPreSubCommand()) { return true; diff --git a/src/command_reporter.cpp b/src/command_reporter.cpp new file mode 100755 index 0000000..d87e737 --- /dev/null +++ b/src/command_reporter.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2024-2024 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 "command_reporter.h" + +#ifdef ENABLE_HISYSEVENT +#include "hisysevent.h" +#include "debug_logger.h" +#endif + +namespace OHOS::Developtools::HiPerf { + +CommandReporter::CommandReporter(const std::string& fullArgument) : subCommand_(fullArgument) {} + +CommandReporter::~CommandReporter() +{ + ReportCommand(); +} + +void CommandReporter::ReportCommand() +{ +#ifdef ENABLE_HISYSEVENT + if (isReported) { + HLOGW("command has been reported"); + return; + } + + int32_t ret = HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::GRAPHIC, "HIPERF_USAGE", + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "MAIN_CMD", mainCommand_, + "SUB_CMD", subCommand_, + "TARGET_PROCESS", targetProcess_, + "ERROR_CODE", errorCode_, + "ERROR_MESSAGE", errorMessage_); + if (ret != 0) { + HLOGE("hisysevent report failed, err:%d", ret); + } +#endif + isReported = true; +} + +} // namespace OHOS::Developtools::HiPerf diff --git a/src/subcommand_dump.cpp b/src/subcommand_dump.cpp old mode 100644 new mode 100755 diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp old mode 100644 new mode 100755 index dca44ca..735da5a --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -1930,6 +1930,18 @@ std::string SubCommandRecord::HandleAppInfo() } return err; } + +void SubCommandRecord::AddReportArgs(CommandReporter& reporter) +{ + if (targetSystemWide_) { + reporter.targetProcess_ = "ALL"; + } else if (!appPackage_.empty()) { + reporter.targetProcess_ = appPackage_; + } else { + reporter.targetProcess_ = VectorToString(selectPids_); + } +} + } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/src/subcommand_stat.cpp b/src/subcommand_stat.cpp old mode 100644 new mode 100755 index e8823e4..9ce22a7 --- a/src/subcommand_stat.cpp +++ b/src/subcommand_stat.cpp @@ -805,6 +805,18 @@ bool SubCommandStat::CheckOptions(const std::vector &pids) } return true; } + +void SubCommandStat::AddReportArgs(CommandReporter& reporter) +{ + if (targetSystemWide_) { + reporter.targetProcess_ = "ALL"; + } else if (!appPackage_.empty()) { + reporter.targetProcess_ = appPackage_; + } else { + reporter.targetProcess_ = VectorToString(selectPids_); + } +} + } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/test/BUILD.gn b/test/BUILD.gn old mode 100644 new mode 100755 index 97713e8..97ddeb5 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -68,6 +68,7 @@ common_deps = [ "${hiperf_path}/:hiperf_platform_host", "${hiperf_path}/:hiperf_platform_linux", "${hiperf_path}/interfaces/innerkits/native:hiperf_client_static", + "${hiperf_path}/:hiperf_disable_hisysevent", "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", ] @@ -121,6 +122,7 @@ fuzz_deps = [ "${hiperf_path}/:hiperf_platform_host", "${hiperf_path}/:hiperf_platform_linux", "${hiperf_path}/interfaces/innerkits/native:hiperf_client_static", + "${hiperf_path}/:hiperf_disable_hisysevent", ] ohos_fuzztest("CommandLineFuzzTest") { diff --git a/test/unittest/common/native/subcommand_record_test.cpp b/test/unittest/common/native/subcommand_record_test.cpp old mode 100644 new mode 100755 index 58895ca..b511d7a --- a/test/unittest/common/native/subcommand_record_test.cpp +++ b/test/unittest/common/native/subcommand_record_test.cpp @@ -1580,6 +1580,52 @@ HWTEST_F(SubCommandRecordTest, TestCmdlineSizeErr, TestSize.Level1) { TestRecordCommand("-d 2 -a -f 2000 --cmdline-size", false, false); } + +/** + * @tc.name: AddReportArgs + * @tc.desc: Test AddReportArgs with -a + * @tc.type: FUNC + */ +HWTEST_F(SubCommandRecordTest, ReportSampleAll, TestSize.Level1) +{ + SubCommandRecord command; + command.targetSystemWide_ = true; + + CommandReporter reporter("record"); + command.AddReportArgs(reporter); + EXPECT_EQ(reporter.targetProcess_, "ALL"); +} + +/** + * @tc.name: AddReportArgs + * @tc.desc: Test AddReportArgs with -p + * @tc.type: FUNC + */ +HWTEST_F(SubCommandRecordTest, ReportSamplePid, TestSize.Level1) +{ + SubCommandRecord command; + command.selectPids_ = {1, 2, 3}; + + CommandReporter reporter("record"); + command.AddReportArgs(reporter); + EXPECT_EQ(reporter.targetProcess_, "ALL"); +} + +/** + * @tc.name: AddReportArgs + * @tc.desc: Test AddReportArgs with --app + * @tc.type: FUNC + */ +HWTEST_F(SubCommandRecordTest, ReportSampleApp, TestSize.Level1) +{ + SubCommandRecord command; + command.targetSystemWide_ = true; + + CommandReporter reporter("record"); + command.AddReportArgs(reporter); + EXPECT_EQ(reporter.targetProcess_, "ALL"); +} + } // 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 old mode 100644 new mode 100755 index 9ef3853..2813919 --- a/test/unittest/common/native/subcommand_stat_test.cpp +++ b/test/unittest/common/native/subcommand_stat_test.cpp @@ -2258,6 +2258,51 @@ HWTEST_F(SubCommandStatTest, CheckPidAndApp, TestSize.Level1) printf("output:\n%s", stringOut.c_str()); } } + +/** + * @tc.name: AddReportArgs + * @tc.desc: Test AddReportArgs with -a + * @tc.type: FUNC + */ +HWTEST_F(SubCommandStatTest, ReportSampleAll, TestSize.Level1) +{ + SubCommandStat command; + command.targetSystemWide_ = true; + + CommandReporter reporter("stat"); + command.AddReportArgs(reporter); + EXPECT_EQ(reporter.targetProcess_, "ALL"); +} + +/** + * @tc.name: AddReportArgs + * @tc.desc: Test AddReportArgs with -p + * @tc.type: FUNC + */ +HWTEST_F(SubCommandStatTest, ReportSamplePid, TestSize.Level1) +{ + SubCommandStat command; + command.selectPids_ = {1, 2, 3}; + + CommandReporter reporter("stat"); + command.AddReportArgs(reporter); + EXPECT_EQ(reporter.targetProcess_, "ALL"); +} + +/** + * @tc.name: AddReportArgs + * @tc.desc: Test AddReportArgs with --app + * @tc.type: FUNC + */ +HWTEST_F(SubCommandStatTest, ReportSampleApp, TestSize.Level1) +{ + SubCommandStat command; + command.targetSystemWide_ = true; + + CommandReporter reporter("stat"); + command.AddReportArgs(reporter); + EXPECT_EQ(reporter.targetProcess_, "ALL"); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS