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