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