106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#include "plugin_service.h"
1706f6ba60Sopenharmony_ci
1806f6ba60Sopenharmony_ci#include <cinttypes>
1906f6ba60Sopenharmony_ci#include <fcntl.h>
2006f6ba60Sopenharmony_ci#include <sys/wait.h>
2106f6ba60Sopenharmony_ci#include <unistd.h>
2206f6ba60Sopenharmony_ci
2306f6ba60Sopenharmony_ci#include "plugin_command_builder.h"
2406f6ba60Sopenharmony_ci#include "plugin_service_impl.h"
2506f6ba60Sopenharmony_ci#include "plugin_session_manager.h"
2606f6ba60Sopenharmony_ci#include "profiler_capability_manager.h"
2706f6ba60Sopenharmony_ci#include "profiler_data_repeater.h"
2806f6ba60Sopenharmony_ci#include "securec.h"
2906f6ba60Sopenharmony_ci#include "share_memory_allocator.h"
3006f6ba60Sopenharmony_ci#include "socket_context.h"
3106f6ba60Sopenharmony_ci
3206f6ba60Sopenharmony_cinamespace {
3306f6ba60Sopenharmony_ciconst int PAGE_BYTES = 4096;
3406f6ba60Sopenharmony_ciconst int DEFAULT_EVENT_POLLING_INTERVAL = 5000;
3506f6ba60Sopenharmony_ciconstexpr uint32_t FLUSH_BASELINE = (1U << 21); // need to flush data size with offline mode
3606f6ba60Sopenharmony_ciconstexpr uint32_t STOP_BASELINE = (1U << 22); // need to stop take data size with offline mode
3706f6ba60Sopenharmony_ci} // namespace
3806f6ba60Sopenharmony_ci
3906f6ba60Sopenharmony_ciPluginService::PluginService()
4006f6ba60Sopenharmony_ci{
4106f6ba60Sopenharmony_ci    pluginIdCounter_ = 0;
4206f6ba60Sopenharmony_ci    if (getuid() == 0) {
4306f6ba60Sopenharmony_ci        StartService(DEFAULT_UNIX_SOCKET_FULL_PATH);
4406f6ba60Sopenharmony_ci    } else {
4506f6ba60Sopenharmony_ci        StartService(DEFAULT_UNIX_SOCKET_PATH);
4606f6ba60Sopenharmony_ci    }
4706f6ba60Sopenharmony_ci
4806f6ba60Sopenharmony_ci    pluginCommandBuilder_ = std::make_shared<PluginCommandBuilder>();
4906f6ba60Sopenharmony_ci
5006f6ba60Sopenharmony_ci    eventPoller_ = std::make_unique<EpollEventPoller>(DEFAULT_EVENT_POLLING_INTERVAL);
5106f6ba60Sopenharmony_ci    CHECK_NOTNULL(eventPoller_, NO_RETVAL, "create event poller FAILED!");
5206f6ba60Sopenharmony_ci
5306f6ba60Sopenharmony_ci    eventPoller_->Init();
5406f6ba60Sopenharmony_ci}
5506f6ba60Sopenharmony_ci
5606f6ba60Sopenharmony_ciPluginService::~PluginService()
5706f6ba60Sopenharmony_ci{
5806f6ba60Sopenharmony_ci    if (eventPoller_) {
5906f6ba60Sopenharmony_ci        eventPoller_->Stop();
6006f6ba60Sopenharmony_ci        eventPoller_->Finalize();
6106f6ba60Sopenharmony_ci    }
6206f6ba60Sopenharmony_ci}
6306f6ba60Sopenharmony_ci
6406f6ba60Sopenharmony_cibool PluginService::StartEpollThread()
6506f6ba60Sopenharmony_ci{
6606f6ba60Sopenharmony_ci    if (eventPoller_) {
6706f6ba60Sopenharmony_ci        return eventPoller_->Start();
6806f6ba60Sopenharmony_ci    }
6906f6ba60Sopenharmony_ci    return false;
7006f6ba60Sopenharmony_ci}
7106f6ba60Sopenharmony_ci
7206f6ba60Sopenharmony_cibool PluginService::StopEpollThread()
7306f6ba60Sopenharmony_ci{
7406f6ba60Sopenharmony_ci    if (eventPoller_) {
7506f6ba60Sopenharmony_ci        return eventPoller_->Stop();
7606f6ba60Sopenharmony_ci    }
7706f6ba60Sopenharmony_ci    return false;
7806f6ba60Sopenharmony_ci}
7906f6ba60Sopenharmony_ci
8006f6ba60Sopenharmony_civoid PluginService::SetPluginSessionManager(const PluginSessionManagerPtr& pluginSessionManager)
8106f6ba60Sopenharmony_ci{
8206f6ba60Sopenharmony_ci    pluginSessionManager_ = pluginSessionManager;
8306f6ba60Sopenharmony_ci}
8406f6ba60Sopenharmony_ci
8506f6ba60Sopenharmony_civoid PluginService::SetProfilerSessionConfig(const std::shared_ptr<ProfilerSessionConfig> profilerSessionConfig,
8606f6ba60Sopenharmony_ci                                             const std::vector<std::string>& pluginNames)
8706f6ba60Sopenharmony_ci{
8806f6ba60Sopenharmony_ci    for (const std::string& name : pluginNames) {
8906f6ba60Sopenharmony_ci        profilerSessionConfigs_[name] = profilerSessionConfig;
9006f6ba60Sopenharmony_ci    }
9106f6ba60Sopenharmony_ci}
9206f6ba60Sopenharmony_ci
9306f6ba60Sopenharmony_ciSemaphorePtr PluginService::GetSemaphore(uint32_t id) const
9406f6ba60Sopenharmony_ci{
9506f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
9606f6ba60Sopenharmony_ci    auto it = waitSemphores_.find(id);
9706f6ba60Sopenharmony_ci    if (it != waitSemphores_.end()) {
9806f6ba60Sopenharmony_ci        return it->second;
9906f6ba60Sopenharmony_ci    }
10006f6ba60Sopenharmony_ci    return nullptr;
10106f6ba60Sopenharmony_ci}
10206f6ba60Sopenharmony_ci
10306f6ba60Sopenharmony_cibool PluginService::StartService(const std::string& unixSocketName)
10406f6ba60Sopenharmony_ci{
10506f6ba60Sopenharmony_ci    pluginServiceImpl_ = std::make_shared<PluginServiceImpl>(*this);
10606f6ba60Sopenharmony_ci    serviceEntry_ = std::make_shared<ServiceEntry>();
10706f6ba60Sopenharmony_ci    if (!serviceEntry_->StartServer(unixSocketName)) {
10806f6ba60Sopenharmony_ci        pluginServiceImpl_ = nullptr;
10906f6ba60Sopenharmony_ci        serviceEntry_ = nullptr;
11006f6ba60Sopenharmony_ci        PROFILER_LOG_DEBUG(LOG_CORE, "Start IPC Service FAIL");
11106f6ba60Sopenharmony_ci        return false;
11206f6ba60Sopenharmony_ci    }
11306f6ba60Sopenharmony_ci    serviceEntry_->RegisterService(*pluginServiceImpl_.get());
11406f6ba60Sopenharmony_ci    return true;
11506f6ba60Sopenharmony_ci}
11606f6ba60Sopenharmony_ci
11706f6ba60Sopenharmony_cistatic ShareMemoryBlock::ReusePolicy GetReusePolicy(const ProfilerSessionConfig::BufferConfig& bufferConfig)
11806f6ba60Sopenharmony_ci{
11906f6ba60Sopenharmony_ci    if (bufferConfig.policy() == ProfilerSessionConfig::BufferConfig::RECYCLE) {
12006f6ba60Sopenharmony_ci        return ShareMemoryBlock::DROP_OLD;
12106f6ba60Sopenharmony_ci    }
12206f6ba60Sopenharmony_ci    return ShareMemoryBlock::DROP_NONE;
12306f6ba60Sopenharmony_ci}
12406f6ba60Sopenharmony_ci
12506f6ba60Sopenharmony_ci// create plugin session with buffer config
12606f6ba60Sopenharmony_cibool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
12706f6ba60Sopenharmony_ci                                        const ProfilerSessionConfig::BufferConfig& bufferConfig,
12806f6ba60Sopenharmony_ci                                        const ProfilerDataRepeaterPtr& dataRepeater)
12906f6ba60Sopenharmony_ci{
13006f6ba60Sopenharmony_ci    uint32_t pluginId = 0;
13106f6ba60Sopenharmony_ci    PluginContextPtr pluginCtx = nullptr;
13206f6ba60Sopenharmony_ci    std::string pluginName = pluginConfig.name();
13306f6ba60Sopenharmony_ci    isProtobufSerialize_ = pluginConfig.is_protobuf_serialize();
13406f6ba60Sopenharmony_ci    std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
13506f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
13606f6ba60Sopenharmony_ci
13706f6ba60Sopenharmony_ci    pluginCtx->profilerDataRepeater = dataRepeater;
13806f6ba60Sopenharmony_ci
13906f6ba60Sopenharmony_ci    uint32_t bufferSize = bufferConfig.pages() * PAGE_BYTES;
14006f6ba60Sopenharmony_ci    auto cmd = pluginCommandBuilder_->BuildCreateSessionCmd(pluginConfig, bufferSize);
14106f6ba60Sopenharmony_ci    CHECK_TRUE(cmd != nullptr, false, "CreatePluginSession BuildCreateSessionCmd FAIL %s", pluginName.c_str());
14206f6ba60Sopenharmony_ci
14306f6ba60Sopenharmony_ci    auto smb = ShareMemoryAllocator::GetInstance().CreateMemoryBlockLocal(pluginName, bufferSize);
14406f6ba60Sopenharmony_ci    CHECK_TRUE(smb != nullptr, false, "CreateMemoryBlockLocal FAIL %s", pluginName.c_str());
14506f6ba60Sopenharmony_ci
14606f6ba60Sopenharmony_ci    auto policy = GetReusePolicy(bufferConfig);
14706f6ba60Sopenharmony_ci    PROFILER_LOG_DEBUG(LOG_CORE, "CreatePluginSession policy = %d", (int)policy);
14806f6ba60Sopenharmony_ci    smb->SetReusePolicy(policy);
14906f6ba60Sopenharmony_ci
15006f6ba60Sopenharmony_ci    auto notifier = EventNotifier::Create(0, EventNotifier::NONBLOCK);
15106f6ba60Sopenharmony_ci    CHECK_NOTNULL(notifier, false, "create EventNotifier for %s failed!", pluginName.c_str());
15206f6ba60Sopenharmony_ci    CHECK_TRUE(pluginServiceImpl_->SendHeartBeat(*pluginCtx->context), false,
15306f6ba60Sopenharmony_ci               "%s hiprofiler_plugin process is off line!", __func__);
15406f6ba60Sopenharmony_ci    pluginCtx->shareMemoryBlock = smb;
15506f6ba60Sopenharmony_ci    pluginCtx->eventNotifier = notifier;
15606f6ba60Sopenharmony_ci    pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
15706f6ba60Sopenharmony_ci    pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
15806f6ba60Sopenharmony_ci    pluginCtx->context->SendFileDescriptor(smb->GetfileDescriptor());
15906f6ba60Sopenharmony_ci    pluginCtx->context->SendFileDescriptor(notifier->GetFd());
16006f6ba60Sopenharmony_ci
16106f6ba60Sopenharmony_ci    auto configIter = profilerSessionConfigs_.find(pluginName);
16206f6ba60Sopenharmony_ci    if (configIter == profilerSessionConfigs_.end()) {
16306f6ba60Sopenharmony_ci        PROFILER_LOG_ERROR(LOG_CORE, "profiler session config not set fot plugin name: %s", pluginName.c_str());
16406f6ba60Sopenharmony_ci        return false;
16506f6ba60Sopenharmony_ci    }
16606f6ba60Sopenharmony_ci    if (configIter->second->session_mode() == ProfilerSessionConfig::OFFLINE) {
16706f6ba60Sopenharmony_ci        eventPoller_->AddFileDescriptor(notifier->GetFd(),
16806f6ba60Sopenharmony_ci                                        [this, pluginCtx] { this->ReadShareMemoryOffline(*pluginCtx); });
16906f6ba60Sopenharmony_ci    } else if (configIter->second->session_mode() == ProfilerSessionConfig::ONLINE) {
17006f6ba60Sopenharmony_ci        eventPoller_->AddFileDescriptor(notifier->GetFd(),
17106f6ba60Sopenharmony_ci                                        [this, pluginCtx] { this->ReadShareMemoryOnline(*pluginCtx); });
17206f6ba60Sopenharmony_ci    }
17306f6ba60Sopenharmony_ci    PROFILER_LOG_DEBUG(LOG_CORE, "CreatePluginSession %s done, shmem fd = %d",
17406f6ba60Sopenharmony_ci                       pluginName.c_str(), smb->GetfileDescriptor());
17506f6ba60Sopenharmony_ci    return true;
17606f6ba60Sopenharmony_ci}
17706f6ba60Sopenharmony_ci
17806f6ba60Sopenharmony_ci// create plugin session without buffer config
17906f6ba60Sopenharmony_cibool PluginService::CreatePluginSession(const ProfilerPluginConfig& pluginConfig,
18006f6ba60Sopenharmony_ci                                        const ProfilerDataRepeaterPtr& dataRepeater)
18106f6ba60Sopenharmony_ci{
18206f6ba60Sopenharmony_ci    uint32_t pluginId = 0;
18306f6ba60Sopenharmony_ci    PluginContextPtr pluginCtx = nullptr;
18406f6ba60Sopenharmony_ci    std::string pluginName = pluginConfig.name();
18506f6ba60Sopenharmony_ci    std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
18606f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
18706f6ba60Sopenharmony_ci
18806f6ba60Sopenharmony_ci    pluginCtx->profilerDataRepeater = dataRepeater;
18906f6ba60Sopenharmony_ci    pluginCtx->shareMemoryBlock = nullptr;
19006f6ba60Sopenharmony_ci
19106f6ba60Sopenharmony_ci    auto cmd = pluginCommandBuilder_->BuildCreateSessionCmd(pluginConfig, 0);
19206f6ba60Sopenharmony_ci    CHECK_TRUE(cmd != nullptr, false, "CreatePluginSession BuildCreateSessionCmd FAIL %s", pluginName.c_str());
19306f6ba60Sopenharmony_ci    CHECK_TRUE(pluginServiceImpl_->SendHeartBeat(*pluginCtx->context), false,
19406f6ba60Sopenharmony_ci               "%s hiprofiler_plugin process is off line!", __func__);
19506f6ba60Sopenharmony_ci    pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
19606f6ba60Sopenharmony_ci    pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
19706f6ba60Sopenharmony_ci    PROFILER_LOG_DEBUG(LOG_CORE, "CreatePluginSession %s done!", pluginName.c_str());
19806f6ba60Sopenharmony_ci    return true;
19906f6ba60Sopenharmony_ci}
20006f6ba60Sopenharmony_ci
20106f6ba60Sopenharmony_cibool PluginService::StartPluginSession(const ProfilerPluginConfig& config)
20206f6ba60Sopenharmony_ci{
20306f6ba60Sopenharmony_ci    uint32_t pluginId = 0;
20406f6ba60Sopenharmony_ci    PluginContextPtr pluginCtx = nullptr;
20506f6ba60Sopenharmony_ci    std::string pluginName = config.name();
20606f6ba60Sopenharmony_ci    std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
20706f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
20806f6ba60Sopenharmony_ci
20906f6ba60Sopenharmony_ci    auto cmd = pluginCommandBuilder_->BuildStartSessionCmd(config, pluginId);
21006f6ba60Sopenharmony_ci    CHECK_TRUE(cmd != nullptr, false, "StartPluginSession BuildStartSessionCmd FAIL %s", pluginName.c_str());
21106f6ba60Sopenharmony_ci    CHECK_TRUE(pluginServiceImpl_->SendHeartBeat(*pluginCtx->context), false,
21206f6ba60Sopenharmony_ci               "%s hiprofiler_plugin process is off line!", __func__);
21306f6ba60Sopenharmony_ci    pluginCtx->profilerPluginState.set_state(ProfilerPluginState::IN_SESSION);
21406f6ba60Sopenharmony_ci    pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
21506f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "StartPluginSession %s done!", pluginName.c_str());
21606f6ba60Sopenharmony_ci    return true;
21706f6ba60Sopenharmony_ci}
21806f6ba60Sopenharmony_ci
21906f6ba60Sopenharmony_cibool PluginService::StopPluginSession(const std::string& pluginName)
22006f6ba60Sopenharmony_ci{
22106f6ba60Sopenharmony_ci    uint32_t pluginId = 0;
22206f6ba60Sopenharmony_ci    PluginContextPtr pluginCtx = nullptr;
22306f6ba60Sopenharmony_ci    std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
22406f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
22506f6ba60Sopenharmony_ci
22606f6ba60Sopenharmony_ci    auto cmd = pluginCommandBuilder_->BuildStopSessionCmd(pluginId);
22706f6ba60Sopenharmony_ci    CHECK_TRUE(cmd != nullptr, false, "StopPluginSession BuildStopSessionCmd FAIL %s", pluginName.c_str());
22806f6ba60Sopenharmony_ci    CHECK_TRUE(pluginServiceImpl_->SendHeartBeat(*pluginCtx->context), false,
22906f6ba60Sopenharmony_ci               "%s hiprofiler_plugin process is off line!", __func__);
23006f6ba60Sopenharmony_ci    pluginCtx->profilerPluginState.set_state(ProfilerPluginState::LOADED);
23106f6ba60Sopenharmony_ci    pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
23206f6ba60Sopenharmony_ci    auto sem = GetSemaphoreFactory().Create(0);
23306f6ba60Sopenharmony_ci    CHECK_NOTNULL(sem, false, "create Semaphore for stop %s FAILED!", pluginName.c_str());
23406f6ba60Sopenharmony_ci
23506f6ba60Sopenharmony_ci    waitSemphores_[cmd->command_id()] = sem;
23606f6ba60Sopenharmony_ci    PROFILER_LOG_DEBUG(LOG_CORE, "=== StopPluginSession %s Waiting ... ===", pluginName.c_str());
23706f6ba60Sopenharmony_ci    // wait on semaphore at most 30 seconds.
23806f6ba60Sopenharmony_ci    if (!sem->TimedWait(30)) {
23906f6ba60Sopenharmony_ci        // semaphore timeout
24006f6ba60Sopenharmony_ci        PROFILER_LOG_DEBUG(LOG_CORE, "=== StopPluginSession Waiting FAIL ===");
24106f6ba60Sopenharmony_ci        return false;
24206f6ba60Sopenharmony_ci    }
24306f6ba60Sopenharmony_ci    PROFILER_LOG_DEBUG(LOG_CORE, "StopPluginSession %s done!", pluginName.c_str());
24406f6ba60Sopenharmony_ci    return true;
24506f6ba60Sopenharmony_ci}
24606f6ba60Sopenharmony_ci
24706f6ba60Sopenharmony_cibool PluginService::DestroyPluginSession(const std::string& pluginName)
24806f6ba60Sopenharmony_ci{
24906f6ba60Sopenharmony_ci    uint32_t pluginId = 0;
25006f6ba60Sopenharmony_ci    PluginContextPtr pluginCtx = nullptr;
25106f6ba60Sopenharmony_ci    std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
25206f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
25306f6ba60Sopenharmony_ci
25406f6ba60Sopenharmony_ci    auto cmd = pluginCommandBuilder_->BuildDestroySessionCmd(pluginId);
25506f6ba60Sopenharmony_ci    CHECK_TRUE(cmd != nullptr, false, "DestroyPluginSession BuildDestroySessionCmd FAIL %s", pluginName.c_str());
25606f6ba60Sopenharmony_ci
25706f6ba60Sopenharmony_ci    if (profilerSessionConfigs_.find(pluginName) != profilerSessionConfigs_.end()) {
25806f6ba60Sopenharmony_ci        profilerSessionConfigs_.erase(profilerSessionConfigs_.find(pluginName));
25906f6ba60Sopenharmony_ci    }
26006f6ba60Sopenharmony_ci    if (pluginCtx->shareMemoryBlock) {
26106f6ba60Sopenharmony_ci        ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockLocal(pluginName);
26206f6ba60Sopenharmony_ci    }
26306f6ba60Sopenharmony_ci
26406f6ba60Sopenharmony_ci    if (pluginCtx->eventNotifier) {
26506f6ba60Sopenharmony_ci        eventPoller_->RemoveFileDescriptor(pluginCtx->eventNotifier->GetFd());
26606f6ba60Sopenharmony_ci        pluginCtx->eventNotifier = nullptr;
26706f6ba60Sopenharmony_ci    }
26806f6ba60Sopenharmony_ci    CHECK_TRUE(pluginServiceImpl_->SendHeartBeat(*pluginCtx->context), false,
26906f6ba60Sopenharmony_ci               "%s hiprofiler_plugin process is off line!", __func__);
27006f6ba60Sopenharmony_ci    pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
27106f6ba60Sopenharmony_ci    pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
27206f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "DestroyPluginSession %s done!", pluginName.c_str());
27306f6ba60Sopenharmony_ci    return true;
27406f6ba60Sopenharmony_ci}
27506f6ba60Sopenharmony_ci
27606f6ba60Sopenharmony_cibool PluginService::RefreshPluginSession(const std::string& pluginName)
27706f6ba60Sopenharmony_ci{
27806f6ba60Sopenharmony_ci    uint32_t pluginId = 0;
27906f6ba60Sopenharmony_ci    PluginContextPtr pluginCtx = nullptr;
28006f6ba60Sopenharmony_ci    std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
28106f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
28206f6ba60Sopenharmony_ci
28306f6ba60Sopenharmony_ci    auto cmd = pluginCommandBuilder_->BuildRefreshSessionCmd(pluginId);
28406f6ba60Sopenharmony_ci    CHECK_TRUE(cmd != nullptr, false, "RefreshPluginSession BuildRefreshSessionCmd FAIL %s", pluginName.c_str());
28506f6ba60Sopenharmony_ci    CHECK_TRUE(pluginServiceImpl_->SendHeartBeat(*pluginCtx->context), false,
28606f6ba60Sopenharmony_ci               "%s hiprofiler_plugin process is off line!", __func__);
28706f6ba60Sopenharmony_ci    pluginServiceImpl_->PushCommand(*pluginCtx->context, cmd);
28806f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "RefreshPluginSession %s done!", pluginName.c_str());
28906f6ba60Sopenharmony_ci    return true;
29006f6ba60Sopenharmony_ci}
29106f6ba60Sopenharmony_ci
29206f6ba60Sopenharmony_cibool PluginService::RemovePluginSessionCtx(const std::string& pluginName)
29306f6ba60Sopenharmony_ci{
29406f6ba60Sopenharmony_ci    PluginContextPtr pluginCtx = GetPluginContext(pluginName).second;
29506f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginCtx, false, "get PluginContext failed!");
29606f6ba60Sopenharmony_ci
29706f6ba60Sopenharmony_ci    if (pluginCtx->shareMemoryBlock) {
29806f6ba60Sopenharmony_ci        ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockLocal(pluginName);
29906f6ba60Sopenharmony_ci        pluginCtx->shareMemoryBlock = nullptr;
30006f6ba60Sopenharmony_ci    }
30106f6ba60Sopenharmony_ci
30206f6ba60Sopenharmony_ci    if (pluginCtx->eventNotifier) {
30306f6ba60Sopenharmony_ci        eventPoller_->RemoveFileDescriptor(pluginCtx->eventNotifier->GetFd());
30406f6ba60Sopenharmony_ci        pluginCtx->eventNotifier = nullptr;
30506f6ba60Sopenharmony_ci    }
30606f6ba60Sopenharmony_ci
30706f6ba60Sopenharmony_ci    pluginCtx->profilerPluginState.set_state(ProfilerPluginState::INITED);
30806f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "RemovePluginSessionCtx %s done!", pluginName.c_str());
30906f6ba60Sopenharmony_ci    return true;
31006f6ba60Sopenharmony_ci}
31106f6ba60Sopenharmony_ci
31206f6ba60Sopenharmony_cistd::pair<uint32_t, PluginContextPtr> PluginService::GetPluginContext(const std::string& pluginName)
31306f6ba60Sopenharmony_ci{
31406f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
31506f6ba60Sopenharmony_ci    CHECK_TRUE(nameIndex_.count(pluginName) > 0, std::make_pair(0, nullptr),
31606f6ba60Sopenharmony_ci               "GetPluginContext failed, plugin name `%s` not found!", pluginName.c_str());
31706f6ba60Sopenharmony_ci    uint32_t id = nameIndex_[pluginName];
31806f6ba60Sopenharmony_ci
31906f6ba60Sopenharmony_ci    CHECK_TRUE(pluginContext_.count(id) > 0, std::make_pair(id, nullptr), "plugin id %u not found!", id);
32006f6ba60Sopenharmony_ci    return std::make_pair(id, pluginContext_[id]);
32106f6ba60Sopenharmony_ci}
32206f6ba60Sopenharmony_ci
32306f6ba60Sopenharmony_ciPluginContextPtr PluginService::GetPluginContextById(uint32_t id)
32406f6ba60Sopenharmony_ci{
32506f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
32606f6ba60Sopenharmony_ci    CHECK_TRUE(pluginContext_.count(id) > 0, nullptr, "plugin id %u not found!", id);
32706f6ba60Sopenharmony_ci    return pluginContext_[id];
32806f6ba60Sopenharmony_ci}
32906f6ba60Sopenharmony_ci
33006f6ba60Sopenharmony_cibool PluginService::AddPluginInfo(const PluginInfo& pluginInfo)
33106f6ba60Sopenharmony_ci{
33206f6ba60Sopenharmony_ci    if (nameIndex_.find(pluginInfo.name) == nameIndex_.end()) { // add new plugin
33306f6ba60Sopenharmony_ci        auto pluginCtx = std::make_shared<PluginContext>();
33406f6ba60Sopenharmony_ci        CHECK_NOTNULL(pluginCtx, false, "create PluginContext failed!");
33506f6ba60Sopenharmony_ci
33606f6ba60Sopenharmony_ci        ProfilerPluginCapability capability;
33706f6ba60Sopenharmony_ci        capability.set_path(pluginInfo.path);
33806f6ba60Sopenharmony_ci        capability.set_name(pluginInfo.name);
33906f6ba60Sopenharmony_ci        CHECK_TRUE(ProfilerCapabilityManager::GetInstance().AddCapability(capability), false,
34006f6ba60Sopenharmony_ci                   "AddPluginInfo AddCapability FAIL");
34106f6ba60Sopenharmony_ci
34206f6ba60Sopenharmony_ci        pluginCtx->name = pluginInfo.name;
34306f6ba60Sopenharmony_ci        pluginCtx->path = pluginInfo.path;
34406f6ba60Sopenharmony_ci        pluginCtx->context = pluginInfo.context;
34506f6ba60Sopenharmony_ci        pluginCtx->config.set_name(pluginInfo.name);
34606f6ba60Sopenharmony_ci        pluginCtx->config.set_plugin_sha256(pluginInfo.sha256);
34706f6ba60Sopenharmony_ci        pluginCtx->profilerPluginState.set_name(pluginInfo.name);
34806f6ba60Sopenharmony_ci        pluginCtx->profilerPluginState.set_state(ProfilerPluginState::REGISTERED);
34906f6ba60Sopenharmony_ci        pluginCtx->sha256 = pluginInfo.sha256;
35006f6ba60Sopenharmony_ci        pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
35106f6ba60Sopenharmony_ci        pluginCtx->isStandaloneFileData = pluginInfo.isStandaloneFileData;
35206f6ba60Sopenharmony_ci        pluginCtx->outFileName = pluginInfo.outFileName;
35306f6ba60Sopenharmony_ci        pluginCtx->pluginVersion = pluginInfo.pluginVersion;
35406f6ba60Sopenharmony_ci
35506f6ba60Sopenharmony_ci        uint32_t pluginId = ++pluginIdCounter_;
35606f6ba60Sopenharmony_ci        std::unique_lock<std::mutex> lock(mutex_);
35706f6ba60Sopenharmony_ci        pluginContext_[pluginId] = pluginCtx;
35806f6ba60Sopenharmony_ci        nameIndex_[pluginInfo.name] = pluginId;
35906f6ba60Sopenharmony_ci    } else { // update sha256 or bufferSizeHint
36006f6ba60Sopenharmony_ci        std::unique_lock<std::mutex> lock(mutex_);
36106f6ba60Sopenharmony_ci        CHECK_TRUE(nameIndex_.count(pluginInfo.name) > 0, false, "plugin name %s not found!", pluginInfo.name.c_str());
36206f6ba60Sopenharmony_ci
36306f6ba60Sopenharmony_ci        uint32_t pluginId = nameIndex_[pluginInfo.name];
36406f6ba60Sopenharmony_ci        CHECK_TRUE(pluginContext_.count(pluginId) > 0, false, "plugin id %u not found!", pluginId);
36506f6ba60Sopenharmony_ci        auto pluginCtx = pluginContext_[pluginId];
36606f6ba60Sopenharmony_ci
36706f6ba60Sopenharmony_ci        if (pluginInfo.sha256 != "") {
36806f6ba60Sopenharmony_ci            pluginCtx->sha256 = pluginInfo.sha256;
36906f6ba60Sopenharmony_ci        }
37006f6ba60Sopenharmony_ci        if (pluginInfo.bufferSizeHint != 0) {
37106f6ba60Sopenharmony_ci            pluginCtx->bufferSizeHint = pluginInfo.bufferSizeHint;
37206f6ba60Sopenharmony_ci        }
37306f6ba60Sopenharmony_ci        if (pluginInfo.isStandaloneFileData != false) {
37406f6ba60Sopenharmony_ci            pluginCtx->isStandaloneFileData = pluginInfo.isStandaloneFileData;
37506f6ba60Sopenharmony_ci        }
37606f6ba60Sopenharmony_ci        if (pluginInfo.outFileName != "") {
37706f6ba60Sopenharmony_ci            pluginCtx->outFileName = pluginInfo.outFileName;
37806f6ba60Sopenharmony_ci        }
37906f6ba60Sopenharmony_ci        if (pluginInfo.pluginVersion != "") {
38006f6ba60Sopenharmony_ci            pluginCtx->pluginVersion = pluginInfo.pluginVersion;
38106f6ba60Sopenharmony_ci        }
38206f6ba60Sopenharmony_ci    }
38306f6ba60Sopenharmony_ci    PROFILER_LOG_DEBUG(LOG_CORE, "AddPluginInfo for %s done!", pluginInfo.name.c_str());
38406f6ba60Sopenharmony_ci
38506f6ba60Sopenharmony_ci    return true;
38606f6ba60Sopenharmony_ci}
38706f6ba60Sopenharmony_ci
38806f6ba60Sopenharmony_cibool PluginService::GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo)
38906f6ba60Sopenharmony_ci{
39006f6ba60Sopenharmony_ci    uint32_t pluginId = 0;
39106f6ba60Sopenharmony_ci    PluginContextPtr pluginCtx = nullptr;
39206f6ba60Sopenharmony_ci    std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
39306f6ba60Sopenharmony_ci    CHECK_TRUE(pluginId, false, "plugin name %s not found!", pluginName.c_str());
39406f6ba60Sopenharmony_ci    CHECK_TRUE(pluginCtx, false, "plugin id %u not found!", pluginId);
39506f6ba60Sopenharmony_ci
39606f6ba60Sopenharmony_ci    pluginInfo.id = pluginId;
39706f6ba60Sopenharmony_ci    pluginInfo.name = pluginCtx->name;
39806f6ba60Sopenharmony_ci    pluginInfo.path = pluginCtx->path;
39906f6ba60Sopenharmony_ci    pluginInfo.sha256 = pluginCtx->sha256;
40006f6ba60Sopenharmony_ci    pluginInfo.bufferSizeHint = pluginCtx->bufferSizeHint;
40106f6ba60Sopenharmony_ci    return true;
40206f6ba60Sopenharmony_ci}
40306f6ba60Sopenharmony_ci
40406f6ba60Sopenharmony_cibool PluginService::RemovePluginInfo(const PluginInfo& pluginInfo)
40506f6ba60Sopenharmony_ci{
40606f6ba60Sopenharmony_ci    uint32_t pluginId = pluginInfo.id;
40706f6ba60Sopenharmony_ci    PluginContextPtr pluginCtx = GetPluginContextById(pluginId);
40806f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginCtx, false, "RemovePluginInfo failed, id %d not found!", pluginId);
40906f6ba60Sopenharmony_ci
41006f6ba60Sopenharmony_ci    std::string pluginName = pluginCtx->config.name();
41106f6ba60Sopenharmony_ci    CHECK_TRUE(ProfilerCapabilityManager::GetInstance().RemoveCapability(pluginName), false,
41206f6ba60Sopenharmony_ci               "RemovePluginInfo RemoveCapability FAIL %d", pluginId);
41306f6ba60Sopenharmony_ci
41406f6ba60Sopenharmony_ci    auto pluginState = pluginCtx->profilerPluginState.state();
41506f6ba60Sopenharmony_ci    if (pluginState == ProfilerPluginState::LOADED || pluginState == ProfilerPluginState::IN_SESSION) {
41606f6ba60Sopenharmony_ci        std::vector<std::string> pluginNames = {pluginName};
41706f6ba60Sopenharmony_ci        pluginSessionManager_->InvalidatePluginSessions(pluginNames);
41806f6ba60Sopenharmony_ci        pluginSessionManager_->RemovePluginSessions(pluginNames);
41906f6ba60Sopenharmony_ci        this->RemovePluginSessionCtx(pluginName);
42006f6ba60Sopenharmony_ci    }
42106f6ba60Sopenharmony_ci
42206f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
42306f6ba60Sopenharmony_ci    nameIndex_.erase(pluginName);
42406f6ba60Sopenharmony_ci    pluginContext_.erase(pluginId);
42506f6ba60Sopenharmony_ci    PROFILER_LOG_DEBUG(LOG_CORE, "RemovePluginInfo for %s done!", pluginName.c_str());
42606f6ba60Sopenharmony_ci    return true;
42706f6ba60Sopenharmony_ci}
42806f6ba60Sopenharmony_ci
42906f6ba60Sopenharmony_civoid PluginService::ReadShareMemoryOffline(PluginContext& context)
43006f6ba60Sopenharmony_ci{
43106f6ba60Sopenharmony_ci    CHECK_NOTNULL(context.shareMemoryBlock, NO_RETVAL, "smb of %s is null!", context.path.c_str());
43206f6ba60Sopenharmony_ci    CHECK_NOTNULL(traceWriter_, NO_RETVAL, "traceWriter_ is null!");
43306f6ba60Sopenharmony_ci    if (context.eventNotifier) {
43406f6ba60Sopenharmony_ci        context.eventNotifier->Take();
43506f6ba60Sopenharmony_ci    }
43606f6ba60Sopenharmony_ci
43706f6ba60Sopenharmony_ci    uint32_t stopTakeDataSize = 0;
43806f6ba60Sopenharmony_ci    while (true) {
43906f6ba60Sopenharmony_ci        int retval = 0;
44006f6ba60Sopenharmony_ci        bool ret = context.shareMemoryBlock->TakeData([&](const int8_t data[], uint32_t size) -> bool {
44106f6ba60Sopenharmony_ci            CHECK_NOTNULL(data, false, "memory block data is null!");
44206f6ba60Sopenharmony_ci            retval = traceWriter_->Write(data, size);
44306f6ba60Sopenharmony_ci            CHECK_TRUE(retval != -1, false, "need to splite file");
44406f6ba60Sopenharmony_ci            CHECK_TRUE(retval > 0, false, "write %d bytes failed!", size);
44506f6ba60Sopenharmony_ci            return true;
44606f6ba60Sopenharmony_ci        }, isProtobufSerialize_);
44706f6ba60Sopenharmony_ci
44806f6ba60Sopenharmony_ci        if (retval == -1) {
44906f6ba60Sopenharmony_ci            PROFILER_LOG_DEBUG(LOG_CORE, "need to clear share memory block and report the basic data");
45006f6ba60Sopenharmony_ci            pluginSessionManager_->RefreshPluginSession();
45106f6ba60Sopenharmony_ci            break;
45206f6ba60Sopenharmony_ci        }
45306f6ba60Sopenharmony_ci
45406f6ba60Sopenharmony_ci        dataFlushSize_ += static_cast<uint32_t>(retval);
45506f6ba60Sopenharmony_ci        stopTakeDataSize += static_cast<uint32_t>(retval);
45606f6ba60Sopenharmony_ci        if (stopTakeDataSize > STOP_BASELINE) {
45706f6ba60Sopenharmony_ci            break;
45806f6ba60Sopenharmony_ci        } else if (dataFlushSize_ > FLUSH_BASELINE) {
45906f6ba60Sopenharmony_ci            traceWriter_->Flush();
46006f6ba60Sopenharmony_ci            traceWriter_->Finish();
46106f6ba60Sopenharmony_ci            dataFlushSize_ = 0;
46206f6ba60Sopenharmony_ci        }
46306f6ba60Sopenharmony_ci
46406f6ba60Sopenharmony_ci        if (!ret) { // no data to read
46506f6ba60Sopenharmony_ci            break;
46606f6ba60Sopenharmony_ci        }
46706f6ba60Sopenharmony_ci    }
46806f6ba60Sopenharmony_ci    traceWriter_->Flush();
46906f6ba60Sopenharmony_ci    traceWriter_->Finish();
47006f6ba60Sopenharmony_ci}
47106f6ba60Sopenharmony_ci
47206f6ba60Sopenharmony_civoid PluginService::ReadShareMemoryOnline(PluginContext& context)
47306f6ba60Sopenharmony_ci{
47406f6ba60Sopenharmony_ci    CHECK_NOTNULL(context.shareMemoryBlock, NO_RETVAL, "smb of %s is null!", context.path.c_str());
47506f6ba60Sopenharmony_ci    if (context.eventNotifier) {
47606f6ba60Sopenharmony_ci        context.eventNotifier->Take();
47706f6ba60Sopenharmony_ci    }
47806f6ba60Sopenharmony_ci
47906f6ba60Sopenharmony_ci    while (true) {
48006f6ba60Sopenharmony_ci        auto pluginData = std::make_shared<ProfilerPluginData>();
48106f6ba60Sopenharmony_ci        bool ret = context.shareMemoryBlock->TakeData([&](const int8_t data[], uint32_t size) -> bool {
48206f6ba60Sopenharmony_ci            int retval = pluginData->ParseFromArray(reinterpret_cast<const char*>(data), size);
48306f6ba60Sopenharmony_ci            CHECK_TRUE(retval, false, "parse %d bytes failed!", size);
48406f6ba60Sopenharmony_ci            return true;
48506f6ba60Sopenharmony_ci        }, isProtobufSerialize_);
48606f6ba60Sopenharmony_ci        if (!ret) {
48706f6ba60Sopenharmony_ci            break;
48806f6ba60Sopenharmony_ci        }
48906f6ba60Sopenharmony_ci        if (!context.profilerDataRepeater->PutPluginData(pluginData)) {
49006f6ba60Sopenharmony_ci            break;
49106f6ba60Sopenharmony_ci        }
49206f6ba60Sopenharmony_ci    }
49306f6ba60Sopenharmony_ci}
49406f6ba60Sopenharmony_ci
49506f6ba60Sopenharmony_civoid PluginService::FlushShareMemory(PluginContext& context)
49606f6ba60Sopenharmony_ci{
49706f6ba60Sopenharmony_ci    CHECK_NOTNULL(context.shareMemoryBlock, NO_RETVAL, "smb of %s is null!", context.path.c_str());
49806f6ba60Sopenharmony_ci    CHECK_NOTNULL(traceWriter_, NO_RETVAL, "traceWriter_ is null!");
49906f6ba60Sopenharmony_ci
50006f6ba60Sopenharmony_ci    while (true) {
50106f6ba60Sopenharmony_ci        bool ret = context.shareMemoryBlock->TakeData([&](const int8_t data[], uint32_t size) -> bool {
50206f6ba60Sopenharmony_ci            int retval = traceWriter_->Write(data, size);
50306f6ba60Sopenharmony_ci            CHECK_TRUE(retval > 0, false, "write %d bytes failed!", size);
50406f6ba60Sopenharmony_ci            return true;
50506f6ba60Sopenharmony_ci        }, isProtobufSerialize_);
50606f6ba60Sopenharmony_ci        if (!ret) { // no data to read
50706f6ba60Sopenharmony_ci            break;
50806f6ba60Sopenharmony_ci        }
50906f6ba60Sopenharmony_ci    }
51006f6ba60Sopenharmony_ci    traceWriter_->Finish();
51106f6ba60Sopenharmony_ci}
51206f6ba60Sopenharmony_ci
51306f6ba60Sopenharmony_civoid PluginService::FlushAllData(const std::string& pluginName)
51406f6ba60Sopenharmony_ci{
51506f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "FlushAllData for %s start!", pluginName.c_str());
51606f6ba60Sopenharmony_ci    auto configIter = profilerSessionConfigs_.find(pluginName);
51706f6ba60Sopenharmony_ci    if (configIter == profilerSessionConfigs_.end()) {
51806f6ba60Sopenharmony_ci        PROFILER_LOG_ERROR(LOG_CORE, "profiler session config not set fot plugin name: %s", pluginName.c_str());
51906f6ba60Sopenharmony_ci        return;
52006f6ba60Sopenharmony_ci    }
52106f6ba60Sopenharmony_ci    if (!configIter->second->discard_cache_data()) {
52206f6ba60Sopenharmony_ci        uint32_t pluginId = 0;
52306f6ba60Sopenharmony_ci        PluginContextPtr pluginCtx = nullptr;
52406f6ba60Sopenharmony_ci        std::tie(pluginId, pluginCtx) = GetPluginContext(pluginName);
52506f6ba60Sopenharmony_ci        CHECK_NOTNULL(pluginCtx, NO_RETVAL, "%s: get PluginContext(%s) failed!", __func__, pluginName.c_str());
52606f6ba60Sopenharmony_ci        if (configIter->second->session_mode() == ProfilerSessionConfig::OFFLINE) {
52706f6ba60Sopenharmony_ci            FlushShareMemory(*pluginCtx);
52806f6ba60Sopenharmony_ci        } else if (configIter->second->session_mode() == ProfilerSessionConfig::ONLINE) {
52906f6ba60Sopenharmony_ci            ReadShareMemoryOnline(*pluginCtx);
53006f6ba60Sopenharmony_ci        }
53106f6ba60Sopenharmony_ci    }
53206f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "FlushAllData for %s done!", pluginName.c_str());
53306f6ba60Sopenharmony_ci}
53406f6ba60Sopenharmony_ci
53506f6ba60Sopenharmony_cibool PluginService::AppendResult(NotifyResultRequest& request)
53606f6ba60Sopenharmony_ci{
53706f6ba60Sopenharmony_ci    pluginCommandBuilder_->GetedCommandResponse(request.command_id());
53806f6ba60Sopenharmony_ci    auto sem = GetSemaphore(request.command_id());
53906f6ba60Sopenharmony_ci    if (sem) {
54006f6ba60Sopenharmony_ci        sem->Post();
54106f6ba60Sopenharmony_ci    }
54206f6ba60Sopenharmony_ci
54306f6ba60Sopenharmony_ci    int size = request.result_size();
54406f6ba60Sopenharmony_ci    PROFILER_LOG_DEBUG(LOG_CORE, "AppendResult size:%d, cmd id:%d", size, request.command_id());
54506f6ba60Sopenharmony_ci    for (int i = 0; i < size; i++) {
54606f6ba60Sopenharmony_ci        PluginResult pr = request.result(i);
54706f6ba60Sopenharmony_ci        if (pr.data().size() > 0) {
54806f6ba60Sopenharmony_ci            PROFILER_LOG_DEBUG(LOG_CORE, "AppendResult Size : %zu", pr.data().size());
54906f6ba60Sopenharmony_ci            uint32_t pluginId = pr.plugin_id();
55006f6ba60Sopenharmony_ci            PluginContextPtr pluginCtx = GetPluginContextById(pluginId);
55106f6ba60Sopenharmony_ci            CHECK_NOTNULL(pluginCtx, false, "plugin id %u not found!", pluginId);
55206f6ba60Sopenharmony_ci            if (pluginCtx->profilerDataRepeater == nullptr) {
55306f6ba60Sopenharmony_ci                PROFILER_LOG_DEBUG(LOG_CORE, "AppendResult profilerDataRepeater==nullptr %s %d",
55406f6ba60Sopenharmony_ci                                   pr.status().name().c_str(), pluginId);
55506f6ba60Sopenharmony_ci                return false;
55606f6ba60Sopenharmony_ci            }
55706f6ba60Sopenharmony_ci            auto pluginData = std::make_shared<ProfilerPluginData>();
55806f6ba60Sopenharmony_ci            pluginData->set_name(pr.status().name());
55906f6ba60Sopenharmony_ci            pluginData->set_status(0);
56006f6ba60Sopenharmony_ci            pluginData->set_data(pr.data());
56106f6ba60Sopenharmony_ci            if (!pluginCtx->profilerDataRepeater->PutPluginData(pluginData)) {
56206f6ba60Sopenharmony_ci                return false;
56306f6ba60Sopenharmony_ci            }
56406f6ba60Sopenharmony_ci        } else if (pr.out_file_name() != "") { // updata plugin outFileName
56506f6ba60Sopenharmony_ci            std::unique_lock<std::mutex> lock(mutex_);
56606f6ba60Sopenharmony_ci            auto pluginId = pr.plugin_id();
56706f6ba60Sopenharmony_ci            CHECK_TRUE(pluginContext_.count(pluginId) > 0, false, "plugin id %u not found!", pluginId);
56806f6ba60Sopenharmony_ci            pluginContext_[pluginId]->outFileName = pr.out_file_name();
56906f6ba60Sopenharmony_ci        } else {
57006f6ba60Sopenharmony_ci            PROFILER_LOG_DEBUG(LOG_CORE, "Flush?Data From ShareMemory?");
57106f6ba60Sopenharmony_ci        }
57206f6ba60Sopenharmony_ci    }
57306f6ba60Sopenharmony_ci    return true;
57406f6ba60Sopenharmony_ci}
57506f6ba60Sopenharmony_ci
57606f6ba60Sopenharmony_cistd::vector<ProfilerPluginState> PluginService::GetPluginStatus()
57706f6ba60Sopenharmony_ci{
57806f6ba60Sopenharmony_ci    std::vector<ProfilerPluginState> status;
57906f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
58006f6ba60Sopenharmony_ci    for (auto& entry : pluginContext_) {
58106f6ba60Sopenharmony_ci        status.push_back(entry.second->profilerPluginState);
58206f6ba60Sopenharmony_ci    }
58306f6ba60Sopenharmony_ci    return status;
58406f6ba60Sopenharmony_ci}
58506f6ba60Sopenharmony_ci
58606f6ba60Sopenharmony_ciuint32_t PluginService::GetPluginIdByName(std::string name)
58706f6ba60Sopenharmony_ci{
58806f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
58906f6ba60Sopenharmony_ci    if (nameIndex_.find(name) == nameIndex_.end()) {
59006f6ba60Sopenharmony_ci        return 0;
59106f6ba60Sopenharmony_ci    }
59206f6ba60Sopenharmony_ci    return nameIndex_[name];
59306f6ba60Sopenharmony_ci}
59406f6ba60Sopenharmony_ci
59506f6ba60Sopenharmony_civoid PluginService::SetTraceWriter(const TraceFileWriterPtr& traceWriter)
59606f6ba60Sopenharmony_ci{
59706f6ba60Sopenharmony_ci    traceWriter_ = traceWriter;
59806f6ba60Sopenharmony_ci}