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
26 namespace OHOS {
27 namespace SmartPerf {
EditorCommand(int argc,std::vector<std::string> v)28 EditorCommand::EditorCommand(int argc, std::vector<std::string> v)
29 {
30 if (argc >= threeParamMore) {
31 int type = 2;
32 float time = 0.0;
33 int typeName = 4;
34 float noNameType = 5.0;
35 if (v[type] == "coldStart") {
36 time = SmartPerf::EditorCommand::ColdStart(v);
37 } else if (v[type] == "hotStart") {
38 time = SmartPerf::EditorCommand::HotStart(v);
39 } else if (v[type] == "responseTime") {
40 time = SmartPerf::EditorCommand::ResponseTime();
41 } else if (v[type] == "completeTime") {
42 time = SmartPerf::EditorCommand::CompleteTime();
43 }
44 if (time == noNameType) {
45 std::cout << v[typeName] << " Duplicate Application Name" << std::endl;
46 } else {
47 std::cout << "time:" << time << std::endl;
48 }
49 }
50 }
ResponseTime()51 float EditorCommand::ResponseTime()
52 {
53 OHOS::SmartPerf::ParseClickResponseTrace pcrt;
54 OHOS::SmartPerf::StartUpDelay sd;
55 std::string cmdResult;
56 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
57 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "response" + ".ftrace";
58 std::thread thGetTrace = sd.ThreadGetTrace("response", traceName);
59 thGetTrace.join();
60 float time = pcrt.ParseResponseTrace(traceName);
61 return time;
62 }
CompleteTime()63 float EditorCommand::CompleteTime()
64 {
65 OHOS::SmartPerf::StartUpDelay sd;
66 OHOS::SmartPerf::ParseClickCompleteTrace pcct;
67 std::string cmdResult;
68 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
69 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "complete" + ".ftrace";
70 std::thread thGetTrace = sd.ThreadGetTrace("complete", traceName);
71 thGetTrace.join();
72 float time = pcct.ParseCompleteTrace(traceName);
73 return time;
74 }
ColdStart(std::vector<std::string> v)75 float EditorCommand::ColdStart(std::vector<std::string> v)
76 {
77 OHOS::SmartPerf::StartUpDelay sd;
78 OHOS::SmartPerf::ParseTrace parseTrace;
79 std::string cmdResult;
80 int type = 4;
81 int typePKG = 3;
82 float noNameType = 5.0;
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 sd.InitXY2(v[type], pathJson, v[typePKG]);
90 if (sd.pointXY == "0 0") {
91 return noNameType;
92 } else {
93 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "coldStart" + ".ftrace";
94 std::thread thGetTrace = sd.ThreadGetTrace("coldStart", traceName);
95 std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
96 sleep(1);
97 SPUtils::LoadCmd(cmd, cmdResult);
98 std::string pid = sd.GetPidByPkg(v[typePKG]);
99 thGetTrace.join();
100 std::string deviceType = sd.GetDeviceType();
101 float time = 0.0;
102 if (deviceType == " rk3568") {
103 time = parseTrace.ParseTraceCold(traceName, pid);
104 } else {
105 time = parseTrace.ParseTraceNoah(traceName, pid);
106 }
107 return time;
108 }
109 }
HotStart(std::vector<std::string> v)110 float EditorCommand::HotStart(std::vector<std::string> v)
111 {
112 OHOS::SmartPerf::StartUpDelay sd;
113 OHOS::SmartPerf::ParseTrace parseTrace;
114 std::string cmdResult;
115 int type = 4;
116 int typePKG = 3;
117 float noNameType = 5.0;
118 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.json", cmdResult);
119 SPUtils::LoadCmd("rm -rfv /data/local/tmp/*.ftrace", cmdResult);
120 SPUtils::LoadCmd("uitest dumpLayout", cmdResult);
121 sleep(1);
122 size_t position = cmdResult.find(":");
123 std::string pathJson = cmdResult.substr(position + 1);
124 sd.InitXY2(v[type], pathJson, v[typePKG]);
125 if (sd.pointXY == "0 0") {
126 return noNameType;
127 } else {
128 std::string cmd = "uinput -T -d " + sd.pointXY + " -u " + sd.pointXY;
129 sleep(1);
130 SPUtils::LoadCmd(cmd, cmdResult);
131 sleep(1);
132 SPUtils::LoadCmd("uinput -T -m 600 2760 600 1300 200", cmdResult);
133 sleep(1);
134 std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "hotStart" + ".ftrace";
135 std::thread thGetTrace = sd.ThreadGetTrace("hotStart", traceName);
136 SPUtils::LoadCmd(cmd, cmdResult);
137 std::string pid = sd.GetPidByPkg(v[typePKG]);
138 thGetTrace.join();
139 std::string deviceType = sd.GetDeviceType();
140 float time = 0.0;
141 if (deviceType == " rk3568") {
142 time = parseTrace.ParseTraceCold(traceName, pid);
143 } else {
144 time = parseTrace.ParseTraceNoah(traceName, pid);
145 }
146 return time;
147 }
148 }
149 }
150 }
151