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 = std::stof(endTime);
59 int endFlagNum = std::stof(endTimeFlag);
60 int startNum = std::stof(startTime);
61 int timeNum = endNum - endFlagNum;
62 float interval = 0.3;
63 if (timeNum < interval) {
64 endTimeFlag = endTime;
65 continue;
66 }
67 if (std::stof(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 (std::stof(endTime) == 0 || std::stof(startTime) == 0) {
86 } else {
87 double displayTime = 0.032;
88 completeTime = std::stof(endTime) - std::stof(startTime) + displayTime;
89 }
90 return completeTime;
91 }
GetPid(std::string line,const std::string & pn,const std::string & pb)92 std::string ParseClickCompleteTrace::GetPid(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(std::string line,const std::string & startTimeBefore)124 std::string ParseClickCompleteTrace::GetStartTime(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 }