• 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 "unistd.h"
16 #include <thread>
17 #include <cstdio>
18 #include <cstring>
19 #include <map>
20 #include <sstream>
21 #include "include/control_call_cmd.h"
22 #include "include/startup_delay.h"
23 #include "include/parse_trace.h"
24 #include "include/sp_utils.h"
25 #include "include/parse_click_complete_trace.h"
26 #include "include/parse_click_response_trace.h"
27 #include "include/sp_parse_fps.h"
28 #include "include/parse_page_fps_trace.h"
29 #include "include/parse_start_frame_trace.h"
30 #include "include/parse_start_trace_noh.h"
31 
32 namespace OHOS {
33 namespace SmartPerf {
GetResult(int argc,std::vector<std::string> v)34 std::string ControlCallCmd::GetResult(int argc, std::vector<std::string> v)
35 {
36     std::ostringstream stream;
37     if (v[ohType] == "ohtest") {
38         isOhTest = true;
39     }
40     if (v[typeName] == "coldStart") {
41         time = SmartPerf::ControlCallCmd::ColdStart(v);
42         stream << time;
43         result = "time:" + stream.str();
44     } else if (v[typeName] == "hotStart") {
45         time = SmartPerf::ControlCallCmd::HotStart(v);
46         stream << time;
47         result = "time:" + stream.str();
48     } else if (v[typeName] == "responseTime") {
49         time = SmartPerf::ControlCallCmd::ResponseTime();
50         stream << time;
51         result = "time:" + stream.str();
52     } else if (v[typeName] == "completeTime") {
53         time = SmartPerf::ControlCallCmd::CompleteTime();
54         stream << time;
55         result = "time:" + stream.str();
56     } else if (v[typeName] == "coldStartHM") {
57         time = SmartPerf::ControlCallCmd::ColdStartHM(v);
58         stream << time;
59         result = "time:" + stream.str();
60     } else if (v[typeName] == "fps") {
61         result = SmartPerf::ControlCallCmd::SlideFps(v);
62         std::cout << result << std::endl;
63     }  else if (v[typeName] == "pagefps") {
64         double fps = SmartPerf::ControlCallCmd::PageFps();
65         stream << fps;
66         result = "FPS:" + stream.str() + "fps";
67     } else if (v[typeName] == "FPS") {
68         result = SmartPerf::ControlCallCmd::SlideFPS(v);
69         std::cout << result << std::endl;
70     } else if (v[typeName] == "startFrame") {
71         double fps = SmartPerf::ControlCallCmd::StartFrameFps(v);
72         stream << fps;
73         result = "FPS:" + stream.str() + "fps";
74     } else if (v[typeName] == "fpsohtest") {
75         SPUtils::LoadCmd("GP_daemon_fps 10", result);
76     }
77     if (time == noNameType) {
78         std::cout << "Startup error, unknown application or application not responding"<< std::endl;
79     } else {
80         std::cout << result << std::endl;
81     }
82     return result;
83 }
PageFps()84 double ControlCallCmd::PageFps()
85 {
86     OHOS::SmartPerf::StartUpDelay sd;
87     OHOS::SmartPerf::PageFpsTrace pageFpsTrace;
88     std::string cmdResult;
89     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
90     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "fps" + ".ftrace";
91     std::thread thGetTrace = sd.ThreadGetTrace("fps", traceName);
92     thGetTrace.join();
93     double fps = pageFpsTrace.ParsePageFpsTrace(traceName);
94     return fps;
95 }
SlideFps(std::vector<std::string> v)96 std::string ControlCallCmd::SlideFps(std::vector<std::string> v)
97 {
98     OHOS::SmartPerf::StartUpDelay sd;
99     ParseFPS parseFPS;
100     std::string cmdResult;
101     int typePKG = 3;
102     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
103     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "fps" + ".ftrace";
104     std::thread thGetTrace = sd.ThreadGetTrace("fps", traceName);
105     thGetTrace.join();
106     std::string fps = parseFPS.ParseTraceFile(traceName, v[typePKG]);
107     return fps;
108 }
SlideFPS(std::vector<std::string> v)109 std::string ControlCallCmd::SlideFPS(std::vector<std::string> v)
110 {
111     OHOS::SmartPerf::StartUpDelay sd;
112     ParseFPS parseFPS;
113     std::string cmdResult;
114     int type = 4;
115     int typePKG = 3;
116     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
117     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
118     SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
119     sleep(1);
120     size_t position = cmdResult.find(":");
121     size_t position2 = cmdResult.find("json");
122     std::string pathJson = cmdResult.substr(position + 1, position2 - position + typePKG);
123     std::string deviceType = sd.GetDeviceType();
124     sd.InitXY2(v[type], pathJson, v[typePKG]);
125     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "fps" + ".ftrace";
126     std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
127     sleep(1);
128     SPUtils::LoadCmd(cmd, cmdResult);
129     sleep(1);
130     std::string topPkg = SPUtils::GetTopPkgName();
131     std::string pid = sd.GetPidByPkg(v[typePKG]);
132     if (topPkg.find(v[typePKG]) == std::string::npos || pid == "") {
133         return "";
134     }
135     std::thread thGetTrace = sd.ThreadGetTrace("fps", traceName);
136     cmd = "uinput -T -m 650 1500 650 500 30";
137     SPUtils::LoadCmd(cmd, cmdResult);
138     thGetTrace.join();
139     std::string fps = parseFPS.ParseTraceFile(traceName, v[typePKG]);
140     return fps;
141 }
ResponseTime()142 double ControlCallCmd::ResponseTime()
143 {
144     OHOS::SmartPerf::ParseClickResponseTrace pcrt;
145     OHOS::SmartPerf::StartUpDelay sd;
146     std::string cmdResult;
147     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
148     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "response" + ".ftrace";
149     std::thread thGetTrace = sd.ThreadGetTrace("response", traceName);
150     thGetTrace.join();
151     time = pcrt.ParseResponseTrace(traceName);
152     return time;
153 }
ColdStartHM(std::vector<std::string> v)154 double ControlCallCmd::ColdStartHM(std::vector<std::string> v)
155 {
156     OHOS::SmartPerf::StartUpDelay sd;
157     OHOS::SmartPerf::ParseTrace parseTrace;
158     std::string cmdResult;
159     int typePKG = 3;
160     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
161     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "coldStart" + ".ftrace";
162     std::thread thGetTrace = sd.ThreadGetTrace("coldStart", traceName);
163     thGetTrace.join();
164     std::string pid = sd.GetPidByPkg(v[typePKG]);
165     return parseTrace.ParseTraceCold(traceName, pid);
166 }
CompleteTime()167 double ControlCallCmd::CompleteTime()
168 {
169     OHOS::SmartPerf::StartUpDelay sd;
170     OHOS::SmartPerf::ParseClickCompleteTrace pcct;
171     std::string cmdResult;
172     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
173     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "complete" + ".ftrace";
174     std::thread thGetTrace = sd.ThreadGetTrace("complete", traceName);
175     thGetTrace.join();
176     time = pcct.ParseCompleteTrace(traceName);
177     return time;
178 }
StartFrameFps(std::vector<std::string> v)179 double ControlCallCmd::StartFrameFps(std::vector<std::string> v)
180 {
181     OHOS::SmartPerf::StartUpDelay sd;
182     OHOS::SmartPerf::ParseTrace parseTrace;
183     OHOS::SmartPerf::StartFrameTraceNoh startFrameTraceNoh;
184     std::string cmdResult;
185     int type = 4;
186     int typePKG = 3;
187     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
188     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
189     SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
190     sleep(1);
191     size_t position = cmdResult.find(":");
192     size_t position2 = cmdResult.find("json");
193     std::string pathJson = cmdResult.substr(position + 1, position2 - position + typePKG);
194     sd.InitXY2(v[type], pathJson, v[typePKG]);
195     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "coldStart" + ".ftrace";
196     std::thread thGetTrace = sd.ThreadGetTrace("coldStart", traceName);
197     std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
198     sleep(1);
199     SPUtils::LoadCmd(cmd, cmdResult);
200     sleep(1);
201     std::string topPkg = SPUtils::GetTopPkgName();
202     std::string pid = sd.GetPidByPkg(v[typePKG]);
203     thGetTrace.join();
204     double fps = startFrameTraceNoh.ParseStartFrameTraceNoh(traceName);
205     return fps;
206 }
ColdStart(std::vector<std::string> v)207 double ControlCallCmd::ColdStart(std::vector<std::string> v)
208 {
209     OHOS::SmartPerf::StartUpDelay sd;
210     OHOS::SmartPerf::ParseTrace parseTrace;
211     std::string cmdResult;
212     int type = 4;
213     int typePKG = 3;
214     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
215     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
216     SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
217     sleep(1);
218     size_t position = cmdResult.find(":");
219     size_t position2 = cmdResult.find("json");
220     std::string pathJson = cmdResult.substr(position + 1, position2 - position + typePKG);
221     std::string deviceType = sd.GetDeviceType();
222     sd.InitXY2(v[type], pathJson, v[typePKG]);
223     if (sd.pointXY == "0 0") {
224         return noNameType;
225     } else {
226         std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "coldStart" + ".ftrace";
227         std::thread thGetTrace = sd.ThreadGetTrace("coldStart", traceName);
228         std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
229         sleep(1);
230         SPUtils::LoadCmd(cmd, cmdResult);
231         sleep(1);
232         std::string topPkg = SPUtils::GetTopPkgName();
233         std::string pid = sd.GetPidByPkg(v[typePKG]);
234         thGetTrace.join();
235         if (topPkg.find(v[typePKG]) == std::string::npos || pid == "") {
236             return noNameType;
237         }
238         if (isOhTest) {
239             time = parseTrace.ParseTraceCold(traceName, pid);
240         } else {
241             time = parseTrace.ParseTraceNoh(traceName, pid);
242         }
243         return time;
244     }
245 }
HotStart(std::vector<std::string> v)246 double ControlCallCmd::HotStart(std::vector<std::string> v)
247 {
248     OHOS::SmartPerf::StartUpDelay sd;
249     OHOS::SmartPerf::ParseTrace parseTrace;
250     std::string cmdResult;
251     std::string deviceType = sd.GetDeviceType();
252     if (isOhTest) {
253         SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
254         std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "hotStart" + ".ftrace";
255         std::thread thGetTrace = sd.ThreadGetTrace("hotStart", traceName);
256         thGetTrace.join();
257         return parseTrace.ParseTraceHot(traceName);
258     } else {
259         int type = 4;
260         int typePKG = 3;
261         SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
262         SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
263         SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
264         sleep(1);
265         size_t position = cmdResult.find(":");
266         size_t position2 = cmdResult.find("json");
267         std::string pathJson = cmdResult.substr(position + 1, position2 - position + typePKG);
268         sd.InitXY2(v[type], pathJson, v[typePKG]);
269         if (sd.pointXY == "0 0") {
270             return noNameType;
271         } else {
272             std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
273             SPUtils::LoadCmd(cmd, cmdResult);
274             sleep(1);
275             sd.ChangeToBackground();
276             std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "hotStart" + ".ftrace";
277             std::thread thGetTrace = sd.ThreadGetTrace("hotStart", traceName);
278             sleep(1);
279             SPUtils::LoadCmd(cmd, cmdResult);
280             sleep(1);
281             std::string topPkg = SPUtils::GetTopPkgName();
282             std::string pid = sd.GetPidByPkg(v[typePKG]);
283             thGetTrace.join();
284             if (topPkg.find(v[typePKG]) == std::string::npos || pid == "") {
285                 return noNameType;
286             }
287             return parseTrace.ParseTraceNoh(traceName, pid);
288         }
289     }
290 }
291 }
292 }
293