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 <iostream>
17 #include <fstream>
18 #include <string>
19 #include <vector>
20 #include <cstdio>
21 #include <sstream>
22 #include <iomanip>
23 #include "include/parse_click_complete_trace.h"
24 #include "include/sp_utils.h"
25 #include "include/sp_log.h"
26 namespace OHOS {
27 namespace SmartPerf {
ParseCompleteTrace(std::string fileNamePath)28 double ParseClickCompleteTrace::ParseCompleteTrace(std::string fileNamePath)
29 {
30 int conversion = 1000;
31 char realPath[PATH_MAX] = {0x00};
32 if ((realpath(fileNamePath.c_str(), realPath) == nullptr)) {
33 std::cout << "" << std::endl;
34 }
35 infile.open(realPath);
36 if (infile.fail()) {
37 LOGE("ParseClickCompleteTrace open file(%s) fialed ", fileNamePath.c_str());
38 return 0;
39 }
40 completeTime = SmartPerf::ParseClickCompleteTrace::GetLineTime();
41 infile.close();
42 return completeTime * conversion;
43 }
GetLineTime()44 double ParseClickCompleteTrace::GetLineTime()
45 {
46 std::string line;
47 std::string endTime = "0";
48 std::string::size_type doComposition;
49 while (getline(infile, line)) {
50 appPid = SmartPerf::ParseClickCompleteTrace::GetPid(line, "pid", appPid);
51 startTime = SmartPerf::ParseClickCompleteTrace::GetStartTime(line, startTime);
52 doComposition = line.find("H:RSMainThread::DoComposition");
53 if (doComposition != std::string::npos) {
54 size_t subNum = 5;
55 size_t position1 = line.find("....");
56 size_t position2 = line.find(":");
57 endTime = line.substr(position1 + subNum, position2 - position1 - subNum);
58 int endNum = SPUtilesTye::StringToSometype<float>(endTime);
59 int endFlagNum = SPUtilesTye::StringToSometype<float>(endTimeFlag);
60 int startNum = SPUtilesTye::StringToSometype<float>(startTime);
61 int timeNum = endNum - endFlagNum;
62 float interval = 0.3;
63 if (timeNum < interval) {
64 endTimeFlag = endTime;
65 continue;
66 }
67 if (SPUtilesTye::StringToSometype<float>(endTimeFlag) == 0) {
68 endTimeFlag = endTime;
69 } else if (endFlagNum != 0 && startNum != 0 && timeNum > interval) {
70 break;
71 } else {
72 endTimeFlag = endTime;
73 }
74 }
75 }
76 completeTime = SmartPerf::ParseClickCompleteTrace::GetTime(endTime);
77 return completeTime;
78 }
GetTime(std::string endTime)79 double ParseClickCompleteTrace::GetTime(std::string endTime)
80 {
81 size_t point = endTime.find(".");
82 float subNum = 2;
83 endTime = endTime.substr(point - subNum);
84 startTime = startTime.substr(point - subNum);
85 if (SPUtilesTye::StringToSometype<float>(endTime) == 0 || SPUtilesTye::StringToSometype<float>(startTime) == 0) {
86 } else {
87 double displayTime = 0.032;
88 completeTime = SPUtilesTye::StringToSometype<float>(endTime) -
89 SPUtilesTye::StringToSometype<float>(startTime) + displayTime;
90 }
91 return completeTime;
92 }
GetPid(std::string line,const std::string & pn,const std::string & pb)93 std::string ParseClickCompleteTrace::GetPid(std::string line, const std::string &pn, const std::string &pb)
94 {
95 if (appPidnum == 0) {
96 std::string::size_type positionPackgeName;
97 size_t packageNameNumSize = 5;
98 if (pn.length() < packageNameNumSize) {
99 std::string::size_type positionAppspawn;
100 positionPackgeName = line.find("task_newtask: pid=");
101 positionAppspawn = line.find("comm=appspawn");
102 if (positionPackgeName != std::string::npos && positionAppspawn != std::string::npos) {
103 size_t position1 = line.find("pid=");
104 size_t position2 = line.find(" comm=appspawn");
105 size_t subNum = 4;
106 appPid = line.substr(position1 + subNum, position2 - position1 - subNum);
107 appPidnum++;
108 } else {
109 appPid = pb;
110 }
111 } else {
112 positionPackgeName = line.find(pn);
113 if (positionPackgeName != std::string::npos) {
114 size_t p1 = line.find(pn);
115 size_t p2 = line.find(" prio");
116 appPid = line.substr(p1 + pn.length(), p2 - p1 - pn.length());
117 appPidnum++;
118 } else {
119 appPid = pb;
120 }
121 }
122 }
123 return appPid;
124 }
GetStartTime(std::string line,const std::string & startTimeBefore)125 std::string ParseClickCompleteTrace::GetStartTime(std::string line, const std::string &startTimeBefore)
126 {
127 std::string::size_type te = line.find("H:touchEventDispatch");
128 std::string::size_type td = line.find("H:TouchEventDispatch");
129 std::string::size_type pd = line.find("H:PointerEventDispatch");
130 std::string::size_type kd = line.find("H:KeyEventDispatch");
131 std::string::size_type nop = std::string::npos;
132 if (te != nop || td != nop || pd != nop || kd != nop) {
133 size_t touchNum = 3;
134 if (flagTouch <= touchNum) {
135 size_t position1 = line.find("....");
136 size_t position2 = line.find(":");
137 size_t subNum = 5;
138 startTime = line.substr(position1 + subNum, position2 - position1 - subNum);
139 flagTime = "0";
140 flagTouch++;
141 } else {
142 startTime = startTimeBefore;
143 }
144 } else {
145 startTime = startTimeBefore;
146 }
147 return startTime;
148 }
149 }
150 }