1 /* 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 20 #include <functional> 21 #include <memory> 22 #include <limits> 23 #include <string> 24 #include <vector> 25 26 #include <android-base/logging.h> 27 #include <android-base/macros.h> 28 #include <android-base/parseint.h> 29 30 class Command { 31 public: Command(const std::string & name,const std::string & short_help_string,const std::string & long_help_string)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) { 35 } 36 ~Command()37 virtual ~Command() { 38 } 39 Name()40 const std::string& Name() const { 41 return name_; 42 } 43 ShortHelpString()44 const std::string& ShortHelpString() const { 45 return short_help_string_; 46 } 47 LongHelpString()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 54 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 72 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); 75 76 private: 77 const std::string name_; 78 const std::string short_help_string_; 79 const std::string long_help_string_; 80 81 DISALLOW_COPY_AND_ASSIGN(Command); 82 }; 83 84 void RegisterCommand(const std::string& cmd_name, 85 const std::function<std::unique_ptr<Command>(void)>& callback); 86 void UnRegisterCommand(const std::string& cmd_name); 87 std::unique_ptr<Command> CreateCommandInstance(const std::string& cmd_name); 88 const std::vector<std::string> GetAllCommandNames(); 89 bool RunSimpleperfCmd(int argc, char** argv); 90 91 namespace simpleperf { 92 extern bool log_to_android_buffer; 93 } 94 95 #endif // SIMPLE_PERF_COMMAND_H_ 96