• 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 <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 namespace OHOS {
26     namespace SmartPerf {
ParseCompleteTrace(std::string fileNamePath)27         float ParseClickCompleteTrace::ParseCompleteTrace(std::string fileNamePath)
28         {
29             int conversion = 1000;
30             infile.open(fileNamePath);
31             if (infile.fail()) {
32                 std::cout << "File " << "open fail" << std::endl;
33                 return 0;
34             } else {
35                 completeTime = SmartPerf::ParseClickCompleteTrace::GetLineTime();
36             }
37             infile.close();
38             return completeTime * conversion;
39         }
GetLineTime()40         float ParseClickCompleteTrace::GetLineTime()
41         {
42             std::string line;
43             std::string endTime = "0";
44             std::string::size_type doComposition;
45             while (getline(infile, line)) {
46                 appPid = SmartPerf::ParseClickCompleteTrace::GetPid(line, "pid", appPid);
47                 startTime = SmartPerf::ParseClickCompleteTrace::GetStartTime(line, startTime);
48                 doComposition = line.find("H:RSMainThread::DoComposition");
49                 if (doComposition != std::string::npos) {
50                     size_t subNum = 5;
51                     size_t position1 = line.find("....");
52                     size_t position2 = line.find(":");
53                     endTime = line.substr(position1 + subNum, position2 - position1 - subNum);
54                     int endNum = std::stof(endTime);
55                     int endFlagNum = std::stof(endTimeFlag);
56                     int startNum = std::stof(startTime);
57                     int timeNum = endNum - endFlagNum;
58                     float interval = 0.3;
59                     if (timeNum < interval) {
60                         endTimeFlag = endTime;
61                     } else {
62                         if (std::stof(endTimeFlag) == 0) {
63                             endTimeFlag = endTime;
64                         } else if (endFlagNum != 0 && startNum != 0 && timeNum > interval) {
65                             break;
66                         } else {
67                             endTimeFlag = endTime;
68                         }
69                     }
70                 }
71             }
72             completeTime = SmartPerf::ParseClickCompleteTrace::GetTime(endTime);
73             return completeTime;
74         }
GetTime(std::string endTime)75         float ParseClickCompleteTrace::GetTime(std::string endTime)
76         {
77             size_t point = endTime.find(".");
78             float subNum = 2;
79             endTime = endTime.substr(point - subNum);
80             startTime = startTime.substr(point - subNum);
81             if (std::stof(endTime) == 0 || std::stof(startTime) == 0) {
82             } else {
83                 float displayTime = 0.032;
84                 completeTime = std::stof(endTime) - std::stof(startTime) + displayTime;
85             }
86             return completeTime;
87         }
GetPid(std::string line,const std::string & pn,const std::string & pb)88         std::string  ParseClickCompleteTrace::GetPid(std::string line, const std::string &pn, const std::string &pb)
89         {
90             if (appPidnum == 0) {
91                 std::string::size_type positionPackgeName;
92                 size_t packageNameNumSize = 5;
93                 if (pn.length() < packageNameNumSize) {
94                     std::string::size_type positionAppspawn;
95                     positionPackgeName = line.find("task_newtask: pid=");
96                     positionAppspawn = line.find("comm=appspawn");
97                     if (positionPackgeName != std::string::npos && positionAppspawn != std::string::npos) {
98                         size_t position1 = line.find("pid=");
99                         size_t position2 = line.find(" comm=appspawn");
100                         size_t subNum = 4;
101                         appPid = line.substr(position1 + subNum, position2 - position1 - subNum);
102                         appPidnum++;
103                     } else {
104                         appPid = pb;
105                     }
106                 } else {
107                     positionPackgeName = line.find(pn);
108                     if (positionPackgeName != std::string::npos) {
109                         size_t p1 = line.find(pn);
110                         size_t p2 = line.find(" prio");
111                         appPid = line.substr(p1 + pn.length(), p2 - p1 - pn.length());
112                         appPidnum++;
113                     } else {
114                         appPid = pb;
115                     }
116                 }
117             }
118             return appPid;
119         }
GetStartTime(std::string line,const std::string & startTimeBefore)120         std::string  ParseClickCompleteTrace::GetStartTime(std::string line, const std::string &startTimeBefore)
121         {
122             std::string::size_type touchEventDisPos = line.find("H:touchEventDispatch");
123             std::string::size_type mTouchEventDisPos = line.find("H:TouchEventDispatch");
124             if (mTouchEventDisPos != std::string::npos || touchEventDisPos != std::string::npos) {
125                 size_t touchNum = 3;
126                 if (flagTouch <= touchNum) {
127                     size_t position1 = line.find("....");
128                     size_t position2 = line.find(":");
129                     size_t subNum = 5;
130                     startTime = line.substr(position1 + subNum, position2 - position1 - subNum);
131                     flagTime = "0";
132                     flagTouch++;
133                 } else {
134                     startTime = startTimeBefore;
135                 }
136             } else {
137                 startTime = startTimeBefore;
138             }
139             return startTime;
140         }
141     }
142 }