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 #include "include/sp_log.h"
21 #include "include/sp_utils.h"
22
23 namespace OHOS {
24 namespace SmartPerf {
ParseSlideFpsTraceNoh(std::string file)25 double ParseSlideFpsTrace::ParseSlideFpsTraceNoh(std::string file)
26 {
27 double fps = -1.0;
28 char realPath[PATH_MAX] = {0x00};
29 if ((realpath(file.c_str(), realPath) == nullptr)) {
30 std::cout << "" << std::endl;
31 }
32 infile.open(realPath);
33 if (infile.fail()) {
34 LOGE("ParseSlideFpsTrace open file(%s) fialed ", file.c_str());
35 return fps;
36 }
37 fps = SmartPerf::ParseSlideFpsTrace::CalculateTime();
38 infile.close();
39 return fps;
40 }
41
CalculateTime()42 double ParseSlideFpsTrace::CalculateTime()
43 {
44 std::string line;
45 int two = 2;
46 while (getline(infile, line)) {
47 if (line.find("H:touchEventDispatch") != std::string::npos) {
48 count++;
49 if (count == four) {
50 needTime = true;
51 frameNow = 0;
52 touchTime = SPUtilesTye::StringToSometype<double>(ParseSlideFpsTrace::GetLineTime(line));
53 LOGD("ParseSlideFpsTrace::touchTime: (%s)", std::to_string(touchTime).c_str());
54 swiperFlingFlag = 0;
55 }
56 } else if (line.find("H:RSMainThread::DoComposition") != std::string::npos) {
57 frameNow++;
58 doCompositionTime = SPUtilesTye::StringToSometype<double>(ParseSlideFpsTrace::GetLineTime(line));
59 LOGD("ParseSlideFpsTrace::doCompositionTime: (%s)", std::to_string(doCompositionTime).c_str());
60 } else if (line.find("H:WEB_LIST_FLING") != std::string::npos ||
61 line.find("H:APP_LIST_FLING,") != std::string::npos) {
62 listFlag++;
63 if (listFlag == two) {
64 completeTime = doCompositionTime;
65 frameNum = frameNow;
66 LOGD("ParseSlideFpsTrace::completeTime: (%s), ParseSlideFpsTrace::frameNum: (%d)",
67 std::to_string(completeTime).c_str(), frameNum);
68 break;
69 }
70 }
71 AppSwiperScroll(line);
72 }
73 if (completeTime == 0 || responseTime == 0) {
74 return -1;
75 } else {
76 double fps = 0;
77 if ((completeTime - responseTime) > 0) {
78 fps = (frameNum - 1) / (completeTime - responseTime);
79 } else {
80 fps = 0;
81 }
82 double flagNum = 120;
83 double flagNumb = 121;
84 if (fps > flagNum && fps < flagNumb) {
85 fps = flagNum;
86 }
87 return fps;
88 }
89 return -1.0;
90 }
91
AppSwiperScroll(std::string line)92 void ParseSlideFpsTrace::AppSwiperScroll(std::string line)
93 {
94 if (line.find("H:APP_SWIPER_SCROLL,") != std::string::npos) {
95 if (swiperScrollFlag == 0) {
96 touchTime = SPUtilesTye::StringToSometype<double>(ParseSlideFpsTrace::GetLineTime(line));
97 LOGD("AppSwiperScroll.touchTime: (%s)", std::to_string(touchTime).c_str());
98 needTime = true;
99 swiperScrollFlag = 1;
100 }
101 }
102 if (line.find("H:APP_SWIPER_FLING,") != std::string::npos) {
103 if (swiperFlingFlag == 1) {
104 completeTime = doCompositionTime;
105 frameNum = frameNow;
106 LOGD("AppSwiperScroll.completeTime: (%s), AppSwiperScroll.frameNum: (%d)",
107 std::to_string(completeTime).c_str(), frameNum);
108 }
109 swiperFlingFlag++;
110 }
111 if (touchTime != 0 && (doCompositionTime - touchTime) > completionTime && needTime) {
112 frameNow = 1;
113 needTime = false;
114 responseTime = doCompositionTime;
115 LOGD("AppSwiperScroll.responseTime: (%s)", std::to_string(responseTime).c_str());
116 }
117 }
118
GetLineTime(std::string lineStr) const119 std::string ParseSlideFpsTrace::GetLineTime(std::string lineStr) const
120 {
121 size_t num = 7;
122 size_t position1 = lineStr.find("....");
123 size_t position2 = lineStr.find(":");
124 return lineStr.substr(position1 + num, position2 - position1 - num);
125 }
CutString(std::string lineStr,const std::string & start,const std::string & end,size_t offset) const126 std::string ParseSlideFpsTrace::CutString(std::string lineStr, const std::string &start,
127 const std::string &end, size_t offset) const
128 {
129 size_t position1 = lineStr.find(start);
130 size_t position2 = lineStr.find(end);
131 return lineStr.substr(position1 + offset, position2 - position1 - offset);
132 }
133 }
134 }
135