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