• 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 
16 #include <thread>
17 #include <iostream>
18 #include <fstream>
19 #include <string>
20 #include <vector>
21 #include <cstdio>
22 #include <sstream>
23 #include <iomanip>
24 #include "include/parse_start_frame_trace.h"
25 
26 namespace OHOS {
27     namespace SmartPerf {
ParseStartFrameTraceNoh(std::string file)28         double StartFrameTraceNoh::ParseStartFrameTraceNoh(std::string file)
29         {
30             double frameRate = -1.0;
31             infile.open(file);
32             if (infile.fail()) {
33                 std::cout << "file open fail:" << file << std::endl;
34                 return frameRate;
35             } else {
36                 frameRate = SmartPerf::StartFrameTraceNoh::CalculateTime();
37             }
38             return frameRate;
39         }
CalculateTime()40         double StartFrameTraceNoh::CalculateTime()
41         {
42             std::string line;
43             while (getline(infile, line)) {
44                 if (line.find("H:touchEventDispatch") != std::string::npos) {
45                     startLine = line;
46                     frameNum = 0;
47                 } else if (line.find("H:RSUniRender::Process:[leashWindow") != std::string::npos) {
48                     std::string tmpStr = Split(line, "H:RSUniRender::Process:[leashWindow")[1];
49                     std::string numsStr = CutString(tmpStr, "(", ")", 1);
50                     std::vector<std::string> nums = Split(numsStr, ",");
51                     std::string alpha = Split(tmpStr, "Alpha:")[1];
52                     bool full = stof(nums[0]) == 0 && stof(nums[1]) == 0 && stof(nums[2]) == 1344 && stof(nums[3]) == 2772 ? true : false;
53                     if (full && stof(alpha) == 1) {
54                         fullScreenLine = line;
55                         break;
56                     }
57                 } else if (line.find("H:RSMainThread::DoComposition") != std::string::npos) {
58                     isNeedEnd = true;
59                     frameNum++;
60                     count = 0;
61                     int size = 2;
62                     pid = CutString(line, "B|", "|H:RSMainThread::DoComposition", size);
63                     frameStartTime = std::stod(GetLineTime(line));
64                     startTimes.frameId = frameNum;
65                     startTimes.times = frameStartTime;
66                     startTimeArr.push_back(endTimes);
67                 }
68                 if (isNeedEnd) {
69                     if (line.find("B|" + pid + "|") != std::string::npos && line.find("-" + pid) != std::string::npos) {
70                             count++;
71                     } else if (line.find("E|" + pid + "|") != maxnpos && line.find("-" + pid) != maxnpos) {
72                         count--;
73                     }
74                     if (count == 0) {
75                         frameEndTime = std::stod(GetLineTime(line));
76                         endTimes.frameId = frameNum;
77                         endTimes.times = frameEndTime;
78                         endTimeArr.push_back(endTimes);
79                         isNeedEnd = false;
80                     }
81                 }
82             }
83             double fps = GetFps();
84             return fps;
85         }
GetFps()86         double StartFrameTraceNoh::GetFps()
87         {
88             if (startLine.compare("") == 0) {
89                 std::cout << "can not find start point {H:touchEventDispatch}" << std::endl;
90             } else if (fullScreenLine.compare("") == 0) {
91                 std::cout << "can not find complete point {H:RSMainThread::leashWindow}" << std::endl;
92             } else {
93                 double startTime = std::stod(SmartPerf::StartFrameTraceNoh::GetLineTime(startLine));
94                 double endTime = std::stod(SmartPerf::StartFrameTraceNoh::GetLineTime(fullScreenLine));
95                 for (int i = 0; i < startTimeArr.size() - 1; ++i) {
96                     double result = 0;
97                     if (startTimeArr[i].times >= startTime && endTimeArr[i].times > startTimeArr[i].times) {
98                         int next = i + 1;
99                         result = startTimeArr[next].times - endTimeArr[i].times;
100                     }
101                     double mark = 0.05;
102                     if (result > mark) {
103                         std::cout<<"NO."<<startTimeArr[i].frameId<<"fps Time: "<< result << "s" <<std::endl;
104                     }
105                 }
106                 return frameNum / (endTime - startTime);
107             }
108             return -1.0;
109         }
GetLineTime(std::string line)110         std::string StartFrameTraceNoh::GetLineTime(std::string line)
111         {
112             size_t num = 7;
113             size_t position1 = line.find("....");
114             size_t position2 = line.find(":");
115             return line.substr(position1 + num, position2 - position1 - num);
116         }
CutString(std::string line,std::string start,std::string end,size_t offset)117         std::string StartFrameTraceNoh::CutString(std::string line, std::string start, std::string end, size_t offset)
118         {
119             size_t position1 = line.find(start);
120             size_t position2 = line.find(end);
121             return line.substr(position1 + offset, position2 - position1 - offset);
122         }
Split(std::string str,std::string pattern)123         std::vector<std::string> StartFrameTraceNoh::Split(std::string str, std::string pattern)
124         {
125             std::string::size_type pos;
126             std::vector<std::string> result;
127             str += pattern;
128             size_t size = str.size();
129             for (size_t i = 0; i < size; i++) {
130                 pos = str.find(pattern, i);
131                 if (pos < size) {
132                     std::string s = str.substr(i, pos - i);
133                     result.push_back(s);
134                     i = pos + pattern.size() - 1;
135                 }
136             }
137             return result;
138         }
139     }
140 }