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