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 <thread> 16 #include <iostream> 17 #include <fstream> 18 #include <string> 19 #include <vector> 20 #include <cstdio> 21 #include <sstream> 22 #include <iomanip> 23 #include "include/parse_trace.h" 24 #include "include/sp_utils.h" 25 namespace OHOS { 26 namespace SmartPerf { ParseTraceNoah(const std::string & fileNamePath,const std::string & appPid)27 float ParseTrace::ParseTraceNoah(const std::string &fileNamePath, const std::string &appPid) 28 { 29 int conversion = 1000; 30 float code = -1; 31 infile.open(fileNamePath); 32 if (infile.fail()) { 33 std::cout << "File " << "open fail" << std::endl; 34 return 0; 35 } else { 36 code = SmartPerf::ParseTrace::ParseNoahTrace(fileNamePath, appPid); 37 } 38 infile.close(); 39 return code * conversion; 40 } ParseNoahTrace(const std::string & fileNamePath,const std::string & appPid)41 float ParseTrace::ParseNoahTrace(const std::string &fileNamePath, const std::string &appPid) 42 { 43 std::string line; 44 std::string::size_type positionPid; 45 float codeTime = -1; 46 while (getline(infile, line)) { 47 startTime = SmartPerf::ParseTrace::GetStartTime(line, startTime); 48 windowTime = SmartPerf::ParseTrace::GetWindowTime(line, windowTime); 49 frameId = SmartPerf::ParseTrace::GetFrameId(line, appPid, frameId); 50 positionPid = line.find("[" + appPid + ", " + frameId + "]"); 51 if (positionPid != std::string::npos) { 52 size_t position1 = line.find("...."); 53 size_t position2 = line.find(":"); 54 size_t flagTimeSize = 5; 55 flagTime = line.substr(position1 + flagTimeSize, position2 - position1 - flagTimeSize); 56 if (std::stof(endTime) == 0) { 57 endTime = flagTime; 58 } else { 59 float dataThreshold = 0.3; 60 if ((std::stof(flagTime) - std::stof(endTime)) > dataThreshold) { 61 break; 62 } else { 63 endTime = flagTime; 64 } 65 } 66 } 67 } 68 codeTime = SmartPerf::ParseTrace::GetTimeNoah(startTime, endTime, windowTime); 69 return codeTime; 70 } GetFrameId(std::string line,std::string appPid,std::string fid)71 std::string ParseTrace::GetFrameId(std::string line, std::string appPid, std::string fid) 72 { 73 std::string::size_type positionTransactionFlag = line.find("transactionFlag:[" + appPid); 74 if (positionTransactionFlag != std::string::npos) { 75 size_t positionFrame1 = line.rfind("[" + appPid + ","); 76 size_t positionFrame2 = line.rfind("],isUni:1"); 77 size_t subNum = 3; 78 frameId = line.substr(positionFrame1 + subNum + appPid.length(), positionFrame2 - positionFrame1 - subNum - appPid.length()); 79 } else { 80 frameId = fid; 81 } 82 return frameId; 83 } GetWindowTime(std::string line,std::string wt)84 std::string ParseTrace::GetWindowTime(std::string line, std::string wt) 85 { 86 std::string::size_type positionWindow = line.find("H:RSUniRender::Process:[leashWindow"); 87 if (positionWindow != std::string::npos) { 88 size_t positionWindow1 = line.rfind(")"); 89 size_t subNumSize = 4; 90 std::string windowSizeFlag = line.substr(positionWindow1 - subNumSize, subNumSize); 91 std::string windowSize = "0"; 92 if (std::stof(windowSize) == 0) { 93 int windowSizeNum = 1024; 94 if (std::stof(windowSizeFlag) == windowSizeNum) { 95 windowSize = "0"; 96 } else { 97 windowSize = windowSizeFlag; 98 } 99 } else { 100 if (std::stof(windowSize) != std::stof(windowSizeFlag)) { 101 size_t subNum = 5; 102 size_t position1 = line.find("...."); 103 size_t position2 = line.find(":"); 104 windowTime = line.substr(position1 + subNum, position2 - position1 - subNum); 105 windowSize = windowSizeFlag; 106 } else { 107 windowTime = wt; 108 } 109 } 110 } 111 return windowTime; 112 } ParseTraceCold(const std::string & fileNamePath,const std::string & appPid)113 float ParseTrace::ParseTraceCold(const std::string &fileNamePath, const std::string &appPid) 114 { 115 int conversion = 1000; 116 float code = -1; 117 infile.open(fileNamePath); 118 if (infile.fail()) { 119 std::cout << "File " << "open fail" << std::endl; 120 return 0; 121 } else { 122 code = SmartPerf::ParseTrace::ParseCodeTrace(fileNamePath, appPid); 123 } 124 infile.close(); 125 return code * conversion; 126 } ParseTraceHot(const std::string & fileNamePath)127 float ParseTrace::ParseTraceHot(const std::string &fileNamePath) 128 { 129 int conversion = 1000; 130 float code = -1; 131 infile.open(fileNamePath); 132 if (infile.fail()) { 133 std::cout << "File " << "open fail" << std::endl; 134 return 0; 135 } else { 136 code = SmartPerf::ParseTrace::ParseHotTrace(fileNamePath); 137 } 138 infile.close(); 139 return code * conversion; 140 } ParseCodeTrace(const std::string & fileNamePath,const std::string & appPid)141 float ParseTrace::ParseCodeTrace(const std::string &fileNamePath, const std::string &appPid) 142 { 143 std::string line; 144 std::string::size_type tracingMarkWrite; 145 std::string::size_type fourPoint; 146 float codeTime = -1; 147 while (getline(infile, line)) { 148 startTime = SmartPerf::ParseTrace::GetStartTime(line, startTime); 149 tracingMarkWrite = line.find("tracing_mark_write: B|"+ appPid + "|H:RSRenderThread DrawFrame:"); 150 fourPoint = line.find("...."); 151 if (tracingMarkWrite != std::string::npos && fourPoint != std::string::npos) { 152 size_t p1 = line.find("...."); 153 size_t p2 = line.find(":"); 154 size_t subNum = 5; 155 endTime = line.substr(p1 + subNum, p2 - p1 - subNum); 156 int endNum = std::stof(endTime); 157 int endFlagNum = std::stof(endTimeFlag); 158 int startNum = std::stof(startTime); 159 int timeNum = endNum - endFlagNum; 160 float interval = 0.3; 161 if (timeNum < interval) { 162 endTimeFlag = endTime; 163 } else { 164 if (std::stof(endTimeFlag) == 0) { 165 endTimeFlag = endTime; 166 } else if (endFlagNum != 0 && startNum != 0 && timeNum > interval) { 167 break; 168 } else { 169 endTimeFlag = endTime; 170 } 171 } 172 } 173 } 174 codeTime = SmartPerf::ParseTrace::GetTime(startTime, endTime); 175 return codeTime; 176 } ParseHotTrace(const std::string & fileNamePath)177 float ParseTrace::ParseHotTrace(const std::string &fileNamePath) 178 { 179 std::string line; 180 std::string::size_type doComposition; 181 float codeTime = -1; 182 while (getline(infile, line)) { 183 startTime=SmartPerf::ParseTrace::GetStartTime(line, startTime); 184 doComposition = line.find("H:RSMainThread::DoComposition"); 185 if (doComposition != std::string::npos) { 186 size_t position1 = line.find("...."); 187 size_t position2 = line.find(":"); 188 int subNum = 5; 189 endTime = line.substr(position1 + subNum, position2 - position1 - subNum); 190 int endNum = std::stof(endTime); 191 int endFlagNum = std::stof(endTimeFlag); 192 int startNum = std::stof(startTime); 193 int timeNum = endNum - endFlagNum; 194 float interval = 0.3; 195 if (timeNum < interval) { 196 endTimeFlag = endTime; 197 } else { 198 if (std::stof(endTimeFlag) == 0) { 199 endTimeFlag = endTime; 200 } else if (endFlagNum != 0 && startNum != 0 && timeNum > interval) { 201 break; 202 } else { 203 endTimeFlag = endTime; 204 } 205 } 206 } 207 } 208 codeTime = SmartPerf::ParseTrace::GetTime(startTime, endTime); 209 return codeTime; 210 } GetTime(std::string start,std::string end)211 float ParseTrace::GetTime(std::string start, std::string end) 212 { 213 size_t point = end.find("."); 214 float codeTime = -1; 215 size_t subNum = 2; 216 end = end.substr(point - subNum); 217 start = start.substr(point - subNum); 218 if (std::stof(end) == 0 || std::stof(start) == 0) { 219 } else { 220 float displayTime = 0.040; 221 codeTime = std::stof(end) - std::stof(start) + displayTime; 222 } 223 return codeTime; 224 } GetTimeNoah(std::string start,std::string end,std::string wt)225 float ParseTrace::GetTimeNoah(std::string start, std::string end, std::string wt) 226 { 227 float startTimeThreshold = 1.2; 228 float codeTime = -1; 229 if (std::stof(end) < std::stof(wt) && std::stof(end) != 0) { 230 end = wt; 231 } 232 if (std::stof(end) - std::stof(start) > startTimeThreshold) { 233 end = wt; 234 } 235 size_t point = end.find("."); 236 size_t subNum = 2; 237 end = end.substr(point - subNum); 238 start = start.substr(point - subNum); 239 if (std::stof(end) == 0 || std::stof(start) == 0) { 240 } else { 241 float displayTime = 0.040; 242 codeTime = std::stof(end) - std::stof(start) + displayTime; 243 } 244 return codeTime; 245 } GetStartTime(std::string line,const std::string & startTimeBefore)246 std::string ParseTrace::GetStartTime(std::string line, const std::string &startTimeBefore) 247 { 248 std::string::size_type touchEventDisPos = line.find("H:touchEventDispatch"); 249 std::string::size_type mTouchEventDisPos = line.find("H:TouchEventDispatch"); 250 if (mTouchEventDisPos != std::string::npos || touchEventDisPos != std::string::npos) { 251 size_t touchNum = 3; 252 if (flagTouch <= touchNum) { 253 size_t position1 = line.find("...."); 254 size_t position2 = line.find(":"); 255 size_t subNum = 5; 256 startTime = line.substr(position1 + subNum, position2 - position1 - subNum); 257 flagTime = "0"; 258 flagTouch++; 259 } else { 260 startTime = startTimeBefore; 261 } 262 } else { 263 startTime = startTimeBefore; 264 } 265 return startTime; 266 } 267 } 268 }