• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ios>
17 #include <vector>
18 #include <iostream>
19 #include <fstream>
20 #include <regex>
21 #include "include/startup_delay.h"
22 #include "include/sp_utils.h"
23 namespace OHOS {
24 namespace SmartPerf {
StartUpDelay()25 StartUpDelay::StartUpDelay() {}
~StartUpDelay()26 StartUpDelay::~StartUpDelay() {}
GetTrace(const std::string & sessionID,const std::string & traceName)27 void StartUpDelay::GetTrace(const std::string &sessionID, const std::string &traceName)
28 {
29     std::string result;
30     std::string cmdString{"bytrace -t 5 -b 20480 --overwrite idle ace app ohos ability graphic "};
31     std::string cmdStringEnd{"sched freq irq sync workq pagecache multimodalinput > "};
32     SPUtils::LoadCmd(cmdString + cmdStringEnd + traceName, result);
33 }
ThreadGetTrace(const std::string & sessionID,const std::string & traceName)34 std::thread StartUpDelay::ThreadGetTrace(const std::string &sessionID, const std::string &traceName)
35 {
36     std::thread thGetTrace(&StartUpDelay::GetTrace, this, sessionID, traceName);
37     return thGetTrace;
38 }
GetLayout()39 void StartUpDelay::GetLayout()
40 {
41     std::string result;
42     SPUtils::LoadCmd("uitest dumpLayout", result);
43 }
ThreadGetLayout()44 std::thread StartUpDelay::ThreadGetLayout()
45 {
46     std::thread thGetLayout(&StartUpDelay::GetLayout, this);
47     return thGetLayout;
48 }
ChangeToBackground()49 void StartUpDelay::ChangeToBackground()
50 {
51     std::string result;
52     SPUtils::LoadCmd("uinput -K -d 2 -u 2", result);
53 }
GetPidByPkg(const std::string & curPkgName)54 std::string StartUpDelay::GetPidByPkg(const std::string &curPkgName)
55 {
56     std::string resultPid;
57     SPUtils::LoadCmd("pidof " + curPkgName, resultPid);
58     return resultPid;
59 }
GetDeviceType()60 std::string StartUpDelay::GetDeviceType()
61 {
62     std::string cmdResult;
63     SPUtils::LoadCmd("param get |grep ohos.boot.hardware", cmdResult);
64     size_t splitFlag = cmdResult.find("= ");
65     std::string deviceType = cmdResult.substr(splitFlag + 1);
66     return deviceType;
67 }
InitXY2(const std::string & curAppName,const std::string & fileName,const std::string & appPkgName)68 void StartUpDelay::InitXY2(const std::string &curAppName, const std::string &fileName, const std::string &appPkgName)
69 {
70     std::ifstream file(fileName, std::ios::in);
71     std::string strLine = "";
72     std::regex pattern("\\d+");
73     while (getline(file, strLine)) {
74         size_t appIndex = strLine.find(curAppName);
75         size_t appPkgIndex = strLine.find(appPkgName);
76         if (appIndex > 0 && appPkgIndex < appIndex) {
77             size_t bounds = strLine.rfind("bounds", appIndex);
78             if (bounds > 0) {
79                 std::string boundStr = strLine.substr(bounds, 30);
80                 std::smatch result;
81                 std::string::const_iterator iterStart = boundStr.begin();
82                 std::string::const_iterator iterEnd = boundStr.end();
83                 std::vector<std::string> pointVector;
84                 while (std::regex_search(iterStart, iterEnd, result, pattern)) {
85                     std::string startX = result[0];
86                     iterStart = result[0].second;
87                     pointVector.push_back(startX);
88                 }
89                 size_t num = 3;
90                 size_t pointNum = pointVector.size();
91                 if (pointNum > num) {
92                     int x = (std::atoi(pointVector[2].c_str()) + std::atoi(pointVector[0].c_str())) / 2;
93                     int y = (std::atoi(pointVector[3].c_str()) + std::atoi(pointVector[1].c_str())) / 2;
94                     pointXY = std::to_string(x) + " " + std::to_string(y);
95                 } else {
96                     size_t leftStart = boundStr.find_first_of("[");
97                     size_t leftEnd = boundStr.find_first_of("]");
98                     pointXY = boundStr.substr(leftStart + 1, leftEnd - leftStart - 1);
99                     pointXY = pointXY.replace(pointXY.find(","), 1, " ");
100                 }
101                 break;
102             }
103         } else {
104             break;
105         }
106     }
107 }
108 }
109 }
110