xref: /developtools/hiperf/include/subcommand.h (revision 48f512ce)
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
21namespace OHOS {
22namespace Developtools {
23namespace HiPerf {
24class SubCommand {
25public:
26    SubCommand(const std::string &name, const std::string &brief, const std::string &help)
27        : name_(name), brief_(brief), help_(help)
28    {
29    }
30
31    virtual ~SubCommand() {}
32
33    const std::string &Name() const
34    {
35        return name_;
36    }
37    const std::string &Brief() const
38    {
39        return brief_;
40    }
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
50    bool OnPreSubCommand()
51    {
52        if (showHelp_) {
53            printf("%s\n", Help().c_str());
54            return true;
55        }
56        return false;
57    };
58
59    virtual void DumpOptions() const {}
60
61    // args should be empty after all the args processed
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
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 &cmdName);
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);
94private:
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
100protected:
101    const std::string name_;
102    const std::string brief_;
103    std::string help_;
104    bool dumpOptions_ = false;
105    bool showHelp_ = false;
106    bool isHM_ = false;
107};
108} // namespace HiPerf
109} // namespace Developtools
110} // namespace OHOS
111#endif // HIPERF_SUBCOMMAND_H_
112