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 #include "utilities.h" 19 #include "virtual_runtime.h" 20 21 namespace OHOS { 22 namespace Developtools { 23 namespace HiPerf { 24 class SubCommand { 25 public: SubCommand(const std::string & name,const std::string & brief,const std::string & help)26 SubCommand(const std::string &name, const std::string &brief, const std::string &help) 27 : name_(name), brief_(brief), help_(help) 28 { 29 } 30 ~SubCommand()31 virtual ~SubCommand() {} 32 Name()33 const std::string &Name() const 34 { 35 return name_; 36 } Brief()37 const std::string &Brief() const 38 { 39 return brief_; 40 } Help()41 const std::string &Help() const 42 { 43 return help_; 44 } 45 46 // parse option first 47 bool OnSubCommandOptions(std::vector<std::string> args); 48 49 // some help cmd OnPreSubCommand()50 bool OnPreSubCommand() 51 { 52 if (showHelp_) { 53 printf("%s\n", Help().c_str()); 54 return true; 55 } 56 return false; 57 }; 58 DumpOptions()59 virtual void DumpOptions() const {} 60 61 // args should be empty after all the args processed ParseOption(std::vector<std::string> & args)62 virtual bool ParseOption(std::vector<std::string> &args) 63 { 64 args.clear(); // all the args is processed 65 return true; 66 } 67 68 // return false means cmd failed 69 virtual bool OnSubCommand(std::vector<std::string> &args) = 0; 70 // some test code will use this for simple OnSubCommand(std::string stringArgs)71 bool OnSubCommand(std::string stringArgs) 72 { 73 auto args = StringSplit(stringArgs, " "); 74 return OnSubCommand(args); 75 }; 76 77 // called from main 78 static bool RegisterSubCommand(std::string, std::unique_ptr<SubCommand>); 79 80 // get some cmd 81 static const std::map<std::string, std::unique_ptr<SubCommand>> &GetSubCommands(); 82 static SubCommand *FindSubCommand(std::string); 83 84 // for test code 85 static void ClearSubCommands(); 86 87 // check restart option 88 bool CheckRestartOption(std::string appPackage, bool targetSystemWide, bool restart, 89 std::vector<pid_t> &selectPids); 90 91 // handle subcommand exclude 92 bool HandleSubCommandExclude(const std::vector<pid_t> &excludeTids, const std::vector<std::string> 93 &excludeThreadNames, std::vector<pid_t> &selectTids); 94 private: 95 void ExcludeTidsFromSelectTids(const std::vector<pid_t> &excludeTids, std::vector<pid_t> &selectTids); 96 void ExcludeThreadsFromSelectTids(const std::vector<std::string> &excludeThreadNames, 97 std::vector<pid_t> &selectTids); 98 VirtualRuntime virtualRuntime_; 99 const int CHECK_FREQUENCY = 100; 100 const uint64_t CHECK_TIMEOUT = 30; 101 102 protected: 103 const std::string name_; 104 const std::string brief_; 105 std::string help_; 106 bool dumpOptions_ = false; 107 bool showHelp_ = false; 108 }; 109 } // namespace HiPerf 110 } // namespace Developtools 111 } // namespace OHOS 112 #endif // HIPERF_SUBCOMMAND_H_ 113