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