From b13db55ef89e5dad0db3f4da8b50996f9c562dac Mon Sep 17 00:00:00 2001 From: yuanye Date: Thu, 2 Jan 2025 19:05:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=89=93=E7=82=B9=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=94=99=E8=AF=AF=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuanye --- include/command_reporter.h | 41 +++++++++++++- include/subcommand.h | 6 +- include/subcommand_dump.h | 2 +- include/subcommand_help.h | 2 +- include/subcommand_list.h | 2 +- include/subcommand_record.h | 3 +- include/subcommand_report.h | 2 +- include/subcommand_stat.h | 2 +- include/utilities.h | 7 +++ src/command.cpp | 6 +- src/command_reporter.cpp | 11 +++- src/hiperf_libreport.cpp | 6 +- src/subcommand_dump.cpp | 18 +++--- src/subcommand_help.cpp | 4 +- src/subcommand_list.cpp | 7 ++- src/subcommand_record.cpp | 56 ++++++++++--------- src/subcommand_report.cpp | 14 ++--- src/subcommand_stat.cpp | 20 +++---- test/unittest/common/native/command_test.cpp | 6 +- .../common/native/include/subcommand_test.h | 6 +- .../common/native/subcommand_help_test.cpp | 6 +- .../common/native/subcommand_list_test.cpp | 14 ++--- .../common/native/subcommand_test.cpp | 4 +- 23 files changed, 150 insertions(+), 95 deletions(-) diff --git a/include/command_reporter.h b/include/command_reporter.h index ccad170..6b5f76b 100644 --- a/include/command_reporter.h +++ b/include/command_reporter.h @@ -18,6 +18,44 @@ #include namespace OHOS::Developtools::HiPerf { +#define MAKE_ERROR_ITEM(GEN_ITEM) \ + GEN_ITEM(NO_ERROR), /* 0 */ \ + GEN_ITEM(PROCESS_CONTROL_FAIL), /* 1 */ \ + GEN_ITEM(PREPARE_SYS_KERNEL_FAIL), /* 2 */ \ + GEN_ITEM(PREPARE_PERF_EVENT_FAIL), /* 3 */ \ + GEN_ITEM(PREPARE_TACKING_FAIL), /* 4 */ \ + GEN_ITEM(CREATE_OUTPUT_FILE_FAIL), /* 5 */ \ + GEN_ITEM(PREPARE_VIRTUAL_RUNTIME_FAIL), /* 6 */ \ + GEN_ITEM(PREPARE_START_TRACKING_FAIL), /* 7 */ \ + GEN_ITEM(START_TRACKING_FAIL), /* 8 */ \ + GEN_ITEM(FINISH_WRITE_RECORD_FILE_FAIL), /* 9 */ \ + GEN_ITEM(POST_PROCESS_RECORD_FILE), /* 10 */ \ + GEN_ITEM(CHECK_RESTART_OPTION_FAIL), /* 11 */ \ + GEN_ITEM(CHECK_SELECT_CPU_PID_FAIL), /* 12 */ \ + GEN_ITEM(CHECK_STAT_OPTION_FAIL), /* 13 */ \ + GEN_ITEM(CHECK_APP_RUNNING_FAIL), /* 14 */ \ + GEN_ITEM(CHECK_OPTION_PID_FAIL), /* 15 */ \ + GEN_ITEM(CHECK_OPTION_PID_APP_FAIL), /* 16 */ \ + GEN_ITEM(PREPAIR_EVENTS_FAIL), /* 17 */ \ + GEN_ITEM(PREPARE_OUTPUT_FAIL), /* 18 */ \ + GEN_ITEM(LOAD_PERF_DATA_FAIL), /* 19 */ \ + GEN_ITEM(LOAD_SECOND_PERF_DATA_FAIL), /* 20 */ \ + GEN_ITEM(OUTPUT_REPORT_FAIL), /* 21 */ \ + GEN_ITEM(PREPARE_DUMP_OUTPUT_FAIL), /* 22 */ \ + GEN_ITEM(DUMP_ELF_FILE_ERROR), /* 23 */ \ + GEN_ITEM(DUMP_PROTO_FILE_ERROR), /* 24 */ \ + GEN_ITEM(ACCESS_DATA_FILE_FAIL), /* 25 */ \ + GEN_ITEM(OPEN_DATA_FILE_FAIL), /* 26 */ \ + GEN_ITEM(SET_SYMBOLS_PATH_FAIL), /* 27 */ \ + GEN_ITEM(OPTION_NOT_SUPPORT), /* 28 */ \ + GEN_ITEM(SUBCOMMAND_OPTIONS_ERROR) /* 29 */ + +#define FOR_ERROR_ENUM(x) x + +enum class HiperfError : int32_t { + MAKE_ERROR_ITEM(FOR_ERROR_ENUM) +}; + class CommandReporter { public: explicit CommandReporter(const std::string& fullArgument); @@ -29,8 +67,7 @@ public: std::string subCommand_ = ""; std::string caller_ = ""; std::string targetProcess_ = ""; - int32_t errorCode_ = 0; - std::string errorMessage_ = ""; + HiperfError errorCode_ = HiperfError::NO_ERROR; private: bool isReported_ = false; diff --git a/include/subcommand.h b/include/subcommand.h index 7c443c2..73c32d3 100644 --- a/include/subcommand.h +++ b/include/subcommand.h @@ -70,13 +70,13 @@ public: // add args for hisysevent virtual void AddReportArgs(CommandReporter& reporter) {}; - // return false means cmd failed - virtual bool OnSubCommand(std::vector &args) = 0; + // return NO_ERROR means cmd success + virtual HiperfError OnSubCommand(std::vector& args) = 0; // some test code will use this for simple bool OnSubCommand(std::string stringArgs) { auto args = StringSplit(stringArgs, " "); - return OnSubCommand(args); + return OnSubCommand(args) != HiperfError::NO_ERROR; }; // get some cmd diff --git a/include/subcommand_dump.h b/include/subcommand_dump.h index 75a8262..53c257d 100644 --- a/include/subcommand_dump.h +++ b/include/subcommand_dump.h @@ -68,7 +68,7 @@ public: } ~SubCommandDump() override; - bool OnSubCommand(std::vector &args) override; + HiperfError OnSubCommand(std::vector& args) override; bool ParseOption(std::vector &args) override; static bool RegisterSubCommandDump(void); diff --git a/include/subcommand_help.h b/include/subcommand_help.h index 3e1fc02..4c071c6 100644 --- a/include/subcommand_help.h +++ b/include/subcommand_help.h @@ -33,7 +33,7 @@ public: { } - bool OnSubCommand(std::vector &args) override; + HiperfError OnSubCommand(std::vector& args) override; static void RegisterSubCommandHelp(void); static bool OnHelp(std::vector &args); diff --git a/include/subcommand_list.h b/include/subcommand_list.h index 64d29a9..94e8b74 100644 --- a/include/subcommand_list.h +++ b/include/subcommand_list.h @@ -47,7 +47,7 @@ public: { } - bool OnSubCommand(std::vector &args) override; + HiperfError OnSubCommand(std::vector& args) override; static void RegisterSubCommandList(void); diff --git a/include/subcommand_record.h b/include/subcommand_record.h index ea12baa..03aa792 100644 --- a/include/subcommand_record.h +++ b/include/subcommand_record.h @@ -204,7 +204,7 @@ public: } ~SubCommandRecord(); - bool OnSubCommand(std::vector &args) override; + HiperfError OnSubCommand(std::vector& args) override; bool ParseOption(std::vector &args) override; void DumpOptions(void) const override; @@ -310,6 +310,7 @@ private: std::function postProcess = [](bool) {}; }; std::unordered_map controlCommandHandlerMap_ = {}; + inline void CreateClientThread(); void ClientCommandHandle(); void InitControlCommandHandlerMap(); void DispatchControlCommand(const std::string& command); diff --git a/include/subcommand_report.h b/include/subcommand_report.h index edde006..5edfcde 100644 --- a/include/subcommand_report.h +++ b/include/subcommand_report.h @@ -103,7 +103,7 @@ public: // clang-format on { } - bool OnSubCommand(std::vector &args) override; + HiperfError OnSubCommand(std::vector& args) override; bool ParseOption(std::vector &args) override; void DumpOptions(void) const override; static bool RegisterSubCommandReport(void); diff --git a/include/subcommand_stat.h b/include/subcommand_stat.h index 6f53610..27a7f39 100644 --- a/include/subcommand_stat.h +++ b/include/subcommand_stat.h @@ -88,7 +88,7 @@ public: { } - bool OnSubCommand(std::vector &args) override; + HiperfError OnSubCommand(std::vector& args) override; bool ParseOption(std::vector &args) override; bool ParseSpecialOption(std::vector &args); void DumpOptions(void) const override; diff --git a/include/utilities.h b/include/utilities.h index 5d81a96..d9ea3b5 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -390,6 +390,13 @@ inline bool CheckOutOfRange(const T& value, const T& min, const T& max) { return value < min || value > max; } + +#define RETURN_IF(condition, retValue) do { \ + if (condition) { \ + return retValue; \ + } \ +} while (0) + } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/src/command.cpp b/src/command.cpp index 879ec66..710e921 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -59,21 +59,23 @@ bool Command::DispatchCommands(std::vector arguments) 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; } HLOGD("OnSubCommand -> %s", subCommand->Name().c_str()); - if (!subCommand->OnSubCommand(arguments)) { + HiperfError err = subCommand->OnSubCommand(arguments); + if (err != HiperfError::NO_ERROR) { printf("subcommand '%s' failed\n", subCommand->Name().c_str()); + reporter.errorCode_ = err; return false; } else { HLOGD("OnSubCommand successed"); return true; } } else { + reporter.errorCode_ = HiperfError::SUBCOMMAND_OPTIONS_ERROR; HLOGD("OnSubCommandOptions interrupt the process."); return false; } diff --git a/src/command_reporter.cpp b/src/command_reporter.cpp index 6b99870..02abdcf 100644 --- a/src/command_reporter.cpp +++ b/src/command_reporter.cpp @@ -23,6 +23,11 @@ namespace OHOS::Developtools::HiPerf { +#define FOR_ERROR_NAME(x) #x +static const char* const ERROR_MESSAGE[] = { + MAKE_ERROR_ITEM(FOR_ERROR_NAME) +}; + CommandReporter::CommandReporter(const std::string& fullArgument) : subCommand_(fullArgument) { #if defined(is_ohos) && is_ohos @@ -43,6 +48,8 @@ void CommandReporter::ReportCommand() return; } + int32_t errorCode = static_cast(errorCode_); + int32_t ret = HiSysEventWrite( OHOS::HiviewDFX::HiSysEvent::Domain::PROFILER, "HIPERF_USAGE", OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, @@ -50,8 +57,8 @@ void CommandReporter::ReportCommand() "SUB_CMD", subCommand_, "CALLER", caller_, "TARGET_PROCESS", targetProcess_, - "ERROR_CODE", errorCode_, - "ERROR_MESSAGE", errorMessage_); + "ERROR_CODE", errorCode, + "ERROR_MESSAGE", ERROR_MESSAGE[errorCode]); if (ret != 0) { HIPERF_HILOGE(MODULE_DEFAULT, "hisysevent report failed, err:%{public}d", ret); } diff --git a/src/hiperf_libreport.cpp b/src/hiperf_libreport.cpp index 688acdc..0781fa2 100644 --- a/src/hiperf_libreport.cpp +++ b/src/hiperf_libreport.cpp @@ -60,7 +60,7 @@ int Report(const char *perfFile, const char *reportFile, const char *reportOptio } } if (report->ParseOption(args)) { - return report->OnSubCommand(args) ? 0 : -1; + return report->OnSubCommand(args) == HiperfError::NO_ERROR ? 0 : -1; } } else { printf("path is nullptr\n"); @@ -89,7 +89,7 @@ int ReportUnwindJson(const char *perfFile, const char *reportFile, const char *s args.emplace_back(symbolsDir); } if (report->ParseOption(args)) { - return report->OnSubCommand(args) ? 0 : -1; + return report->OnSubCommand(args) == HiperfError::NO_ERROR ? 0 : -1; } } return -1; @@ -194,7 +194,7 @@ int Dump(const char *fileName) std::vector args; args.emplace_back(fileName); if (dump->ParseOption(args)) { - return dump->OnSubCommand(args) ? 0 : -1; + return dump->OnSubCommand(args) == HiperfError::NO_ERROR ? 0 : -1; } } return -1; diff --git a/src/subcommand_dump.cpp b/src/subcommand_dump.cpp index f84b51b..f620a65 100644 --- a/src/subcommand_dump.cpp +++ b/src/subcommand_dump.cpp @@ -134,32 +134,30 @@ SubCommandDump::~SubCommandDump() SymbolsFile::onRecording_ = true; // back to default for UT } -bool SubCommandDump::OnSubCommand(std::vector &args) +HiperfError SubCommandDump::OnSubCommand(std::vector& args) { - if (!PrepareDumpOutput()) { - return false; - } + RETURN_IF(!PrepareDumpOutput(), HiperfError::PREPARE_DUMP_OUTPUT_FAIL); if (!elfFileName_.empty()) { - return DumpElfFile(); + return DumpElfFile() ? HiperfError::NO_ERROR : HiperfError::DUMP_ELF_FILE_ERROR; } #if defined(HAVE_PROTOBUF) && HAVE_PROTOBUF if (!protobufDumpFileName_.empty()) { - return DumpProtoFile(); + return DumpProtoFile() ? HiperfError::NO_ERROR : HiperfError::DUMP_PROTO_FILE_ERROR; } #endif if (access(dumpFileName_.c_str(), F_OK) != 0) { printf("Can not access data file %s\n", dumpFileName_.c_str()); - return false; + return HiperfError::ACCESS_DATA_FILE_FAIL; } // only one file should created HLOG_ASSERT_MESSAGE(reader_ == nullptr, " perf file reader for %s\n", dumpFileName_.c_str()); reader_ = PerfFileReader::Instance(dumpFileName_); if (reader_ == nullptr) { HLOGE("HiperfFileReader::Instance(%s) return null", dumpFileName_.c_str()); - return false; + return HiperfError::OPEN_DATA_FILE_FAIL; } // any way tell symbols this is not on device @@ -171,7 +169,7 @@ bool SubCommandDump::OnSubCommand(std::vector &args) // user give us path , we enable unwind if (!vr_.SetSymbolsPaths(dumpSymbolsPaths_)) { printf("Failed to set symbol path(%s)\n", VectorToString(dumpSymbolsPaths_).c_str()); - return false; + return HiperfError::SET_SYMBOLS_PATH_FAIL; } } @@ -191,7 +189,7 @@ bool SubCommandDump::OnSubCommand(std::vector &args) DumpFeaturePortion(indent_); } - return true; + return HiperfError::NO_ERROR; } bool SubCommandDump::DumpElfFile() diff --git a/src/subcommand_help.cpp b/src/subcommand_help.cpp index 55de8cb..3f2ebbb 100644 --- a/src/subcommand_help.cpp +++ b/src/subcommand_help.cpp @@ -19,11 +19,11 @@ namespace OHOS { namespace Developtools { namespace HiPerf { -bool SubCommandHelp::OnSubCommand(std::vector &args) +HiperfError SubCommandHelp::OnSubCommand(std::vector& args) { HLOGV("enter"); OnHelp(args); - return true; + return HiperfError::NO_ERROR; } void SubCommandHelp::RegisterSubCommandHelp() diff --git a/src/subcommand_list.cpp b/src/subcommand_list.cpp index 3f2b6a4..f34254a 100644 --- a/src/subcommand_list.cpp +++ b/src/subcommand_list.cpp @@ -19,7 +19,7 @@ namespace OHOS { namespace Developtools { namespace HiPerf { -bool SubCommandList::OnSubCommand(std::vector &args) +HiperfError SubCommandList::OnSubCommand(std::vector& args) { std::vector requestEventTypes; @@ -35,14 +35,15 @@ bool SubCommandList::OnSubCommand(std::vector &args) auto it = SUPPORT_NAME_OPTIONS.find(requestEventType); if (it == SUPPORT_NAME_OPTIONS.end()) { printf("not support option: '%s'\n", requestEventType.c_str()); - return false; + return HiperfError::OPTION_NOT_SUPPORT; } else { requestEventTypes.push_back(it->second); } } ShowSupportEventsTypes(requestEventTypes); - return true; + return HiperfError::NO_ERROR; } + bool SubCommandList::ShowSupportEventsTypes(std::vector &requestEventTypes) { // each type diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index ed53c64..2d04587 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -1146,6 +1146,14 @@ void SubCommandRecord::InitControlCommandHandlerMap() }); } +inline void SubCommandRecord::CreateClientThread() +{ + // make a thread wait the other command + if (clientPipeOutput_ != -1) { + clientCommandHanle_ = std::thread(&SubCommandRecord::ClientCommandHandle, this); + } +} + void SubCommandRecord::ClientCommandHandle() { using namespace HiperfClient; @@ -1405,53 +1413,47 @@ void SubCommandRecord::WaitFifoReply(int fd, const std::chrono::milliseconds &ti } } -bool SubCommandRecord::OnSubCommand(std::vector &args) +HiperfError SubCommandRecord::OnSubCommand(std::vector& args) { HIPERF_HILOGI(MODULE_DEFAULT, "SubCommandRecord onSubCommand start"); if (!ProcessControl()) { - return false; + return HiperfError::PROCESS_CONTROL_FAIL; } else if (isFifoClient_) { - return true; + return HiperfError::NO_ERROR; } // prepare PerfEvents - if (!PrepareSysKernel() || !PreparePerfEvent()) { - return false; - } + RETURN_IF(!PrepareSysKernel(), HiperfError::PREPARE_SYS_KERNEL_FAIL); + RETURN_IF(!PreparePerfEvent(), HiperfError::PREPARE_PERF_EVENT_FAIL); // prepar some attr before CreateInitRecordFile - CHECK_TRUE(!perfEvents_.PrepareTracking(), false, LOG_TYPE_WITH_HILOG, "Fail to prepare tracking "); + CHECK_TRUE(!perfEvents_.PrepareTracking(), HiperfError::PREPARE_TACKING_FAIL, + LOG_TYPE_WITH_HILOG, "Fail to prepare tracking "); HIPERF_HILOGI(MODULE_DEFAULT, "SubCommandRecord perfEvents prepared"); if (!backtrack_ && !CreateInitRecordFile(delayUnwind_ ? false : compressData_)) { HLOGE("Fail to create record file %s", outputFilename_.c_str()); HIPERF_HILOGE(MODULE_DEFAULT, "Fail to create record file %s", outputFilename_.c_str()); - return false; + return HiperfError::CREATE_OUTPUT_FILE_FAIL; } - if (!PrepareVirtualRuntime()) { - return false; - } + RETURN_IF(!PrepareVirtualRuntime(), HiperfError::PREPARE_VIRTUAL_RUNTIME_FAIL); HIPERF_HILOGI(MODULE_DEFAULT, "SubCommandRecord virtualRuntime prepared"); - // make a thread wait the other command - if (clientPipeOutput_ != -1) { - clientCommandHanle_ = std::thread(&SubCommandRecord::ClientCommandHandle, this); - } + CreateClientThread(); //write comm event WriteCommEventBeforeSampling(); SetExcludeHiperf(); HIPERF_HILOGI(MODULE_DEFAULT, "SubCommandRecord StartTracking"); - // start tracking - if (isDataSizeLimitStop_) { - // mmap record size has been larger than limit, dont start sampling. - } else if (restart_ && controlCmd_ == CONTROL_CMD_PREPARE) { - CHECK_TRUE(!perfEvents_.StartTracking(isFifoServer_), false, 0, ""); - } else { - if (!perfEvents_.StartTracking((!isFifoServer_) && (clientPipeInput_ == -1))) { - return false; + // if mmap record size has been larger than limit, dont start sampling. + if (!isDataSizeLimitStop_) { + if (restart_ && controlCmd_ == CONTROL_CMD_PREPARE) { + RETURN_IF(!perfEvents_.StartTracking(isFifoServer_), HiperfError::PREPARE_START_TRACKING_FAIL); + } else { + RETURN_IF(!perfEvents_.StartTracking((!isFifoServer_) && (clientPipeInput_ == -1)), + HiperfError::START_TRACKING_FAIL); } } HIPERF_HILOGI(MODULE_DEFAULT, "SubCommandRecord perfEvents tracking finish"); @@ -1465,11 +1467,11 @@ bool SubCommandRecord::OnSubCommand(std::vector &args) if (!FinishWriteRecordFile()) { HLOGE("Fail to finish record file %s", outputFilename_.c_str()); HIPERF_HILOGE(MODULE_DEFAULT, "Fail to finish record file %s", outputFilename_.c_str()); - return false; + return HiperfError::FINISH_WRITE_RECORD_FILE_FAIL; } else if (!PostProcessRecordFile()) { HLOGE("Fail to post process record file"); HIPERF_HILOGE(MODULE_DEFAULT, "Fail to post process record file"); - return false; + return HiperfError::POST_PROCESS_RECORD_FILE; } RecordCompleted(); } @@ -1481,7 +1483,7 @@ bool SubCommandRecord::OnSubCommand(std::vector &args) CloseClientThread(); RemoveVdsoTmpFile(); HIPERF_HILOGI(MODULE_DEFAULT, "SubCommandRecord finish"); - return true; + return HiperfError::NO_ERROR; } void SubCommandRecord::CloseClientThread() @@ -2114,7 +2116,7 @@ bool SubCommandRecord::OnlineReportData() args.emplace_back(outputFilename_); args.emplace_back("-s"); if (reporter->ParseOption(args)) { - ret = reporter->OnSubCommand(args); + ret = (reporter->OnSubCommand(args) != HiperfError::NO_ERROR); } if (remove(tempFileName.c_str()) != 0) { diff --git a/src/subcommand_report.cpp b/src/subcommand_report.cpp index 999f323..8015852 100644 --- a/src/subcommand_report.cpp +++ b/src/subcommand_report.cpp @@ -587,37 +587,35 @@ SubCommandReport::~SubCommandReport() SymbolsFile::onRecording_ = true; // back to default for UT } -bool SubCommandReport::OnSubCommand(std::vector &args) +HiperfError SubCommandReport::OnSubCommand(std::vector& args) { - if (!PrepareOutput()) { - return false; - } + RETURN_IF(!PrepareOutput(), HiperfError::PREPARE_OUTPUT_FAIL); // any way tell symbols this is not on recording SymbolsFile::onRecording_ = false; printf("loading data\n"); if (!LoadPerfData()) { - return false; + return HiperfError::LOAD_PERF_DATA_FAIL; } if (diffMode_) { // we are in diff mode index_ = SECOND; // load again with second file - CHECK_TRUE(!LoadPerfData(), false, 0, ""); + CHECK_TRUE(!LoadPerfData(), HiperfError::LOAD_SECOND_PERF_DATA_FAIL, 0, ""); // back to first index_ = FIRST; } printf("prepare report\n"); - CHECK_TRUE(!OutputReport(), false, 1, "OutputReport failed"); + CHECK_TRUE(!OutputReport(), HiperfError::OUTPUT_REPORT_FAIL, 1, "OutputReport failed"); #ifdef HIPERF_DEBUG_TIME printf("SymbolicRecordTimes: %0.3f ms\n", GetReport(FIRST).virtualRuntime_.symbolicRecordTimes_.count() / MS_DURATION); #endif printf("report done\n"); - return true; + return HiperfError::NO_ERROR; } bool SubCommandReport::RegisterSubCommandReport() diff --git a/src/subcommand_stat.cpp b/src/subcommand_stat.cpp index 69e2f16..0dffbae 100644 --- a/src/subcommand_stat.cpp +++ b/src/subcommand_stat.cpp @@ -635,27 +635,27 @@ void SubCommandStat::SetPerfEvent() perfEvents_.SetStatCallBack(Report); } -bool SubCommandStat::OnSubCommand(std::vector &args) +HiperfError SubCommandStat::OnSubCommand(std::vector& args) { - CHECK_TRUE(HelpOption(), true, 0, ""); + CHECK_TRUE(HelpOption(), HiperfError::NO_ERROR, 0, ""); if (!CheckRestartOption(appPackage_, targetSystemWide_, restart_, selectPids_)) { - return false; + return HiperfError::CHECK_RESTART_OPTION_FAIL; } // check option if (!CheckSelectCpuPidOption()) { - return false; + return HiperfError::CHECK_SELECT_CPU_PID_FAIL; } if (!CheckOptions(selectPids_)) { HLOGV("CheckOptions() failed"); - return false; + return HiperfError::CHECK_STAT_OPTION_FAIL; } if (!CheckAppIsRunning(selectPids_, appPackage_, checkAppMs_)) { HLOGV("CheckAppIsRunning() failed"); - return false; + return HiperfError::CHECK_APP_RUNNING_FAIL; } if (!CheckOptionPid(selectPids_)) { HLOGV("CheckOptionPid() failed"); - return false; + return HiperfError::CHECK_OPTION_PID_FAIL; } perfEvents_.SetCpu(selectCpus_); @@ -673,12 +673,12 @@ bool SubCommandStat::OnSubCommand(std::vector &args) perfEvents_.SetPid(pids); if (!CheckOptionPidAndApp(pids)) { HLOGV("CheckOptionPidAndApp() failed"); - return false; + return HiperfError::CHECK_OPTION_PID_APP_FAIL; } SetPerfEvent(); if (!PrepairEvents()) { HLOGV("PrepairEvents() failed"); - return false; + return HiperfError::PREPAIR_EVENTS_FAIL; } // preapare fd @@ -687,7 +687,7 @@ bool SubCommandStat::OnSubCommand(std::vector &args) // start tracking perfEvents_.StartTracking(); - return true; + return HiperfError::NO_ERROR; } bool RegisterSubCommandStat() diff --git a/test/unittest/common/native/command_test.cpp b/test/unittest/common/native/command_test.cpp index 169c68b..fb62436 100644 --- a/test/unittest/common/native/command_test.cpp +++ b/test/unittest/common/native/command_test.cpp @@ -44,13 +44,15 @@ void CommandTest::TearDownTestCase() {} void CommandTest::SetUp() { + static constexpr HiperfError noError = HiperfError::NO_ERROR; + static constexpr HiperfError optionNotSupport = HiperfError::OPTION_NOT_SUPPORT; ASSERT_EQ(Option::RegisterMainOption(TEST_OPTION_TRUE, TEST_OPTION_HELP, OptionAlwaysTrue), true); ASSERT_EQ(Option::RegisterMainOption(TEST_OPTION_FALSE, TEST_OPTION_HELP, OptionAlwaysFalse), true); - EXPECT_CALL(*subCommandAlwaysTure, OnSubCommand(testing::_)).WillRepeatedly(testing::Return(true)); - EXPECT_CALL(*subCommandAlwaysFalse, OnSubCommand(testing::_)).WillRepeatedly(testing::Return(false)); + EXPECT_CALL(*subCommandAlwaysTure, OnSubCommand(testing::_)).WillRepeatedly(testing::Return(noError)); + EXPECT_CALL(*subCommandAlwaysFalse, OnSubCommand(testing::_)).WillRepeatedly(testing::Return(optionNotSupport)); ASSERT_TRUE(SubCommand::RegisterSubCommand(subCommandAlwaysTure.get()->Name(), std::move(subCommandAlwaysTure))); diff --git a/test/unittest/common/native/include/subcommand_test.h b/test/unittest/common/native/include/subcommand_test.h index 07c3b7c..25fc160 100644 --- a/test/unittest/common/native/include/subcommand_test.h +++ b/test/unittest/common/native/include/subcommand_test.h @@ -41,9 +41,9 @@ class SubCommandTest : public SubCommand { public: explicit SubCommandTest(std::string name) : SubCommand(name, TEST_BRIEF, TEST_HELP) {} - bool OnSubCommand(std::vector &args) override + HiperfError OnSubCommand(std::vector& args) override { - return true; + return HiperfError::NO_ERROR; } }; @@ -54,7 +54,7 @@ class MockSubCommand : public SubCommand { public: explicit MockSubCommand(std::string name) : SubCommand(name, TEST_BRIEF, TEST_HELP) {} MockSubCommand() : MockSubCommand("mock") {} - MOCK_METHOD1(OnSubCommand, bool(std::vector &args)); + MOCK_METHOD1(OnSubCommand, HiperfError(std::vector &args)); }; } // namespace HiPerf } // namespace Developtools diff --git a/test/unittest/common/native/subcommand_help_test.cpp b/test/unittest/common/native/subcommand_help_test.cpp index 2113570..1cd951a 100644 --- a/test/unittest/common/native/subcommand_help_test.cpp +++ b/test/unittest/common/native/subcommand_help_test.cpp @@ -41,9 +41,9 @@ public: { } - bool OnSubCommand(std::vector &args) override + HiperfError OnSubCommand(std::vector& args) override { - return true; + return HiperfError::NO_ERROR; } }; @@ -73,7 +73,7 @@ HWTEST_F(SubCommandHelpTest, TestOnSubCommand, TestSize.Level1) std::vector args; args = {"--help"}; - EXPECT_EQ(subCommandHelp.OnSubCommand(args), true); + EXPECT_EQ(subCommandHelp.OnSubCommand(args), HiperfError::NO_ERROR); } /** diff --git a/test/unittest/common/native/subcommand_list_test.cpp b/test/unittest/common/native/subcommand_list_test.cpp index a550b16..3ba6f5d 100644 --- a/test/unittest/common/native/subcommand_list_test.cpp +++ b/test/unittest/common/native/subcommand_list_test.cpp @@ -52,7 +52,7 @@ HWTEST_F(SubCommandListTest, TestOnSubCommandHW, TestSize.Level1) stdoutRecord.Start(); args = {"hw"}; - EXPECT_EQ(subCommandList.OnSubCommand(args), true); + EXPECT_EQ(subCommandList.OnSubCommand(args), HiperfError::NO_ERROR); std::string stringOut = stdoutRecord.Stop(); } @@ -68,7 +68,7 @@ HWTEST_F(SubCommandListTest, TestOnSubCommandSW, TestSize.Level1) stdoutRecord.Start(); args = {"sw"}; - EXPECT_EQ(subCommandList.OnSubCommand(args), true); + EXPECT_EQ(subCommandList.OnSubCommand(args), HiperfError::NO_ERROR); std::string stringOut = stdoutRecord.Stop(); } @@ -84,7 +84,7 @@ HWTEST_F(SubCommandListTest, TestOnSubCommandTP, TestSize.Level1) stdoutRecord.Start(); args = {"tp"}; - EXPECT_EQ(subCommandList.OnSubCommand(args), true); // still not support + EXPECT_EQ(subCommandList.OnSubCommand(args), HiperfError::NO_ERROR); // still not support std::string stringOut = stdoutRecord.Stop(); } @@ -100,7 +100,7 @@ HWTEST_F(SubCommandListTest, TestOnSubCommandCACHE, TestSize.Level1) stdoutRecord.Start(); args = {"cache"}; - EXPECT_EQ(subCommandList.OnSubCommand(args), true); + EXPECT_EQ(subCommandList.OnSubCommand(args), HiperfError::NO_ERROR); std::string stringOut = stdoutRecord.Stop(); } @@ -116,7 +116,7 @@ HWTEST_F(SubCommandListTest, TestOnSubCommandRAW, TestSize.Level1) stdoutRecord.Start(); args = {"raw"}; - EXPECT_EQ(subCommandList.OnSubCommand(args), true); + EXPECT_EQ(subCommandList.OnSubCommand(args), HiperfError::NO_ERROR); std::string stringOut = stdoutRecord.Stop(); } @@ -132,7 +132,7 @@ HWTEST_F(SubCommandListTest, TestOnSubCommandERROR, TestSize.Level1) stdoutRecord.Start(); args = {"error"}; - EXPECT_EQ(subCommandList.OnSubCommand(args), false); + EXPECT_EQ(subCommandList.OnSubCommand(args), HiperfError::OPTION_NOT_SUPPORT); std::string stringOut = stdoutRecord.Stop(); } @@ -148,7 +148,7 @@ HWTEST_F(SubCommandListTest, TestOnSubCommandEmpty, TestSize.Level1) stdoutRecord.Start(); args.clear(); - EXPECT_EQ(subCommandList.OnSubCommand(args), true); + EXPECT_EQ(subCommandList.OnSubCommand(args), HiperfError::NO_ERROR); std::string stringOut = stdoutRecord.Stop(); } diff --git a/test/unittest/common/native/subcommand_test.cpp b/test/unittest/common/native/subcommand_test.cpp index d64c37a..a1d5754 100644 --- a/test/unittest/common/native/subcommand_test.cpp +++ b/test/unittest/common/native/subcommand_test.cpp @@ -33,9 +33,9 @@ public: class SubcommandObj : public SubCommand { public: SubcommandObj() : SubCommand("subcomm", "test subcomm", "ut test subcomm") {} - bool OnSubCommand(std::vector &args) override + HiperfError OnSubCommand(std::vector& args) override { - return true; + return HiperfError::NO_ERROR; } }; -- Gitee