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 <regex>
22 #include "unistd.h"
23 #include "include/startup_delay.h"
24 #include "include/sp_utils.h"
25 namespace OHOS {
26 namespace SmartPerf {
StartUpDelay()27 StartUpDelay::StartUpDelay() {}
~StartUpDelay()28 StartUpDelay::~StartUpDelay() {}
GetTrace(const std::string & sessionID,const std::string & traceName)29 void StartUpDelay::GetTrace(const std::string &sessionID, const std::string &traceName)
30 {
31 std::string result;
32 std::string cmdString{"bytrace -t 5 -b 20480 --overwrite idle ace app ohos ability graphic "};
33 std::string cmdStringEnd{"sched freq irq sync workq pagecache multimodalinput > "};
34 SPUtils::LoadCmd(cmdString + cmdStringEnd + traceName, result);
35 }
ThreadGetTrace(const std::string & sessionID,const std::string & traceName)36 std::thread StartUpDelay::ThreadGetTrace(const std::string &sessionID, const std::string &traceName)
37 {
38 std::thread thGetTrace(&StartUpDelay::GetTrace, this, sessionID, traceName);
39 return thGetTrace;
40 }
GetLayout()41 void StartUpDelay::GetLayout()
42 {
43 std::string result;
44 SPUtils::LoadCmd("uitest dumpLayout", result);
45 }
ThreadGetLayout()46 std::thread StartUpDelay::ThreadGetLayout()
47 {
48 std::thread thGetLayout(&StartUpDelay::GetLayout, this);
49 return thGetLayout;
50 }
ChangeToBackground()51 void StartUpDelay::ChangeToBackground()
52 {
53 std::string result;
54 sleep(1);
55 SPUtils::LoadCmd("uinput -T -m 600 2760 600 1300 200", result);
56 sleep(1);
57 }
GetPidByPkg(const std::string & curPkgName)58 std::string StartUpDelay::GetPidByPkg(const std::string &curPkgName)
59 {
60 std::string resultPid;
61 SPUtils::LoadCmd("pidof " + curPkgName, resultPid);
62 size_t splitFlag = resultPid.find(" ");
63 resultPid = resultPid.substr(0, splitFlag);
64 return resultPid;
65 }
GetDeviceType()66 std::string StartUpDelay::GetDeviceType()
67 {
68 std::string cmdResult;
69 SPUtils::LoadCmd("param get |grep ohos.boot.hardware", cmdResult);
70 size_t splitFlag = cmdResult.find("= ");
71 std::string deviceType = cmdResult.substr(splitFlag + 1);
72 return deviceType;
73 }
InitXY2(const std::string & curAppName,const std::string & fileName,const std::string & appPkgName)74 void StartUpDelay::InitXY2(const std::string &curAppName, const std::string &fileName, const std::string &appPkgName)
75 {
76 std::ifstream file(fileName, std::ios::in);
77 std::string strLine = "";
78 std::regex pattern("\\d+");
79 size_t findIndex = std::string::npos;
80 while (getline(file, strLine)) {
81 size_t appPkgIndex = strLine.find("AppName_text_" + appPkgName);
82 size_t appIndex = strLine.find(curAppName);
83 if (appIndex != std::string::npos) {
84 findIndex = appIndex;
85 } else {
86 findIndex = appPkgIndex;
87 }
88 if (findIndex != std::string::npos) {
89 size_t bounds = strLine.rfind("bounds", findIndex);
90 if (bounds > 0) {
91 std::string boundStr = strLine.substr(bounds, 30);
92 std::smatch result;
93 std::string::const_iterator iterStart = boundStr.begin();
94 std::string::const_iterator iterEnd = boundStr.end();
95 std::vector<std::string> pointVector;
96 while (std::regex_search(iterStart, iterEnd, result, pattern)) {
97 std::string startX = result[0];
98 iterStart = result[0].second;
99 pointVector.push_back(startX);
100 }
101 size_t num = 3;
102 size_t pointNum = pointVector.size();
103 if (pointNum > num) {
104 int x = (std::atoi(pointVector[2].c_str()) + std::atoi(pointVector[0].c_str())) / 2;
105 int y = (std::atoi(pointVector[3].c_str()) + std::atoi(pointVector[1].c_str())) / 2;
106 pointXY = std::to_string(x) + " " + std::to_string(y);
107 } else {
108 size_t leftStart = boundStr.find_first_of("[");
109 size_t leftEnd = boundStr.find_first_of("]");
110 pointXY = boundStr.substr(leftStart + 1, leftEnd - leftStart - 1);
111 pointXY = pointXY.replace(pointXY.find(","), 1, " ");
112 }
113 break;
114 }
115 } else {
116 break;
117 }
118 }
119 }
InitXY(const std::string & curAppName,const std::string & fileName)120 void StartUpDelay::InitXY(const std::string &curAppName, const std::string &fileName)
121 {
122 std::ifstream file(fileName, std::ios::in);
123 std::string strLine = "";
124 std::regex pattern("\\d+");
125 while (getline(file, strLine)) {
126 size_t appIndex = strLine.find(curAppName);
127 if (appIndex > 0) {
128 size_t bounds = strLine.rfind("bounds", appIndex);
129 if (bounds > 0) {
130 std::string boundStr = strLine.substr(bounds, 30);
131 std::smatch result;
132 std::string::const_iterator iterStart = boundStr.begin();
133 std::string::const_iterator iterEnd = boundStr.end();
134 std::vector<std::string> pointVector;
135 while (std::regex_search(iterStart, iterEnd, result, pattern)) {
136 std::string startX = result[0];
137 iterStart = result[0].second;
138 pointVector.push_back(startX);
139 }
140 size_t num = 3;
141 size_t pointNum = pointVector.size();
142 if (pointNum > num) {
143 int x = (std::atoi(pointVector[2].c_str()) + std::atoi(pointVector[0].c_str())) / 2;
144 int y = (std::atoi(pointVector[3].c_str()) + std::atoi(pointVector[1].c_str())) / 2;
145 pointXY = std::to_string(x) + " " + std::to_string(y);
146 } else {
147 size_t leftStart = boundStr.find_first_of("[");
148 size_t leftEnd = boundStr.find_first_of("]");
149 pointXY = boundStr.substr(leftStart + 1, leftEnd - leftStart - 1);
150 pointXY = pointXY.replace(pointXY.find(","), 1, " ");
151 }
152 break;
153 }
154 } else {
155 break;
156 }
157 }
158 }
159 }
160 }
161