1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 <fstream>
16 #include <string>
17 #include <iostream>
18 #include <regex>
19 #include "include/parse_slide_fps_trace.h"
20
21 namespace OHOS {
22 namespace SmartPerf {
ParseSlideFpsTraceNoh(std::string file)23 double ParseSlideFpsTrace::ParseSlideFpsTraceNoh(std::string file)
24 {
25 double fps = -1.0;
26 infile.open(file);
27 if (infile.fail()) {
28 std::cout << "file open fail:" << file << std::endl;
29 return fps;
30 } else {
31 fps = SmartPerf::ParseSlideFpsTrace::CalculateTime();
32 }
33 return fps;
34 }
35
GetLastCompleteLine()36 void ParseSlideFpsTrace::GetLastCompleteLine()
37 {
38 if (lastCompleteLine != "") {
39 double completeNow = std::stod(SmartPerf::ParseSlideFpsTrace::GetLineTime(newCompleteLine));
40 double completeLast = std::stod(SmartPerf::ParseSlideFpsTrace::GetLineTime(lastCompleteLine));
41 float num = 0.1;
42 if (completeNow - completeLast > num) {
43 completeLine = lastCompleteLine;
44 updateCount = true;
45 frameNum = frameNow - 1;
46 }
47 }
48 }
49
CalculateTime()50 double ParseSlideFpsTrace::CalculateTime()
51 {
52 std::string line;
53 while (getline(infile, line)) {
54 if (line.find("H:RSJankStats::RecordAnimationDynamicFrameRate") != std::string::npos) {
55 std::string delimiter = "frame rate is ";
56 size_t pos1 = line.find(delimiter);
57 std::string result1 = line.substr(pos1 + delimiter.length());
58 std::string delimiter1 = ":";
59 size_t pos2 = line.find(delimiter1);
60 std::string result2 = result1.substr(0, pos2);
61 frameRate = std::stod(result2);
62 }
63 if (line.find("H:touchEventDispatch") != std::string::npos) {
64 count++;
65 if (count == four) {
66 needUpdateResponseLine = true;
67 frameNow = 0;
68 }
69 } else if (line.find("H:RSMainThread::DoComposition") != std::string::npos && !updateCount) {
70 frameNow++;
71 if (needUpdateResponseLine) {
72 responseLine = line;
73 needUpdateResponseLine = false;
74 }
75 newCompleteLine = line;
76 GetLastCompleteLine();
77 lastCompleteLine = newCompleteLine;
78 }
79 }
80 if (frameRate == 0) {
81 completeLine = newCompleteLine;
82 frameNum = frameNow;
83 double responseTime = std::stod(SmartPerf::ParseSlideFpsTrace::GetLineTime(responseLine));
84 double completeTime = std::stod(SmartPerf::ParseSlideFpsTrace::GetLineTime(completeLine));
85 return frameNum / (completeTime - responseTime);
86 } else {
87 return frameRate;
88 }
89 return -1.0;
90 }
GetLineTime(std::string lineStr) const91 std::string ParseSlideFpsTrace::GetLineTime(std::string lineStr) const
92 {
93 size_t num = 7;
94 size_t position1 = lineStr.find("....");
95 size_t position2 = lineStr.find(":");
96 return lineStr.substr(position1 + num, position2 - position1 - num);
97 }
CutString(std::string lineStr,std::string start,std::string end,size_t offset) const98 std::string ParseSlideFpsTrace::CutString(std::string lineStr, std::string start, std::string end, size_t offset) const
99 {
100 size_t position1 = lineStr.find(start);
101 size_t position2 = lineStr.find(end);
102 return lineStr.substr(position1 + offset, position2 - position1 - offset);
103 }
104 }
105 }
106