blob: 2ce789e4c435bf988ce03dc5c415315db096ec8e [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SIMPLE_PERF_COMMAND_H_
18#define SIMPLE_PERF_COMMAND_H_
19
Yabin Cuif79f07e2015-06-01 11:21:37 -070020#include <functional>
21#include <memory>
Yabin Cuiacf04b22018-04-18 13:10:40 -070022#include <limits>
Yabin Cui67d3abd2015-04-16 15:26:31 -070023#include <string>
24#include <vector>
25
Yabin Cuiacf04b22018-04-18 13:10:40 -070026#include <android-base/logging.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080027#include <android-base/macros.h>
Yabin Cuiacf04b22018-04-18 13:10:40 -070028#include <android-base/parseint.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070029
30class Command {
31 public:
32 Command(const std::string& name, const std::string& short_help_string,
33 const std::string& long_help_string)
34 : name_(name), short_help_string_(short_help_string), long_help_string_(long_help_string) {
Yabin Cui67d3abd2015-04-16 15:26:31 -070035 }
36
37 virtual ~Command() {
38 }
39
40 const std::string& Name() const {
41 return name_;
42 }
43
44 const std::string& ShortHelpString() const {
45 return short_help_string_;
46 }
47
48 const std::string LongHelpString() const {
49 return long_help_string_;
50 }
51
52 virtual bool Run(const std::vector<std::string>& args) = 0;
53
Yabin Cuiacf04b22018-04-18 13:10:40 -070054 template <typename T>
55 bool GetUintOption(const std::vector<std::string>& args, size_t* pi, T* value, uint64_t min = 0,
56 uint64_t max = std::numeric_limits<T>::max(), bool allow_suffixes = false) {
57 if (!NextArgumentOrError(args, pi)) {
58 return false;
59 }
60 uint64_t tmp_value;
61 if (!android::base::ParseUint(args[*pi], &tmp_value, max, allow_suffixes) || tmp_value < min) {
62 LOG(ERROR) << "Invalid argument for option " << args[*pi - 1] << ": " << args[*pi];
63 return false;
64 }
65 *value = static_cast<T>(tmp_value);
66 return true;
67 }
68
69 bool GetDoubleOption(const std::vector<std::string>& args, size_t* pi, double* value,
70 double min = 0, double max = std::numeric_limits<double>::max());
71
Yabin Cuif79f07e2015-06-01 11:21:37 -070072 protected:
73 bool NextArgumentOrError(const std::vector<std::string>& args, size_t* pi);
74 void ReportUnknownOption(const std::vector<std::string>& args, size_t i);
Yabin Cui67d3abd2015-04-16 15:26:31 -070075
76 private:
77 const std::string name_;
78 const std::string short_help_string_;
79 const std::string long_help_string_;
80
Yabin Cui67d3abd2015-04-16 15:26:31 -070081 DISALLOW_COPY_AND_ASSIGN(Command);
82};
83
Yabin Cuif79f07e2015-06-01 11:21:37 -070084void RegisterCommand(const std::string& cmd_name,
Yabin Cui3e4c5952016-07-26 15:03:27 -070085 const std::function<std::unique_ptr<Command>(void)>& callback);
Yabin Cuif79f07e2015-06-01 11:21:37 -070086void UnRegisterCommand(const std::string& cmd_name);
87std::unique_ptr<Command> CreateCommandInstance(const std::string& cmd_name);
88const std::vector<std::string> GetAllCommandNames();
Yabin Cui616b3a02017-07-14 15:59:56 -070089bool RunSimpleperfCmd(int argc, char** argv);
Yabin Cuif79f07e2015-06-01 11:21:37 -070090
Yabin Cui67d3abd2015-04-16 15:26:31 -070091#endif // SIMPLE_PERF_COMMAND_H_