• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include <cstdio>
16 #include <thread>
17 #include <cstring>
18 #include "unistd.h"
19 #include <fstream>
20 #include <sstream>
21 #include "include/smartperf_command.h"
22 #include "include/editor_command.h"
23 #include "include/profiler_fps.h"
24 #include "include/client_control.h"
25 #include "include/sp_utils.h"
26 #include "include/sp_log.h"
27 #include "include/common.h"
28 #include "parameters.h"
29 
GetOptions(const std::vector<std::string> & argv)30 static std::string GetOptions(const std::vector<std::string> &argv)
31 {
32     std::string str = "";
33     std::string strFlag;
34     bool isFill = false;
35     for (std::size_t i = 0; i < argv.size(); i++) {
36         if (!isFill) {
37             strFlag = argv[i];
38             if (strFlag.find("SP_daemon") != std::string::npos) {
39                 isFill = true;
40             }
41         } else {
42             str += argv[i];
43             if (i + 1 != argv.size()) {
44                 str += " ";
45             }
46         }
47     }
48     return str;
49 }
KeyInsert(std::set<std::string> & keysMap)50 static void KeyInsert(std::set<std::string> &keysMap)
51 {
52     keysMap.insert("editor");
53     keysMap.insert("profilerfps");
54     keysMap.insert("start");
55     keysMap.insert("stop");
56     keysMap.insert("screen");
57     keysMap.insert("clear");
58     keysMap.insert("server");
59     keysMap.insert("sections");
60     keysMap.insert("deviceinfo");
61     keysMap.insert("ohtestfps");
62     keysMap.insert("editorServer");
63     keysMap.insert("recordcapacity");
64 }
g_checkCmdParam(std::vector<std::string> & argv,std::string & errorInfo)65 static bool g_checkCmdParam(std::vector<std::string> &argv, std::string &errorInfo)
66 {
67     std::string str = GetOptions(argv);
68     std::set<std::string> keys; // Includes three parts "SP_daemon" CommandType and CommandHelp
69     if (str.empty()) {
70         return true;
71     }
72     // 'help' and 'version' start with "--" and are processed separately
73     if (str.find("--help") != std::string::npos || str.find("--version") != std::string::npos) {
74         std::vector<std::string> out;
75         OHOS::SmartPerf::SPUtils::StrSplit(str, "-", out);
76         if (out.size() != 1) {
77             errorInfo = "--help and --version cannot be used together with other options";
78             return false;
79         } else {
80             return true;
81         }
82     }
83     KeyInsert(keys);
84     if (argv[1].find("editorServer:") != std::string::npos) {
85         keys.insert(argv[1].substr(1).c_str());
86     }
87     for (auto a : OHOS::SmartPerf::COMMAND_MAP) {
88         keys.insert(a.first.substr(1)); // No prefix required '-'
89     }
90 
91     /* ************The command line for the following parameters is not implemented****************** */
92     auto itr = keys.find("f1");
93     if (keys.end() != itr) {
94         keys.erase(itr);
95     }
96     itr = keys.find("f2");
97     if (keys.end() != itr) {
98         keys.erase(itr);
99     }
100     itr = keys.find("fl");
101     if (keys.end() != itr) {
102         keys.erase(itr);
103     }
104     itr = keys.find("ftl");
105     if (keys.end() != itr) {
106         keys.erase(itr);
107     }
108     return OHOS::SmartPerf::SPUtils::VeriyParameter(keys, str, errorInfo);
109 }
110 
SocketStopCommand()111 static void SocketStopCommand()
112 {
113     OHOS::SmartPerf::ClientControl cc;
114     cc.SocketStop();
115 }
116 
RecordCapacity()117 static void RecordCapacity()
118 {
119     const std::string capacityRmPath = "/sys/class/power_supply/Battery/capacity_rm";
120     const std::string capacitySavePath = "/data/local/tmp/powerLeftRecord.csv";
121     std::string capacityString;
122     std::ifstream infile(capacitySavePath.c_str());
123 
124     if (infile.is_open()) {
125         std::stringstream buffer;
126         int capacityLine = 0;
127         std::string line;
128         const int MAX_RECORD_COUNT = 100;
129         buffer << infile.rdbuf();
130         capacityString = buffer.str();
131         infile.close();
132 
133         while (std::getline(buffer, line)) {
134             capacityLine++;
135         }
136         if (capacityLine == MAX_RECORD_COUNT) {
137             std::size_t pos = capacityString.find('\n');
138             if (pos != std::string::npos) {
139                 capacityString = capacityString.substr(pos + 1);
140             }
141         }
142     }
143 
144     std::ofstream outFile(capacitySavePath.c_str(), std::ios::out | std::ios::trunc);
145     if (!outFile.is_open()) {
146         std::cout << "Error opening capacity file!" << std::endl;
147         return;
148     }
149     std::string recordPower;
150     auto recordTime = std::to_string(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()));
151     OHOS::SmartPerf::SPUtils::LoadFile(capacityRmPath, recordPower);
152     std::cout << "recordTime: " << recordTime << std::endl << "recordPower: " << recordPower << std::endl;
153     capacityString += recordTime + "," + recordPower;
154     outFile << capacityString << std::endl;
155     if (outFile.fail()) {
156         const int bufSize = 256;
157         char buf[bufSize] = { 0 };
158         std::cout << "Error writing capacity failed:" << strerror_r(errno, buf, bufSize) << std::endl;
159     }
160     outFile.close();
161 }
162 
ProcessSpecificParameter(int argc,char * argv[],std::vector<std::string> & vec)163 static int ProcessSpecificParameter(int argc, char *argv[], std::vector<std::string> &vec)
164 {
165     if (argc > 1 && strcmp(argv[1], "-editor") == 0) {
166         OHOS::SmartPerf::EditorCommand(argc, vec);
167         return 0;
168     } else if (argc > 1 && strcmp(argv[1], "-profilerfps") == 0) {
169         OHOS::SmartPerf::ProfilerFPS::GetInstance().GetFPS(vec);
170         return 0;
171     } else if (argc > 1 && strcmp(argv[1], "-start") == 0) {
172         std::string startStr = "";
173         std::string endStr = "";
174         std::string pidCmd = OHOS::SmartPerf::CMD_COMMAND_MAP.at(OHOS::SmartPerf::CmdCommand::PIDOF_SP);
175         OHOS::SmartPerf::SPUtils::LoadCmd(pidCmd, startStr);
176         OHOS::SmartPerf::ClientControl cc;
177         cc.StartSPDaemon();
178         OHOS::SmartPerf::SPUtils::LoadCmd(pidCmd, endStr);
179         std::vector<std::string> startParams;
180         std::vector<std::string> endParams;
181         OHOS::SmartPerf::SPUtils::StrSplit(startStr, " ", startParams);
182         OHOS::SmartPerf::SPUtils::StrSplit(endStr, " ", endParams);
183         std::string result;
184         for (int i = 2; i < argc; i++) {
185             result += argv[i];
186             if (i != argc - 1) {
187                 result += " ";
188             }
189         }
190         if (startParams.size() == endParams.size()) {
191             std::cout << "The last collection is interrupted." << std::endl;
192             std::cout << "SP_daemon -start " << result << " started collecting..." << std::endl;
193         }
194         cc.SocketStart(result);
195         return 1;
196     } else if (argc > 1 && strcmp(argv[1], "-stop") == 0) {
197         SocketStopCommand();
198         return 1;
199     } else if (argc > 1 && strcmp(argv[1], "-deviceinfo") == 0) {
200         std::cout << OHOS::SmartPerf::SPUtils::GetDeviceInfoMap() << std::endl;
201         return 0;
202     } else if (argc > 1 && strcmp(argv[1], "-ohtestfps") == 0) {
203         OHOS::SmartPerf::ProfilerFPS::GetInstance().GetOhFps(vec);
204         return 0;
205     } else if (argc > 1 && strcmp(argv[1], "-recordcapacity") == 0) {
206         RecordCapacity();
207         return 0;
208     }
209 
210     return 1;
211 }
212 
main(int argc,char * argv[])213 int main(int argc, char *argv[])
214 {
215     if (!OHOS::system::GetBoolParameter("const.security.developermode.state", true)) {
216         std::cout << "Not a development mode state" << std::endl;
217         return 0;
218     }
219     if (argc < 0) {
220         std::cout << "Invalid argument count" << std::endl;
221         return -1;
222     }
223     std::string errorInfo;
224     std::vector<std::string> vec;
225     const int maxExpectedArgs = 100;
226     for (int i = 0; i < argc && i < maxExpectedArgs; i++) {
227         vec.push_back(argv[i]);
228     }
229     if (!g_checkCmdParam(vec, errorInfo)) {
230         std::cout << "SP_daemon:" << errorInfo << std::endl <<
231              "Usage: SP_daemon [options] [arguments]" << std::endl << std::endl <<
232              "Try `SP_daemon --help' for more options." << std::endl;
233         return 0;
234     }
235     OHOS::SmartPerf::SPUtils::SetRkFlag();
236     if (ProcessSpecificParameter(argc, argv, vec) == 0) {
237         return 0;
238     }
239     OHOS::SmartPerf::SmartPerfCommand cmd(vec);
240     std::cout << cmd.ExecCommand() << std::endl;
241     return 0;
242 }
243