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 -K -d 2 -u 2", 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 return resultPid;
63 }
GetDeviceType()64 std::string StartUpDelay::GetDeviceType()
65 {
66 std::string cmdResult;
67 SPUtils::LoadCmd("param get |grep ohos.boot.hardware", cmdResult);
68 size_t splitFlag = cmdResult.find("= ");
69 std::string deviceType = cmdResult.substr(splitFlag + 1);
70 return deviceType;
71 }
InitXY2(const std::string & curAppName,const std::string & fileName,const std::string & appPkgName)72 void StartUpDelay::InitXY2(const std::string &curAppName, const std::string &fileName, const std::string &appPkgName)
73 {
74 std::ifstream file(fileName, std::ios::in);
75 std::string strLine = "";
76 std::regex pattern("\\d+");
77 size_t findIndex = std::string::npos;
78 while (getline(file, strLine)) {
79 size_t appPkgIndex = strLine.find("AppName_text_" + appPkgName);
80 size_t appIndex = strLine.find(curAppName);
81 if (appIndex != std::string::npos) {
82 findIndex = appIndex;
83 } else {
84 findIndex = appPkgIndex;
85 }
86 if (findIndex != std::string::npos) {
87 size_t bounds = strLine.rfind("bounds", findIndex);
88 if (bounds > 0) {
89 std::string boundStr = strLine.substr(bounds, 30);
90 std::smatch result;
91 std::string::const_iterator iterStart = boundStr.begin();
92 std::string::const_iterator iterEnd = boundStr.end();
93 std::vector<std::string> pointVector;
94 while (std::regex_search(iterStart, iterEnd, result, pattern)) {
95 std::string startX = result[0];
96 iterStart = result[0].second;
97 pointVector.push_back(startX);
98 }
99 size_t num = 3;
100 size_t pointNum = pointVector.size();
101 if (pointNum > num) {
102 int x = (std::atoi(pointVector[2].c_str()) + std::atoi(pointVector[0].c_str())) / 2;
103 int y = (std::atoi(pointVector[3].c_str()) + std::atoi(pointVector[1].c_str())) / 2;
104 pointXY = std::to_string(x) + " " + std::to_string(y);
105 } else {
106 size_t leftStart = boundStr.find_first_of("[");
107 size_t leftEnd = boundStr.find_first_of("]");
108 pointXY = boundStr.substr(leftStart + 1, leftEnd - leftStart - 1);
109 pointXY = pointXY.replace(pointXY.find(","), 1, " ");
110 }
111 break;
112 }
113 } else {
114 break;
115 }
116 }
117 }
InitXY(const std::string & curAppName,const std::string & fileName)118 void StartUpDelay::InitXY(const std::string &curAppName, const std::string &fileName)
119 {
120 std::ifstream file(fileName, std::ios::in);
121 std::string strLine = "";
122 std::regex pattern("\\d+");
123 while (getline(file, strLine)) {
124 size_t appIndex = strLine.find(curAppName);
125 if (appIndex > 0) {
126 size_t bounds = strLine.rfind("bounds", appIndex);
127 if (bounds > 0) {
128 std::string boundStr = strLine.substr(bounds, 30);
129 std::smatch result;
130 std::string::const_iterator iterStart = boundStr.begin();
131 std::string::const_iterator iterEnd = boundStr.end();
132 std::vector<std::string> pointVector;
133 while (std::regex_search(iterStart, iterEnd, result, pattern)) {
134 std::string startX = result[0];
135 iterStart = result[0].second;
136 pointVector.push_back(startX);
137 }
138 size_t num = 3;
139 size_t pointNum = pointVector.size();
140 if (pointNum > num) {
141 int x = (std::atoi(pointVector[2].c_str()) + std::atoi(pointVector[0].c_str())) / 2;
142 int y = (std::atoi(pointVector[3].c_str()) + std::atoi(pointVector[1].c_str())) / 2;
143 pointXY = std::to_string(x) + " " + std::to_string(y);
144 } else {
145 size_t leftStart = boundStr.find_first_of("[");
146 size_t leftEnd = boundStr.find_first_of("]");
147 pointXY = boundStr.substr(leftStart + 1, leftEnd - leftStart - 1);
148 pointXY = pointXY.replace(pointXY.find(","), 1, " ");
149 }
150 break;
151 }
152 } else {
153 break;
154 }
155 }
156 }
157 }
158 }
159