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 "include/editor_command.h"
20 #include "include/startup_delay.h"
21 #include "include/parse_trace.h"
22 #include "include/sp_utils.h"
23 #include "include/parse_click_complete_trace.h"
24 #include "include/parse_click_response_trace.h"
25 #include "include/sp_parse_fps.h"
26
27 namespace OHOS {
28 namespace SmartPerf {
EditorCommand(int argc,std::vector<std::string> v)29 EditorCommand::EditorCommand(int argc, std::vector<std::string> v)
30 {
31 if (argc >= threeParamMore) {
32 int ohType = 5;
33 int type = 2;
34 float time = 0.0;
35 float noNameType = -1.0;
36 if (v[ohType] == "ohtest") {
37 isOhTest = true;
38 }
39 if (v[type] == "coldStart") {
40 time = SmartPerf::EditorCommand::ColdStart(v);
41 } else if (v[type] == "hotStart") {
42 time = SmartPerf::EditorCommand::HotStart(v);
43 } else if (v[type] == "responseTime") {
44 time = SmartPerf::EditorCommand::ResponseTime();
45 } else if (v[type] == "completeTime") {
46 time = SmartPerf::EditorCommand::CompleteTime();
47 } else if (v[type] == "coldStartHM") {
48 time = SmartPerf::EditorCommand::ColdStartHM(v);
49 } else if (v[type] == "fps") {
50 std::cout << SmartPerf::EditorCommand::SlideFps(v)<< std::endl;
51 return;
52 } else if (v[type] == "FPS") {
53 std::cout << SmartPerf::EditorCommand::SlideFPS(v)<< std::endl;
54 return;
55 }
56 if (time == noNameType) {
57 std::cout << "Startup error, unknown application or application not responding"<< std::endl;
58 } else {
59 std::cout << "time:" << time << std::endl;
60 }
61 }
62 }
SlideFps(std::vector<std::string> v)63 std::string EditorCommand::SlideFps(std::vector<std::string> v)
64 {
65 OHOS::SmartPerf::StartUpDelay sd;
66 ParseFPS parseFPS;
67 std::string cmdResult;
68 int typePKG = 3;
69 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
70 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "fps" + ".ftrace";
71 std::thread thGetTrace = sd.ThreadGetTrace("fps", traceName);
72 thGetTrace.join();
73 std::string fps = parseFPS.ParseTraceFile(traceName, v[typePKG]);
74 return fps;
75 }
SlideFPS(std::vector<std::string> v)76 std::string EditorCommand::SlideFPS(std::vector<std::string> v)
77 {
78 OHOS::SmartPerf::StartUpDelay sd;
79 ParseFPS parseFPS;
80 std::string cmdResult;
81 int type = 4;
82 int typePKG = 3;
83 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
84 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
85 SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
86 sleep(1);
87 size_t position = cmdResult.find(":");
88 std::string pathJson = cmdResult.substr(position + 1);
89 std::string deviceType = sd.GetDeviceType();
90 sd.InitXY2(v[type], pathJson, v[typePKG]);
91 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "fps" + ".ftrace";
92 std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
93 sleep(1);
94 SPUtils::LoadCmd(cmd, cmdResult);
95 sleep(1);
96 std::string topPkg = SPUtils::GetTopPkgName();
97 std::string pid = sd.GetPidByPkg(v[typePKG]);
98 if (topPkg.find(v[typePKG]) == std::string::npos || pid == "") {
99 return "";
100 }
101 std::thread thGetTrace = sd.ThreadGetTrace("fps", traceName);
102 cmd = "uinput -T -m 650 1500 650 500 30";
103 SPUtils::LoadCmd(cmd, cmdResult);
104 thGetTrace.join();
105 std::string fps = parseFPS.ParseTraceFile(traceName, v[typePKG]);
106 return fps;
107 }
ResponseTime()108 float EditorCommand::ResponseTime()
109 {
110 OHOS::SmartPerf::ParseClickResponseTrace pcrt;
111 OHOS::SmartPerf::StartUpDelay sd;
112 std::string cmdResult;
113 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
114 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "response" + ".ftrace";
115 std::thread thGetTrace = sd.ThreadGetTrace("response", traceName);
116 thGetTrace.join();
117 float time = pcrt.ParseResponseTrace(traceName);
118 return time;
119 }
ColdStartHM(std::vector<std::string> v)120 float EditorCommand::ColdStartHM(std::vector<std::string> v)
121 {
122 OHOS::SmartPerf::StartUpDelay sd;
123 OHOS::SmartPerf::ParseTrace parseTrace;
124 std::string cmdResult;
125 int typePKG = 3;
126 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
127 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "coldStart" + ".ftrace";
128 std::thread thGetTrace = sd.ThreadGetTrace("coldStart", traceName);
129 thGetTrace.join();
130 std::string pid = sd.GetPidByPkg(v[typePKG]);
131 return parseTrace.ParseTraceCold(traceName, pid);
132 }
CompleteTime()133 float EditorCommand::CompleteTime()
134 {
135 OHOS::SmartPerf::StartUpDelay sd;
136 OHOS::SmartPerf::ParseClickCompleteTrace pcct;
137 std::string cmdResult;
138 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
139 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "complete" + ".ftrace";
140 std::thread thGetTrace = sd.ThreadGetTrace("complete", traceName);
141 thGetTrace.join();
142 float time = pcct.ParseCompleteTrace(traceName);
143 return time;
144 }
ColdStart(std::vector<std::string> v)145 float EditorCommand::ColdStart(std::vector<std::string> v)
146 {
147 OHOS::SmartPerf::StartUpDelay sd;
148 OHOS::SmartPerf::ParseTrace parseTrace;
149 std::string cmdResult;
150 int type = 4;
151 int typePKG = 3;
152 float noNameType = -1.0;
153 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
154 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
155 SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
156 sleep(1);
157 size_t position = cmdResult.find(":");
158 std::string pathJson = cmdResult.substr(position + 1);
159 std::string deviceType = sd.GetDeviceType();
160 sd.InitXY2(v[type], pathJson, v[typePKG]);
161 if (sd.pointXY == "0 0") {
162 return noNameType;
163 } else {
164 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "coldStart" + ".ftrace";
165 std::thread thGetTrace = sd.ThreadGetTrace("coldStart", traceName);
166 std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
167 sleep(1);
168 SPUtils::LoadCmd(cmd, cmdResult);
169 sleep(1);
170 std::string topPkg = SPUtils::GetTopPkgName();
171 std::string pid = sd.GetPidByPkg(v[typePKG]);
172 thGetTrace.join();
173 if (topPkg.find(v[typePKG]) == std::string::npos || pid == "") {
174 return noNameType;
175 }
176 float time = 0.0;
177 if (isOhTest) {
178 time = parseTrace.ParseTraceCold(traceName, pid);
179 } else {
180 time = parseTrace.ParseTraceNoh(traceName, pid);
181 }
182 return time;
183 }
184 }
HotStart(std::vector<std::string> v)185 float EditorCommand::HotStart(std::vector<std::string> v)
186 {
187 OHOS::SmartPerf::StartUpDelay sd;
188 OHOS::SmartPerf::ParseTrace parseTrace;
189 std::string cmdResult;
190 std::string deviceType = sd.GetDeviceType();
191 if (isOhTest) {
192 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
193 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "hotStart" + ".ftrace";
194 std::thread thGetTrace = sd.ThreadGetTrace("hotStart", traceName);
195 thGetTrace.join();
196 return parseTrace.ParseTraceHot(traceName);
197 } else {
198 int type = 4;
199 int typePKG = 3;
200 float noNameType = -1.0;
201 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
202 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
203 SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
204 sleep(1);
205 size_t position = cmdResult.find(":");
206 std::string pathJson = cmdResult.substr(position + 1);
207 sd.InitXY2(v[type], pathJson, v[typePKG]);
208 if (sd.pointXY == "0 0") {
209 return noNameType;
210 } else {
211 std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
212 SPUtils::LoadCmd(cmd, cmdResult);
213 sd.ChangeToBackground();
214 std::string topPkgBefore = SPUtils::GetTopPkgName();
215 if (topPkgBefore.find(v[typePKG]) != std::string::npos) {
216 return noNameType;
217 }
218 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "hotStart" + ".ftrace";
219 std::thread thGetTrace = sd.ThreadGetTrace("hotStart", traceName);
220 sleep(1);
221 SPUtils::LoadCmd(cmd, cmdResult);
222 sleep(1);
223 std::string topPkg = SPUtils::GetTopPkgName();
224 std::string pid = sd.GetPidByPkg(v[typePKG]);
225 thGetTrace.join();
226 if (topPkg.find(v[typePKG]) == std::string::npos || pid == "") {
227 return noNameType;
228 }
229 return parseTrace.ParseTraceNoh(traceName, pid);
230 }
231 }
232 }
233 }
234 }
235