• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)143 bool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
144                                         const ProfilerDataRepeaterPtr& dataRepeater)
145 {
146     uint32_t pluginId = 0;
147     PluginContextPtr pluginCtx = nullptr;
148     std::string pluginName = pluginConfig.name();
149     std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
150     CHECK_NOTNULL(pluginCtx, false, "create PluginContext failed!");
151 
152     pluginCtx->profilerDataRepeater = dataRepeater;
153     pluginCtx->shareMemoryBlock = nullptr;
154 
155     pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
156 
157     PROFILER_LOG_DEBUG(LOG_CORE, "CreatePluginSession %s done!", pluginName.c_str());
158     return true;
159 }
160 
CreatePluginSession(const ProfilerPluginConfig & pluginConfig,const ProfilerSessionConfig::BufferConfig & bufferConfig,const ProfilerDataRepeaterPtr & dataRepeater)161 bool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
162                                         const ProfilerSessionConfig::BufferConfig& bufferConfig,
163                                         const ProfilerDataRepeaterPtr& dataRepeater)
164 {
165     return CreatePluginSession(pluginConfig, dataRepeater);
166 }
167 
StartPluginSession(const ProfilerPluginConfig & pluginConfig)168 bool PluginService::StartPluginSession(const ProfilerPluginConfig& pluginConfig)
169 {
170     uint32_t pluginId = 0;
171     PluginContextPtr pluginCtx = nullptr;
172     std::string pluginName = pluginConfig.name();
173     std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
174 
175     pluginCtx->profilerPluginState.set_state(ProfilerPluginState::IN_SESSION);
176     PROFILER_LOG_INFO(LOG_CORE, "StartPluginSession %s done!", pluginName.c_str());
177     return true;
178 }
179 
StopPluginSession(const std::string & pluginName)180 bool PluginService::StopPluginSession(const std::string& pluginName)
181 {
182     uint32_t pluginId = 0;
183     PluginContextPtr pluginCtx = nullptr;
184     std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
185 
186     pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
187 
188     PROFILER_LOG_DEBUG(LOG_CORE, "StopPluginSession %s done!", pluginName.c_str());
189     return true;
190 }
191 
DestroyPluginSession(const std::string & pluginName)192 bool PluginService::DestroyPluginSession(const std::string& pluginName)
193 {
194     uint32_t pluginId = 0;
195     PluginContextPtr pluginCtx = nullptr;
196     std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
197 
198     if (pluginCtx->shareMemoryBlock) {
199         pluginCtx->shareMemoryBlock = nullptr;
200     }
201 
202     if (pluginCtx->eventNotifier) {
203         eventPoller_->RemoveFileDescriptor(pluginCtx->eventNotifier->GetFd());
204         pluginCtx->eventNotifier = nullptr;
205     }
206 
207     pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
208     PROFILER_LOG_INFO(LOG_CORE, "DestroyPluginSession %s done!", pluginName.c_str());
209     return true;
210 }
211 
RefreshPluginSession(const std::string & pluginName)212 bool PluginService::RefreshPluginSession(const std::string& pluginName)
213 {
214     uint32_t pluginId = 0;
215     PluginContextPtr pluginCtx = nullptr;
216     std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
217     PROFILER_LOG_INFO(LOG_CORE, "RefreshPluginSession %s done!", pluginName.c_str());
218     return true;
219 }
220 
GetPluginContext(const std::string & pluginName)221 std::pair<uint32_t, PluginContextPtr> PluginService::GetPluginContext(const std::string& pluginName)
222 {
223     std::unique_lock<std::mutex> lock(mutex_);
224     CHECK_TRUE(nameIndex_.count(pluginName) > 0, std::make_pair(0, nullptr),
225                "GetPluginContext failed, plugin name `%s` not found!", pluginName.c_str());
226     uint32_t id = nameIndex_[pluginName];
227 
228     CHECK_TRUE(pluginContext_.count(id) > 0, std::make_pair(id, nullptr), "plugin id %u not found!", id);
229     return std::make_pair(id, pluginContext_[id]);
230 }
231 
GetPluginContextById(uint32_t id)232 PluginContextPtr PluginService::GetPluginContextById(uint32_t id)
233 {
234     std::unique_lock<std::mutex> lock(mutex_);
235     CHECK_TRUE(pluginContext_.count(id) > 0, nullptr, "plugin id %u not found!", id);
236     return pluginContext_[id];
237 }
238 
AddPluginInfo(const PluginInfo & pluginInfo)239 bool PluginService::AddPluginInfo(const PluginInfo& pluginInfo)
240 {
241     if (nameIndex_.find(pluginInfo.name) == nameIndex_.end()) { // add new plugin
242         auto pluginCtx = std::make_shared<PluginContext>();
243         CHECK_NOTNULL(pluginCtx, false, "create PluginContext failed!");
244 
245         ProfilerPluginCapability capability;
246         capability.set_path(pluginInfo.path);
247         capability.set_name(pluginInfo.name);
248         CHECK_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(capability), false,
249                    "AddPluginInfo AddCapability FAIL");
250 
251         pluginCtx->name = pluginInfo.name;
252         pluginCtx->path = pluginInfo.path;
253         pluginCtx->context = pluginInfo.context;
254         pluginCtx->config.set_name(pluginInfo.name);
255         pluginCtx->config.set_plugin_sha256(pluginInfo.sha256);
256         pluginCtx->profilerPluginState.set_name(pluginInfo.name);
257         pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
258         pluginCtx->sha256 = pluginInfo.sha256;
259         pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
260 
261         uint32_t pluginId = ++pluginIdCounter_;
262         std::unique_lock<std::mutex> lock(mutex_);
263         pluginContext_[pluginId] = pluginCtx;
264         nameIndex_[pluginInfo.name] = pluginId;
265     } else { // update sha256 or bufferSizeHint
266         std::unique_lock<std::mutex> lock(mutex_);
267         CHECK_TRUE(nameIndex_.count(pluginInfo.name) > 0, false, "plugin name %s not found!", pluginInfo.name.c_str());
268 
269         uint32_t pluginId = nameIndex_[pluginInfo.name];
270         CHECK_TRUE(pluginContext_.count(pluginId) > 0, false, "plugin id %u not found!", pluginId);
271         auto pluginCtx = pluginContext_[pluginId];
272 
273         if (pluginInfo.sha256 != "") {
274             pluginCtx->sha256 = pluginInfo.sha256;
275         }
276         if (pluginInfo.bufferSizeHint != 0) {
277             pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
278         }
279     }
280     PROFILER_LOG_DEBUG(LOG_CORE, "AddPluginInfo for %s done!", pluginInfo.name.c_str());
281 
282     return true;
283 }
284 
GetPluginInfo(const std::string & pluginName,PluginInfo & pluginInfo)285 bool PluginService::GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo)
286 {
287     uint32_t pluginId = 0;
288     PluginContextPtr pluginCtx = nullptr;
289     std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
290     CHECK_TRUE(pluginId, false, "plugin name %s not found!", pluginName.c_str());
291     CHECK_TRUE(pluginCtx, false, "plugin id %u not found!", pluginId);
292 
293     pluginInfo.id = pluginId;
294     pluginInfo.name = pluginCtx->name;
295     pluginInfo.path = pluginCtx->path;
296     pluginInfo.sha256 = pluginCtx->sha256;
297     pluginInfo.bufferSizeHint = pluginCtx->bufferSizeHint;
298     return true;
299 }
300 
RemovePluginInfo(const PluginInfo & pluginInfo)301 bool PluginService::RemovePluginInfo(const PluginInfo& pluginInfo)
302 {
303     uint32_t pluginId = pluginInfo.id;
304     PluginContextPtr pluginCtx = GetPluginContextById(pluginId);
305     CHECK_NOTNULL(pluginCtx, false, "RemovePluginInfo failed, id %d not found!", pluginId);
306 
307     std::string pluginName = pluginCtx->config.name();
308     CHECK_TRUE(ProfilerCapabilityManager::GetInstance().RemoveCapability(pluginName), false,
309                "RemovePluginInfo RemoveCapability FAIL %d", pluginId);
310 
311     std::unique_lock<std::mutex> lock(mutex_);
312     nameIndex_.erase(pluginName);
313     pluginContext_.erase(pluginId);
314     PROFILER_LOG_DEBUG(LOG_CORE, "RemovePluginInfo for %s done!", pluginName.c_str());
315     return true;
316 }
317 
SetTraceWriter(const TraceFileWriterPtr & traceWriter)318 void PluginService::SetTraceWriter(const TraceFileWriterPtr& traceWriter)
319 {
320     traceWriter_ = traceWriter;
321 }
322 
StartEpollThread()323 bool PluginService::StartEpollThread()
324 {
325     if (eventPoller_) {
326         return eventPoller_->Start();
327     }
328     return false;
329 }
330 
StopEpollThread()331 bool PluginService::StopEpollThread()
332 {
333     if (eventPoller_) {
334         return eventPoller_->Stop();
335     }
336     return false;
337 }
338 
FlushAllData(const std::string & pluginName)339 void PluginService::FlushAllData(const std::string& pluginName)
340 {
341     PROFILER_LOG_INFO(LOG_CORE, "FlushAllData for %s", pluginName.c_str());
342 }