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