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_ = nullptr;
115 pluginServiceImpl_ = nullptr;
116 pluginCommandBuilder_ = nullptr;
117 }
118
~PluginService()119 PluginService::~PluginService() {}
120
SetPluginSessionManager(const PluginSessionManagerPtr & pluginSessionManager)121 void PluginService::SetPluginSessionManager(const PluginSessionManagerPtr& pluginSessionManager)
122 {
123 pluginSessionManager_ = pluginSessionManager;
124 }
125
SetProfilerSessionConfig(const ProfilerSessionConfig & profilerSessionConfig)126 void PluginService::SetProfilerSessionConfig(const ProfilerSessionConfig& profilerSessionConfig)
127 {
128 profilerSessionConfig_ = profilerSessionConfig;
129 }
130
CreatePluginSession(const ProfilerPluginConfig & pluginConfig,const ProfilerDataRepeaterPtr & dataRepeater)131 bool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
132 const ProfilerDataRepeaterPtr& dataRepeater)
133 {
134 uint32_t pluginId = 0;
135 PluginContextPtr pluginCtx = nullptr;
136 std::string pluginName = pluginConfig.name();
137 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
138 CHECK_NOTNULL(pluginCtx, false, "create PluginContext failed!");
139
140 pluginCtx->profilerDataRepeater = dataRepeater;
141 pluginCtx->shareMemoryBlock = nullptr;
142
143 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
144
145 HILOG_DEBUG(LOG_CORE, "CreatePluginSession %s done!", pluginName.c_str());
146 return true;
147 }
148
CreatePluginSession(const ProfilerPluginConfig & pluginConfig,const ProfilerSessionConfig::BufferConfig & bufferConfig,const ProfilerDataRepeaterPtr & dataRepeater)149 bool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
150 const ProfilerSessionConfig::BufferConfig& bufferConfig,
151 const ProfilerDataRepeaterPtr& dataRepeater)
152 {
153 return CreatePluginSession(pluginConfig, dataRepeater);
154 }
155
StartPluginSession(const ProfilerPluginConfig & pluginConfig)156 bool PluginService::StartPluginSession(const ProfilerPluginConfig& pluginConfig)
157 {
158 uint32_t pluginId = 0;
159 PluginContextPtr pluginCtx = nullptr;
160 std::string pluginName = pluginConfig.name();
161 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
162
163 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::IN_SESSION);
164 HILOG_INFO(LOG_CORE, "StartPluginSession %s done!", pluginName.c_str());
165 return true;
166 }
167
StopPluginSession(const std::string & pluginName)168 bool PluginService::StopPluginSession(const std::string& pluginName)
169 {
170 uint32_t pluginId = 0;
171 PluginContextPtr pluginCtx = nullptr;
172 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
173
174 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
175
176 HILOG_DEBUG(LOG_CORE, "StopPluginSession %s done!", pluginName.c_str());
177 return true;
178 }
179
DestroyPluginSession(const std::string & pluginName)180 bool PluginService::DestroyPluginSession(const std::string& pluginName)
181 {
182 uint32_t pluginId = 0;
183 PluginContextPtr pluginCtx = nullptr;
184 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
185
186 if (pluginCtx->shareMemoryBlock) {
187 pluginCtx->shareMemoryBlock = nullptr;
188 }
189
190 if (pluginCtx->eventNotifier) {
191 eventPoller_->RemoveFileDescriptor(pluginCtx->eventNotifier->GetFd());
192 pluginCtx->eventNotifier = nullptr;
193 }
194
195 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
196 HILOG_INFO(LOG_CORE, "DestroyPluginSession %s done!", pluginName.c_str());
197 return true;
198 }
199
RefreshPluginSession(const std::string & pluginName)200 bool PluginService::RefreshPluginSession(const std::string& pluginName)
201 {
202 uint32_t pluginId = 0;
203 PluginContextPtr pluginCtx = nullptr;
204 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
205 HILOG_INFO(LOG_CORE, "RefreshPluginSession %s done!", pluginName.c_str());
206 return true;
207 }
208
GetPluginContext(const std::string & pluginName)209 std::pair<uint32_t, PluginContextPtr> PluginService::GetPluginContext(const std::string& pluginName)
210 {
211 std::unique_lock<std::mutex> lock(mutex_);
212 CHECK_TRUE(nameIndex_.count(pluginName) > 0, std::make_pair(0, nullptr),
213 "GetPluginContext failed, plugin name `%s` not found!", pluginName.c_str());
214 uint32_t id = nameIndex_[pluginName];
215
216 CHECK_TRUE(pluginContext_.count(id) > 0, std::make_pair(id, nullptr), "plugin id %u not found!", id);
217 return std::make_pair(id, pluginContext_[id]);
218 }
219
GetPluginContextById(uint32_t id)220 PluginContextPtr PluginService::GetPluginContextById(uint32_t id)
221 {
222 std::unique_lock<std::mutex> lock(mutex_);
223 CHECK_TRUE(pluginContext_.count(id) > 0, nullptr, "plugin id %u not found!", id);
224 return pluginContext_[id];
225 }
226
AddPluginInfo(const PluginInfo & pluginInfo)227 bool PluginService::AddPluginInfo(const PluginInfo& pluginInfo)
228 {
229 if (nameIndex_.find(pluginInfo.name) == nameIndex_.end()) { // add new plugin
230 auto pluginCtx = std::make_shared<PluginContext>();
231 CHECK_NOTNULL(pluginCtx, false, "create PluginContext failed!");
232
233 ProfilerPluginCapability capability;
234 capability.set_path(pluginInfo.path);
235 capability.set_name(pluginInfo.name);
236 CHECK_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(capability), false,
237 "AddPluginInfo AddCapability FAIL");
238
239 pluginCtx->name = pluginInfo.name;
240 pluginCtx->path = pluginInfo.path;
241 pluginCtx->context = pluginInfo.context;
242 pluginCtx->config.set_name(pluginInfo.name);
243 pluginCtx->config.set_plugin_sha256(pluginInfo.sha256);
244 pluginCtx->profilerPluginState.set_name(pluginInfo.name);
245 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
246 pluginCtx->sha256 = pluginInfo.sha256;
247 pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
248
249 uint32_t pluginId = ++pluginIdCounter_;
250 std::unique_lock<std::mutex> lock(mutex_);
251 pluginContext_[pluginId] = pluginCtx;
252 nameIndex_[pluginInfo.name] = pluginId;
253 } else { // update sha256 or bufferSizeHint
254 std::unique_lock<std::mutex> lock(mutex_);
255 CHECK_TRUE(nameIndex_.count(pluginInfo.name) > 0, false, "plugin name %s not found!", pluginInfo.name.c_str());
256
257 uint32_t pluginId = nameIndex_[pluginInfo.name];
258 CHECK_TRUE(pluginContext_.count(pluginId) > 0, false, "plugin id %u not found!", pluginId);
259 auto pluginCtx = pluginContext_[pluginId];
260
261 if (pluginInfo.sha256 != "") {
262 pluginCtx->sha256 = pluginInfo.sha256;
263 }
264 if (pluginInfo.bufferSizeHint != 0) {
265 pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
266 }
267 }
268 HILOG_DEBUG(LOG_CORE, "AddPluginInfo for %s done!", pluginInfo.name.c_str());
269
270 return true;
271 }
272
GetPluginInfo(const std::string & pluginName,PluginInfo & pluginInfo)273 bool PluginService::GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo)
274 {
275 uint32_t pluginId = 0;
276 PluginContextPtr pluginCtx = nullptr;
277 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
278 CHECK_TRUE(pluginId, false, "plugin name %s not found!", pluginName.c_str());
279 CHECK_TRUE(pluginCtx, false, "plugin id %u not found!", pluginId);
280
281 pluginInfo.id = pluginId;
282 pluginInfo.name = pluginCtx->name;
283 pluginInfo.path = pluginCtx->path;
284 pluginInfo.sha256 = pluginCtx->sha256;
285 pluginInfo.bufferSizeHint = pluginCtx->bufferSizeHint;
286 return true;
287 }
288
RemovePluginInfo(const PluginInfo & pluginInfo)289 bool PluginService::RemovePluginInfo(const PluginInfo& pluginInfo)
290 {
291 uint32_t pluginId = pluginInfo.id;
292 PluginContextPtr pluginCtx = GetPluginContextById(pluginId);
293 CHECK_NOTNULL(pluginCtx, false, "RemovePluginInfo failed, id %d not found!", pluginId);
294
295 std::string pluginName = pluginCtx->config.name();
296 CHECK_TRUE(ProfilerCapabilityManager::GetInstance().RemoveCapability(pluginName), false,
297 "RemovePluginInfo RemoveCapability FAIL %d", pluginId);
298
299 std::unique_lock<std::mutex> lock(mutex_);
300 nameIndex_.erase(pluginName);
301 pluginContext_.erase(pluginId);
302 HILOG_DEBUG(LOG_CORE, "RemovePluginInfo for %s done!", pluginName.c_str());
303 return true;
304 }
305
SetTraceWriter(const TraceFileWriterPtr & traceWriter)306 void PluginService::SetTraceWriter(const TraceFileWriterPtr& traceWriter)
307 {
308 traceWriter_ = traceWriter;
309 }