1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 #define LOG_TAG "PluginServiceStub"
16 #include "plugin_service_stubs.h"
17
18 #include <map>
19 #include <memory>
20 #include "logging.h"
21 #include "profiler_capability_manager.h"
22 #include "profiler_data_repeater.h"
23
24 #ifdef USE_PLUGIN_SERVICE_STUB
25 using PluginServiceStubPtr = STD_PTR(shared, PluginServiceStub);
GetInstance()26 PluginServiceStubPtr PluginServiceStub::GetInstance()
27 {
28 static std::weak_ptr<PluginServiceStub> instance;
29 auto stub = instance.lock();
30 if (stub) {
31 return stub;
32 }
33 stub = std::make_shared<PluginServiceStub>();
34 instance = stub;
35 return stub;
36 }
37
SetCreateResult(bool value)38 void PluginServiceStub::SetCreateResult(bool value)
39 {
40 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
41 createResult_ = value;
42 }
43
GetCreateResult() const44 bool PluginServiceStub::GetCreateResult() const
45 {
46 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, createResult_);
47 return createResult_;
48 }
49
SetStartResult(bool value)50 void PluginServiceStub::SetStartResult(bool value)
51 {
52 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
53 startResult_ = value;
54 }
55
GetStartResult() const56 bool PluginServiceStub::GetStartResult() const
57 {
58 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, startResult_);
59 return startResult_;
60 }
61
SetStopResult(bool value)62 void PluginServiceStub::SetStopResult(bool value)
63 {
64 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
65 stopResult_ = value;
66 }
67
GetStopResult() const68 bool PluginServiceStub::GetStopResult() const
69 {
70 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, stopResult_);
71 return stopResult_;
72 }
73
SetDestroyResult(bool value)74 void PluginServiceStub::SetDestroyResult(bool value)
75 {
76 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
77 destroyResult_ = value;
78 }
79
GetDestroyResult() const80 bool PluginServiceStub::GetDestroyResult() const
81 {
82 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, destroyResult_);
83 return destroyResult_;
84 }
85
SetAddResult(bool value)86 void PluginServiceStub::SetAddResult(bool value)
87 {
88 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
89 addResult_ = value;
90 }
91
GetAddResult()92 bool PluginServiceStub::GetAddResult()
93 {
94 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, addResult_);
95 return addResult_;
96 }
97
SetRemoveResult(bool value)98 void PluginServiceStub::SetRemoveResult(bool value)
99 {
100 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
101 removeResult_ = value;
102 }
103
GetRemoveResult()104 bool PluginServiceStub::GetRemoveResult()
105 {
106 PROFILER_LOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, removeResult_);
107 return removeResult_;
108 }
109 #endif
110
PluginService()111 PluginService::PluginService()
112 {
113 pluginIdCounter_ = 0;
114 serviceEntry_ = nullptr;
115 pluginServiceImpl_ = nullptr;
116 pluginCommandBuilder_ = nullptr;
117 const int pollingInterval = 5000;
118 eventPoller_ = std::make_unique<EpollEventPoller>(pollingInterval);
119 eventPoller_->Init();
120 }
121
~PluginService()122 PluginService::~PluginService()
123 {
124 if (eventPoller_) {
125 eventPoller_->Stop();
126 eventPoller_->Finalize();
127 }
128 }
129
SetPluginSessionManager(const PluginSessionManagerPtr & pluginSessionManager)130 void PluginService::SetPluginSessionManager(const PluginSessionManagerPtr& pluginSessionManager)
131 {
132 pluginSessionManager_ = pluginSessionManager;
133 }
134
SetProfilerSessionConfig(const std::shared_ptr<ProfilerSessionConfig> profilerSessionConfig,const std::vector<std::string> & pluginNames)135 void PluginService::SetProfilerSessionConfig(const std::shared_ptr<ProfilerSessionConfig> profilerSessionConfig,
136 const std::vector<std::string>& pluginNames)
137 {
138 for (const std::string& name : pluginNames) {
139 profilerSessionConfigs_[name] = profilerSessionConfig;
140 }
141 }
142
CreatePluginSession(const ProfilerPluginConfig & pluginConfig,const ProfilerDataRepeaterPtr & dataRepeater,const ProfilerStateRepeaterPtr & stateRepeater)143 bool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
144 const ProfilerDataRepeaterPtr& dataRepeater,
145 const ProfilerStateRepeaterPtr& stateRepeater)
146 {
147 uint32_t pluginId = 0;
148 PluginContextPtr pluginCtx = nullptr;
149 std::string pluginName = pluginConfig.name();
150 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
151 CHECK_NOTNULL(pluginCtx, false, "create PluginContext failed!");
152
153 pluginCtx->profilerDataRepeater = dataRepeater;
154 pluginCtx->profilerStateRepeater = stateRepeater;
155 pluginCtx->shareMemoryBlock = nullptr;
156
157 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
158
159 PROFILER_LOG_DEBUG(LOG_CORE, "CreatePluginSession %s done!", pluginName.c_str());
160 return true;
161 }
162
CreatePluginSession(const ProfilerPluginConfig & pluginConfig,const ProfilerSessionConfig::BufferConfig & bufferConfig,const ProfilerDataRepeaterPtr & dataRepeater,const ProfilerStateRepeaterPtr & stateRepeater)163 bool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
164 const ProfilerSessionConfig::BufferConfig& bufferConfig,
165 const ProfilerDataRepeaterPtr& dataRepeater,
166 const ProfilerStateRepeaterPtr& stateRepeater)
167 {
168 return CreatePluginSession(pluginConfig, dataRepeater, stateRepeater);
169 }
170
StartPluginSession(const ProfilerPluginConfig & pluginConfig)171 bool PluginService::StartPluginSession(const ProfilerPluginConfig& pluginConfig)
172 {
173 uint32_t pluginId = 0;
174 PluginContextPtr pluginCtx = nullptr;
175 std::string pluginName = pluginConfig.name();
176 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
177
178 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::IN_SESSION);
179 PROFILER_LOG_INFO(LOG_CORE, "StartPluginSession %s done!", pluginName.c_str());
180 return true;
181 }
182
StopPluginSession(const std::string & pluginName)183 bool PluginService::StopPluginSession(const std::string& pluginName)
184 {
185 uint32_t pluginId = 0;
186 PluginContextPtr pluginCtx = nullptr;
187 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
188
189 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
190
191 PROFILER_LOG_DEBUG(LOG_CORE, "StopPluginSession %s done!", pluginName.c_str());
192 return true;
193 }
194
DestroyPluginSession(const std::string & pluginName)195 bool PluginService::DestroyPluginSession(const std::string& pluginName)
196 {
197 uint32_t pluginId = 0;
198 PluginContextPtr pluginCtx = nullptr;
199 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
200
201 if (pluginCtx->shareMemoryBlock) {
202 pluginCtx->shareMemoryBlock = nullptr;
203 }
204
205 if (pluginCtx->eventNotifier) {
206 eventPoller_->RemoveFileDescriptor(pluginCtx->eventNotifier->GetFd());
207 pluginCtx->eventNotifier = nullptr;
208 }
209
210 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
211 PROFILER_LOG_INFO(LOG_CORE, "DestroyPluginSession %s done!", pluginName.c_str());
212 return true;
213 }
214
RefreshPluginSession(const std::string & pluginName)215 bool PluginService::RefreshPluginSession(const std::string& pluginName)
216 {
217 uint32_t pluginId = 0;
218 PluginContextPtr pluginCtx = nullptr;
219 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
220 PROFILER_LOG_INFO(LOG_CORE, "RefreshPluginSession %s done!", pluginName.c_str());
221 return true;
222 }
223
GetPluginContext(const std::string & pluginName)224 std::pair<uint32_t, PluginContextPtr> PluginService::GetPluginContext(const std::string& pluginName)
225 {
226 std::unique_lock<std::mutex> lock(mutex_);
227 CHECK_TRUE(nameIndex_.count(pluginName) > 0, std::make_pair(0, nullptr),
228 "GetPluginContext failed, plugin name `%s` not found!", pluginName.c_str());
229 uint32_t id = nameIndex_[pluginName];
230
231 CHECK_TRUE(pluginContext_.count(id) > 0, std::make_pair(id, nullptr), "plugin id %u not found!", id);
232 return std::make_pair(id, pluginContext_[id]);
233 }
234
GetPluginContextById(uint32_t id)235 PluginContextPtr PluginService::GetPluginContextById(uint32_t id)
236 {
237 std::unique_lock<std::mutex> lock(mutex_);
238 CHECK_TRUE(pluginContext_.count(id) > 0, nullptr, "plugin id %u not found!", id);
239 return pluginContext_[id];
240 }
241
AddPluginInfo(const PluginInfo & pluginInfo)242 bool PluginService::AddPluginInfo(const PluginInfo& pluginInfo)
243 {
244 if (nameIndex_.find(pluginInfo.name) == nameIndex_.end()) { // add new plugin
245 auto pluginCtx = std::make_shared<PluginContext>();
246 CHECK_NOTNULL(pluginCtx, false, "create PluginContext failed!");
247
248 ProfilerPluginCapability capability;
249 capability.set_path(pluginInfo.path);
250 capability.set_name(pluginInfo.name);
251 CHECK_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(capability), false,
252 "AddPluginInfo AddCapability FAIL");
253
254 pluginCtx->name = pluginInfo.name;
255 pluginCtx->path = pluginInfo.path;
256 pluginCtx->context = pluginInfo.context;
257 pluginCtx->config.set_name(pluginInfo.name);
258 pluginCtx->config.set_plugin_sha256(pluginInfo.sha256);
259 pluginCtx->profilerPluginState.set_name(pluginInfo.name);
260 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
261 pluginCtx->sha256 = pluginInfo.sha256;
262 pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
263
264 uint32_t pluginId = ++pluginIdCounter_;
265 std::unique_lock<std::mutex> lock(mutex_);
266 pluginContext_[pluginId] = pluginCtx;
267 nameIndex_[pluginInfo.name] = pluginId;
268 } else { // update sha256 or bufferSizeHint
269 std::unique_lock<std::mutex> lock(mutex_);
270 CHECK_TRUE(nameIndex_.count(pluginInfo.name) > 0, false, "plugin name %s not found!", pluginInfo.name.c_str());
271
272 uint32_t pluginId = nameIndex_[pluginInfo.name];
273 CHECK_TRUE(pluginContext_.count(pluginId) > 0, false, "plugin id %u not found!", pluginId);
274 auto pluginCtx = pluginContext_[pluginId];
275
276 if (pluginInfo.sha256 != "") {
277 pluginCtx->sha256 = pluginInfo.sha256;
278 }
279 if (pluginInfo.bufferSizeHint != 0) {
280 pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
281 }
282 }
283 PROFILER_LOG_DEBUG(LOG_CORE, "AddPluginInfo for %s done!", pluginInfo.name.c_str());
284
285 return true;
286 }
287
GetPluginInfo(const std::string & pluginName,PluginInfo & pluginInfo)288 bool PluginService::GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo)
289 {
290 uint32_t pluginId = 0;
291 PluginContextPtr pluginCtx = nullptr;
292 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
293 CHECK_TRUE(pluginId, false, "plugin name %s not found!", pluginName.c_str());
294 CHECK_TRUE(pluginCtx, false, "plugin id %u not found!", pluginId);
295
296 pluginInfo.id = pluginId;
297 pluginInfo.name = pluginCtx->name;
298 pluginInfo.path = pluginCtx->path;
299 pluginInfo.sha256 = pluginCtx->sha256;
300 pluginInfo.bufferSizeHint = pluginCtx->bufferSizeHint;
301 return true;
302 }
303
RemovePluginInfo(const PluginInfo & pluginInfo)304 bool PluginService::RemovePluginInfo(const PluginInfo& pluginInfo)
305 {
306 uint32_t pluginId = pluginInfo.id;
307 PluginContextPtr pluginCtx = GetPluginContextById(pluginId);
308 CHECK_NOTNULL(pluginCtx, false, "RemovePluginInfo failed, id %d not found!", pluginId);
309
310 std::string pluginName = pluginCtx->config.name();
311 CHECK_TRUE(ProfilerCapabilityManager::GetInstance().RemoveCapability(pluginName), false,
312 "RemovePluginInfo RemoveCapability FAIL %d", pluginId);
313
314 std::unique_lock<std::mutex> lock(mutex_);
315 nameIndex_.erase(pluginName);
316 pluginContext_.erase(pluginId);
317 PROFILER_LOG_DEBUG(LOG_CORE, "RemovePluginInfo for %s done!", pluginName.c_str());
318 return true;
319 }
320
SetTraceWriter(const TraceFileWriterPtr & traceWriter)321 void PluginService::SetTraceWriter(const TraceFileWriterPtr& traceWriter)
322 {
323 traceWriter_ = traceWriter;
324 }
325
StartEpollThread()326 bool PluginService::StartEpollThread()
327 {
328 if (eventPoller_) {
329 return eventPoller_->Start();
330 }
331 return false;
332 }
333
StopEpollThread()334 bool PluginService::StopEpollThread()
335 {
336 if (eventPoller_) {
337 return eventPoller_->Stop();
338 }
339 return false;
340 }
341
FlushAllData(const std::string & pluginName)342 void PluginService::FlushAllData(const std::string& pluginName)
343 {
344 PROFILER_LOG_INFO(LOG_CORE, "FlushAllData for %s", pluginName.c_str());
345 }