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 <thread>
16 #include <cstdio>
17 #include <ios>
18 #include <vector>
19 #include <iostream>
20 #include <fstream>
21 #include <sstream>
22 #include <regex>
23 #include "unistd.h"
24 #include "include/startup_delay.h"
25 #include "include/sp_utils.h"
26 namespace OHOS {
27 namespace SmartPerf {
StartUpDelay()28 StartUpDelay::StartUpDelay() {}
~StartUpDelay()29 StartUpDelay::~StartUpDelay() {}
GetTrace(const std::string & sessionID,const std::string & traceName)30 void StartUpDelay::GetTrace(const std::string &sessionID, const std::string &traceName)
31 {
32 std::string result;
33 std::string cmdString;
34 if (access("/sys/kernel/tracing/hongmeng", F_OK) != -1) {
35 cmdString = "hitrace -t 5 -b 102400 --overwrite idle ace app ohos ability graphic ";
36 } else {
37 cmdString = "hitrace -t 5 -b 20480 --overwrite idle ace app ohos ability graphic ";
38 }
39 std::string cmdStringEnd{"sched freq sync workq multimodalinput > "};
40 SPUtils::LoadCmd(cmdString + cmdStringEnd + traceName, result);
41 }
ThreadGetTrace(const std::string & sessionID,const std::string & traceName)42 std::thread StartUpDelay::ThreadGetTrace(const std::string &sessionID, const std::string &traceName)
43 {
44 std::thread thGetTrace(&StartUpDelay::GetTrace, this, sessionID, traceName);
45 return thGetTrace;
46 }
GetLayout()47 void StartUpDelay::GetLayout()
48 {
49 std::string result;
50 SPUtils::LoadCmd("uitest dumpLayout", result);
51 }
ThreadGetLayout()52 std::thread StartUpDelay::ThreadGetLayout()
53 {
54 std::thread thGetLayout(&StartUpDelay::GetLayout, this);
55 return thGetLayout;
56 }
InputEvent(const std::string & point)57 void StartUpDelay::InputEvent(const std::string &point)
58 {
59 std::string cmdResult = "";
60 int time = 4;
61 sleep(time);
62 std::string cmd = "uinput -T -d " + point + " -u" + point;
63 SPUtils::LoadCmd(cmd, cmdResult);
64 }
ThreadInputEvent(const std::string & point)65 std::thread StartUpDelay::ThreadInputEvent(const std::string &point)
66 {
67 std::thread thInputEvent(&StartUpDelay::InputEvent, this, point);
68 return thInputEvent;
69 }
GetHisysId()70 void StartUpDelay::GetHisysId()
71 {
72 int time = 10;
73 sleep(time);
74 std::string str = "";
75 SPUtils::LoadCmd("ps -ef |grep hisysevent", str);
76 std::stringstream ss(str);
77 std::string line = "";
78 getline(ss, line);
79 std::stringstream ssLine(line);
80 std::string word = "";
81 std::string secondStr;
82 int count = 0;
83 int num = 2;
84 while (ssLine >> word) {
85 count++;
86 if (count == num) {
87 secondStr = word;
88 break;
89 }
90 }
91 SPUtils::LoadCmd("kill " + secondStr, str);
92 }
GetHisysIdResponse() const93 void StartUpDelay::GetHisysIdResponse() const
94 {
95 int time = 10;
96 sleep(time);
97 std::string str = "";
98 SPUtils::LoadCmd("ps -ef |grep INTERACTION_RESPONSE_LATENCY", str);
99 std::stringstream ss(str);
100 std::string line = "";
101 getline(ss, line);
102 std::stringstream ssLine(line);
103 std::string word = "";
104 std::string secondStr;
105 int count = 0;
106 int num = 2;
107 while (ssLine >> word) {
108 count++;
109 if (count == num) {
110 secondStr = word;
111 break;
112 }
113 }
114 SPUtils::LoadCmd("kill " + secondStr, str);
115 }
GetHisysIdComplete() const116 void StartUpDelay::GetHisysIdComplete() const
117 {
118 int time = 10;
119 sleep(time);
120 std::string str = "";
121 SPUtils::LoadCmd("ps -ef |grep INTERACTION_COMPLETED_LATENCY", str);
122 std::stringstream ss(str);
123 std::string line = "";
124 getline(ss, line);
125 std::stringstream ssLine(line);
126 std::string word = "";
127 std::string secondStr;
128 int count = 0;
129 int num = 2;
130 while (ssLine >> word) {
131 count++;
132 if (count == num) {
133 secondStr = word;
134 break;
135 }
136 }
137 SPUtils::LoadCmd("kill " + secondStr, str);
138 }
GetSpTcp()139 bool StartUpDelay::GetSpTcp()
140 {
141 std::string resultPid;
142 std::string str;
143 SPUtils::LoadCmd("pidof SP_daemon", resultPid);
144 size_t splitFlag = resultPid.find(" ");
145 resultPid = resultPid.substr(0, splitFlag);
146 SPUtils::LoadCmd("kill " + resultPid, str);
147 return false;
148 }
ThreadGetHisysIdResponse() const149 std::thread StartUpDelay::ThreadGetHisysIdResponse() const
150 {
151 std::thread thGetHisysIdResponse(&StartUpDelay::GetHisysIdResponse, this);
152 return thGetHisysIdResponse;
153 }
ThreadGetHisysId()154 std::thread StartUpDelay::ThreadGetHisysId()
155 {
156 std::thread thGetHisysId(&StartUpDelay::GetHisysId, this);
157 return thGetHisysId;
158 }
ThreadGetHisysIdComplete() const159 std::thread StartUpDelay::ThreadGetHisysIdComplete() const
160 {
161 std::thread thGetHisysIdComplete(&StartUpDelay::GetHisysIdComplete, this);
162 return thGetHisysIdComplete;
163 }
ChangeToBackground()164 void StartUpDelay::ChangeToBackground()
165 {
166 std::string result;
167 sleep(1);
168 SPUtils::LoadCmd("uinput -T -m 600 2760 600 1300 200", result);
169 sleep(1);
170 }
GetPidByPkg(const std::string & curPkgName)171 std::string StartUpDelay::GetPidByPkg(const std::string &curPkgName)
172 {
173 std::string resultPid;
174 SPUtils::LoadCmd("pidof " + curPkgName, resultPid);
175 size_t splitFlag = resultPid.find(" ");
176 resultPid = resultPid.substr(0, splitFlag);
177 return resultPid;
178 }
GetDeviceType()179 std::string StartUpDelay::GetDeviceType()
180 {
181 std::string cmdResult;
182 SPUtils::LoadCmd("param get |grep ohos.boot.hardware", cmdResult);
183 size_t splitFlag = cmdResult.find("= ");
184 std::string deviceType = cmdResult.substr(splitFlag + 1);
185 return deviceType;
186 }
InitXY2(const std::string & curAppName,const std::string & fileName,const std::string & appPkgName)187 void StartUpDelay::InitXY2(const std::string &curAppName, const std::string &fileName, const std::string &appPkgName)
188 {
189 std::ifstream file(fileName, std::ios::in);
190 std::string strLine = "";
191 std::regex pattern("\\d+");
192 size_t findIndex = std::string::npos;
193 while (getline(file, strLine)) {
194 size_t appPkgIndex = strLine.find("AppName_text_" + appPkgName);
195 size_t appIndex = strLine.find(curAppName);
196 if (appIndex != std::string::npos) {
197 findIndex = appIndex;
198 } else {
199 findIndex = appPkgIndex;
200 }
201 if (findIndex != std::string::npos) {
202 size_t bounds = strLine.rfind("bounds", findIndex);
203 if (bounds > 0) {
204 std::string boundStr = strLine.substr(bounds, 30);
205 std::smatch result;
206 std::string::const_iterator iterStart = boundStr.begin();
207 std::string::const_iterator iterEnd = boundStr.end();
208 std::vector<std::string> pointVector;
209 while (std::regex_search(iterStart, iterEnd, result, pattern)) {
210 std::string startX = result[0];
211 iterStart = result[0].second;
212 pointVector.push_back(startX);
213 }
214 size_t num = 3;
215 size_t pointNum = pointVector.size();
216 if (pointNum > num) {
217 int x = (std::atoi(pointVector[2].c_str()) + std::atoi(pointVector[0].c_str())) / 2;
218 int y = (std::atoi(pointVector[3].c_str()) + std::atoi(pointVector[1].c_str())) / 2;
219 pointXY = std::to_string(x) + " " + std::to_string(y);
220 } else {
221 size_t leftStart = boundStr.find_first_of("[");
222 size_t leftEnd = boundStr.find_first_of("]");
223 pointXY = boundStr.substr(leftStart + 1, leftEnd - leftStart - 1);
224 pointXY = pointXY.replace(pointXY.find(","), 1, " ");
225 }
226 break;
227 }
228 } else {
229 break;
230 }
231 }
232 }
InitXY(const std::string & curAppName,const std::string & fileName)233 void StartUpDelay::InitXY(const std::string &curAppName, const std::string &fileName)
234 {
235 std::ifstream file(fileName, std::ios::in);
236 std::string strLine = "";
237 std::regex pattern("\\d+");
238 while (getline(file, strLine)) {
239 size_t appIndex = strLine.find(curAppName);
240 if (appIndex > 0) {
241 size_t bounds = strLine.rfind("bounds", appIndex);
242 if (bounds > 0) {
243 std::string boundStr = strLine.substr(bounds, 30);
244 std::smatch result;
245 std::string::const_iterator iterStart = boundStr.begin();
246 std::string::const_iterator iterEnd = boundStr.end();
247 std::vector<std::string> pointVector;
248 while (std::regex_search(iterStart, iterEnd, result, pattern)) {
249 std::string startX = result[0];
250 iterStart = result[0].second;
251 pointVector.push_back(startX);
252 }
253 size_t num = 3;
254 size_t pointNum = pointVector.size();
255 if (pointNum > num) {
256 int x = (std::atoi(pointVector[2].c_str()) + std::atoi(pointVector[0].c_str())) / 2;
257 int y = (std::atoi(pointVector[3].c_str()) + std::atoi(pointVector[1].c_str())) / 2;
258 pointXY = std::to_string(x) + " " + std::to_string(y);
259 } else {
260 size_t leftStart = boundStr.find_first_of("[");
261 size_t leftEnd = boundStr.find_first_of("]");
262 pointXY = boundStr.substr(leftStart + 1, leftEnd - leftStart - 1);
263 pointXY = pointXY.replace(pointXY.find(","), 1, " ");
264 }
265 break;
266 }
267 } else {
268 break;
269 }
270 }
271 }
272 }
273 }
274