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#define LOG_TAG "PluginSession"
1606f6ba60Sopenharmony_ci#include "plugin_session.h"
1706f6ba60Sopenharmony_ci#include "logging.h"
1806f6ba60Sopenharmony_ci#include "plugin_service.h"
1906f6ba60Sopenharmony_ci
2006f6ba60Sopenharmony_ciPluginSession::PluginSession(const ProfilerPluginConfig& pluginConfig,
2106f6ba60Sopenharmony_ci                             const PluginServiceWeakPtr& pluginService,
2206f6ba60Sopenharmony_ci                             const ProfilerDataRepeaterPtr& dataRepeater)
2306f6ba60Sopenharmony_ci    : state_(PluginSession::INITIAL),
2406f6ba60Sopenharmony_ci      withBufferConfig_(false),
2506f6ba60Sopenharmony_ci      pluginConfig_(pluginConfig),
2606f6ba60Sopenharmony_ci      pluginService_(pluginService),
2706f6ba60Sopenharmony_ci      dataRepeater_(dataRepeater)
2806f6ba60Sopenharmony_ci{
2906f6ba60Sopenharmony_ci    Create();
3006f6ba60Sopenharmony_ci}
3106f6ba60Sopenharmony_ci
3206f6ba60Sopenharmony_ciPluginSession::PluginSession(const ProfilerPluginConfig& pluginConfig,
3306f6ba60Sopenharmony_ci                             const ProfilerSessionConfig::BufferConfig& bufferConfig,
3406f6ba60Sopenharmony_ci                             const PluginServiceWeakPtr& pluginService,
3506f6ba60Sopenharmony_ci                             const ProfilerDataRepeaterPtr& dataRepeater)
3606f6ba60Sopenharmony_ci    : state_(PluginSession::INITIAL),
3706f6ba60Sopenharmony_ci      withBufferConfig_(true),
3806f6ba60Sopenharmony_ci      pluginConfig_(pluginConfig),
3906f6ba60Sopenharmony_ci      bufferConfig_(bufferConfig),
4006f6ba60Sopenharmony_ci      pluginService_(pluginService),
4106f6ba60Sopenharmony_ci      dataRepeater_(dataRepeater)
4206f6ba60Sopenharmony_ci{
4306f6ba60Sopenharmony_ci    Create();
4406f6ba60Sopenharmony_ci}
4506f6ba60Sopenharmony_ci
4606f6ba60Sopenharmony_ciPluginSession::~PluginSession()
4706f6ba60Sopenharmony_ci{
4806f6ba60Sopenharmony_ci    if (state_ != INITIAL) {
4906f6ba60Sopenharmony_ci        Destroy();
5006f6ba60Sopenharmony_ci    }
5106f6ba60Sopenharmony_ci}
5206f6ba60Sopenharmony_ci
5306f6ba60Sopenharmony_cibool PluginSession::Create()
5406f6ba60Sopenharmony_ci{
5506f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
5606f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "CreatePluginSession for %s...", pluginConfig_.name().c_str());
5706f6ba60Sopenharmony_ci    CHECK_TRUE(state_ == INITIAL, false, "plugin state %d invalid!", state_);
5806f6ba60Sopenharmony_ci
5906f6ba60Sopenharmony_ci    auto pluginService = pluginService_.lock(); // promote to shared_ptr
6006f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
6106f6ba60Sopenharmony_ci
6206f6ba60Sopenharmony_ci    bool retval = false;
6306f6ba60Sopenharmony_ci    if (withBufferConfig_) {
6406f6ba60Sopenharmony_ci        retval = pluginService->CreatePluginSession(pluginConfig_, bufferConfig_, dataRepeater_);
6506f6ba60Sopenharmony_ci        PROFILER_LOG_INFO(LOG_CORE, "CreatePluginSession with buffer for %s %s!", pluginConfig_.name().c_str(),
6606f6ba60Sopenharmony_ci                          retval ? "OK" : "FAIL");
6706f6ba60Sopenharmony_ci    } else {
6806f6ba60Sopenharmony_ci        retval = pluginService->CreatePluginSession(pluginConfig_, dataRepeater_);
6906f6ba60Sopenharmony_ci        PROFILER_LOG_INFO(LOG_CORE, "CreatePluginSession for %s %s!",
7006f6ba60Sopenharmony_ci                          pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
7106f6ba60Sopenharmony_ci    }
7206f6ba60Sopenharmony_ci    CHECK_TRUE(retval, false, "call PluginService::CreatePluginSession failed!");
7306f6ba60Sopenharmony_ci
7406f6ba60Sopenharmony_ci    state_ = CREATED;
7506f6ba60Sopenharmony_ci    return retval;
7606f6ba60Sopenharmony_ci}
7706f6ba60Sopenharmony_ci
7806f6ba60Sopenharmony_cibool PluginSession::Destroy()
7906f6ba60Sopenharmony_ci{
8006f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
8106f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "DestroyPluginSession for %s...", pluginConfig_.name().c_str());
8206f6ba60Sopenharmony_ci    RETURN_IF(state_ == INITIAL, false, "plugin state %d, no need to destroy!", state_);
8306f6ba60Sopenharmony_ci    CHECK_TRUE(state_ == CREATED || state_ == STARTED, false, "plugin state %d invalid!", state_);
8406f6ba60Sopenharmony_ci
8506f6ba60Sopenharmony_ci    auto pluginService = pluginService_.lock();
8606f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
8706f6ba60Sopenharmony_ci
8806f6ba60Sopenharmony_ci    if (state_ == STARTED) {
8906f6ba60Sopenharmony_ci        PROFILER_LOG_INFO(LOG_CORE, "PluginSession::Destroy state is STARED, need stop!");
9006f6ba60Sopenharmony_ci        StopLocked();
9106f6ba60Sopenharmony_ci    }
9206f6ba60Sopenharmony_ci
9306f6ba60Sopenharmony_ci    bool retval = pluginService->DestroyPluginSession(pluginConfig_.name());
9406f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "DestroyPluginSession for %s %s!",
9506f6ba60Sopenharmony_ci                      pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
9606f6ba60Sopenharmony_ci    CHECK_TRUE(retval, false, "call PluginService::DestroyPluginSession failed!");
9706f6ba60Sopenharmony_ci
9806f6ba60Sopenharmony_ci    state_ = INITIAL;
9906f6ba60Sopenharmony_ci    return true;
10006f6ba60Sopenharmony_ci}
10106f6ba60Sopenharmony_ci
10206f6ba60Sopenharmony_cibool PluginSession::IsAvailable() const
10306f6ba60Sopenharmony_ci{
10406f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
10506f6ba60Sopenharmony_ci    return state_ != INITIAL;
10606f6ba60Sopenharmony_ci}
10706f6ba60Sopenharmony_ci
10806f6ba60Sopenharmony_ciPluginSession::State PluginSession::GetState() const
10906f6ba60Sopenharmony_ci{
11006f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
11106f6ba60Sopenharmony_ci    return state_;
11206f6ba60Sopenharmony_ci}
11306f6ba60Sopenharmony_ci
11406f6ba60Sopenharmony_civoid PluginSession::Invalidate()
11506f6ba60Sopenharmony_ci{
11606f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
11706f6ba60Sopenharmony_ci    state_ = INVALID;
11806f6ba60Sopenharmony_ci}
11906f6ba60Sopenharmony_ci
12006f6ba60Sopenharmony_cibool PluginSession::Start()
12106f6ba60Sopenharmony_ci{
12206f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
12306f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "StartPluginSession for %s...", pluginConfig_.name().c_str());
12406f6ba60Sopenharmony_ci    CHECK_TRUE(state_ == CREATED, false, "plugin state %d invalid!", state_);
12506f6ba60Sopenharmony_ci
12606f6ba60Sopenharmony_ci    auto pluginService = pluginService_.lock();
12706f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
12806f6ba60Sopenharmony_ci
12906f6ba60Sopenharmony_ci    bool retval = pluginService->StartPluginSession(pluginConfig_);
13006f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "StartPluginSession for %s %s!", pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
13106f6ba60Sopenharmony_ci    CHECK_TRUE(retval, false, "call PluginService::StartPluginSession failed!");
13206f6ba60Sopenharmony_ci
13306f6ba60Sopenharmony_ci    state_ = STARTED;
13406f6ba60Sopenharmony_ci    return retval;
13506f6ba60Sopenharmony_ci}
13606f6ba60Sopenharmony_ci
13706f6ba60Sopenharmony_cibool PluginSession::Refresh()
13806f6ba60Sopenharmony_ci{
13906f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
14006f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "Refresh for %s...", pluginConfig_.name().c_str());
14106f6ba60Sopenharmony_ci    CHECK_TRUE(state_ == STARTED, false, "plugin state %d invalid!", state_);
14206f6ba60Sopenharmony_ci
14306f6ba60Sopenharmony_ci    auto pluginService = pluginService_.lock();
14406f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
14506f6ba60Sopenharmony_ci
14606f6ba60Sopenharmony_ci    bool retval = pluginService->RefreshPluginSession(pluginConfig_.name());
14706f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "RefreshPluginSession for %s %s!",
14806f6ba60Sopenharmony_ci                      pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
14906f6ba60Sopenharmony_ci    CHECK_TRUE(retval, false, "call PluginService::RefreshPluginSession failed!");
15006f6ba60Sopenharmony_ci
15106f6ba60Sopenharmony_ci    return retval;
15206f6ba60Sopenharmony_ci}
15306f6ba60Sopenharmony_ci
15406f6ba60Sopenharmony_cibool PluginSession::Stop()
15506f6ba60Sopenharmony_ci{
15606f6ba60Sopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
15706f6ba60Sopenharmony_ci    return StopLocked();
15806f6ba60Sopenharmony_ci}
15906f6ba60Sopenharmony_ci
16006f6ba60Sopenharmony_cibool PluginSession::StopLocked()
16106f6ba60Sopenharmony_ci{
16206f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "StopPluginSession for %s...", pluginConfig_.name().c_str());
16306f6ba60Sopenharmony_ci    CHECK_TRUE(state_ == STARTED, false, "plugin state %d invalid!", state_);
16406f6ba60Sopenharmony_ci
16506f6ba60Sopenharmony_ci    auto pluginService = pluginService_.lock();
16606f6ba60Sopenharmony_ci    CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
16706f6ba60Sopenharmony_ci
16806f6ba60Sopenharmony_ci    bool retval = pluginService->StopPluginSession(pluginConfig_.name());
16906f6ba60Sopenharmony_ci    PROFILER_LOG_INFO(LOG_CORE, "StopPluginSession for %s %s!", pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
17006f6ba60Sopenharmony_ci    CHECK_TRUE(retval, false, "call PluginService::StopPluginSession failed!");
17106f6ba60Sopenharmony_ci
17206f6ba60Sopenharmony_ci    state_ = CREATED;
17306f6ba60Sopenharmony_ci    return retval;
17406f6ba60Sopenharmony_ci}