/* * Copyright (c) 2021-2022 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 HIPERF_SUBCOMMAND_H_ #define HIPERF_SUBCOMMAND_H_ #include #include "utilities.h" #include "virtual_runtime.h" namespace OHOS { namespace Developtools { namespace HiPerf { class SubCommand { public: SubCommand(const std::string &name, const std::string &brief, const std::string &help) : name_(name), brief_(brief), help_(help) { } virtual ~SubCommand() {} const std::string &Name() const { return name_; } const std::string &Brief() const { return brief_; } const std::string &Help() const { return help_; } // parse option first bool OnSubCommandOptions(std::vector args); // some help cmd bool OnPreSubCommand() { if (showHelp_) { printf("%s\n", Help().c_str()); return true; } return false; }; virtual void DumpOptions() const {} // args should be empty after all the args processed virtual bool ParseOption(std::vector &args) { args.clear(); // all the args is processed return true; } // return false means cmd failed virtual bool 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); }; // called from main static bool RegisterSubCommand(std::string, std::unique_ptr); // get some cmd static const std::map> &GetSubCommands(); static SubCommand *FindSubCommand(std::string); // for test code static void ClearSubCommands(); // check restart option bool CheckRestartOption(std::string appPackage, bool targetSystemWide, bool restart, std::vector &selectPids); // handle subcommand exclude bool HandleSubCommandExclude(const std::vector &excludeTids, const std::vector &excludeThreadNames, std::vector &selectTids); private: void ExcludeTidsFromSelectTids(const std::vector &excludeTids, std::vector &selectTids); void ExcludeThreadsFromSelectTids(const std::vector &excludeThreadNames, std::vector &selectTids); VirtualRuntime virtualRuntime_; const int CHECK_FREQUENCY = 100; const uint64_t CHECK_TIMEOUT = 30; protected: const std::string name_; const std::string brief_; std::string help_; bool dumpOptions_ = false; bool showHelp_ = false; }; } // namespace HiPerf } // namespace Developtools } // namespace OHOS #endif // HIPERF_SUBCOMMAND_H_