1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef HIPERF_SUBCOMMAND_H_ 16 #define HIPERF_SUBCOMMAND_H_ 17 #include <string> 18 19 #include "command_reporter.h" 20 #include "utilities.h" 21 #include "virtual_runtime.h" 22 23 namespace OHOS { 24 namespace Developtools { 25 namespace HiPerf { 26 class SubCommand { 27 public: SubCommand(const std::string & name,const std::string & brief,const std::string & help)28 SubCommand(const std::string &name, const std::string &brief, const std::string &help) 29 : name_(name), brief_(brief), help_(help) 30 { 31 } 32 ~SubCommand()33 virtual ~SubCommand() {} 34 Name()35 const std::string &Name() const 36 { 37 return name_; 38 } Brief()39 const std::string &Brief() const 40 { 41 return brief_; 42 } Help()43 const std::string &Help() const 44 { 45 return help_; 46 } 47 48 // parse option first 49 bool OnSubCommandOptions(std::vector<std::string> args); 50 51 // some help cmd OnPreSubCommand()52 bool OnPreSubCommand() 53 { 54 if (showHelp_) { 55 printf("%s\n", Help().c_str()); 56 return true; 57 } 58 return false; 59 }; 60 DumpOptions()61 virtual void DumpOptions() const {} 62 63 // args should be empty after all the args processed ParseOption(std::vector<std::string> & args)64 virtual bool ParseOption(std::vector<std::string> &args) 65 { 66 args.clear(); // all the args is processed 67 return true; 68 } 69 70 // add args for hisysevent AddReportArgs(CommandReporter & reporter)71 virtual void AddReportArgs(CommandReporter& reporter) {}; 72 73 // return NO_ERROR means cmd success 74 virtual HiperfError OnSubCommand(std::vector<std::string>& args) = 0; 75 // some test code will use this for simple OnSubCommand(std::string stringArgs)76 bool OnSubCommand(std::string stringArgs) 77 { 78 auto args = StringSplit(stringArgs, " "); 79 return OnSubCommand(args) != HiperfError::NO_ERR; 80 }; 81 82 // get some cmd 83 static bool RegisterSubCommand(const std::string& cmdName, std::function<SubCommand&()> func); 84 static const std::map<std::string, std::function<SubCommand&()>> &GetSubCommands(); 85 static SubCommand *FindSubCommand(std::string &cmdName); 86 87 // for test code 88 static bool RegisterSubCommand(const std::string& cmdName, std::unique_ptr<SubCommand> subCommand); 89 static void ClearSubCommands(); 90 91 // check restart option 92 bool CheckRestartOption(std::string &appPackage, bool targetSystemWide, bool restart, 93 std::vector<pid_t> &selectPids); 94 95 // handle subcommand exclude 96 bool HandleSubCommandExclude(const std::vector<pid_t> &excludeTids, const std::vector<std::string> 97 &excludeThreadNames, std::vector<pid_t> &selectTids); 98 private: 99 void ExcludeTidsFromSelectTids(const std::vector<pid_t> &excludeTids, std::vector<pid_t> &selectTids); 100 void ExcludeThreadsFromSelectTids(const std::vector<std::string> &excludeThreadNames, 101 std::vector<pid_t> &selectTids); 102 VirtualRuntime virtualRuntime_; 103 104 static std::mutex subCommandMutex_; 105 static std::map<std::string, std::unique_ptr<SubCommand>> subCommandMap_; 106 static std::map<std::string, std::function<SubCommand&()>> subCommandFuncMap_; 107 // Above guarded by subCommandMutex_ 108 protected: 109 const std::string name_; 110 const std::string brief_; 111 std::string help_; 112 bool dumpOptions_ = false; 113 bool showHelp_ = false; 114 bool isHM_ = false; 115 }; 116 } // namespace HiPerf 117 } // namespace Developtools 118 } // namespace OHOS 119 #endif // HIPERF_SUBCOMMAND_H_ 120