2021-12-24 00:46:38 -08:00
|
|
|
/*
|
2022-03-07 15:07:57 +08:00
|
|
|
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
|
2021-12-24 00:46:38 -08:00
|
|
|
* 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 <string>
|
2024-11-02 15:41:47 +08:00
|
|
|
|
|
|
|
#include "command_reporter.h"
|
2022-02-08 17:54:18 +08:00
|
|
|
#include "utilities.h"
|
2023-05-31 18:13:59 +08:00
|
|
|
#include "virtual_runtime.h"
|
2021-12-24 00:46:38 -08:00
|
|
|
|
|
|
|
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<std::string> args);
|
|
|
|
|
|
|
|
// some help cmd
|
|
|
|
bool OnPreSubCommand()
|
|
|
|
{
|
|
|
|
if (showHelp_) {
|
|
|
|
printf("%s\n", Help().c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2022-03-25 11:49:41 +08:00
|
|
|
virtual void DumpOptions() const {}
|
2021-12-24 00:46:38 -08:00
|
|
|
|
|
|
|
// args should be empty after all the args processed
|
|
|
|
virtual bool ParseOption(std::vector<std::string> &args)
|
|
|
|
{
|
|
|
|
args.clear(); // all the args is processed
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-11-02 15:41:47 +08:00
|
|
|
// add args for hisysevent
|
|
|
|
virtual void AddReportArgs(CommandReporter& reporter) {};
|
|
|
|
|
2025-01-02 19:05:34 +08:00
|
|
|
// return NO_ERROR means cmd success
|
|
|
|
virtual HiperfError OnSubCommand(std::vector<std::string>& args) = 0;
|
2021-12-24 00:46:38 -08:00
|
|
|
// some test code will use this for simple
|
|
|
|
bool OnSubCommand(std::string stringArgs)
|
|
|
|
{
|
|
|
|
auto args = StringSplit(stringArgs, " ");
|
2025-02-13 04:41:42 +00:00
|
|
|
return OnSubCommand(args) != HiperfError::NO_ERR;
|
2021-12-24 00:46:38 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// get some cmd
|
2024-12-19 17:26:55 +08:00
|
|
|
static bool RegisterSubCommand(const std::string& cmdName, std::function<SubCommand&()> func);
|
|
|
|
static const std::map<std::string, std::function<SubCommand&()>> &GetSubCommands();
|
2024-05-08 11:21:09 +08:00
|
|
|
static SubCommand *FindSubCommand(std::string &cmdName);
|
2021-12-24 00:46:38 -08:00
|
|
|
|
|
|
|
// for test code
|
2024-12-19 17:26:55 +08:00
|
|
|
static bool RegisterSubCommand(const std::string& cmdName, std::unique_ptr<SubCommand> subCommand);
|
2021-12-24 00:46:38 -08:00
|
|
|
static void ClearSubCommands();
|
2024-12-19 17:26:55 +08:00
|
|
|
|
2023-05-31 18:13:59 +08:00
|
|
|
// check restart option
|
2023-09-05 17:53:57 +08:00
|
|
|
bool CheckRestartOption(std::string &appPackage, bool targetSystemWide, bool restart,
|
2023-05-31 18:13:59 +08:00
|
|
|
std::vector<pid_t> &selectPids);
|
|
|
|
|
|
|
|
// handle subcommand exclude
|
|
|
|
bool HandleSubCommandExclude(const std::vector<pid_t> &excludeTids, const std::vector<std::string>
|
|
|
|
&excludeThreadNames, std::vector<pid_t> &selectTids);
|
|
|
|
private:
|
|
|
|
void ExcludeTidsFromSelectTids(const std::vector<pid_t> &excludeTids, std::vector<pid_t> &selectTids);
|
|
|
|
void ExcludeThreadsFromSelectTids(const std::vector<std::string> &excludeThreadNames,
|
|
|
|
std::vector<pid_t> &selectTids);
|
|
|
|
VirtualRuntime virtualRuntime_;
|
2021-12-24 00:46:38 -08:00
|
|
|
|
2024-12-19 17:26:55 +08:00
|
|
|
static std::mutex subCommandMutex_;
|
|
|
|
static std::map<std::string, std::unique_ptr<SubCommand>> subCommandMap_;
|
|
|
|
static std::map<std::string, std::function<SubCommand&()>> subCommandFuncMap_;
|
|
|
|
// Above guarded by subCommandMutex_
|
2021-12-24 00:46:38 -08:00
|
|
|
protected:
|
|
|
|
const std::string name_;
|
|
|
|
const std::string brief_;
|
|
|
|
std::string help_;
|
|
|
|
bool dumpOptions_ = false;
|
|
|
|
bool showHelp_ = false;
|
2023-10-29 16:38:55 +08:00
|
|
|
bool isHM_ = false;
|
2021-12-24 00:46:38 -08:00
|
|
|
};
|
|
|
|
} // namespace HiPerf
|
|
|
|
} // namespace Developtools
|
|
|
|
} // namespace OHOS
|
2022-03-25 11:49:41 +08:00
|
|
|
#endif // HIPERF_SUBCOMMAND_H_
|