• 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 #include "include/sp_parse_fps.h"
ParseFPS()16 ParseFPS::ParseFPS()
17 {
18     pattern = std::regex("(\\d+).(\\d{6})");
19     pidPattern = std::regex("\\|(\\d+)\\|");
20 }
~ParseFPS()21 ParseFPS::~ParseFPS()
22 {
23 }
StrSplit(const SpString & content,const SpString & sp,std::vector<SpString> & out)24 void ParseFPS::StrSplit(const SpString &content, const SpString &sp, std::vector<SpString> &out)
25 {
26     size_t index = 0;
27     while (index != SpString::npos) {
28         size_t tEnd = content.find_first_of(sp, index);
29         SpString tmp = content.substr(index, tEnd - index);
30         if (tmp != "" && tmp != " ") {
31             out.push_back(tmp);
32         }
33         if (tEnd == SpString::npos) {
34             break;
35         }
36         index = tEnd + 1;
37     }
38 }
GetAndSetPageType(Line & line,PageType & pageType)39 void ParseFPS::GetAndSetPageType(Line& line, PageType& pageType)
40 {
41     if (line.empty()) {
42         return;
43     }
44     if (line.find(ROSENRENDERWEB) != SpString::npos) {
45         pageType = WEB;
46     } else if (line.find(ROSENRENDERTEXTURE) != SpString::npos) {
47         pageType = VIDEO;
48     } else {
49         pageType = LARGE;
50     }
51 }
GetTouchEventNum(Line & line,TouchEvent & touchEvent)52 unsigned int ParseFPS::GetTouchEventNum(Line& line, TouchEvent& touchEvent)
53 {
54     if (line.empty()) {
55         return 0;
56     }
57     if (line.find(TOUCHEVENT_FLAG) != SpString::npos || line.find(HTOUCHEVENT_FLAG) != SpString::npos) {
58         ++touchEvent.tEventDisNum;
59     }
60     return touchEvent.tEventDisNum;
61 }
ParseBranch(FilePath & filePath,PackageName & pN,PageType & pT,TouchEvent & tE)62 const FpsResult ParseFPS::ParseBranch(FilePath& filePath, PackageName& pN, PageType& pT, TouchEvent& tE)
63 {
64     FpsResult fps = "0";
65     if (tE.touchFlag) {
66         std::vector<SpString> vecPackNames;
67         //Get the time period in the renderservice
68         float staticTime = 2.0f;
69         this->StrSplit(pN, ".", vecPackNames);
70         SpString uiPoint = uniProcess + vecPackNames.back();
71         switch (pT) {
72             case PageType::VIDEO:{
73                 if (filePath.find(FLING) != SpString::npos) {
74                     staticTime = 0.5f;
75                     fps =  PraseFPSTrace(filePath, staticTime, uiPoint);
76                 } else {
77                     fps =  PraseFPSTrace(filePath, staticTime, videoPoint);
78                 }
79                 break;
80             }
81             case PageType::WEB:{
82                 fps =  PraseFPSTrace(filePath, staticTime, webPoint);
83                 break;
84             }
85             default:{
86                 fps =  PraseFPSTrace(filePath, staticTime, uiPoint);
87                 break;
88             }
89         }
90     }
91     return fps;
92 }
ParseTraceFile(FilePath & filePath,PackageName & packageName)93 FpsResult  ParseFPS::ParseTraceFile(FilePath& filePath, PackageName& packageName)
94 {
95     if (filePath.empty() || packageName.empty()) {
96         return PARAMS_EMPTY;
97     }
98     FpsResult fps;
99     FileSteamPtr inFile(new std::ifstream());
100     inFile->open(filePath);
101     if (inFile->fail()) {
102         std::cout<<"File: "<<filePath<<" open failed!"<<std::endl;
103         return FILE_OPEN_FAILED;
104     } else {
105         while (std::getline(*inFile, lineClient)) {
106             if (this->GetTouchEventNum(lineClient, touchEventClient) > 0) {
107                 touchEventClient.touchFlag = true;
108             }
109             this->GetAndSetPageType(lineClient, pageTypeClient);
110         }
111         fps = this->ParseBranch(filePath, packageName, pageTypeClient, touchEventClient);
112     }
113     return "FPS:"+fps+"fps";
114 }
StaticHandoffStartTime(Line & line,RecordFpsVars & rfv)115 void ParseFPS::StaticHandoffStartTime(Line& line, RecordFpsVars& rfv)
116 {
117     if (line.empty()) {
118         return;
119     }
120     if (line.find(TOUCHEVENT_FLAG) != SpString::npos || line.find(HTOUCHEVENT_FLAG) != SpString::npos) {
121         ++rfV.tEventDisNum;
122         std::smatch result;
123         unsigned int tNum = 4;
124         if (tNum == rfV.tEventDisNum) {
125             if (std::regex_search(line, result, pattern)) {
126                 rfV.leaveStartTime = result[0];
127             }
128         }
129         if (rfV.tEventDisNum == touchEventClient.tEventDisNum) {
130             if (std::regex_search(line, result, pattern)) {
131                 rfV.isStaticsLeaveTime = true;
132             }
133         }
134     }
135 }
DecHandOffTime(Line & line,RecordFpsVars & rfv)136 void ParseFPS::DecHandOffTime(Line& line, RecordFpsVars& rfv)
137 {
138     if (line.empty()) {
139         return;
140     }
141     if (rfV.isStaticsLeaveTime) {
142         if (line.find(doPoint) != SpString::npos) {
143             std::smatch result;
144             if (std::regex_search(line, result, pattern)) {
145                 if (rfV.startFlag == 0) {
146                     rfV.leaveStartTime = rfV.leaveEndTime = result[0];
147                 }
148                 ++rfV.startFlag;
149             }
150             if (rfV.pidMatchStr.empty()) {
151                 if (std::regex_search(line, result, pidPattern)) {
152                     rfV.pidMatchStr = result[0];
153                 }
154             }
155             rfV.isAddFrame = true;
156         }
157     }
158 }
CountRsEndTime(Line & line,RecordFpsVars & rfv,float staticTime,SpString uiPoint)159 bool ParseFPS::CountRsEndTime(Line& line, RecordFpsVars& rfv, float staticTime, SpString uiPoint)
160 {
161     if (line.empty()) {
162         return false;
163     }
164     if (!rfV.pidMatchStr.empty() && rfV.isAddFrame) {
165         SpString pid = rfV.pidMatchStr.substr(1, rfV.pidMatchStr.length() - 2);
166         if (line.find(uiPoint) != SpString::npos) {
167             rfV.isHasUI = true;
168         }
169         if (line.find("B|" + pid + "|") != SpString::npos && line.find("-" + pid) != SpString::npos) {
170             beQueue.push(line);
171         }
172         if (line.find("E|" + pid + "|") != SpString::npos && line.find("-" + pid) != SpString::npos) {
173             beQueue.pop();
174         }
175         if (beQueue.empty()) {
176             rfV.isAddFrame = false;
177             if (rfV.isHasUI) {
178                 rfV.isHasUI = false;
179                 if (std::stof(rfV.leaveEndTime) - std::stof(rfV.leaveStartTime) < staticTime) {
180                     std::smatch result;
181                     if (std::regex_search(line, result, pattern)) {
182                         float intervalTime = 0.1;
183                         float intervalResult = std::stof(result[0]) - std::stof(rfV.leaveEndTime);
184                         if (intervalResult < intervalTime) {
185                             ++rfV.frameNum;
186                             rfV.leaveEndTime = result[0];
187                         } else {
188                             ++rfV.frameNum;
189                             std::cout<<"NO."<<rfV.frameNum<<"fps Time: "<< intervalResult << "s" <<std::endl;
190                             rfV.leaveEndTime = result[0];
191                         }
192                     }
193                 } else {
194                     return true;
195                 }
196             }
197         }
198     }
199     return false;
200 }
PraseFPSTrace(FilePath & filePath,float staticTime,SpString uiPoint)201 FpsResult  ParseFPS::PraseFPSTrace(FilePath& filePath, float staticTime, SpString uiPoint)
202 {
203     if (!this->lineClient.empty()) {
204         this->lineClient.clear();
205     }
206     FileSteamPtr inFile(new std::ifstream());
207     inFile->open(filePath);
208     if (inFile->fail()) {
209         std::cout<<"File: "<<filePath<<" open failed!"<<std::endl;
210         return FILE_OPEN_FAILED;
211     } else {
212         // std::cout<<"File: "<<filePath<<" open success!"<<std::endl;
213         while (std::getline(*inFile, this->lineClient)) {
214             this->StaticHandoffStartTime(lineClient, rfV);
215             this->DecHandOffTime(lineClient, rfV);
216             if (this->CountRsEndTime(lineClient, rfV, staticTime, uiPoint)) {
217                 break;
218             }
219         }
220         const auto duration = std::stof(rfV.leaveEndTime) - std::stof(rfV.leaveStartTime);
221         const auto complexFps1 =  rfV.frameNum / duration;
222         if (complexFps1 == -0 || (duration == 0 && rfV.frameNum == 0)) {
223             printf("failed Operation\n");
224             rfV.complexFps = "-1";
225         } else {
226             rfV.complexFps = std::to_string(complexFps1);
227         }
228     }
229     return rfV.complexFps;
230 }