• 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 
17 #include <fstream>
18 #include <string>
19 #include "include/parse_page_fps_trace.h"
20 #include "include/sp_log.h"
21 namespace OHOS {
22 namespace SmartPerf {
PageFpsTrace(std::string file)23 double ParsePageFpsTrace::PageFpsTrace(std::string file)
24 {
25     double fps = -1.0;
26     char realPath[PATH_MAX] = {0x00};
27     if (realpath(file.c_str(), realPath) == nullptr) {
28         std::cout << "" << std::endl;
29     }
30     infile.open(realPath);
31     if (infile.fail()) {
32         std::cout << "file open fail:" << file << std::endl;
33         LOGE("ParsePageFpsTrace open file(%s) fialed ", file.c_str());
34         return fps;
35     } else {
36         fps = SmartPerf::ParsePageFpsTrace::CalculateTime();
37     }
38     return fps;
39 }
CalculateTime()40 double ParsePageFpsTrace::CalculateTime()
41 {
42     std::string line;
43     size_t offset = 2;
44     double minFrameInterval = 0.05;
45     while (getline(infile, line)) {
46         if (line.find("H:touchEventDispatch") != std::string::npos) {
47             startLine = line;
48             needUpdateResponseLine = true;
49             frameNum = 0;
50         } else if (line.find("H:RSMainThread::DoComposition") != std::string::npos) {
51             updateCount = true;
52             count = 0;
53             pid = SmartPerf::ParsePageFpsTrace::CutString(line, "B|", "|H:RSMainThread::DoComposition", offset);
54             frameNum++;
55             if (needUpdateResponseLine) {
56                 responseLine = line;
57                 needUpdateResponseLine = false;
58             }
59             frameStartTime = std::stod(SmartPerf::ParsePageFpsTrace::GetLineTime(line));
60         }
61         if (updateCount) {
62             if (line.find("B|" + pid + "|") != std::string::npos && line.find("-" + pid) != std::string::npos) {
63                 count++;
64             } else if (line.find("E|" + pid + "|") != std::string::npos && line.find("-" + pid) != std::string::npos) {
65                 count--;
66             }
67             if (count == 0) {
68                 completeLine = line;
69                 frameStartInterval = frameEndTime;
70                 frameEndTime = std::stod(SmartPerf::ParsePageFpsTrace::GetLineTime(completeLine));
71                 updateCount = false;
72             }
73         }
74         if (frameStartInterval != 0) {
75             double frameInterval = frameStartTime - frameStartInterval;
76             if (frameInterval > minFrameInterval) {
77                 std::cout << "NO." << frameNum << "fps Time: " << frameInterval << "s" << std::endl;
78             }
79         }
80     }
81     return CalculateTimeEnd();
82 }
83 
CalculateTimeEnd()84 double ParsePageFpsTrace::CalculateTimeEnd()
85 {
86     if (startLine.compare("") == 0) {
87         LOGW("can not find start point {H:touchEventDispatch}");
88     } else if (completeLine.compare("") == 0) {
89         LOGW("can not find response and complete point {H:RSMainThread::DoComposition}");
90     } else {
91         double responseTime = std::stod(SmartPerf::ParsePageFpsTrace::GetLineTime(responseLine));
92         double completeTime = std::stod(SmartPerf::ParsePageFpsTrace::GetLineTime(completeLine));
93         return frameNum / (completeTime - responseTime);
94     }
95     return -1.0;
96 }
97 
GetLineTime(std::string line)98 std::string ParsePageFpsTrace::GetLineTime(std::string line)
99 {
100     size_t num = 7;
101     size_t position1 = line.find("....");
102     size_t position2 = line.find(":");
103     return line.substr(position1 + num, position2 - position1 - num);
104 }
CutString(std::string line,const std::string & start,const std::string & end,size_t offset)105 std::string ParsePageFpsTrace::CutString(std::string line, const std::string &start,
106     const std::string &end, size_t offset)
107 {
108     size_t position1 = line.find(start);
109     size_t position2 = line.find(end);
110     return line.substr(position1 + offset, position2 - position1 - offset);
111 }
112 }
113 }