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