• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. 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 
16 #include "plugin_module.h"
17 
18 #include <dlfcn.h>
19 #include <iostream>
20 
21 #include "logging.h"
22 #include "plugin_module_api.h"
23 
PluginModule(const std::string & path)24 PluginModule::PluginModule(const std::string& path)
25     : handle_(nullptr), running_(false), path_(path), structPtr_(nullptr) {}
26 
~PluginModule()27 PluginModule::~PluginModule() {}
28 
ComputeSha256()29 std::string PluginModule::ComputeSha256()
30 {
31     return "";
32 }
33 
Load()34 bool PluginModule::Load()
35 {
36     if (handle_ != nullptr) {
37         PROFILER_LOG_DEBUG(LOG_CORE, "%s:already open", __func__);
38         return false;
39     }
40 
41     handle_ = dlopen(path_.c_str(), RTLD_NODELETE);
42     if (handle_ == nullptr) {
43         PROFILER_LOG_DEBUG(LOG_CORE, "%s:dlopen err:%s.", __func__, dlerror());
44         return false;
45     }
46     return true;
47 }
48 
Unload()49 bool PluginModule::Unload()
50 {
51     PROFILER_LOG_INFO(LOG_CORE, "%s:unload ready!", __func__);
52     if (handle_ != nullptr) {
53         int ret = dlclose(handle_);
54         PROFILER_LOG_INFO(LOG_CORE, "%s:unload plugin ret = %d", __func__, ret);
55         handle_ = nullptr;
56         structPtr_ = nullptr;
57         return true;
58     }
59 
60     return false;
61 }
62 
GetInfo(PluginModuleInfo & info)63 bool PluginModule::GetInfo(PluginModuleInfo& info)
64 {
65     if (handle_ != nullptr) {
66         if (structPtr_ == nullptr) {
67             return false;
68         }
69         info.bufferSizeHint = structPtr_->resultBufferSizeHint;
70         info.name.assign(structPtr_->name);
71         info.isStandaloneFileData = structPtr_->isStandaloneFileData;
72         info.outFileName.assign(structPtr_->outFileName);
73         info.pluginVersion.assign(structPtr_->version);
74         return true;
75     }
76     return false;
77 }
78 
CheckDataReady()79 bool PluginModule::CheckDataReady()
80 {
81     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
82         if (structPtr_->callbacks->onReportStateCallback != nullptr) {
83             return structPtr_->callbacks->onReportStateCallback();
84         }
85     }
86     return false;
87 }
88 
GetSampleMode() const89 PluginModule::SampleMode PluginModule::GetSampleMode() const
90 {
91     if (structPtr_ && structPtr_->callbacks) {
92         if (structPtr_->callbacks->onPluginReportResult != nullptr) {
93             return SampleMode::POLLING;
94         } else if (structPtr_->callbacks->onRegisterWriterStruct != nullptr) {
95             return SampleMode::STREAMING;
96         }
97     }
98     return SampleMode::UNKNOWN;
99 }
100 
SetConfigData(const std::string & data)101 void PluginModule::SetConfigData(const std::string& data)
102 {
103     configData_ = data;
104 }
105 
GetConfigData() const106 std::string PluginModule::GetConfigData() const
107 {
108     return configData_;
109 }
110 
SetClockId(clockid_t clockId)111 void PluginModule::SetClockId(clockid_t clockId)
112 {
113     clockId_ = clockId;
114     PROFILER_LOG_INFO(LOG_CORE, "SetClockId: plugin=%s, clock=%d", GetPluginName().c_str(), clockId);
115     if (writerAdapter_ != nullptr && writerAdapter_->GetWriter() != nullptr) {
116         writerAdapter_->GetWriter()->SetClockId(clockId);
117     }
118 }
119 
GetClockId() const120 clockid_t PluginModule::GetClockId() const
121 {
122     return clockId_;
123 }
124 
GetPluginName(std::string & pluginName)125 bool PluginModule::GetPluginName(std::string& pluginName)
126 {
127     if (handle_ != nullptr) {
128         CHECK_NOTNULL(structPtr_, false, "structPtr_ is nullptr");
129         pluginName.assign(structPtr_->name);
130         return true;
131     }
132     return false;
133 }
134 
GetBufferSizeHint(uint32_t & bufferSizeHint)135 bool PluginModule::GetBufferSizeHint(uint32_t& bufferSizeHint)
136 {
137     if (handle_ != nullptr) {
138         CHECK_NOTNULL(structPtr_, false, "structPtr_ is nullptr");
139         bufferSizeHint = structPtr_->resultBufferSizeHint;
140         return true;
141     }
142     return false;
143 }
144 
GetStandaloneFileData()145 bool PluginModule::GetStandaloneFileData()
146 {
147     if (handle_ != nullptr) {
148         CHECK_NOTNULL(structPtr_, false, "structPtr_ is nullptr");
149         return structPtr_->isStandaloneFileData;
150     }
151     return false;
152 }
153 
GetOutFileName(std::string & outFileName)154 bool PluginModule::GetOutFileName(std::string& outFileName)
155 {
156     if (handle_ != nullptr) {
157         CHECK_NOTNULL(structPtr_, false, "structPtr_ is nullptr");
158         outFileName.assign(structPtr_->outFileName);
159         return true;
160     }
161     return false;
162 }
163 
GetPluginVersion(std::string & pluginVersion)164 bool PluginModule::GetPluginVersion(std::string& pluginVersion)
165 {
166     if (handle_ != nullptr) {
167         if (structPtr_ == nullptr) {
168             return false;
169         }
170         pluginVersion.assign(structPtr_->version);
171         return true;
172     }
173     return false;
174 }
175 
GetPath()176 std::string PluginModule::GetPath()
177 {
178     return path_;
179 }
180 
GetPluginName()181 std::string PluginModule::GetPluginName()
182 {
183     if (pluginName_ == "") {
184         if (handle_ != nullptr) {
185             CHECK_NOTNULL(structPtr_, "", "structPtr_ is nullptr");
186             pluginName_.assign(structPtr_->name);
187         }
188     }
189     return pluginName_;
190 }
191 
IsLoaded()192 bool PluginModule::IsLoaded()
193 {
194     return (handle_ != nullptr);
195 }
196 
IsRunning()197 bool PluginModule::IsRunning()
198 {
199     return running_;
200 }
201 
BindFunctions()202 bool PluginModule::BindFunctions()
203 {
204     CHECK_NOTNULL(handle_, false, "%s:plugin not load", __func__);
205     if (structPtr_ == nullptr) {
206         structPtr_ = static_cast<PluginModuleStruct*>(dlsym(handle_, "g_pluginModule"));
207         if (structPtr_ == nullptr) {
208             PROFILER_LOG_DEBUG(LOG_CORE, "%s:structPtr_ == nullptr", __func__);
209             return false;
210         }
211     }
212 
213     if (structPtr_->callbacks == nullptr) {
214         PROFILER_LOG_DEBUG(LOG_CORE, "%s:structPtr_->callbacks == nullptr", __func__);
215         return false;
216     }
217 
218     if ((structPtr_->callbacks->onPluginSessionStart == nullptr) ||
219         (structPtr_->callbacks->onPluginSessionStop == nullptr)) {
220         PROFILER_LOG_DEBUG(LOG_CORE, "%s:onPluginSessionStart == nullptr", __func__);
221         return false;
222     }
223 
224     return true;
225 }
226 
StartSession(const uint8_t * buffer,uint32_t size)227 bool PluginModule::StartSession(const uint8_t* buffer, uint32_t size)
228 {
229     PROFILER_LOG_DEBUG(LOG_CORE, "StartSession");
230     CHECK_NOTNULL(handle_, false, "%s:plugin not load", __func__);
231     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
232         if (structPtr_->callbacks->onPluginSessionStart) {
233             running_ = true;
234             return (structPtr_->callbacks->onPluginSessionStart(buffer, size) == 0);
235         }
236     }
237     return false;
238 }
239 
StopSession()240 bool PluginModule::StopSession()
241 {
242     PROFILER_LOG_INFO(LOG_CORE, "%s:stop Session ready!", __func__);
243     CHECK_NOTNULL(handle_, false, "%s:plugin not load", __func__);
244     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
245         if (structPtr_->callbacks->onPluginSessionStop != nullptr) {
246             running_ = false;
247             return (structPtr_->callbacks->onPluginSessionStop() == 0);
248         }
249     }
250     return false;
251 }
252 
ReportBasicData()253 bool PluginModule::ReportBasicData()
254 {
255     PROFILER_LOG_INFO(LOG_CORE, "%s:report basic data ready!", __func__);
256     CHECK_NOTNULL(handle_, false, "%s:plugin not load", __func__);
257     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
258         if (structPtr_->callbacks->onReportBasicDataCallback != nullptr) {
259             return (structPtr_->callbacks->onReportBasicDataCallback() == 0);
260         }
261     }
262     return false;
263 }
264 
ReportResult(uint8_t * buffer,uint32_t size)265 int32_t PluginModule::ReportResult(uint8_t* buffer, uint32_t size)
266 {
267     CHECK_NOTNULL(handle_, -1, "%s:plugin not load", __func__);
268     if (first_) {
269         lastTime_ = std::chrono::steady_clock::now();
270         first_ = false;
271     } else {
272         std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
273         lastTime_ = t1;
274     }
275 
276     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
277         if (structPtr_->callbacks->onPluginReportResult != nullptr) {
278             return structPtr_->callbacks->onPluginReportResult(buffer, size);
279         }
280     }
281 
282     return -1;
283 }
284 
ReportResultOptimize()285 PluginReportResultOptimizeCallback PluginModule::ReportResultOptimize()
286 {
287     CHECK_NOTNULL(handle_, nullptr, "%s:plugin not open", __func__);
288     if (first_) {
289         lastTime_ = std::chrono::steady_clock::now();
290         first_ = false;
291     } else {
292         std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
293         lastTime_ = t1;
294     }
295 
296     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
297         if (structPtr_->callbacks->onPluginReportResultOptimize != nullptr) {
298             return structPtr_->callbacks->onPluginReportResultOptimize;
299         }
300     }
301 
302     return nullptr;
303 }
304 
RegisterWriter(const BufferWriterPtr writer,bool isProtobufSerialize)305 bool PluginModule::RegisterWriter(const BufferWriterPtr writer, bool isProtobufSerialize)
306 {
307     isProtobufSerialize_ = isProtobufSerialize;
308     writerAdapter_ = std::make_shared<WriterAdapter>(isProtobufSerialize);
309     writerAdapter_->SetWriter(writer);
310     CHECK_NOTNULL(writer, true, "%s:bufferWriter is null, update WriterAdapter only!", __func__);
311     if (structPtr_ != nullptr && structPtr_->callbacks != nullptr) {
312         if (structPtr_->callbacks->onRegisterWriterStruct != nullptr) {
313             return structPtr_->callbacks->onRegisterWriterStruct(writerAdapter_->GetStruct());
314         }
315     }
316     return true;
317 }
318 
GetWriter()319 WriterPtr PluginModule::GetWriter()
320 {
321     CHECK_NOTNULL(writerAdapter_, nullptr, "%s:pluginModule nullptr", __func__);
322     return writerAdapter_->GetWriter();
323 }
324