• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 
16 #ifndef ARGUMENT_PARSER
17 #define ARGUMENT_PARSER
18 
19 #include <iostream>
20 #include <string>
21 #include <unordered_map>
22 #include <map>
23 #include <variant>
24 #include <optional>
25 #include <vector>
26 #include <set>
27 
28 namespace OHOS::SmartPerf {
29 class ArgumentParser {
30 public:
31     using ArgValue = std::variant<int, std::string, bool>;
32     enum class ArgType { INT, STRING, BOOL };
33     struct ArgumentSpec {
34         ArgType type;
35         std::string description;
36         std::optional<int> min;
37         std::optional<int> max;
38     };
39 
40     void AddArgument(const std::string& name, ArgumentSpec info);
41     void Parse(int argc, char* argv[]);
42     void Parse(const std::string& input);
43     std::optional<ArgValue> Get(const std::string& name) const;
44     void PrintHelp() const;
45     const std::unordered_map<std::string, ArgValue>& Values() const;
46     const std::vector<std::string>& Errors() const;
47     bool Ok() const;
48     bool IsHelpMode() const;
49     void SetValue(const std::string& key, const ArgValue& value);
50 
51 private:
52     void HandleIntParameter(const std::string& key, std::string value, const ArgumentSpec& spec);
53 
54     std::unordered_map<std::string, ArgumentSpec> specs_ = {
55         {"-N", {ArgumentParser::ArgType::INT,
56             "set the collection times(default value is 0) range[1,2147483647], for example: -N 10", 1, 2147483647}
57         },
58         {"-PKG", {ArgumentParser::ArgType::STRING, "set package name, must add, for example: -PKG ohos.samples.ecg"}},
59         {"-PID", {ArgumentParser::ArgType::INT, "set process pid, must add, for example: -PID 3568"}},
60         {"-threads", {ArgumentParser::ArgType::BOOL,
61             "get threads, must add -PID or -PKG, for example: -threads -PID 3568 or -threads -PKG ohos.samples.ecg"}
62         },
63         {"-fds", {ArgumentParser::ArgType::BOOL,
64             "get file descriptor, must add -PID or -PKG, for example: -fds -PID 3568 or -fds -PKG ohos.samples.ecg"}
65         },
66         {"-c", {ArgumentParser::ArgType::BOOL,
67             "get device CPU frequency and CPU usage, process CPU usage and CPU load"}},
68         {"-g", {ArgumentParser::ArgType::BOOL, "get device GPU frequency and GPU load"}},
69         {"-f", {ArgumentParser::ArgType::BOOL,
70             "get app refresh fps(frames per second), fps jitters, and refresh rate"}},
71         {"-profilerfps", {ArgumentParser::ArgType::BOOL, "get refresh fps and timestamp"}},
72         {"-t", {ArgumentParser::ArgType::BOOL, "get remaining battery power and temperature"}},
73         {"-p", {ArgumentParser::ArgType::BOOL,
74             "get battery power consumption and voltage (Not supported by some devices)"}
75         },
76         {"-print", {ArgumentParser::ArgType::BOOL, "start mode print log"}},
77         {"-r", {ArgumentParser::ArgType::BOOL, "get process memory and total memory"}},
78         {"-snapshot", {ArgumentParser::ArgType::BOOL, "get screen capture"}},
79         {"-net", {ArgumentParser::ArgType::BOOL, "get uplink and downlink traffic"}},
80         {"-start", {ArgumentParser::ArgType::BOOL, "collection start command"}},
81         {"-stop", {ArgumentParser::ArgType::BOOL, "collection stop command"}},
82         {"-VIEW", {ArgumentParser::ArgType::STRING, "set layler, for example: -VIEW DisplayNode"}},
83         {"-OUT", {ArgumentParser::ArgType::STRING, "set csv output path"}},
84         {"-d", {ArgumentParser::ArgType::BOOL, "get device DDR information"}},
85         {"-screen", {ArgumentParser::ArgType::BOOL, "get screen resolution"}},
86         {"-deviceinfo", {ArgumentParser::ArgType::BOOL, "get device information"}},
87         {"-ohtestfps", {ArgumentParser::ArgType::BOOL,
88             "used by the vilidator to obtain the fps, the collection times can be set"}
89         },
90         {"-editorServer", {ArgumentParser::ArgType::BOOL,
91             "start a process to listen to the socket message of the editor"}},
92         {"-deviceServer", {ArgumentParser::ArgType::BOOL,
93             "start a process to listen to the socket message of the device"}},
94         {"-recordcapacity", {ArgumentParser::ArgType::BOOL, "get the battery level difference"}},
95         {"--version", {ArgumentParser::ArgType::BOOL, "get version"}},
96         {"--help", {ArgumentParser::ArgType::BOOL, "get help"}},
97         {"responseTime", {ArgumentParser::ArgType::BOOL,
98             "get the page response delay after an application is operated"}},
99         {"completeTime", {ArgumentParser::ArgType::BOOL,
100             "get the page completion delay after an application is operated"}},
101         {"fpsohtest", {ArgumentParser::ArgType::BOOL, "used by the vilidator to obtain the fps"}},
102         {"-gc", {ArgumentParser::ArgType::BOOL, "get gpu counter default frequency is 50"}},
103         {"-GPU_COUNTER", {ArgumentParser::ArgType::INT,
104             std::string("get gpu counter frequency which can be set in range [50,1000] and ") +
105             "must be a factor of 50, for example: -GPU_COUNTER 250",
106             50, 1000}
107         },
108         {"-ge", {ArgumentParser::ArgType::BOOL, "get game event"}},
109         {"-fc", {ArgumentParser::ArgType::BOOL, "get caton info"}},
110         {"-server", {ArgumentParser::ArgType::BOOL, "server"}},
111         {"-o", {ArgumentParser::ArgType::BOOL, "sdk data recv"}},
112         {"-ci", {ArgumentParser::ArgType::BOOL, "get cpu instructions and cycles"}},
113         {"-fl", {ArgumentParser::ArgType::INT, "set fps"}},
114         {"-ftl", {ArgumentParser::ArgType::INT, "set frameTime"}},
115         {"-lockfreq", {ArgumentParser::ArgType::BOOL}},
116         {"-nav", {ArgumentParser::ArgType::BOOL, "get page navigation information"}},
117         {"-SESSIONID", {ArgumentParser::ArgType::STRING, "session id"}},
118 
119         // UDP
120         {"-CPU", {ArgumentParser::ArgType::BOOL}},
121         {"-GPU", {ArgumentParser::ArgType::BOOL}},
122         {"-FPS", {ArgumentParser::ArgType::BOOL}},
123         {"-LOW_POWER", {ArgumentParser::ArgType::BOOL}},
124         {"-FDS", {ArgumentParser::ArgType::BOOL}},
125         {"-TEMP", {ArgumentParser::ArgType::BOOL}},
126         {"-POWER", {ArgumentParser::ArgType::BOOL}},
127         {"-RAM", {ArgumentParser::ArgType::BOOL}},
128         {"-SCREEN", {ArgumentParser::ArgType::BOOL}},
129         {"-DDR", {ArgumentParser::ArgType::BOOL}},
130         {"-NET", {ArgumentParser::ArgType::BOOL}},
131         {"-HCI", {ArgumentParser::ArgType::BOOL}},
132         {"-TRACE", {ArgumentParser::ArgType::BOOL}},
133     };
134     std::unordered_map<std::string, ArgValue> values_;
135     std::vector<std::string> errors_;
136     bool helpMode_{false};
137 };
138 }
139 
140 #endif // ARGUMENT_PARSER