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
16 #include "plugin_service.h"
17
18 #include <cinttypes>
19 #include <fcntl.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22
23 #include "plugin_command_builder.h"
24 #include "plugin_service_impl.h"
25 #include "plugin_session_manager.h"
26 #include "profiler_capability_manager.h"
27 #include "profiler_data_repeater.h"
28 #include "securec.h"
29 #include "share_memory_allocator.h"
30 #include "socket_context.h"
31
32 namespace {
33 const int PAGE_BYTES = 4096;
34 const int DEFAULT_EVENT_POLLING_INTERVAL = 5000;
35 constexpr uint32_t FLUSH_BASELINE = (1U << 21); // need to flush data size with offline mode
36 constexpr uint32_t STOP_BASELINE = (1U << 22); // need to stop take data size with offline mode
37 } // namespace
38
PluginService()39 PluginService::PluginService()
40 {
41 pluginIdCounter_ = 0;
42 if (getuid() == 0) {
43 StartService(DEFAULT_UNIX_SOCKET_FULL_PATH);
44 } else {
45 StartService(DEFAULT_UNIX_SOCKET_PATH);
46 }
47
48 pluginCommandBuilder_ = std::make_shared<PluginCommandBuilder>();
49
50 eventPoller_ = std::make_unique<EpollEventPoller>(DEFAULT_EVENT_POLLING_INTERVAL);
51 CHECK_NOTNULL(eventPoller_, NO_RETVAL, "create event poller FAILED!");
52
53 eventPoller_->Init();
54 eventPoller_->Start();
55 }
56
~PluginService()57 PluginService::~PluginService()
58 {
59 if (eventPoller_) {
60 eventPoller_->Stop();
61 eventPoller_->Finalize();
62 }
63 }
64
SetPluginSessionManager(const PluginSessionManagerPtr & pluginSessionManager)65 void PluginService::SetPluginSessionManager(const PluginSessionManagerPtr& pluginSessionManager)
66 {
67 pluginSessionManager_ = pluginSessionManager;
68 }
69
SetProfilerSessionConfig(const ProfilerSessionConfig & profilerSessionConfig)70 void PluginService::SetProfilerSessionConfig(const ProfilerSessionConfig& profilerSessionConfig)
71 {
72 profilerSessionConfig_ = profilerSessionConfig;
73 }
74
GetSemaphore(uint32_t id) const75 SemaphorePtr PluginService::GetSemaphore(uint32_t id) const
76 {
77 std::unique_lock<std::mutex> lock(mutex_);
78 auto it = waitSemphores_.find(id);
79 if (it != waitSemphores_.end()) {
80 return it->second;
81 }
82 return nullptr;
83 }
84
StartService(const std::string & unixSocketName)85 bool PluginService::StartService(const std::string& unixSocketName)
86 {
87 pluginServiceImpl_ = std::make_shared<PluginServiceImpl>(*this);
88 serviceEntry_ = std::make_shared<ServiceEntry>();
89 if (!serviceEntry_->StartServer(unixSocketName)) {
90 pluginServiceImpl_ = nullptr;
91 serviceEntry_ = nullptr;
92 HILOG_DEBUG(LOG_CORE, "Start IPC Service FAIL");
93 return false;
94 }
95 serviceEntry_->RegisterService(*pluginServiceImpl_.get());
96 return true;
97 }
98
GetReusePolicy(const ProfilerSessionConfig::BufferConfig & bufferConfig)99 static ShareMemoryBlock::ReusePolicy GetReusePolicy(const ProfilerSessionConfig::BufferConfig& bufferConfig)
100 {
101 if (bufferConfig.policy() == ProfilerSessionConfig::BufferConfig::RECYCLE) {
102 return ShareMemoryBlock::DROP_OLD;
103 }
104 return ShareMemoryBlock::DROP_NONE;
105 }
106
107 // create plugin session with buffer config
CreatePluginSession(const ProfilerPluginConfig & pluginConfig,const ProfilerSessionConfig::BufferConfig & bufferConfig,const ProfilerDataRepeaterPtr & dataRepeater)108 bool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
109 const ProfilerSessionConfig::BufferConfig& bufferConfig,
110 const ProfilerDataRepeaterPtr& dataRepeater)
111 {
112 uint32_t pluginId = 0;
113 PluginContextPtr pluginCtx = nullptr;
114 std::string pluginName = pluginConfig.name();
115 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
116 CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
117
118 pluginCtx->profilerDataRepeater = dataRepeater;
119
120 uint32_t bufferSize = bufferConfig.pages() * PAGE_BYTES;
121 auto cmd = pluginCommandBuilder_->BuildCreateSessionCmd(pluginConfig, bufferSize);
122 CHECK_TRUE(cmd != nullptr, false, "CreatePluginSession BuildCreateSessionCmd FAIL %s", pluginName.c_str());
123
124 auto smb = ShareMemoryAllocator::GetInstance().CreateMemoryBlockLocal(pluginName, bufferSize);
125 CHECK_TRUE(smb != nullptr, false, "CreateMemoryBlockLocal FAIL %s", pluginName.c_str());
126
127 auto policy = GetReusePolicy(bufferConfig);
128 HILOG_DEBUG(LOG_CORE, "CreatePluginSession policy = %d", (int)policy);
129 smb->SetReusePolicy(policy);
130
131 auto notifier = EventNotifier::Create(0, EventNotifier::NONBLOCK);
132 CHECK_NOTNULL(notifier, false, "create EventNotifier for %s failed!", pluginName.c_str());
133
134 pluginCtx->shareMemoryBlock = smb;
135 pluginCtx->eventNotifier = notifier;
136 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
137
138 pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
139
140 pluginCtx->context->SendFileDescriptor(smb->GetfileDescriptor());
141 pluginCtx->context->SendFileDescriptor(notifier->GetFd());
142
143 if (profilerSessionConfig_.session_mode() == ProfilerSessionConfig::OFFLINE) {
144 eventPoller_->AddFileDescriptor(notifier->GetFd(),
145 std::bind(&PluginService::ReadShareMemoryOffline, this, *pluginCtx));
146 } else if (profilerSessionConfig_.session_mode() == ProfilerSessionConfig::ONLINE) {
147 eventPoller_->AddFileDescriptor(notifier->GetFd(),
148 std::bind(&PluginService::ReadShareMemoryOnline, this, *pluginCtx));
149 }
150 HILOG_DEBUG(LOG_CORE, "CreatePluginSession %s done, shmem fd = %d", pluginName.c_str(), smb->GetfileDescriptor());
151 return true;
152 }
153
154 // create plugin session without buffer config
CreatePluginSession(const ProfilerPluginConfig & pluginConfig,const ProfilerDataRepeaterPtr & dataRepeater)155 bool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
156 const ProfilerDataRepeaterPtr& dataRepeater)
157 {
158 uint32_t pluginId = 0;
159 PluginContextPtr pluginCtx = nullptr;
160 std::string pluginName = pluginConfig.name();
161 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
162 CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
163
164 pluginCtx->profilerDataRepeater = dataRepeater;
165 pluginCtx->shareMemoryBlock = nullptr;
166
167 auto cmd = pluginCommandBuilder_->BuildCreateSessionCmd(pluginConfig, 0);
168 CHECK_TRUE(cmd != nullptr, false, "CreatePluginSession BuildCreateSessionCmd FAIL %s", pluginName.c_str());
169
170 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
171 pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
172
173 HILOG_DEBUG(LOG_CORE, "CreatePluginSession %s done!", pluginName.c_str());
174 return true;
175 }
176
StartPluginSession(const ProfilerPluginConfig & config)177 bool PluginService::StartPluginSession(const ProfilerPluginConfig& config)
178 {
179 uint32_t pluginId = 0;
180 PluginContextPtr pluginCtx = nullptr;
181 std::string pluginName = config.name();
182 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
183 CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
184
185 auto cmd = pluginCommandBuilder_->BuildStartSessionCmd(config, pluginId);
186 CHECK_TRUE(cmd != nullptr, false, "StartPluginSession BuildStartSessionCmd FAIL %s", pluginName.c_str());
187
188 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::IN_SESSION);
189 pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
190 HILOG_INFO(LOG_CORE, "StartPluginSession %s done!", pluginName.c_str());
191 return true;
192 }
193
StopPluginSession(const std::string & pluginName)194 bool PluginService::StopPluginSession(const std::string& pluginName)
195 {
196 uint32_t pluginId = 0;
197 PluginContextPtr pluginCtx = nullptr;
198 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
199 CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
200
201 auto cmd = pluginCommandBuilder_->BuildStopSessionCmd(pluginId);
202 CHECK_TRUE(cmd != nullptr, false, "StopPluginSession BuildStopSessionCmd FAIL %s", pluginName.c_str());
203
204 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
205 pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
206
207 auto sem = GetSemaphoreFactory().Create(0);
208 CHECK_NOTNULL(sem, false, "create Semaphore for stop %s FAILED!", pluginName.c_str());
209
210 waitSemphores_[cmd->command_id()] = sem;
211 HILOG_DEBUG(LOG_CORE, "=== StopPluginSession %s Waiting ... ===", pluginName.c_str());
212 // wait on semaphore at most 30 seconds.
213 if (!sem->TimedWait(30)) {
214 // semaphore timeout
215 HILOG_DEBUG(LOG_CORE, "=== StopPluginSession Waiting FAIL ===");
216 return false;
217 }
218
219 if (!profilerSessionConfig_.discard_cache_data()) {
220 if (profilerSessionConfig_.session_mode() == ProfilerSessionConfig::OFFLINE) {
221 ReadShareMemoryOffline(*pluginCtx);
222 } else if (profilerSessionConfig_.session_mode() == ProfilerSessionConfig::ONLINE) {
223 ReadShareMemoryOnline(*pluginCtx);
224 }
225 }
226 HILOG_DEBUG(LOG_CORE, "StopPluginSession %s done!", pluginName.c_str());
227 return true;
228 }
229
DestroyPluginSession(const std::string & pluginName)230 bool PluginService::DestroyPluginSession(const std::string& pluginName)
231 {
232 uint32_t pluginId = 0;
233 PluginContextPtr pluginCtx = nullptr;
234 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
235 CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
236
237 auto cmd = pluginCommandBuilder_->BuildDestroySessionCmd(pluginId);
238 CHECK_TRUE(cmd != nullptr, false, "DestroyPluginSession BuildDestroySessionCmd FAIL %s", pluginName.c_str());
239
240 if (pluginCtx->shareMemoryBlock) {
241 ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockLocal(pluginName);
242 pluginCtx->shareMemoryBlock = nullptr;
243 }
244
245 if (pluginCtx->eventNotifier) {
246 eventPoller_->RemoveFileDescriptor(pluginCtx->eventNotifier->GetFd());
247 pluginCtx->eventNotifier = nullptr;
248 }
249
250 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
251 pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
252 HILOG_INFO(LOG_CORE, "DestroyPluginSession %s done!", pluginName.c_str());
253 return true;
254 }
255
RefreshPluginSession(const std::string & pluginName)256 bool PluginService::RefreshPluginSession(const std::string& pluginName)
257 {
258 uint32_t pluginId = 0;
259 PluginContextPtr pluginCtx = nullptr;
260 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
261 CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
262
263 auto cmd = pluginCommandBuilder_->BuildRefreshSessionCmd(pluginId);
264 CHECK_TRUE(cmd != nullptr, false, "RefreshPluginSession BuildRefreshSessionCmd FAIL %s", pluginName.c_str());
265
266 pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
267 HILOG_INFO(LOG_CORE, "RefreshPluginSession %s done!", pluginName.c_str());
268 return true;
269 }
270
RemovePluginSessionCtx(const std::string & pluginName)271 bool PluginService::RemovePluginSessionCtx(const std::string& pluginName)
272 {
273 PluginContextPtr pluginCtx = GetPluginContext(pluginName).second;
274 CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
275
276 if (pluginCtx->shareMemoryBlock) {
277 ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockLocal(pluginName);
278 pluginCtx->shareMemoryBlock = nullptr;
279 }
280
281 if (pluginCtx->eventNotifier) {
282 eventPoller_->RemoveFileDescriptor(pluginCtx->eventNotifier->GetFd());
283 pluginCtx->eventNotifier = nullptr;
284 }
285
286 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::INITED);
287 HILOG_INFO(LOG_CORE, "RemovePluginSessionCtx %s done!", pluginName.c_str());
288 return true;
289 }
290
GetPluginContext(const std::string & pluginName)291 std::pair<uint32_t, PluginContextPtr> PluginService::GetPluginContext(const std::string& pluginName)
292 {
293 std::unique_lock<std::mutex> lock(mutex_);
294 CHECK_TRUE(nameIndex_.count(pluginName) > 0, std::make_pair(0, nullptr),
295 "GetPluginContext failed, plugin name `%s` not found!", pluginName.c_str());
296 uint32_t id = nameIndex_[pluginName];
297
298 CHECK_TRUE(pluginContext_.count(id) > 0, std::make_pair(id, nullptr), "plugin id %u not found!", id);
299 return std::make_pair(id, pluginContext_[id]);
300 }
301
GetPluginContextById(uint32_t id)302 PluginContextPtr PluginService::GetPluginContextById(uint32_t id)
303 {
304 std::unique_lock<std::mutex> lock(mutex_);
305 CHECK_TRUE(pluginContext_.count(id) > 0, nullptr, "plugin id %u not found!", id);
306 return pluginContext_[id];
307 }
308
AddPluginInfo(const PluginInfo & pluginInfo)309 bool PluginService::AddPluginInfo(const PluginInfo& pluginInfo)
310 {
311 if (nameIndex_.find(pluginInfo.name) == nameIndex_.end()) { // add new plugin
312 auto pluginCtx = std::make_shared<PluginContext>();
313 CHECK_NOTNULL(pluginCtx, false, "create PluginContext failed!");
314
315 ProfilerPluginCapability capability;
316 capability.set_path(pluginInfo.path);
317 capability.set_name(pluginInfo.name);
318 CHECK_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(capability), false,
319 "AddPluginInfo AddCapability FAIL");
320
321 pluginCtx->name = pluginInfo.name;
322 pluginCtx->path = pluginInfo.path;
323 pluginCtx->context = pluginInfo.context;
324 pluginCtx->config.set_name(pluginInfo.name);
325 pluginCtx->config.set_plugin_sha256(pluginInfo.sha256);
326 pluginCtx->profilerPluginState.set_name(pluginInfo.name);
327 pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
328 pluginCtx->sha256 = pluginInfo.sha256;
329 pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
330 pluginCtx->isStandaloneFileData = pluginInfo.isStandaloneFileData;
331 pluginCtx->outFileName = pluginInfo.outFileName;
332 pluginCtx->pluginVersion = pluginInfo.pluginVersion;
333
334 uint32_t pluginId = ++pluginIdCounter_;
335 std::unique_lock<std::mutex> lock(mutex_);
336 pluginContext_[pluginId] = pluginCtx;
337 nameIndex_[pluginInfo.name] = pluginId;
338 } else { // update sha256 or bufferSizeHint
339 std::unique_lock<std::mutex> lock(mutex_);
340 CHECK_TRUE(nameIndex_.count(pluginInfo.name) > 0, false, "plugin name %s not found!", pluginInfo.name.c_str());
341
342 uint32_t pluginId = nameIndex_[pluginInfo.name];
343 CHECK_TRUE(pluginContext_.count(pluginId) > 0, false, "plugin id %u not found!", pluginId);
344 auto pluginCtx = pluginContext_[pluginId];
345
346 if (pluginInfo.sha256 != "") {
347 pluginCtx->sha256 = pluginInfo.sha256;
348 }
349 if (pluginInfo.bufferSizeHint != 0) {
350 pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
351 }
352 if (pluginInfo.isStandaloneFileData != false) {
353 pluginCtx->isStandaloneFileData = pluginInfo.isStandaloneFileData;
354 }
355 if (pluginInfo.outFileName != "") {
356 pluginCtx->outFileName = pluginInfo.outFileName;
357 }
358 if (pluginInfo.pluginVersion != "") {
359 pluginCtx->pluginVersion = pluginInfo.pluginVersion;
360 }
361 }
362 HILOG_DEBUG(LOG_CORE, "AddPluginInfo for %s done!", pluginInfo.name.c_str());
363
364 return true;
365 }
366
GetPluginInfo(const std::string & pluginName,PluginInfo & pluginInfo)367 bool PluginService::GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo)
368 {
369 uint32_t pluginId = 0;
370 PluginContextPtr pluginCtx = nullptr;
371 std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
372 CHECK_TRUE(pluginId, false, "plugin name %s not found!", pluginName.c_str());
373 CHECK_TRUE(pluginCtx, false, "plugin id %u not found!", pluginId);
374
375 pluginInfo.id = pluginId;
376 pluginInfo.name = pluginCtx->name;
377 pluginInfo.path = pluginCtx->path;
378 pluginInfo.sha256 = pluginCtx->sha256;
379 pluginInfo.bufferSizeHint = pluginCtx->bufferSizeHint;
380 return true;
381 }
382
RemovePluginInfo(const PluginInfo & pluginInfo)383 bool PluginService::RemovePluginInfo(const PluginInfo& pluginInfo)
384 {
385 uint32_t pluginId = pluginInfo.id;
386 PluginContextPtr pluginCtx = GetPluginContextById(pluginId);
387 CHECK_NOTNULL(pluginCtx, false, "RemovePluginInfo failed, id %d not found!", pluginId);
388
389 std::string pluginName = pluginCtx->config.name();
390 CHECK_TRUE(ProfilerCapabilityManager::GetInstance().RemoveCapability(pluginName), false,
391 "RemovePluginInfo RemoveCapability FAIL %d", pluginId);
392
393 auto pluginState = pluginCtx->profilerPluginState.state();
394 if (pluginState == ProfilerPluginState::LOADED || pluginState == ProfilerPluginState::IN_SESSION) {
395 std::vector<std::string> pluginNames = {pluginName};
396 pluginSessionManager_->InvalidatePluginSessions(pluginNames);
397 pluginSessionManager_->RemovePluginSessions(pluginNames);
398 this->RemovePluginSessionCtx(pluginName);
399 }
400
401 std::unique_lock<std::mutex> lock(mutex_);
402 nameIndex_.erase(pluginName);
403 pluginContext_.erase(pluginId);
404 HILOG_DEBUG(LOG_CORE, "RemovePluginInfo for %s done!", pluginName.c_str());
405 return true;
406 }
407
ReadShareMemoryOffline(PluginContext & context)408 void PluginService::ReadShareMemoryOffline(PluginContext& context)
409 {
410 CHECK_NOTNULL(context.shareMemoryBlock, NO_RETVAL, "smb of %s is null!", context.path.c_str());
411 CHECK_NOTNULL(traceWriter_, NO_RETVAL, "traceWriter_ is null!");
412 if (context.eventNotifier) {
413 context.eventNotifier->Take();
414 }
415
416 uint32_t stopTakeDataSize = 0;
417 while (true) {
418 int retval = 0;
419 bool ret = context.shareMemoryBlock->TakeData([&](const int8_t data[], uint32_t size) -> bool {
420 retval = traceWriter_->Write(data, size);
421 CHECK_TRUE(retval != -1, false, "need to splite file");
422 CHECK_TRUE(retval > 0, false, "write %d bytes failed!", size);
423 return true;
424 });
425
426 if (retval == -1) {
427 HILOG_DEBUG(LOG_CORE, "need to clear share memory block and report the basic data");
428 pluginSessionManager_->RefreshPluginSession();
429 break;
430 }
431
432 dataFlushSize_ += static_cast<uint32_t>(retval);
433 stopTakeDataSize += static_cast<uint32_t>(retval);
434 if (stopTakeDataSize > STOP_BASELINE) {
435 traceWriter_->Flush();
436 break;
437 } else if (dataFlushSize_ > FLUSH_BASELINE) {
438 traceWriter_->Flush();
439 dataFlushSize_ = 0;
440 }
441
442 if (!ret) { // no data to read
443 break;
444 }
445 }
446 }
447
ReadShareMemoryOnline(PluginContext & context)448 void PluginService::ReadShareMemoryOnline(PluginContext& context)
449 {
450 CHECK_NOTNULL(context.shareMemoryBlock, NO_RETVAL, "smb of %s is null!", context.path.c_str());
451 if (context.eventNotifier) {
452 context.eventNotifier->Take();
453 }
454
455 while (true) {
456 auto pluginData = std::make_shared<ProfilerPluginData>();
457 bool ret = context.shareMemoryBlock->TakeData([&](const int8_t data[], uint32_t size) -> bool {
458 int retval = pluginData->ParseFromArray(reinterpret_cast<const char*>(data), size);
459 CHECK_TRUE(retval, false, "parse %d bytes failed!", size);
460 return true;
461 });
462 if (!ret) {
463 break;
464 }
465 if (!context.profilerDataRepeater->PutPluginData(pluginData)) {
466 break;
467 }
468 }
469 }
470
AppendResult(NotifyResultRequest & request)471 bool PluginService::AppendResult(NotifyResultRequest& request)
472 {
473 pluginCommandBuilder_->GetedCommandResponse(request.command_id());
474 auto sem = GetSemaphore(request.command_id());
475 if (sem) {
476 sem->Post();
477 }
478
479 int size = request.result_size();
480 HILOG_DEBUG(LOG_CORE, "AppendResult size:%d, cmd id:%d", size, request.command_id());
481 for (int i = 0; i < size; i++) {
482 PluginResult pr = request.result(i);
483 if (pr.data().size() > 0) {
484 HILOG_DEBUG(LOG_CORE, "AppendResult Size : %zu", pr.data().size());
485 uint32_t pluginId = pr.plugin_id();
486 PluginContextPtr pluginCtx = GetPluginContextById(pluginId);
487 CHECK_NOTNULL(pluginCtx, false, "plugin id %u not found!", pluginId);
488 if (pluginCtx->profilerDataRepeater == nullptr) {
489 HILOG_DEBUG(LOG_CORE, "AppendResult profilerDataRepeater==nullptr %s %d", pr.status().name().c_str(),
490 pluginId);
491 return false;
492 }
493 auto pluginData = std::make_shared<ProfilerPluginData>();
494 pluginData->set_name(pr.status().name());
495 pluginData->set_status(0);
496 pluginData->set_data(pr.data());
497 if (!pluginCtx->profilerDataRepeater->PutPluginData(pluginData)) {
498 return false;
499 }
500 } else if (pr.out_file_name() != "") { // updata plugin outFileName
501 std::unique_lock<std::mutex> lock(mutex_);
502 auto pluginId = pr.plugin_id();
503 CHECK_TRUE(pluginContext_.count(pluginId) > 0, false, "plugin id %u not found!", pluginId);
504 pluginContext_[pluginId]->outFileName = pr.out_file_name();
505 } else {
506 HILOG_DEBUG(LOG_CORE, "Flush?Data From ShareMemory?");
507 }
508 }
509 return true;
510 }
511
GetPluginStatus()512 std::vector<ProfilerPluginState> PluginService::GetPluginStatus()
513 {
514 std::vector<ProfilerPluginState> status;
515 std::unique_lock<std::mutex> lock(mutex_);
516 for (auto& entry : pluginContext_) {
517 status.push_back(entry.second->profilerPluginState);
518 }
519 return status;
520 }
521
GetPluginIdByName(std::string name)522 uint32_t PluginService::GetPluginIdByName(std::string name)
523 {
524 std::unique_lock<std::mutex> lock(mutex_);
525 if (nameIndex_.find(name) == nameIndex_.end()) {
526 return 0;
527 }
528 return nameIndex_[name];
529 }
530
SetTraceWriter(const TraceFileWriterPtr & traceWriter)531 void PluginService::SetTraceWriter(const TraceFileWriterPtr& traceWriter)
532 {
533 traceWriter_ = traceWriter;
534 }