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_manager.h"
17
18 #include <cstdio>
19 #include <functional>
20 #include <iomanip>
21
22 #include "common.h"
23 #include "command_poller.h"
24 #include "common_types.pbencoder.h"
25 #include "logging.h"
26 #include "openssl/sha.h"
27 #include "plugin_service_types.pb.h"
28
29 namespace {
30 using namespace OHOS::Developtools::Profiler;
31 constexpr int FILE_READ_CHUNK_SIZE = 4096;
32 constexpr char HEX_CHARS[] = "0123456789abcdef";
33 #define HHB(v) (((v) & 0xF0) >> 4)
34 #define LHB(v) ((v) & 0x0F)
35
ComputeFileSha256(const std::string & path)36 std::string ComputeFileSha256(const std::string& path)
37 {
38 uint8_t out[SHA256_DIGEST_LENGTH];
39 uint8_t buffer[FILE_READ_CHUNK_SIZE];
40 char realPath[PATH_MAX + 1] = {0};
41
42 SHA256_CTX sha;
43 SHA256_Init(&sha);
44
45 size_t nbytes = 0;
46 if ((path.length() >= PATH_MAX) || (realpath(path.c_str(), realPath) == nullptr)) {
47 PROFILER_LOG_ERROR(LOG_CORE, "%s:path is invalid: %s, errno=%d", __func__, path.c_str(), errno);
48 return "";
49 }
50 std::unique_ptr<FILE, decltype(fclose)*> fptr(fopen(realPath, "rb"), fclose);
51 if (fptr == nullptr) {
52 PROFILER_LOG_ERROR(LOG_CORE, "fopen path: %s failed, errno = %d", realPath, errno);
53 return "";
54 }
55 while ((nbytes = fread(buffer, 1, sizeof(buffer), fptr.get())) > 0) {
56 SHA256_Update(&sha, buffer, nbytes);
57 }
58 SHA256_Final(out, &sha);
59
60 std::string result;
61 result.reserve(SHA256_DIGEST_LENGTH + SHA256_DIGEST_LENGTH);
62 for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
63 result.push_back(HEX_CHARS[HHB(out[i])]);
64 result.push_back(HEX_CHARS[LHB(out[i])]);
65 }
66
67 PROFILER_LOG_DEBUG(LOG_CORE, "%s:%s-(%s)", __func__, path.c_str(), result.c_str());
68 return result;
69 }
70 } // namespace
71
~PluginManager()72 PluginManager::~PluginManager() {}
73
SetCommandPoller(const CommandPollerPtr & p)74 void PluginManager::SetCommandPoller(const CommandPollerPtr& p)
75 {
76 this->commandPoller_ = p;
77 }
78
AddPlugin(const std::string & pluginPath)79 bool PluginManager::AddPlugin(const std::string& pluginPath)
80 {
81 PluginModuleInfo info = {"", 0};
82 auto plugin = std::make_shared<PluginModule>(pluginPath);
83 CHECK_TRUE(plugin->Load(), false, "%s:load failed!", __func__);
84
85 if (!plugin->BindFunctions()) {
86 PROFILER_LOG_DEBUG(LOG_CORE, "%s:bindFunctions failed %s", __func__, pluginPath.c_str());
87 plugin->Unload();
88 return false;
89 }
90
91 if (!plugin->GetInfo(info)) {
92 PROFILER_LOG_DEBUG(LOG_CORE, "%s:getinfo failed!", __func__);
93 plugin->Unload();
94 return false;
95 }
96
97 std::string pluginName = info.name;
98 if (pluginIds_.find(pluginName) != pluginIds_.end()) {
99 PROFILER_LOG_DEBUG(LOG_CORE, "%s:already add", __func__);
100 plugin->Unload();
101 return false;
102 }
103 PROFILER_LOG_DEBUG(LOG_CORE, "%s:add plugin name = %s", __func__, pluginName.c_str());
104
105 if (!plugin->Unload()) {
106 PROFILER_LOG_DEBUG(LOG_CORE, "%s:unload failed!", __func__);
107 return false;
108 }
109
110 return RegisterPlugin(plugin, pluginPath, info);
111 }
112
RegisterPlugin(const PluginModulePtr & plugin,const std::string & pluginPath,const PluginModuleInfo & pluginInfo)113 bool PluginManager::RegisterPlugin(const PluginModulePtr& plugin, const std::string& pluginPath,
114 const PluginModuleInfo& pluginInfo)
115 {
116 RegisterPluginRequest request;
117 request.set_request_id(commandPoller_->GetRequestId());
118 request.set_path(pluginPath);
119 request.set_sha256(ComputeFileSha256(pluginPath));
120 RegisterPluginResponse response;
121 request.set_name(pluginInfo.name);
122 request.set_buffer_size_hint(pluginInfo.bufferSizeHint);
123 request.set_is_standalone_data(pluginInfo.isStandaloneFileData);
124 request.set_out_file_name(pluginInfo.outFileName);
125 request.set_plugin_version(pluginInfo.pluginVersion);
126 if (commandPoller_->RegisterPlugin(request, response)) {
127 if (response.status() == ResponseStatus::OK) {
128 PROFILER_LOG_DEBUG(LOG_CORE, "%s:response.plugin_id() = %d", __func__, response.plugin_id());
129 pluginIds_[pluginInfo.name] = response.plugin_id();
130 pluginModules_.insert(std::pair<uint32_t, std::shared_ptr<PluginModule>>(response.plugin_id(), plugin));
131 pluginPathAndNameMap_.insert(
132 {pluginPath, pluginInfo.name}
133 );
134 PROFILER_LOG_DEBUG(LOG_CORE, "%s:registerPlugin ok", __func__);
135 } else {
136 PROFILER_LOG_DEBUG(LOG_CORE, "%s:registerPlugin fail 1", __func__);
137 return false;
138 }
139 } else {
140 PROFILER_LOG_DEBUG(LOG_CORE, "%s:registerPlugin fail 2", __func__);
141 return false;
142 }
143
144 return true;
145 }
146
RemovePlugin(const std::string & pluginPath)147 bool PluginManager::RemovePlugin(const std::string& pluginPath)
148 {
149 CHECK_TRUE(pluginPathAndNameMap_.count(pluginPath) != 0, false,
150 "%s:not find plugin: %s", __func__, pluginPath.c_str());
151
152 std::string pluginName = pluginPathAndNameMap_[pluginPath];
153 auto it = pluginIds_.find(pluginName);
154 if (it == pluginIds_.end()) {
155 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not exist", __func__);
156 return false;
157 }
158 uint32_t index = it->second;
159
160 // stop plugin if plugin running
161 if (pluginModules_[index]->IsRunning()) {
162 PROFILER_LOG_WARN(LOG_CORE, "%s:plugin delete while using, stop plugin", __func__);
163
164 // delete schedule task if POLLING mode
165 if (pluginModules_[index]->GetSampleMode() == PluginModule::SampleMode::POLLING ||
166 pluginModules_[index]->GetSampleMode() == PluginModule::SampleMode::STREAMING) {
167 PROFILER_LOG_WARN(LOG_CORE, "%s:delete schedule task plugin name = %s", __func__, pluginName.c_str());
168 if (!scheduleTaskManager_.UnscheduleTask(scheduleTask_[pluginName])) {
169 PROFILER_LOG_WARN(LOG_CORE, "%s:delete schedule task plugin name = %s failed!",
170 __func__, pluginName.c_str());
171 }
172 }
173
174 if (!pluginModules_[index]->StopSession()) {
175 PROFILER_LOG_WARN(LOG_CORE, "%s:plugin stop failed!", __func__);
176 }
177 }
178
179 // Unload plugin if plugin loaded
180 if (pluginModules_[index]->IsLoaded()) {
181 PROFILER_LOG_WARN(LOG_CORE, "%s:plugin delete while using, unload plugin", __func__);
182 if (!pluginModules_[index]->Unload()) {
183 PROFILER_LOG_WARN(LOG_CORE, "%s:unload plugin failed!", __func__);
184 }
185 }
186
187 UnregisterPluginRequest request;
188 request.set_request_id(commandPoller_->GetRequestId());
189 request.set_plugin_id(index);
190 UnregisterPluginResponse response;
191 if (commandPoller_->UnregisterPlugin(request, response)) {
192 if (response.status() != ResponseStatus::OK) {
193 PROFILER_LOG_DEBUG(LOG_CORE, "%s:registerPlugin fail 1", __func__);
194 return false;
195 }
196 } else {
197 PROFILER_LOG_DEBUG(LOG_CORE, "%s:registerPlugin fail 2", __func__);
198 return false;
199 }
200
201 auto itPluginModules = pluginModules_.find(index);
202 if (it == pluginIds_.end()) {
203 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not exist", __func__);
204 return false;
205 }
206 pluginModules_.erase(itPluginModules);
207 pluginIds_.erase(it);
208 return true;
209 }
210
LoadPlugin(const std::string & pluginName)211 bool PluginManager::LoadPlugin(const std::string& pluginName)
212 {
213 PROFILER_LOG_DEBUG(LOG_CORE, "%s:size = %zu", __func__, pluginIds_.size());
214 auto it = pluginIds_.find(pluginName);
215 CHECK_TRUE(it != pluginIds_.end(), false, "%s:plugin not exist", __func__);
216 uint32_t index = it->second;
217
218 if (!pluginModules_[index]->Load()) {
219 return false;
220 }
221 if (!pluginModules_[index]->BindFunctions()) {
222 return false;
223 }
224 return true;
225 }
226
UnloadPlugin(const std::string & pluginName)227 bool PluginManager::UnloadPlugin(const std::string& pluginName)
228 {
229 auto it = pluginIds_.find(pluginName);
230 if (it == pluginIds_.end()) {
231 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not exist", __func__);
232 return false;
233 }
234
235 return UnloadPlugin(it->second);
236 }
237
UnloadPlugin(const uint32_t pluginId)238 bool PluginManager::UnloadPlugin(const uint32_t pluginId)
239 {
240 PROFILER_LOG_INFO(LOG_CORE, "%s:ready!", __func__);
241 if (pluginModules_.find(pluginId) == pluginModules_.end()) {
242 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not exist", __func__);
243 return false;
244 }
245 if (!pluginModules_[pluginId]->Unload()) {
246 return false;
247 }
248 return true;
249 }
250
CreatePluginSession(const std::vector<ProfilerPluginConfig> & config)251 bool PluginManager::CreatePluginSession(const std::vector<ProfilerPluginConfig>& config)
252 {
253 PROFILER_LOG_DEBUG(LOG_CORE, "%s:ready", __func__);
254 for (size_t idx = 0; idx < config.size(); ++idx) {
255 PROFILER_LOG_DEBUG(LOG_CORE, "%s:config->name() = %s", __func__, config[idx].name().c_str());
256 auto it = pluginIds_.find(config[idx].name());
257 if (it == pluginIds_.end()) {
258 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not find", __func__);
259 return false;
260 }
261
262 PROFILER_LOG_INFO(LOG_CORE, "%s:index = %d, clock = %s", __func__, it->second, config[idx].clock().c_str());
263 pluginModules_[it->second]->SetConfigData(config[idx].config_data());
264 pluginModules_[it->second]->SetClockId(COMMON::GetClockId(config[idx].clock()));
265 }
266 return true;
267 }
268
DestroyPluginSession(const std::vector<uint32_t> & pluginIds)269 bool PluginManager::DestroyPluginSession(const std::vector<uint32_t>& pluginIds)
270 {
271 for (uint32_t id : pluginIds) {
272 auto it = pluginModules_.find(id);
273 if (it == pluginModules_.end()) {
274 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not find", __func__);
275 return false;
276 }
277 }
278 return true;
279 }
280
StartPluginSession(const std::vector<uint32_t> & pluginIds,const std::vector<ProfilerPluginConfig> & config,PluginResult & result)281 bool PluginManager::StartPluginSession(const std::vector<uint32_t>& pluginIds,
282 const std::vector<ProfilerPluginConfig>& config, PluginResult& result)
283 {
284 PROFILER_LOG_INFO(LOG_CORE, "%s:ready!", __func__);
285 size_t idx = 0;
286 ProfilerPluginState* status = result.mutable_status();
287 for (uint32_t id : pluginIds) {
288 auto it = pluginModules_.find(id);
289 if (it == pluginModules_.end()) {
290 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not find", __func__);
291 return false;
292 }
293 auto plugin = pluginModules_[id];
294 auto cfgData = plugin->GetConfigData();
295 std::string pluginName = "";
296 result.set_plugin_id(id);
297 pluginModules_[id]->GetPluginName(pluginName);
298 status->set_version(COMMON::STATE_VERSION);
299 status->set_name(pluginName);
300 if (!plugin->StartSession(reinterpret_cast<const uint8_t*>(cfgData.c_str()), cfgData.size())) {
301 status->set_event_detail("Plugin started failed! plugin: " + pluginName);
302 status->set_event(ProfilerPluginState::PLUGIN_ERR);
303 return false;
304 } else {
305 status->set_event_detail("Plugin loaded success! plugin: " + pluginName);
306 status->set_event(ProfilerPluginState::PLUGIN_LOAD_SUCC);
307 }
308
309 if (plugin->GetSampleMode() == PluginModule::SampleMode::POLLING) {
310 if (idx >= config.size()) {
311 PROFILER_LOG_WARN(LOG_CORE, "%s:idx %zu out of size %zu", __func__, idx, config.size());
312 return false;
313 }
314 if (config[idx].sample_interval() == 0) {
315 PROFILER_LOG_DEBUG(LOG_CORE, "%s:scheduleTask interval == 0 error!", __func__);
316 return false;
317 }
318 auto callback = [this, id, config, idx] { this->PullResult(id, config[idx].is_protobuf_serialize()); };
319 int32_t timerFd = scheduleTaskManager_.ScheduleTask(callback, config[idx].sample_interval());
320 if (timerFd == -1) {
321 PROFILER_LOG_DEBUG(LOG_CORE, "%s:scheduleTask failed!", __func__);
322 return false;
323 }
324 scheduleTask_[config[idx].name()] = timerFd;
325 } else if (plugin->GetSampleMode() == PluginModule::SampleMode::STREAMING) {
326 if (idx >= config.size()) {
327 PROFILER_LOG_WARN(LOG_CORE, "%s:idx %zu out of size %zu", __func__, idx, config.size());
328 return false;
329 }
330 if (config[idx].sample_interval() == 0) {
331 PROFILER_LOG_DEBUG(LOG_CORE, "%s:scheduleTask interval == 0 error!", __func__);
332 return false;
333 }
334 auto callback = [this, id] { this->PullState(id); };
335 int32_t timerFd = scheduleTaskManager_.ScheduleTask(callback, config[idx].sample_interval());
336 if (timerFd == -1) {
337 PROFILER_LOG_DEBUG(LOG_CORE, "%s:scheduleTask failed!", __func__);
338 return false;
339 }
340 scheduleTask_[config[idx].name()] = timerFd;
341 }
342
343 // need update standalone plugin output file name
344 if (plugin->GetStandaloneFileData()) {
345 std::string pluginOutFileName = "";
346 plugin->GetOutFileName(pluginOutFileName);
347 result.set_plugin_id(id);
348 result.set_out_file_name(pluginOutFileName);
349 }
350
351 idx++;
352 }
353
354 return true;
355 }
356
StopPluginSession(const std::vector<uint32_t> & pluginIds)357 bool PluginManager::StopPluginSession(const std::vector<uint32_t>& pluginIds)
358 {
359 PROFILER_LOG_INFO(LOG_CORE, "%s:ready!", __func__);
360 for (uint32_t id : pluginIds) {
361 if (pluginModules_.find(id) == pluginModules_.end()) {
362 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not find", __func__);
363 return false;
364 }
365 if (pluginModules_[id]->GetSampleMode() == PluginModule::SampleMode::POLLING ||
366 pluginModules_[id]->GetSampleMode() == PluginModule::SampleMode::STREAMING) {
367 for (const auto& it : pluginIds_) {
368 if (it.second == id) {
369 PROFILER_LOG_DEBUG(LOG_CORE, "%s:find plugin name = %s", __func__, it.first.c_str());
370 scheduleTaskManager_.UnscheduleTask(scheduleTask_[it.first]);
371 }
372 }
373 }
374 if (!pluginModules_[id]->StopSession()) {
375 return false;
376 }
377 }
378 if (networkProfilerMgr_) {
379 networkProfilerMgr_->StopNetworkProfiler();
380 }
381 return true;
382 }
383
AddNetworkProfilerManager(std::shared_ptr<NetworkProfilerManager> networkProfilerMgr)384 void PluginManager::AddNetworkProfilerManager(std::shared_ptr<NetworkProfilerManager> networkProfilerMgr)
385 {
386 networkProfilerMgr_ = networkProfilerMgr;
387 }
388
StopAllPluginSession()389 bool PluginManager::StopAllPluginSession()
390 {
391 std::vector<uint32_t> vecPluginIds;
392 for (std::map<uint32_t, std::shared_ptr<PluginModule>>::iterator it = pluginModules_.begin();
393 it != pluginModules_.end(); ++it) {
394 vecPluginIds.push_back(it->first);
395 }
396 return StopPluginSession(vecPluginIds);
397 }
398
ReportPluginBasicData(const std::vector<uint32_t> & pluginIds)399 bool PluginManager::ReportPluginBasicData(const std::vector<uint32_t>& pluginIds)
400 {
401 PROFILER_LOG_INFO(LOG_CORE, "%s:ready!", __func__);
402 for (uint32_t id : pluginIds) {
403 CHECK_TRUE(pluginModules_.find(id) != pluginModules_.end(), false, "%s:plugin not find", __func__);
404 // notify plugin to report basic data
405 CHECK_TRUE(pluginModules_[id]->ReportBasicData(), false, "%s:report basic data failed", __func__);
406 }
407 return true;
408 }
409
SubmitResult(const PluginResult & pluginResult)410 bool PluginManager::SubmitResult(const PluginResult& pluginResult)
411 {
412 PROFILER_LOG_INFO(LOG_CORE, "%s:ready!", __func__);
413 NotifyResultRequest request;
414 CHECK_NOTNULL(commandPoller_, false, "%s:commandPoller_ is null", __func__);
415 request.set_request_id(commandPoller_->GetRequestId());
416 request.set_command_id(0);
417 PluginResult* p = request.add_result();
418 *p = pluginResult;
419 NotifyResultResponse response;
420 if (!commandPoller_->NotifyResult(request, response)) {
421 PROFILER_LOG_DEBUG(LOG_CORE, "%s:fail 1", __func__);
422 return false;
423 }
424 if (response.status() != ResponseStatus::OK) {
425 PROFILER_LOG_DEBUG(LOG_CORE, "%s:fail 2", __func__);
426 return false;
427 }
428 PROFILER_LOG_DEBUG(LOG_CORE, "%s:ok", __func__);
429 return true;
430 }
431
PullResult(uint32_t pluginId,bool isProtobufSerialize)432 bool PluginManager::PullResult(uint32_t pluginId, bool isProtobufSerialize)
433 {
434 uint32_t size = 0;
435 std::string name = "";
436 std::string version = "";
437 auto it = pluginModules_.find(pluginId);
438 if (it == pluginModules_.end()) {
439 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not find", __func__);
440 return false;
441 }
442 pluginModules_[pluginId]->GetBufferSizeHint(size);
443 pluginModules_[pluginId]->GetPluginName(name);
444 pluginModules_[pluginId]->GetPluginVersion(version);
445 std::string pluginName = name;
446 int32_t length = 0;
447 if (isProtobufSerialize) {
448 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(size);
449 CHECK_NOTNULL(buffer, false, "%s:buffer new failed!", __func__);
450
451 length = it->second->ReportResult(buffer.get(), size);
452 if (length > 0) {
453 ProfilerPluginData pluginData;
454 pluginData.set_name(name);
455 pluginData.set_version(version);
456 pluginData.set_status(0);
457 pluginData.set_data(buffer.get(), length);
458
459 struct timespec ts;
460 clockid_t clockId = pluginModules_[pluginId]->GetClockId();
461 clock_gettime(clockId, &ts);
462
463 pluginData.set_clock_id(static_cast<ProfilerPluginData_ClockId>(clockId));
464 pluginData.set_tv_sec(ts.tv_sec);
465 pluginData.set_tv_nsec(ts.tv_nsec);
466
467 auto writer = std::static_pointer_cast<BufferWriter>(pluginModules_[pluginId]->GetWriter());
468 CHECK_NOTNULL(writer, false, "PullResult GetWriter nullptr");
469
470 writer->WriteMessage(pluginData, name);
471 writer->Flush();
472 }
473 } else {
474 auto writer = std::static_pointer_cast<BufferWriter>(pluginModules_[pluginId]->GetWriter());
475 CHECK_NOTNULL(writer, false, "PullResult GetWriter nullptr");
476 auto writeCtx = writer->GetCtx();
477 CHECK_NOTNULL(writeCtx, false, "PullResult GetCtx nullptr");
478 writer->ResetPos();
479 ProtoEncoder::ProfilerPluginData pluginData(writeCtx);
480 pluginData.set_name(name);
481 pluginData.set_version(version);
482 pluginData.set_status(0);
483
484 auto callbackFunc = it->second->ReportResultOptimize();
485 if (callbackFunc != nullptr) {
486 pluginData.set_data(callbackFunc);
487 }
488
489 struct timespec ts;
490 clockid_t clockId = pluginModules_[pluginId]->GetClockId();
491 clock_gettime(clockId, &ts);
492
493 pluginData.set_clock_id(static_cast<ProfilerPluginData_ClockId>(clockId));
494 pluginData.set_tv_sec(ts.tv_sec);
495 pluginData.set_tv_nsec(ts.tv_nsec);
496
497 length = pluginData.Finish();
498 writer->UseMemory(length);
499 writer->Flush();
500 }
501
502 if (loadedPlugins_.find(pluginName) == loadedPlugins_.end() && length > 0) {
503 ProfilerPluginState pluginState;
504 pluginState.set_version(COMMON::STATE_VERSION);
505 pluginState.set_name(pluginName);
506 pluginState.set_event_detail("Report data success: " + pluginName);
507 pluginState.set_event(ProfilerPluginState::DATA_READY);
508 if (commandPoller_ != nullptr) {
509 commandPoller_->PushResult(pluginState, pluginId);
510 }
511 loadedPlugins_.insert(pluginName);
512 }
513 return true;
514 }
515
CreateWriter(std::string pluginName,uint32_t bufferSize,int smbFd,int eventFd,bool isProtobufSerialize)516 bool PluginManager::CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd,
517 bool isProtobufSerialize)
518 {
519 auto it = pluginIds_.find(pluginName);
520 if (it == pluginIds_.end()) {
521 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not exist", __func__);
522 return false;
523 }
524 uint32_t index = it->second;
525
526 if (bufferSize > 0) {
527 PROFILER_LOG_DEBUG(LOG_CORE, "%s:%s Use ShareMemory %d", __func__, pluginName.c_str(), bufferSize);
528 std::string pluginVersion = "";
529 pluginModules_[index]->GetPluginVersion(pluginVersion);
530 pluginModules_[index]->RegisterWriter(std::make_shared<BufferWriter>
531 (pluginName, pluginVersion, bufferSize, smbFd, eventFd, index), isProtobufSerialize);
532 } else {
533 PROFILER_LOG_ERROR(LOG_CORE, "%s:no shared memory buffer allocated!", __func__);
534 return false;
535 }
536 return true;
537 }
538
PullState(uint32_t pluginId)539 bool PluginManager::PullState(uint32_t pluginId)
540 {
541 uint32_t size = 0;
542 std::string name = "";
543 auto it = pluginModules_.find(pluginId);
544 if (it == pluginModules_.end()) {
545 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not find", __func__);
546 return false;
547 }
548 pluginModules_[pluginId]->GetBufferSizeHint(size);
549 pluginModules_[pluginId]->GetPluginName(name);
550 std::string pluginName = name;
551 CHECK_TRUE(size > 0, false, "PullState failed! size is 0");
552 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(size);
553 CHECK_NOTNULL(buffer, false, "%s:buffer new failed!", __func__);
554 if ((loadedPlugins_.find(pluginName) == loadedPlugins_.end()) && it->second->CheckDataReady()) {
555 ProfilerPluginState pluginState;
556 pluginState.set_version(COMMON::STATE_VERSION);
557 pluginState.set_event_detail("Report data success: " + pluginName);
558 pluginState.set_event(ProfilerPluginState::DATA_READY);
559 pluginState.set_name(pluginName);
560 if (commandPoller_ != nullptr) {
561 commandPoller_->PushResult(pluginState, pluginId);
562 }
563 loadedPlugins_.insert(pluginName);
564 }
565 return true;
566 }
567
ResetWriter(uint32_t pluginId)568 bool PluginManager::ResetWriter(uint32_t pluginId)
569 {
570 if (pluginModules_.find(pluginId) == pluginModules_.end()) {
571 PROFILER_LOG_DEBUG(LOG_CORE, "%s:plugin not exist", __func__);
572 return false;
573 }
574 PROFILER_LOG_DEBUG(LOG_CORE, "%s:resetWriter %u", __func__, pluginId);
575 pluginModules_[pluginId]->RegisterWriter(nullptr);
576 return true;
577 }
578