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 #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 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
41 createResult_ = value;
42 }
43
GetCreateResult() const44 bool PluginServiceStub::GetCreateResult() const
45 {
46 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, createResult_);
47 return createResult_;
48 }
49
SetStartResult(bool value)50 void PluginServiceStub::SetStartResult(bool value)
51 {
52 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
53 startResult_ = value;
54 }
55
GetStartResult() const56 bool PluginServiceStub::GetStartResult() const
57 {
58 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, startResult_);
59 return startResult_;
60 }
61
SetStopResult(bool value)62 void PluginServiceStub::SetStopResult(bool value)
63 {
64 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
65 stopResult_ = value;
66 }
67
GetStopResult() const68 bool PluginServiceStub::GetStopResult() const
69 {
70 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, stopResult_);
71 return stopResult_;
72 }
73
SetDestroyResult(bool value)74 void PluginServiceStub::SetDestroyResult(bool value)
75 {
76 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
77 destroyResult_ = value;
78 }
79
GetDestroyResult() const80 bool PluginServiceStub::GetDestroyResult() const
81 {
82 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, destroyResult_);
83 return destroyResult_;
84 }
85
SetAddResult(bool value)86 void PluginServiceStub::SetAddResult(bool value)
87 {
88 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
89 addResult_ = value;
90 }
91
GetAddResult()92 bool PluginServiceStub::GetAddResult()
93 {
94 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, addResult_);
95 return addResult_;
96 }
97
SetRemoveResult(bool value)98 void PluginServiceStub::SetRemoveResult(bool value)
99 {
100 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, value);
101 removeResult_ = value;
102 }
103
GetRemoveResult()104 bool PluginServiceStub::GetRemoveResult()
105 {
106 HILOG_DEBUG(LOG_CORE, "%s(%d)", __FUNCTION__, removeResult_);
107 return removeResult_;
108 }
109 #endif
110
PluginService()111 PluginService::PluginService()
112 {
113 pluginIdCounter_ = 0;
114 serviceEntry_ = NULL;
115 pluginServiceImpl_ = NULL;
116 pluginCommandBuilder_ = NULL;
117 }
118
~PluginService()119 PluginService::~PluginService() {}
120
SetPluginSessionManager(const PluginSessionManagerPtr & pluginSessionManager)121 void PluginService::SetPluginSessionManager(const PluginSessionManagerPtr& pluginSessionManager)
122 {
123 pluginSessionManager_ = pluginSessionManager;
124 }
125
CreatePluginSession(const ProfilerPluginConfig & pluginConfig,const ProfilerDataRepeaterPtr & dataRepeater)126 bool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
127 const ProfilerDataRepeaterPtr& dataRepeater)
128 {
129 uint32_t pluginId = 0;
130 PluginContextPtr pluginCtx = nullptr;
131 std::string pluginName = pluginConfig.name();
132 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
133 CHECK_NOTNULL(pluginCtx, false, "create PluginContext failed!");
134
135 pluginCtx->profilerDataRepeater = dataRepeater;
136 pluginCtx->shareMemoryBlock = nullptr;
137
138 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
139
140 HILOG_DEBUG(LOG_CORE, "CreatePluginSession %s done!", pluginName.c_str());
141 return true;
142 }
143
CreatePluginSession(const ProfilerPluginConfig & pluginConfig,const ProfilerSessionConfig::BufferConfig & bufferConfig,const ProfilerDataRepeaterPtr & dataRepeater)144 bool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
145 const ProfilerSessionConfig::BufferConfig& bufferConfig,
146 const ProfilerDataRepeaterPtr& dataRepeater)
147 {
148 return CreatePluginSession(pluginConfig, dataRepeater);
149 }
150
StartPluginSession(const ProfilerPluginConfig & pluginConfig)151 bool PluginService::StartPluginSession(const ProfilerPluginConfig& pluginConfig)
152 {
153 uint32_t pluginId = 0;
154 PluginContextPtr pluginCtx = nullptr;
155 std::string pluginName = pluginConfig.name();
156 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
157
158 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::IN_SESSION);
159 HILOG_INFO(LOG_CORE, "StartPluginSession %s done!", pluginName.c_str());
160 return true;
161 }
162
StopPluginSession(const std::string & pluginName)163 bool PluginService::StopPluginSession(const std::string& pluginName)
164 {
165 uint32_t pluginId = 0;
166 PluginContextPtr pluginCtx = nullptr;
167 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
168
169 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
170
171 HILOG_DEBUG(LOG_CORE, "StopPluginSession %s done!", pluginName.c_str());
172 return true;
173 }
174
DestroyPluginSession(const std::string & pluginName)175 bool PluginService::DestroyPluginSession(const std::string& pluginName)
176 {
177 uint32_t pluginId = 0;
178 PluginContextPtr pluginCtx = nullptr;
179 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
180
181 if (pluginCtx->shareMemoryBlock) {
182 pluginCtx->shareMemoryBlock = nullptr;
183 }
184
185 if (pluginCtx->eventNotifier) {
186 eventPoller_->RemoveFileDescriptor(pluginCtx->eventNotifier->GetFd());
187 pluginCtx->eventNotifier = nullptr;
188 }
189
190 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
191 HILOG_INFO(LOG_CORE, "DestroyPluginSession %s done!", pluginName.c_str());
192 return true;
193 }
194
GetPluginContext(const std::string & pluginName)195 std::pair<uint32_t, PluginContextPtr> PluginService::GetPluginContext(const std::string& pluginName)
196 {
197 std::unique_lock<std::mutex> lock(mutex_);
198 CHECK_TRUE(nameIndex_.count(pluginName) > 0, std::make_pair(0, nullptr),
199 "GetPluginContext failed, plugin name `%s` not found!", pluginName.c_str());
200 uint32_t id = nameIndex_[pluginName];
201
202 CHECK_TRUE(pluginContext_.count(id) > 0, std::make_pair(id, nullptr), "plugin id %u not found!", id);
203 return std::make_pair(id, pluginContext_[id]);
204 }
205
GetPluginContextById(uint32_t id)206 PluginContextPtr PluginService::GetPluginContextById(uint32_t id)
207 {
208 std::unique_lock<std::mutex> lock(mutex_);
209 CHECK_TRUE(pluginContext_.count(id) > 0, nullptr, "plugin id %u not found!", id);
210 return pluginContext_[id];
211 }
212
AddPluginInfo(const PluginInfo & pluginInfo)213 bool PluginService::AddPluginInfo(const PluginInfo& pluginInfo)
214 {
215 if (nameIndex_.find(pluginInfo.name) == nameIndex_.end()) { // add new plugin
216 auto pluginCtx = std::make_shared<PluginContext>();
217 CHECK_NOTNULL(pluginCtx, false, "create PluginContext failed!");
218
219 ProfilerPluginCapability capability;
220 capability.set_path(pluginInfo.path);
221 capability.set_name(pluginInfo.name);
222 CHECK_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(capability), false,
223 "AddPluginInfo AddCapability FAIL");
224
225 pluginCtx->name = pluginInfo.name;
226 pluginCtx->path = pluginInfo.path;
227 pluginCtx->context = pluginInfo.context;
228 pluginCtx->config.set_name(pluginInfo.name);
229 pluginCtx->config.set_plugin_sha256(pluginInfo.sha256);
230 pluginCtx->profilerPluginState.set_name(pluginInfo.name);
231 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
232 pluginCtx->sha256 = pluginInfo.sha256;
233 pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
234
235 uint32_t pluginId = ++pluginIdCounter_;
236 std::unique_lock<std::mutex> lock(mutex_);
237 pluginContext_[pluginId] = pluginCtx;
238 nameIndex_[pluginInfo.name] = pluginId;
239 } else { // update sha256 or bufferSizeHint
240 std::unique_lock<std::mutex> lock(mutex_);
241 CHECK_TRUE(nameIndex_.count(pluginInfo.name) > 0, false, "plugin name %s not found!", pluginInfo.name.c_str());
242
243 uint32_t pluginId = nameIndex_[pluginInfo.name];
244 CHECK_TRUE(pluginContext_.count(pluginId) > 0, false, "plugin id %u not found!", pluginId);
245 auto pluginCtx = pluginContext_[pluginId];
246
247 if (pluginInfo.sha256 != "") {
248 pluginCtx->sha256 = pluginInfo.sha256;
249 }
250 if (pluginInfo.bufferSizeHint != 0) {
251 pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
252 }
253 }
254 HILOG_DEBUG(LOG_CORE, "AddPluginInfo for %s done!", pluginInfo.name.c_str());
255
256 return true;
257 }
258
GetPluginInfo(const std::string & pluginName,PluginInfo & pluginInfo)259 bool PluginService::GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo)
260 {
261 uint32_t pluginId = 0;
262 PluginContextPtr pluginCtx = nullptr;
263 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
264 CHECK_TRUE(pluginId, false, "plugin name %s not found!", pluginName.c_str());
265 CHECK_TRUE(pluginCtx, false, "plugin id %u not found!", pluginId);
266
267 pluginInfo.id = pluginId;
268 pluginInfo.name = pluginCtx->name;
269 pluginInfo.path = pluginCtx->path;
270 pluginInfo.sha256 = pluginCtx->sha256;
271 pluginInfo.bufferSizeHint = pluginCtx->bufferSizeHint;
272 return true;
273 }
274
RemovePluginInfo(const PluginInfo & pluginInfo)275 bool PluginService::RemovePluginInfo(const PluginInfo& pluginInfo)
276 {
277 uint32_t pluginId = pluginInfo.id;
278 PluginContextPtr pluginCtx = GetPluginContextById(pluginId);
279 CHECK_NOTNULL(pluginCtx, false, "RemovePluginInfo failed, id %d not found!", pluginId);
280
281 std::string pluginName = pluginCtx->config.name();
282 CHECK_TRUE(ProfilerCapabilityManager::GetInstance().RemoveCapability(pluginName), false,
283 "RemovePluginInfo RemoveCapability FAIL %d", pluginId);
284
285 std::unique_lock<std::mutex> lock(mutex_);
286 nameIndex_.erase(pluginName);
287 pluginContext_.erase(pluginId);
288 HILOG_DEBUG(LOG_CORE, "RemovePluginInfo for %s done!", pluginName.c_str());
289 return true;
290 }
291