• 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 <future>
22 #include "include/control_call_cmd.h"
23 #include "include/startup_delay.h"
24 #include "include/parse_trace.h"
25 #include "include/sp_utils.h"
26 #include "include/parse_click_complete_trace.h"
27 #include "include/parse_click_response_trace.h"
28 #include "include/sp_parse_fps.h"
29 #include "include/parse_page_fps_trace.h"
30 #include "include/parse_start_frame_trace.h"
31 #include "include/parse_start_trace_noh.h"
32 #include "include/parse_radar.h"
33 #include "include/parse_slide_fps_trace.h"
34 
35 namespace OHOS {
36 namespace SmartPerf {
GetResult(std::vector<std::string> v)37 std::string ControlCallCmd::GetResult(std::vector<std::string> v)
38 {
39     if (v[ohType] == "ohtest") {
40         isOhTest = true;
41     }
42     if (v[typeName] == "coldStart") {
43         time = SmartPerf::ControlCallCmd::ColdStart(v);
44     } else if (v[typeName] == "hotStart") {
45         time = SmartPerf::ControlCallCmd::HotStart(v);
46     } else if (v[typeName] == "responseTime") {
47         time = SmartPerf::ControlCallCmd::ResponseTime();
48     } else if (v[typeName] == "completeTime") {
49         time = SmartPerf::ControlCallCmd::CompleteTime();
50     } else if (v[typeName] == "startResponse") {
51         time = SmartPerf::ControlCallCmd::StartResponse(v);
52     } else if (v[typeName] == "coldStartHM") {
53         time = SmartPerf::ControlCallCmd::ColdStartHM(v);
54     } else if (v[typeName] == "fps") {
55         result = SmartPerf::ControlCallCmd::SlideFps(v);
56     } else if (v[typeName] == "pagefps") {
57         result = SmartPerf::ControlCallCmd::PageFps();
58     } else if (v[typeName] == "startFrame") {
59         result = SmartPerf::ControlCallCmd::StartFrameFps(v);
60     } else if (v[typeName] == "fpsohtest") {
61         SPUtils::LoadCmd("GP_daemon_fps 10", result);
62     } else if (v[typeName] == "frameLoss") {
63         SmartPerf::ControlCallCmd::GetFrame();
64     } else if (v[typeName] == "appStartTime") {
65         result = ControlCallCmd::GetAppStartTime();
66     } else if (v[typeName] == "slideList") {
67         result = ControlCallCmd::SlideList();
68     } else if (v[typeName] == "timeDelay") {
69         result = ControlCallCmd::TimeDelay();
70     }
71     if (time == noNameType) {
72         std::cout << "Startup error, unknown application or application not responding" << std::endl;
73     } else {
74         if (time != 0) {
75             stream << time;
76             result = "time:" + stream.str() + "ms";
77         }
78         std::cout << result << std::endl;
79     }
80     return result;
81 }
TimeDelay()82 std::string ControlCallCmd::TimeDelay()
83 {
84     OHOS::SmartPerf::ParseClickResponseTrace pcrt;
85     OHOS::SmartPerf::StartUpDelay sd;
86     std::string cmdResult;
87     OHOS::SmartPerf::Radar radar;
88     SPUtils::LoadCmd("rm -rfv /data/local/tmp/sp_trace_delay.ftrace", cmdResult);
89     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "delay" + ".ftrace";
90     std::thread thGetTrace = sd.ThreadGetTrace("delay", traceName);
91     std::thread thGetHisysId = sd.ThreadGetHisysIdResponse();
92     std::thread thGetHisysId2 = sd.ThreadGetHisysIdComplete();
93     std::future<std::string> futureResult = std::async(std::launch::async, SPUtils::GetRadarResponse);
94     std::future<std::string> futureResult2 = std::async(std::launch::async, SPUtils::GetRadarComplete);
95     std::string str = futureResult.get();
96     std::string str2 = futureResult2.get();
97     thGetTrace.join();
98     thGetHisysId.join();
99     thGetHisysId2.join();
100     double strResponseTime = radar.ParseRadarResponse(str);
101     stream << strResponseTime;
102     double strCompleteTime = radar.ParseRadarComplete(str2);
103     std::ostringstream streamComplete;
104     streamComplete << strCompleteTime;
105     std::string resultTime = "ResponseTime:" + stream.str() + "ms\n" +
106      "CompleteTime:" + streamComplete.str() + "ms";
107     return resultTime;
108 }
SlideList()109 std::string ControlCallCmd::SlideList()
110 {
111     OHOS::SmartPerf::ParseClickResponseTrace pcrt;
112     OHOS::SmartPerf::StartUpDelay sd;
113     OHOS::SmartPerf::ParseSlideFpsTrace slideFpsTrace;
114     std::string cmdResult;
115     OHOS::SmartPerf::Radar radar;
116     std::string resultStream = "";
117     SPUtils::LoadCmd("rm -rfv /data/local/tmp/sp_trace_fps.ftrace", cmdResult);
118     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "fps" + ".ftrace";
119     if (isOhTest) {
120         std::thread thGetTrace = sd.ThreadGetTrace("fps", traceName);
121         thGetTrace.join();
122         time = pcrt.ParseResponseTrace(traceName);
123     } else {
124         std::thread thGetTrace = sd.ThreadGetTrace("fps", traceName);
125         std::thread thGetHisysId = sd.ThreadGetHisysId();
126         std::string str = SPUtils::GetRadarResponse();
127         thGetTrace.join();
128         thGetHisysId.join();
129         double responseTime = radar.ParseRadarResponse(str);
130         stream << responseTime;
131         std::string responseSlide = "ResponseTime:" + stream.str() + "ms";
132         double sFps = slideFpsTrace.ParseSlideFpsTraceNoh(traceName);
133         std::ostringstream streamFps;
134         streamFps << sFps;
135         resultStream = "FPS:" + streamFps.str() + "fps\n" + responseSlide;
136     }
137     return resultStream;
138 }
GetFrame()139 std::string ControlCallCmd::GetFrame()
140 {
141     OHOS::SmartPerf::StartUpDelay sd;
142     std::string cmdResult;
143     OHOS::SmartPerf::Radar radar;
144     SPUtils::LoadCmd("rm -rfv /data/local/tmp/sp_trace_frame.ftrace", cmdResult);
145     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "frame" + ".ftrace";
146     std::thread thGetTrace = sd.ThreadGetTrace("frame", traceName);
147     std::thread thGetHisysId = sd.ThreadGetHisysId();
148     std::string str = SPUtils::GetRadarFrame();
149     thGetTrace.join();
150     thGetHisysId.join();
151     std::string reslut = radar.ParseRadarFrame(str);
152     return result;
153 }
PageFps()154 std::string ControlCallCmd::PageFps()
155 {
156     OHOS::SmartPerf::StartUpDelay sd;
157     OHOS::SmartPerf::PageFpsTrace pageFpsTrace;
158     std::string cmdResult;
159     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
160     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "fps" + ".ftrace";
161     std::thread thGetTrace = sd.ThreadGetTrace("fps", traceName);
162     thGetTrace.join();
163     double fps = pageFpsTrace.ParsePageFpsTrace(traceName);
164     stream << fps;
165     result = "FPS:" + stream.str() + "fps";
166     return result;
167 }
SlideFps(std::vector<std::string> v)168 std::string ControlCallCmd::SlideFps(std::vector<std::string> v)
169 {
170     OHOS::SmartPerf::StartUpDelay sd;
171     ParseFPS parseFPS;
172     std::string cmdResult;
173     int typePKG = 3;
174     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
175     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "fps" + ".ftrace";
176     std::thread thGetTrace = sd.ThreadGetTrace("fps", traceName);
177     thGetTrace.join();
178     std::string fps = parseFPS.ParseTraceFile(traceName, v[typePKG]);
179     return fps;
180 }
ResponseTime()181 double ControlCallCmd::ResponseTime()
182 {
183     OHOS::SmartPerf::ParseClickResponseTrace pcrt;
184     OHOS::SmartPerf::StartUpDelay sd;
185     std::string cmdResult;
186     OHOS::SmartPerf::Radar radar;
187     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
188     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "response" + ".ftrace";
189     if (isOhTest) {
190         std::thread thGetTrace = sd.ThreadGetTrace("response", traceName);
191         thGetTrace.join();
192         time = pcrt.ParseResponseTrace(traceName);
193     } else {
194         std::thread thGetTrace = sd.ThreadGetTrace("response", traceName);
195         std::thread thGetHisysId = sd.ThreadGetHisysId();
196         std::string str = SPUtils::GetRadarResponse();
197         thGetTrace.join();
198         thGetHisysId.join();
199         time = radar.ParseRadarResponse(str);
200     }
201     return time;
202 }
ColdStartHM(std::vector<std::string> v)203 double ControlCallCmd::ColdStartHM(std::vector<std::string> v)
204 {
205     OHOS::SmartPerf::StartUpDelay sd;
206     OHOS::SmartPerf::ParseTrace parseTrace;
207     std::string cmdResult;
208     int typePKG = 3;
209     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
210     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "coldStart" + ".ftrace";
211     std::thread thGetTrace = sd.ThreadGetTrace("coldStart", traceName);
212     thGetTrace.join();
213     std::string pid = sd.GetPidByPkg(v[typePKG]);
214     return parseTrace.ParseTraceCold(traceName, pid);
215 }
CompleteTime()216 double ControlCallCmd::CompleteTime()
217 {
218     OHOS::SmartPerf::StartUpDelay sd;
219     OHOS::SmartPerf::ParseClickCompleteTrace pcct;
220     std::string cmdResult;
221     OHOS::SmartPerf::Radar radar;
222     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
223     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "complete" + ".ftrace";
224     if (isOhTest) {
225         std::thread thGetTrace = sd.ThreadGetTrace("complete", traceName);
226         thGetTrace.join();
227         time = pcct.ParseCompleteTrace(traceName);
228     } else {
229         std::thread thGetTrace = sd.ThreadGetTrace("complete", traceName);
230         std::thread thGetHisysId = sd.ThreadGetHisysId();
231         std::string str = SPUtils::GetRadarComplete();
232         thGetTrace.join();
233         thGetHisysId.join();
234         time = radar.ParseRadarComplete(str);
235     }
236     return time;
237 }
StartFrameFps(std::vector<std::string> v)238 std::string ControlCallCmd::StartFrameFps(std::vector<std::string> v)
239 {
240     OHOS::SmartPerf::StartUpDelay sd;
241     OHOS::SmartPerf::ParseTrace parseTrace;
242     OHOS::SmartPerf::StartFrameTraceNoh startFrameTraceNoh;
243     std::string cmdResult;
244     int type = 4;
245     int typePKG = 3;
246     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
247     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
248     SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
249     sleep(1);
250     size_t position = cmdResult.find(":");
251     size_t position2 = cmdResult.find("json");
252     std::string pathJson = cmdResult.substr(position + 1, position2 - position + typePKG);
253     sd.InitXY2(v[type], pathJson, v[typePKG]);
254     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "coldStart" + ".ftrace";
255     std::thread thGetTrace = sd.ThreadGetTrace("coldStart", traceName);
256     std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
257     sleep(1);
258     SPUtils::LoadCmd(cmd, cmdResult);
259     sleep(1);
260     thGetTrace.join();
261     double fps = startFrameTraceNoh.ParseStartFrameTraceNoh(traceName);
262     stream << fps;
263     result = "FPS:" + stream.str() + "fps";
264     return result;
265 }
StartResponse(std::vector<std::string> v)266 double ControlCallCmd::StartResponse(std::vector<std::string> v)
267 {
268     OHOS::SmartPerf::StartUpDelay sd;
269     OHOS::SmartPerf::ParseTrace parseTrace;
270     OHOS::SmartPerf::Radar radar;
271     std::string cmdResult;
272     int type = 4;
273     int typePKG = 3;
274     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
275     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
276     SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
277     sleep(1);
278     size_t position = cmdResult.find(":");
279     size_t position2 = cmdResult.find("json");
280     std::string pathJson = cmdResult.substr(position + 1, position2 - position + typePKG);
281     std::string deviceType = sd.GetDeviceType();
282     sd.InitXY2(v[type], pathJson, v[typePKG]);
283     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "startResponse" + ".ftrace";
284     if (sd.pointXY == "0 0") {
285         return noNameType;
286     } else {
287         std::thread thGetTrace = sd.ThreadGetTrace("startResponse", traceName);
288         std::thread thInputEvent = sd.ThreadInputEvent(sd.pointXY);
289         std::thread thGetHisysId = sd.ThreadGetHisysId();
290         std::string str = SPUtils::GetRadar();
291         thGetTrace.join();
292         thInputEvent.join();
293         thGetHisysId.join();
294         time = radar.ParseRadarStartResponse(str);
295         return time;
296     }
297 }
GetAppStartTime()298 std::string ControlCallCmd::GetAppStartTime()
299 {
300     OHOS::SmartPerf::StartUpDelay sd;
301     std::string cmdResult;
302     OHOS::SmartPerf::Radar radar;
303     SPUtils::LoadCmd("rm -rfv /data/local/tmp/sp_trace_start.ftrace", cmdResult);
304     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "start" + ".ftrace";
305     std::thread thGetTrace = sd.ThreadGetTrace("start", traceName);
306     std::thread thGetHisysId = sd.ThreadGetHisysId();
307     std::string str = SPUtils::GetRadar();
308     thGetTrace.join();
309     thGetHisysId.join();
310     std::string resultStream = radar.ParseRadarAppStrart(str);
311     return resultStream;
312 }
ColdStart(std::vector<std::string> v)313 double ControlCallCmd::ColdStart(std::vector<std::string> v)
314 {
315     OHOS::SmartPerf::StartUpDelay sd;
316     OHOS::SmartPerf::ParseTrace parseTrace;
317     OHOS::SmartPerf::Radar radar;
318     std::string cmdResult;
319     int type = 4;
320     int typePKG = 3;
321     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
322     SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
323     SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
324     sleep(1);
325     size_t position = cmdResult.find(":");
326     size_t position2 = cmdResult.find("json");
327     std::string pathJson = cmdResult.substr(position + 1, position2 - position + typePKG);
328     std::string deviceType = sd.GetDeviceType();
329     sd.InitXY2(v[type], pathJson, v[typePKG]);
330     std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "coldStart" + ".ftrace";
331     if (sd.pointXY == "0 0") {
332         return noNameType;
333     } else {
334         if (isOhTest) {
335             std::thread thGetTrace = sd.ThreadGetTrace("coldStart", traceName);
336             std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
337             sleep(1);
338             SPUtils::LoadCmd(cmd, cmdResult);
339             sleep(1);
340             std::string pid = sd.GetPidByPkg(v[typePKG]);
341             thGetTrace.join();
342             time = parseTrace.ParseTraceCold(traceName, pid);
343         } else {
344             std::thread thGetTrace = sd.ThreadGetTrace("coldStart", traceName);
345             std::thread thInputEvent = sd.ThreadInputEvent(sd.pointXY);
346             std::thread thGetHisysId = sd.ThreadGetHisysId();
347             sleep(1);
348             std::string str = SPUtils::GetRadar();
349             thGetTrace.join();
350             thInputEvent.join();
351             thGetHisysId.join();
352             time = radar.ParseRadarStart(str);
353         }
354         return time;
355     }
356 }
HotStart(std::vector<std::string> v)357 double ControlCallCmd::HotStart(std::vector<std::string> v)
358 {
359     OHOS::SmartPerf::StartUpDelay sd;
360     OHOS::SmartPerf::ParseTrace parseTrace;
361     OHOS::SmartPerf::Radar radar;
362     std::string cmdResult;
363     std::string deviceType = sd.GetDeviceType();
364     if (isOhTest) {
365         SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
366         std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "hotStart" + ".ftrace";
367         std::thread thGetTrace = sd.ThreadGetTrace("hotStart", traceName);
368         thGetTrace.join();
369         return parseTrace.ParseTraceHot(traceName);
370     } else {
371         int type = 4;
372         int typePKG = 3;
373         SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
374         SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
375         SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
376         sleep(1);
377         size_t position = cmdResult.find(":");
378         size_t position2 = cmdResult.find("json");
379         std::string pathJson = cmdResult.substr(position + 1, position2 - position + typePKG);
380         sd.InitXY2(v[type], pathJson, v[typePKG]);
381         if (sd.pointXY == "0 0") {
382             return noNameType;
383         } else {
384             std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
385             SPUtils::LoadCmd(cmd, cmdResult);
386             sleep(1);
387             sd.ChangeToBackground();
388             sleep(1);
389             std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "hotStart" + ".ftrace";
390             std::thread thGetTrace = sd.ThreadGetTrace("hotStart", traceName);
391             std::thread thInputEvent = sd.ThreadInputEvent(sd.pointXY);
392             std::thread thGetHisysId = sd.ThreadGetHisysId();
393             sleep(1);
394             std::string str = SPUtils::GetRadar();
395             thGetTrace.join();
396             thInputEvent.join();
397             thGetHisysId.join();
398             time = radar.ParseRadarStart(str);
399             return time;
400         }
401     }
402 }
403 }
404 }
405