• 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 <cstdio>
16 #include <thread>
17 #include <cstring>
18 #include <iterator>
19 #include "include/GameEvent.h"
20 #include "unistd.h"
21 #include "include/heartbeat.h"
22 #include "include/sp_utils.h"
23 #include "include/sp_csv_util.h"
24 #include "include/sp_profiler_factory.h"
25 #include "include/sp_thread_socket.h"
26 #include "include/startup_delay.h"
27 #include "include/ByTrace.h"
28 #include "include/smartperf_command.h"
29 #include "include/sp_log.h"
30 #include "include/RAM.h"
31 #include "include/common.h"
32 #include "include/FPS.h"
33 #include "include/sp_task.h"
34 #include "cpu_info.h"
35 #include "AI_schedule.h"
36 
37 namespace OHOS {
38 namespace SmartPerf {
SmartPerfCommand(std::vector<std::string> & argv)39 SmartPerfCommand::SmartPerfCommand(std::vector<std::string>& argv)
40 {
41     taskMgr_.AddTask(argv);
42     LOGD("SmartPerfCommand::SmartPerfCommand size(%u)", argv.size());
43     if (argv.size() == oneParam) {
44         SPUtils::KillStartDaemon();
45         SpThreadSocket::GetInstance().SetNeedUdpToken(false);
46         DeviceServer(true);
47     }
48     if (argv.size() == twoParam) {
49         auto iterator = COMMAND_HELP_MAP.begin();
50         while (iterator != COMMAND_HELP_MAP.end()) {
51             if (iterator->second.compare(argv[1]) == 0) {
52                 HelpCommand(iterator->first, "");
53                 break;
54             }
55             if (argv[1].find("-editorServer") != std::string::npos) {
56                 WLOGI("############################# Found '-editorServer' argument in argv");
57                 std::string token = argv[1].substr(serverCommandLength, argv[1].length() - serverCommandLength);
58                 SPUtils::KillStartDaemon();
59                 HelpCommand(CommandHelp::EDITORSERVER, token);
60             } else if (argv[1].find("-deviceServer") != std::string::npos) {
61                 WLOGI("############################# Found '-deviceServer' argument in argv");
62                 std::string token = argv[1].substr(serverCommandLength, argv[1].length() - serverCommandLength);
63                 HelpCommand(CommandHelp::DEVICESERVER, token);
64             }
65             ++iterator;
66         }
67     }
68     LOGD("SmartPerfCommand::SmartPerfCommand complete");
69 }
DeviceServer(int isNeedDaemon) const70 void SmartPerfCommand::DeviceServer(int isNeedDaemon) const
71 {
72     EnableWriteLogAndDeleteOldLogFiles();
73     OHOS::SmartPerf::StartUpDelay sd;
74     sd.KillSpProcess();
75     std::string pidStr = sd.GetPidByPkg("SP_daemon");
76     std::string cmdStr = CMD_COMMAND_MAP.at(CmdCommand::TASKSET);
77     std::string result = "";
78     SPUtils::LoadCmd(cmdStr + pidStr, result);
79     if (isNeedDaemon) {
80         daemon(0, 0);
81     }
82     CreateSocketThread();
83 }
HelpCommand(CommandHelp type,const std::string & token) const84 void SmartPerfCommand::HelpCommand(CommandHelp type, const std::string& token) const
85 {
86     LOGD("SmartPerfCommand::HelpCommand  type(%d)", type);
87     if (type == CommandHelp::HELP) {
88         std::cout << smartPerfMsg << std::endl;
89     }
90     if (type == CommandHelp::VERSION) {
91         std::cout << "Version: " << SPUtils::GetVersion() << std::endl;
92     }
93     if (type == CommandHelp::SCREEN) {
94         std::string result = SPUtils::GetScreen();
95         std::cout << result << std::endl;
96     }
97     OHOS::SmartPerf::StartUpDelay sd;
98     if (type == CommandHelp::CLEAR || type == CommandHelp::CLEARALL) {
99         bool isClearTestServer = (type == CommandHelp::CLEARALL);
100         sd.GetSpClear(isClearTestServer);
101     }
102     if (type == CommandHelp::SERVER || type == CommandHelp::EDITORSERVER) {
103         sd.ClearOldServer();
104         SPUtils::GetTtyDeviceFd();
105         std::string pidStr = sd.GetPidByPkg("SP_daemon");
106         std::string cmdStr = CMD_COMMAND_MAP.at(CmdCommand::TASKSET);
107         std::string result = "";
108         SPUtils::LoadCmd(cmdStr + pidStr, result);
109         if (type == CommandHelp::SERVER) {
110             daemon(0, 0);
111         } else {
112             // Editor 拉起 daemon 测试
113             EnableWriteLogAndDeleteOldLogFiles();
114             if (token.empty()) {
115                 WLOGE("Error: token is empty when setting TCP token.");
116                 return;
117             }
118             SpThreadSocket::GetInstance().SetToken(token);
119             WLOGI("############################# EditorServer Socket Create Start, Ready to Start Collector...");
120         }
121         CreateSocketThread();
122     }
123     if (type == CommandHelp::DEVICESERVER) {
124         // device 拉起 daemon 测试
125         if (token.empty()) {
126             WLOGE("Error: token is empty when setting UDP token.");
127             return;
128         }
129         SpThreadSocket::GetInstance().SetToken(token);
130         DeviceServer(false);
131         WLOGI("############################# DeviceServer Socket Create Start, Ready to Start Collector...");
132     }
133 }
134 
CreateSocketThread() const135 void SmartPerfCommand::CreateSocketThread() const
136 {
137     InitSomething();
138     auto tcpSocket = std::thread([]() { SpThreadSocket::GetInstance().Process(ProtoType::TCP); });
139     sleep(1);
140     auto udpSocket = std::thread([]() { SpThreadSocket::GetInstance().Process(ProtoType::UDP); });
141     sleep(1);
142     auto udpexSocket = std::thread([]() { SpThreadSocket::GetInstance().Process(ProtoType::UDPEX); });
143     Heartbeat::GetInstance().UpdatestartTime();
144     std::thread([]() { Heartbeat::GetInstance().HeartbeatRule(); }).detach();
145     tcpSocket.join();
146     udpSocket.join();
147     udpexSocket.join();
148 }
149 
ExecCommand()150 std::string SmartPerfCommand::ExecCommand()
151 {
152     if (taskMgr_.GetArgumentParser().Get("-N") == std::nullopt) {
153         return "command exec finished!";
154     }
155     RAM &ram = RAM::GetInstance();
156     ram.SetFirstFlag();
157     taskMgr_.DeleteTask(&AISchedule::GetInstance());
158     taskMgr_.InitDataCsv();
159     taskMgr_.Start();
160     taskMgr_.Wait();
161     taskMgr_.Stop();
162     taskMgr_.WriteToCSV();
163     return std::string("command exec finished!");
164 }
165 
InitSomething()166 void SmartPerfCommand::InitSomething()
167 {
168     std::string cmdResult;
169     std::string stat = CMD_COMMAND_MAP.at(CmdCommand::PROC_STAT);
170     if (SPUtils::LoadCmd(stat, cmdResult)) {
171         LOGE("SmartPerfCommand::InitSomething Privilege escalation!");
172     };
173 }
174 }
175 }
176