• 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 <thread>
16 #include <cstdio>
17 #include <ios>
18 #include <vector>
19 #include <iostream>
20 #include <fstream>
21 #include <sstream>
22 #include <regex>
23 #include <sys/wait.h>
24 #include <sys/types.h>
25 #include "unistd.h"
26 #include "include/startup_delay.h"
27 #include "include/sp_utils.h"
28 #include "unistd.h"
29 #include "include/sp_log.h"
30 #include "include/common.h"
31 
32 namespace OHOS {
33 namespace SmartPerf {
StartUpDelay()34 StartUpDelay::StartUpDelay() {}
~StartUpDelay()35 StartUpDelay::~StartUpDelay() {}
GetTrace(const std::string & traceName) const36 void StartUpDelay::GetTrace(const std::string &traceName) const
37 {
38     std::string result;
39     std::string cmdString;
40     if (SPUtils::IsHmKernel()) {
41         cmdString = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_1024);
42     } else {
43         cmdString = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_2048);
44     }
45     SPUtils::LoadCmd(cmdString + traceName, result);
46     if (result.find("OpenRecording failed") != std::string::npos) {
47         std::string str;
48         std::string traceFinishStr = "hitrace --trace_finish";
49         SPUtils::LoadCmd(traceFinishStr, str);
50         SPUtils::LoadCmd(cmdString + traceName, result);
51     }
52 }
53 
GetHisysId() const54 void StartUpDelay::GetHisysId() const
55 {
56     int time = 10;
57     sleep(time);
58     std::string str = "";
59     std::string cmd = HISYSEVENT_CMD_MAP.at(HisyseventCmd::HISYSEVENT);
60     SPUtils::LoadCmd(cmd, str);
61     std::stringstream ss(str);
62     std::string line = "";
63     getline(ss, line);
64     std::stringstream ssLine(line);
65     std::string word = "";
66     std::string secondStr;
67     int count = 0;
68     int num = 2;
69     while (ssLine >> word) {
70         count++;
71         if (count == num) {
72             secondStr = word;
73             break;
74         }
75     }
76     std::string killCmd = CMD_COMMAND_MAP.at(CmdCommand::KILL_CMD);
77     SPUtils::LoadCmd(killCmd + secondStr, str);
78 }
79 
GetHisysIdAndKill() const80 void StartUpDelay::GetHisysIdAndKill() const
81 {
82     int time = 10;
83     sleep(time);
84     std::string str = "";
85     std::string cmd = HISYSEVENT_CMD_MAP.at(HisyseventCmd::HISYS_PID);
86     SPUtils::LoadCmd(cmd, str);
87     std::stringstream ss(str);
88     std::vector<std::string> hisysIdVec;
89     std::string singleId;
90     while (ss >> singleId) {
91         hisysIdVec.push_back(singleId);
92     }
93     std::string killCmd = CMD_COMMAND_MAP.at(CmdCommand::KILL_CMD);
94     for (size_t i = 0; i < hisysIdVec.size(); i++) {
95         SPUtils::LoadCmd(killCmd + hisysIdVec[i], str);
96     }
97 }
GetSpTcp()98 bool StartUpDelay::GetSpTcp()
99 {
100     std::string resultPid;
101     std::string str;
102     std::string cmd = CMD_COMMAND_MAP.at(CmdCommand::PIDOF_SP);
103     SPUtils::LoadCmd(cmd, resultPid);
104     std::vector<std::string> vec;
105     std::string token;
106     size_t pos = 0;
107     while ((pos = resultPid.find(' ')) != std::string::npos) {
108         token = resultPid.substr(0, pos);
109         vec.push_back(token);
110         resultPid.erase(0, pos + 1);
111     }
112     if (vec.size() > 0) {
113         std::string killCmd = CMD_COMMAND_MAP.at(CmdCommand::KILL_CMD);
114         for (size_t i = 0; i < vec.size(); i++) {
115             SPUtils::LoadCmd(killCmd + vec[i], str);
116         }
117     }
118     return false;
119 }
120 
GetSpClear() const121 bool StartUpDelay::GetSpClear() const
122 {
123     std::string resultPid;
124     std::string str;
125     std::string cmd = CMD_COMMAND_MAP.at(CmdCommand::PIDOF_SP);
126     SPUtils::LoadCmd("pidof SP_daemon", resultPid);
127     std::string token;
128     std::string curPid = std::to_string(getpid());
129     std::stringstream ss(resultPid);
130     std::string killCmd = CMD_COMMAND_MAP.at(CmdCommand::KILL_CMD);
131     while (ss >> token) {
132         if (token != curPid) {
133             SPUtils::LoadCmd("kill " + token, str);
134         }
135     }
136 
137     return false;
138 }
139 
ClearOldServer() const140 void StartUpDelay::ClearOldServer() const
141 {
142     std::string curPid = std::to_string(getpid());
143     std::string commandServer = CMD_COMMAND_MAP.at(CmdCommand::SERVER_GREP);
144     std::string resultPidServer;
145     std::string commandEditorServer = CMD_COMMAND_MAP.at(CmdCommand::EDITOR_SERVER_GREP);
146     std::string resultPidEditorServer;
147 
148     SPUtils::LoadCmdWithLinkBreak(commandServer, false, resultPidServer);
149     SPUtils::LoadCmdWithLinkBreak(commandEditorServer, false, resultPidEditorServer);
150 
151     std::istringstream iss(resultPidServer + '\n' + resultPidEditorServer);
152     std::string resultLine;
153     std::string killResult;
154     std::string killCmd = CMD_COMMAND_MAP.at(CmdCommand::KILL_CMD);
155     while (std::getline(iss, resultLine)) {
156         if (resultLine.empty() || resultLine.find("sh -c") != std::string::npos) {
157             continue;
158         }
159 
160         std::istringstream lineStream(resultLine);
161         std::string token;
162 
163         int count = 0;
164         while (lineStream >> token) {
165             if (count == 1) {
166                 break;
167             }
168             count++;
169         }
170 
171         if (token != curPid) {
172             SPUtils::LoadCmd(killCmd + token, killResult);
173             LOGI("Find old server: %s, killed.", token.c_str());
174         }
175     }
176 }
177 
GetPidByPkg(const std::string & curPkgName)178 std::string StartUpDelay::GetPidByPkg(const std::string &curPkgName)
179 {
180     std::string resultPid = "";
181     std::string resultProcId = "";
182     const char* args[] = { "pidof", curPkgName.c_str(), nullptr };
183     int pipefd[2];
184     if (pipe(pipefd) == -1) {
185         LOGE("startup_delay::Failed to create pipe");
186         return resultPid;
187     }
188     pid_t pid = fork();
189     if (pid == -1) {
190         LOGE("startup_delay::Failed to fork");
191         return resultPid;
192     } else if (pid == 0) {
193         close(pipefd[0]);
194         dup2(pipefd[1], STDOUT_FILENO);
195         close(pipefd[1]);
196         if (execvp(args[0], const_cast<char *const*>(args)) == -1) {
197             LOGE("startup_delay::Failed to execute pid");
198             return resultPid;
199         }
200     } else {
201         close(pipefd[1]);
202         char buf[1024];
203         ssize_t nread = read(pipefd[0], buf, sizeof(buf));
204         if (nread == -1) {
205             return resultPid;
206         }
207         resultPid = std::string(buf, nread);
208         resultProcId = resultPid.substr(0, resultPid.size() - 1);
209         close(pipefd[0]);
210         waitpid(pid, nullptr, 0);
211     }
212     return resultProcId;
213 }
214 }
215 }
216