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