• 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 <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_click_response_trace.h"
24 #include "include/sp_utils.h"
25 #include "include/sp_log.h"
26 namespace OHOS {
27 namespace SmartPerf {
ParseResponseTrace(std::string fileNamePath)28 double ParseClickResponseTrace::ParseResponseTrace(std::string fileNamePath)
29 {
30     int conversion = 1000;
31     char realPath[PATH_MAX] = {0x00};
32     if ((realpath(fileNamePath.c_str(), realPath) == nullptr)) {
33         std::cout << "" << std::endl;
34     }
35     infile.open(realPath);
36     if (infile.fail()) {
37         LOGE("ParseClickResponseTrace open file(%s) fialed ", fileNamePath.c_str());
38         return 0;
39     }
40     completeTime = SmartPerf::ParseClickResponseTrace::GetLineTime();
41     infile.close();
42     return completeTime * conversion;
43 }
GetLineTime()44 double ParseClickResponseTrace::GetLineTime()
45 {
46     std::string line;
47     std::string startTime = "0";
48     std::string endTime = "0";
49     std::string appPid = "0";
50     std::string::size_type doComposition;
51     size_t subNum = 5;
52     while (getline(infile, line)) {
53         appPid = SmartPerf::ParseClickResponseTrace::GetPid(line, "pid", appPid);
54         startTime = SmartPerf::ParseClickResponseTrace::GetStartTime(line, startTime);
55         doComposition = line.find("H:RSMainThread::DoComposition");
56         if (doComposition != std::string::npos) {
57             size_t position1 = line.find("....");
58             size_t position2 = line.find(":");
59             endTime = line.substr(position1 + subNum, position2 - position1 - subNum);
60             if (std::stof(startTime) == 0) {
61             } else {
62                 break;
63             }
64         }
65     }
66     completeTime = SmartPerf::ParseClickResponseTrace::GetTime(startTime, endTime);
67     return completeTime;
68 }
GetTime(std::string startTime,std::string endTime)69 double ParseClickResponseTrace::GetTime(std::string startTime, std::string endTime)
70 {
71     size_t point = endTime.find(".");
72     float subNum = 2;
73     endTime = endTime.substr(point - subNum);
74     startTime = startTime.substr(point - subNum);
75     if (std::stof(endTime) == 0 || std::stof(startTime) == 0) {
76     } else {
77         double displayTime = 0.032;
78         completeTime = std::stof(endTime) - std::stof(startTime) + displayTime;
79     }
80     return completeTime;
81 }
GetPid(std::string line,const std::string & pn,const std::string & pb)82 std::string ParseClickResponseTrace::GetPid(std::string line, const std::string &pn, const std::string &pb)
83 {
84     std::string appPid;
85     if (appPidnum == 0) {
86         size_t packageNameNumSize = 5;
87         std::string::size_type positionPackgeName;
88         if (pn.length() < packageNameNumSize) {
89             std::string::size_type positionAppspawn;
90             positionPackgeName = line.find("task_newtask: pid=");
91             positionAppspawn = line.find("comm=appspawn");
92             if (positionPackgeName != std::string::npos && positionAppspawn != std::string::npos) {
93                 size_t subNum = 4;
94                 size_t position1 = line.find("pid=");
95                 size_t position2 = line.find(" comm=appspawn");
96                 appPid = line.substr(position1 + subNum, position2 - position1 - subNum);
97                 appPidnum++;
98             } else {
99                 appPid = pb;
100             }
101         } else {
102             positionPackgeName = line.find(pn);
103             if (positionPackgeName != std::string::npos) {
104                 size_t p1 = line.find(pn);
105                 size_t p2 = line.find(" prio");
106                 appPid = line.substr(p1 + pn.length(), p2 - p1 - pn.length());
107                 appPidnum++;
108             } else {
109                 appPid = pb;
110             }
111         }
112     }
113     return appPid;
114 }
GetStartTime(std::string line,const std::string & startTimeBefore)115 std::string ParseClickResponseTrace::GetStartTime(std::string line, const std::string &startTimeBefore)
116 {
117     std::string startTime;
118     std::string::size_type te = line.find("H:touchEventDispatch");
119     std::string::size_type td = line.find("H:TouchEventDispatch");
120     std::string::size_type pd = line.find("H:PointerEventDispatch");
121     std::string::size_type kd = line.find("H:KeyEventDispatch");
122     std::string::size_type nop = std::string::npos;
123     if (te != nop || td != nop || pd != nop || kd != nop) {
124         int touchNum = 3;
125         if (flagTouch <= touchNum) {
126             size_t position1 = line.find("....");
127             size_t position2 = line.find(":");
128             size_t subNum = 5;
129             startTime = line.substr(position1 + subNum, position2 - position1 - subNum);
130             flagTime = "0";
131             flagTouch++;
132         } else {
133             startTime = startTimeBefore;
134         }
135     } else {
136         startTime = startTimeBefore;
137     }
138     return startTime;
139 }
140 }
141 }