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 rkCapacityRmPath = "/data/service/el0/battery/battery/capacity";
121 const std::string capacitySavePath = "/data/local/tmp/powerLeftRecord.csv";
122 std::string capacityString;
123 std::ifstream infile(capacitySavePath.c_str());
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 if (recordPower.empty()) {
153 std::string rkRecordPower;
154 int maxBat = 60;
155 OHOS::SmartPerf::SPUtils::LoadFile(rkCapacityRmPath, rkRecordPower);
156 recordPower = std::to_string(OHOS::SmartPerf::SPUtilesTye::StringToSometype<int>(rkRecordPower) * maxBat);
157 }
158 std::cout << "recordTime: " << recordTime << std::endl << "recordPower: " << recordPower << std::endl;
159 capacityString += recordTime + "," + recordPower;
160 outFile << capacityString << std::endl;
161 if (outFile.fail()) {
162 const int bufSize = 256;
163 char buf[bufSize] = { 0 };
164 std::cout << "Error writing capacity failed:" << strerror_r(errno, buf, bufSize) << std::endl;
165 }
166 outFile.close();
167 }
168
ProcessSpecificParameter(int argc,char * argv[],std::vector<std::string> & vec)169 static int ProcessSpecificParameter(int argc, char *argv[], std::vector<std::string> &vec)
170 {
171 if (argc > 1 && strcmp(argv[1], "-editor") == 0) {
172 OHOS::SmartPerf::EditorCommand(argc, vec);
173 return 0;
174 } else if (argc > 1 && strcmp(argv[1], "-profilerfps") == 0) {
175 OHOS::SmartPerf::ProfilerFPS::GetInstance().GetFPS(vec);
176 return 0;
177 } else if (argc > 1 && strcmp(argv[1], "-start") == 0) {
178 std::string startStr = "";
179 std::string endStr = "";
180 std::string pidCmd = OHOS::SmartPerf::CMD_COMMAND_MAP.at(OHOS::SmartPerf::CmdCommand::PIDOF_SP);
181 OHOS::SmartPerf::SPUtils::LoadCmd(pidCmd, startStr);
182 OHOS::SmartPerf::ClientControl cc;
183 cc.StartSPDaemon();
184 OHOS::SmartPerf::SPUtils::LoadCmd(pidCmd, endStr);
185 std::vector<std::string> startParams;
186 std::vector<std::string> endParams;
187 OHOS::SmartPerf::SPUtils::StrSplit(startStr, " ", startParams);
188 OHOS::SmartPerf::SPUtils::StrSplit(endStr, " ", endParams);
189 std::string result;
190 const int maxExpectedArgs = 100;
191 for (int i = 2; i < argc && i < maxExpectedArgs; i++) {
192 result += argv[i];
193 if (i != argc - 1) {
194 result += " ";
195 }
196 }
197 if (startParams.size() == endParams.size()) {
198 std::cout << "The last collection is interrupted." << std::endl;
199 std::cout << "SP_daemon -start " << result << " started collecting..." << std::endl;
200 }
201 cc.SocketStart(result);
202 return 1;
203 } else if (argc > 1 && strcmp(argv[1], "-stop") == 0) {
204 SocketStopCommand();
205 return 1;
206 } else if (argc > 1 && strcmp(argv[1], "-deviceinfo") == 0) {
207 std::cout << OHOS::SmartPerf::SPUtils::GetDeviceInfoMap() << std::endl;
208 return 0;
209 } else if (argc > 1 && strcmp(argv[1], "-ohtestfps") == 0) {
210 OHOS::SmartPerf::ProfilerFPS::GetInstance().GetOhFps(vec);
211 return 0;
212 } else if (argc > 1 && strcmp(argv[1], "-recordcapacity") == 0) {
213 RecordCapacity();
214 return 0;
215 }
216
217 return 1;
218 }
219
main(int argc,char * argv[])220 int main(int argc, char *argv[])
221 {
222 if (!OHOS::system::GetBoolParameter("const.security.developermode.state", true)) {
223 std::cout << "Not a development mode state" << std::endl;
224 return 0;
225 }
226 if (argc < 0) {
227 std::cout << "Invalid argument count" << std::endl;
228 return -1;
229 }
230 std::string errorInfo;
231 std::vector<std::string> vec;
232 const int maxExpectedArgs = 100;
233 for (int i = 0; i < argc && i < maxExpectedArgs; i++) {
234 vec.push_back(argv[i]);
235 }
236 if (!g_checkCmdParam(vec, errorInfo)) {
237 std::cout << "SP_daemon:" << errorInfo << std::endl <<
238 "Usage: SP_daemon [options] [arguments]" << std::endl << std::endl <<
239 "Try `SP_daemon --help' for more options." << std::endl;
240 return 0;
241 }
242 OHOS::SmartPerf::SPUtils::SetRkFlag();
243 if (ProcessSpecificParameter(argc, argv, vec) == 0) {
244 return 0;
245 }
246 OHOS::SmartPerf::SmartPerfCommand cmd(vec);
247 std::cout << cmd.ExecCommand() << std::endl;
248 return 0;
249 }
250