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
16 #include <thread>
17 #include <iostream>
18 #include <fstream>
19 #include <string>
20 #include <vector>
21 #include <cstdio>
22 #include <sstream>
23 #include <iomanip>
24 #include "include/parse_start_trace_noh.h"
25 #include "include/sp_log.h"
26
27 namespace OHOS {
28 namespace SmartPerf {
ParseStartTraceNohe(std::string file)29 double ParseStartTraceNoh::ParseStartTraceNohe(std::string file)
30 {
31 double codeTime = -1.0;
32 char realPath[PATH_MAX] = {0x00};
33 if (realpath(file.c_str(), realPath) == nullptr) {
34 std::cout << "" << std::endl;
35 }
36 infile.open(realPath);
37 if (infile.fail()) {
38 std::cout << "file open fail:" << file << std::endl;
39 LOGE("ParseStartTraceNoh open file(%s) failed ", file.c_str());
40 return codeTime;
41 } else {
42 codeTime = SmartPerf::ParseStartTraceNoh::CalculateTime();
43 }
44 return codeTime;
45 }
CalculateTime()46 double ParseStartTraceNoh::CalculateTime()
47 {
48 std::string startLine = "";
49 std::string endLine = "";
50 bool findTouch = false;
51 std::string line;
52 while (getline(infile, line)) {
53 if (line.find("H:touchEventDispatch") != std::string::npos) {
54 startLine = line;
55 findTouch = true;
56 } else if (findTouch && line.find("H:RSUniRender::Process:[leashWindow") != std::string::npos) {
57 std::string tmpStr = Split(line, "H:RSUniRender::Process:[leashWindow")[1];
58 std::string numsStr = CutString(tmpStr, "(", ")", 1);
59 std::vector<std::string> nums = Split(numsStr, ",");
60 std::string alpha = Split(tmpStr, "Alpha:")[1];
61 bool full = (stof(nums[0]) == 0 && stof(nums[1]) == 0 && stof(nums[2]) == 1344 && stof(nums[3]) == 2772) ?
62 true :
63 false;
64 if (full && stof(alpha) == 1) {
65 endLine = line;
66 break;
67 }
68 }
69 }
70 if (startLine.compare("") == 0) {
71 std::cout << "can not find start point {H:touchEventDispatch}" << std::endl;
72 LOGE("can not find start point {H:touchEventDispatch}");
73 } else if (endLine.compare("") == 0) {
74 std::cout << "can not find complete point {H:RSMainThread::leashWindow}" << std::endl;
75 LOGE("can not find complete point {H:RSMainThread::leashWindow}");
76 } else {
77 double startTime = std::stod(SmartPerf::ParseStartTraceNoh::GetLineTime(startLine));
78 double endTime = std::stod(SmartPerf::ParseStartTraceNoh::GetLineTime(endLine));
79 return endTime - startTime;
80 }
81 return -1.0;
82 }
GetLineTime(std::string line)83 std::string ParseStartTraceNoh::GetLineTime(std::string line)
84 {
85 size_t num = 7;
86 size_t position1 = line.find("....");
87 size_t position2 = line.find(":");
88 return line.substr(position1 + num, position2 - position1 - num);
89 }
CutString(std::string line,const std::string & start,const std::string & end,size_t offset)90 std::string ParseStartTraceNoh::CutString(std::string line, const std::string &start,
91 const std::string &end, size_t offset)
92 {
93 int position1 = static_cast<int>(line.find(start));
94 int position2 = static_cast<int>(line.find(end));
95 return line.substr(position1 + offset, position2 - position1 - static_cast<int>(offset));
96 }
Split(std::string str,const std::string & pattern)97 std::vector<std::string> ParseStartTraceNoh::Split(std::string str, const std::string &pattern)
98 {
99 std::vector<std::string> result;
100 if (str.empty() || pattern.empty() || str.size() < pattern.size()) {
101 return result;
102 }
103 size_t start = 0; // start index
104 while (start < str.size()) {
105 size_t pos = str.find(pattern, start);
106 if (pos != std::string::npos) {
107 result.push_back(str.substr(start, pos - start));
108 start = pos + pattern.size(); // next start index
109 } else {
110 //the last start
111 result.push_back(str.substr(start));
112 break;
113 }
114 }
115 return result;
116 }
117 }
118 }