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 "hiview_service.h"
16
17 #include <memory>
18 #include <vector>
19 #include <string>
20 #include <cinttypes>
21 #include <cstdio>
22
23 #include "logger.h"
24 #include "hiview_service_adapter.h"
25 #include "hiview_platform.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 DEFINE_LOG_TAG("HiView-Service");
30 namespace {
31 constexpr int MIN_SUPPORT_CMD_SIZE = 1;
32 }
StartService()33 void HiviewService::StartService()
34 {
35 std::unique_ptr<HiviewServiceAdapter> adapter = std::make_unique<HiviewServiceAdapter>();
36 adapter->StartService(this);
37 }
38
DumpRequestDispatcher(int fd,const std::vector<std::string> & cmds)39 void HiviewService::DumpRequestDispatcher(int fd, const std::vector<std::string> &cmds)
40 {
41 if (fd < 0) {
42 HIVIEW_LOGW("invalid fd.");
43 return;
44 }
45
46 if (cmds.size() == 0) {
47 DumpLoadedPluginInfo(fd);
48 return;
49 }
50
51 // hidumper hiviewdfx -d
52 if ((cmds.size() == MIN_SUPPORT_CMD_SIZE) && (cmds[0] == "-d")) {
53 DumpDetailedInfo(fd);
54 return;
55 }
56
57 // hidumper hiviewdfx -p
58 if ((cmds.size() >= MIN_SUPPORT_CMD_SIZE) && (cmds[0] == "-p")) {
59 DumpPluginInfo(fd, cmds);
60 return;
61 }
62
63 PrintUsage(fd);
64 return;
65 }
66
DumpPluginInfo(int fd,const std::vector<std::string> & cmds) const67 void HiviewService::DumpPluginInfo(int fd, const std::vector<std::string> &cmds) const
68 {
69 std::string pluginName = "";
70 const int pluginNameSize = 2;
71 const int pluginNamePos = 1;
72 std::vector<std::string> newCmd;
73 if (cmds.size() >= pluginNameSize) {
74 pluginName = cmds[pluginNamePos];
75 newCmd.insert(newCmd.begin(), cmds.begin() + pluginNamePos, cmds.end());
76 }
77
78 auto &platform = HiviewPlatform::GetInstance();
79 auto const &curPluginMap = platform.GetPluginMap();
80 for (auto const &entry : curPluginMap) {
81 auto const &pluginPtr = entry.second;
82 if (pluginPtr == nullptr) {
83 continue;
84 }
85
86 if (pluginName.empty()) {
87 pluginPtr->Dump(fd, newCmd);
88 continue;
89 }
90
91 if (pluginPtr->GetName() == pluginName) {
92 pluginPtr->Dump(fd, newCmd);
93 break;
94 }
95 }
96 }
97
DumpDetailedInfo(int fd)98 void HiviewService::DumpDetailedInfo(int fd)
99 {
100 if (parser_ != nullptr) {
101 parser_.reset();
102 }
103 DumpLoadedPluginInfo(fd);
104 parser_ = std::make_unique<AuditLogParser>();
105 parser_->StartParse();
106 std::string timeScope = parser_->GetAuditLogTimeScope();
107 dprintf(fd, "%s\n", timeScope.c_str());
108 DumpPluginUsageInfo(fd);
109 DumpThreadUsageInfo(fd);
110 DumpPipelineUsageInfo(fd);
111 parser_.reset();
112 }
113
DumpLoadedPluginInfo(int fd) const114 void HiviewService::DumpLoadedPluginInfo(int fd) const
115 {
116 auto &platform = HiviewPlatform::GetInstance();
117 auto const &curPluginMap = platform.GetPluginMap();
118 dprintf(fd, "Current Loaded Plugins:\n");
119 for (auto const &entry : curPluginMap) {
120 auto const &pluginName = entry.first;
121 if (entry.second != nullptr) {
122 dprintf(fd, "PluginName:%s ", pluginName.c_str());
123 dprintf(fd, "IsDynamic:%s ",
124 (entry.second->GetType() == Plugin::PluginType::DYNAMIC) ? "True" : "False");
125 dprintf(fd, "Version:%s ", (entry.second->GetVersion().c_str()));
126 dprintf(fd,
127 "ThreadName:%s\n",
128 ((entry.second->GetWorkLoop() == nullptr) ? "Null" : entry.second->GetWorkLoop()->GetName().c_str()));
129 }
130 }
131 dprintf(fd, "Dump Plugin Loaded Info Done.\n\n");
132 }
133
DumpPluginUsageInfo(int fd)134 void HiviewService::DumpPluginUsageInfo(int fd)
135 {
136 auto &platform = HiviewPlatform::GetInstance();
137 auto const &curPluginMap = platform.GetPluginMap();
138 for (auto const &entry : curPluginMap) {
139 auto pluginName = entry.first;
140 if (entry.second != nullptr) {
141 DumpPluginUsageInfo(fd, pluginName);
142 }
143 }
144 }
DumpPluginUsageInfo(int fd,const std::string & pluginName) const145 void HiviewService::DumpPluginUsageInfo(int fd, const std::string &pluginName) const
146 {
147 if (parser_ == nullptr) {
148 return;
149 }
150 auto logList = parser_->GetPluginSummary(pluginName);
151 dprintf(fd, "Following events processed By Plugin %s:\n", pluginName.c_str());
152 for (auto &log : logList) {
153 dprintf(fd, " %s.\n", log.c_str());
154 }
155 dprintf(fd, "Dump Plugin Usage Done.\n\n");
156 }
157
DumpThreadUsageInfo(int fd) const158 void HiviewService::DumpThreadUsageInfo(int fd) const
159 {
160 auto &platform = HiviewPlatform::GetInstance();
161 auto const &curThreadMap = platform.GetWorkLoopMap();
162 dprintf(fd, "Start Dump ThreadInfo:\n");
163 for (auto const &entry : curThreadMap) {
164 if (entry.second != nullptr) {
165 std::string name = entry.second->GetName();
166 DumpThreadUsageInfo(fd, name);
167 }
168 }
169 dprintf(fd, "Dump ThreadInfo Done.\n\n");
170 }
171
DumpThreadUsageInfo(int fd,const std::string & threadName) const172 void HiviewService::DumpThreadUsageInfo(int fd, const std::string &threadName) const
173 {
174 if (parser_ == nullptr) {
175 return;
176 }
177 auto logList = parser_->GetThreadSummary(threadName);
178 dprintf(fd, "Following events processed on Thread %s:\n", threadName.c_str());
179 for (auto &log : logList) {
180 dprintf(fd, " %s.\n", log.c_str());
181 }
182 }
183
DumpPipelineUsageInfo(int fd) const184 void HiviewService::DumpPipelineUsageInfo(int fd) const
185 {
186 auto &platform = HiviewPlatform::GetInstance();
187 auto const &curPipelineMap = platform.GetPipelineMap();
188 dprintf(fd, "Start Dump Pipeline Info:\n");
189 for (auto const &entry : curPipelineMap) {
190 auto pipeline = entry.first;
191 DumpPipelineUsageInfo(fd, pipeline);
192 }
193 }
194
DumpPipelineUsageInfo(int fd,const std::string & pipelineName) const195 void HiviewService::DumpPipelineUsageInfo(int fd, const std::string &pipelineName) const
196 {
197 if (parser_ == nullptr) {
198 return;
199 }
200 auto logList = parser_->GetPipelineSummary(pipelineName);
201 dprintf(fd, "Following events processed on Pipeline %s:\n", pipelineName.c_str());
202 for (auto &log : logList) {
203 dprintf(fd, " %s.\n", log.c_str());
204 }
205 dprintf(fd, "Dump Pipeline Usage Info Done.\n\n");
206 }
207
PrintUsage(int fd) const208 void HiviewService::PrintUsage(int fd) const
209 {
210 dprintf(fd, "Hiview Plugin Platform dump options:\n");
211 dprintf(fd, "hidumper hiviewdfx [-d(etail)]\n");
212 dprintf(fd, " [-p(lugin) pluginName]\n");
213 }
214 } // namespace HiviewDFX
215 } // namespace OHOS
216