• 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 constexpr const char *VERSION_TYPE = "const.logsystem.versiontype";
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     if (str.find("-PKG") != std::string::npos && str.find("-PID") != std::string::npos) {
84         errorInfo = "-PKG and -PID cannot be used together with";
85         return false;
86     }
87     KeyInsert(keys);
88     if (argv[1].find("editorServer:") != std::string::npos) {
89         keys.insert(argv[1].substr(1).c_str());
90     }
91     for (auto a : OHOS::SmartPerf::COMMAND_MAP) {
92         keys.insert(a.first.substr(1)); // No prefix required '-'
93     }
94 
95     /* ************The command line for the following parameters is not implemented****************** */
96     auto itr = keys.find("f1");
97     if (keys.end() != itr) {
98         keys.erase(itr);
99     }
100     itr = keys.find("f2");
101     if (keys.end() != itr) {
102         keys.erase(itr);
103     }
104     itr = keys.find("fl");
105     if (keys.end() != itr) {
106         keys.erase(itr);
107     }
108     itr = keys.find("ftl");
109     if (keys.end() != itr) {
110         keys.erase(itr);
111     }
112     return OHOS::SmartPerf::SPUtils::VeriyParameter(keys, str, errorInfo);
113 }
114 
SocketStopCommand()115 static void SocketStopCommand()
116 {
117     OHOS::SmartPerf::ClientControl cc;
118     cc.SocketStop();
119 }
120 
RecordCapacity()121 static void RecordCapacity()
122 {
123     const std::string capacityRmPath = "/sys/class/power_supply/Battery/capacity_rm";
124     const std::string rkCapacityRmPath = "/data/service/el0/battery/battery/capacity";
125     const std::string capacitySavePath = "/data/local/tmp/powerLeftRecord.csv";
126     std::string capacityString;
127     std::ifstream infile(capacitySavePath.c_str());
128     if (infile.is_open()) {
129         std::stringstream buffer;
130         int capacityLine = 0;
131         std::string line;
132         const int MAX_RECORD_COUNT = 100;
133         buffer << infile.rdbuf();
134         capacityString = buffer.str();
135         infile.close();
136 
137         while (std::getline(buffer, line)) {
138             capacityLine++;
139         }
140         if (capacityLine == MAX_RECORD_COUNT) {
141             std::size_t pos = capacityString.find('\n');
142             if (pos != std::string::npos) {
143                 capacityString = capacityString.substr(pos + 1);
144             }
145         }
146     }
147     std::ofstream outFile(capacitySavePath.c_str(), std::ios::out | std::ios::trunc);
148     if (!outFile.is_open()) {
149         std::cout << "Error opening capacity file!" << std::endl;
150         return;
151     }
152     std::string recordPower;
153     auto recordTime = std::to_string(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()));
154     OHOS::SmartPerf::SPUtils::LoadFile(capacityRmPath, recordPower);
155     if (recordPower.empty()) {
156         std::string rkRecordPower;
157         int maxBat = 60;
158         OHOS::SmartPerf::SPUtils::LoadFile(rkCapacityRmPath, rkRecordPower);
159         recordPower = std::to_string(OHOS::SmartPerf::SPUtilesTye::StringToSometype<int>(rkRecordPower) * maxBat);
160     }
161     std::cout << "recordTime: " << recordTime << std::endl << "recordPower: " << recordPower << std::endl;
162     capacityString += recordTime + "," + recordPower;
163     outFile << capacityString << std::endl;
164     if (outFile.fail()) {
165         const int bufSize = 256;
166         char buf[bufSize] = { 0 };
167         std::cout << "Error writing capacity failed:" << strerror_r(errno, buf, bufSize) << std::endl;
168     }
169     outFile.close();
170 }
171 
ProcessSpecificParameter(int argc,char * argv[],std::vector<std::string> & vec)172 static int ProcessSpecificParameter(int argc, char *argv[], std::vector<std::string> &vec)
173 {
174     if (argc > 1 && strcmp(argv[1], "-editor") == 0) {
175         OHOS::SmartPerf::EditorCommand(argc, vec);
176         return 0;
177     } else if (argc > 1 && strcmp(argv[1], "-profilerfps") == 0) {
178         OHOS::SmartPerf::ProfilerFPS::GetInstance().GetFPS(vec);
179         return 0;
180     } else if (argc > 1 && strcmp(argv[1], "-start") == 0) {
181         std::string startStr = "";
182         std::string endStr = "";
183         std::string pidCmd = OHOS::SmartPerf::CMD_COMMAND_MAP.at(OHOS::SmartPerf::CmdCommand::PIDOF_SP);
184         OHOS::SmartPerf::SPUtils::LoadCmd(pidCmd, startStr);
185         OHOS::SmartPerf::ClientControl cc;
186         cc.StartSPDaemon();
187         OHOS::SmartPerf::SPUtils::LoadCmd(pidCmd, endStr);
188         std::vector<std::string> startParams;
189         std::vector<std::string> endParams;
190         OHOS::SmartPerf::SPUtils::StrSplit(startStr, " ", startParams);
191         OHOS::SmartPerf::SPUtils::StrSplit(endStr, " ", endParams);
192         std::string result;
193         const int maxExpectedArgs = 100;
194         for (int i = 2; i < argc && i < maxExpectedArgs; i++) {
195             result += argv[i];
196             if (i != argc - 1) {
197                 result += " ";
198             }
199         }
200         if (startParams.size() == endParams.size()) {
201             std::cout << "The last collection is interrupted." << std::endl;
202             std::cout << "SP_daemon -start " << result << " started collecting..." << std::endl;
203         }
204         cc.SocketStart(result);
205         return 1;
206     } else if (argc > 1 && strcmp(argv[1], "-stop") == 0) {
207         SocketStopCommand();
208         return 1;
209     } else if (argc > 1 && strcmp(argv[1], "-deviceinfo") == 0) {
210         std::cout << OHOS::SmartPerf::SPUtils::GetDeviceInfoMap() << std::endl;
211         return 0;
212     } else if (argc > 1 && strcmp(argv[1], "-ohtestfps") == 0) {
213         OHOS::SmartPerf::ProfilerFPS::GetInstance().GetOhFps(vec);
214         return 0;
215     } else if (argc > 1 && strcmp(argv[1], "-recordcapacity") == 0) {
216         RecordCapacity();
217         return 0;
218     }
219 
220     return 1;
221 }
222 
main(int argc,char * argv[])223 int main(int argc, char *argv[])
224 {
225     if (OHOS::system::GetParameter(VERSION_TYPE, "Unknown") != "beta") {
226         if (!OHOS::system::GetBoolParameter("const.security.developermode.state", true)) {
227             std::cout << "Not a development mode state" << std::endl;
228             return 0;
229         }
230     }
231     if (argc < 0) {
232         std::cout << "Invalid argument count" << std::endl;
233         return -1;
234     }
235     std::string errorInfo;
236     std::vector<std::string> vec;
237     const int maxExpectedArgs = 100;
238     for (int i = 0; i < argc && i < maxExpectedArgs; i++) {
239         vec.push_back(argv[i]);
240     }
241     if (!g_checkCmdParam(vec, errorInfo)) {
242         std::cout << "SP_daemon:" << errorInfo << std::endl <<
243              "Usage: SP_daemon [options] [arguments]" << std::endl << std::endl <<
244              "Try `SP_daemon --help' for more options." << std::endl;
245         return 0;
246     }
247     OHOS::SmartPerf::SPUtils::SetRkFlag();
248     if (ProcessSpecificParameter(argc, argv, vec) == 0) {
249         return 0;
250     }
251     OHOS::SmartPerf::SmartPerfCommand cmd(vec);
252     std::cout << cmd.ExecCommand() << std::endl;
253     return 0;
254 }
255