• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "PluginSession"
16 #include "plugin_session.h"
17 #include "logging.h"
18 #include "plugin_service.h"
19 
PluginSession(const ProfilerPluginConfig & pluginConfig,const PluginServiceWeakPtr & pluginService,const ProfilerDataRepeaterPtr & dataRepeater,const ProfilerStateRepeaterPtr & stateRepeater)20 PluginSession::PluginSession(const ProfilerPluginConfig& pluginConfig,
21                              const PluginServiceWeakPtr& pluginService,
22                              const ProfilerDataRepeaterPtr& dataRepeater,
23                              const ProfilerStateRepeaterPtr& stateRepeater)
24     : state_(PluginSession::INITIAL),
25       withBufferConfig_(false),
26       pluginConfig_(pluginConfig),
27       pluginService_(pluginService),
28       dataRepeater_(dataRepeater),
29       stateRepeater_(stateRepeater)
30 {
31     Create();
32 }
33 
PluginSession(const ProfilerPluginConfig & pluginConfig,const ProfilerSessionConfig::BufferConfig & bufferConfig,const PluginServiceWeakPtr & pluginService,const ProfilerDataRepeaterPtr & dataRepeater,const ProfilerStateRepeaterPtr & stateRepeater)34 PluginSession::PluginSession(const ProfilerPluginConfig& pluginConfig,
35                              const ProfilerSessionConfig::BufferConfig& bufferConfig,
36                              const PluginServiceWeakPtr& pluginService,
37                              const ProfilerDataRepeaterPtr& dataRepeater,
38                              const ProfilerStateRepeaterPtr& stateRepeater)
39     : state_(PluginSession::INITIAL),
40       withBufferConfig_(true),
41       pluginConfig_(pluginConfig),
42       bufferConfig_(bufferConfig),
43       pluginService_(pluginService),
44       dataRepeater_(dataRepeater),
45       stateRepeater_(stateRepeater)
46 {
47     Create();
48 }
49 
~PluginSession()50 PluginSession::~PluginSession()
51 {
52     if (state_ != INITIAL) {
53         Destroy();
54     }
55 }
56 
Create()57 bool PluginSession::Create()
58 {
59     std::unique_lock<std::mutex> lock(mutex_);
60     PROFILER_LOG_INFO(LOG_CORE, "CreatePluginSession for %s...", pluginConfig_.name().c_str());
61     CHECK_TRUE(state_ == INITIAL, false, "plugin state %d invalid!", state_);
62 
63     auto pluginService = pluginService_.lock(); // promote to shared_ptr
64     CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
65 
66     bool retval = false;
67     if (withBufferConfig_) {
68         retval = pluginService->CreatePluginSession(pluginConfig_, bufferConfig_, dataRepeater_, stateRepeater_);
69         PROFILER_LOG_INFO(LOG_CORE, "CreatePluginSession with buffer for %s %s!", pluginConfig_.name().c_str(),
70                           retval ? "OK" : "FAIL");
71     } else {
72         retval = pluginService->CreatePluginSession(pluginConfig_, dataRepeater_, stateRepeater_);
73         PROFILER_LOG_INFO(LOG_CORE, "CreatePluginSession for %s %s!",
74                           pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
75     }
76     CHECK_TRUE(retval, false, "call PluginService::CreatePluginSession failed!");
77 
78     state_ = CREATED;
79     return retval;
80 }
81 
Destroy()82 bool PluginSession::Destroy()
83 {
84     std::unique_lock<std::mutex> lock(mutex_);
85     PROFILER_LOG_INFO(LOG_CORE, "DestroyPluginSession for %s...", pluginConfig_.name().c_str());
86     RETURN_IF(state_ == INITIAL, false, "plugin state %d, no need to destroy!", state_);
87     CHECK_TRUE(state_ == CREATED || state_ == STARTED, false, "plugin state %d invalid!", state_);
88 
89     auto pluginService = pluginService_.lock();
90     CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
91 
92     if (state_ == STARTED) {
93         PROFILER_LOG_INFO(LOG_CORE, "PluginSession::Destroy state is STARED, need stop!");
94         StopLocked();
95     }
96 
97     bool retval = pluginService->DestroyPluginSession(pluginConfig_.name());
98     PROFILER_LOG_INFO(LOG_CORE, "DestroyPluginSession for %s %s!",
99                       pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
100     CHECK_TRUE(retval, false, "call PluginService::DestroyPluginSession failed!");
101 
102     state_ = INITIAL;
103     return true;
104 }
105 
IsAvailable() const106 bool PluginSession::IsAvailable() const
107 {
108     std::unique_lock<std::mutex> lock(mutex_);
109     return state_ != INITIAL;
110 }
111 
GetState() const112 PluginSession::State PluginSession::GetState() const
113 {
114     std::unique_lock<std::mutex> lock(mutex_);
115     return state_;
116 }
117 
Invalidate()118 void PluginSession::Invalidate()
119 {
120     std::unique_lock<std::mutex> lock(mutex_);
121     state_ = INVALID;
122 }
123 
Start()124 bool PluginSession::Start()
125 {
126     std::unique_lock<std::mutex> lock(mutex_);
127     PROFILER_LOG_INFO(LOG_CORE, "StartPluginSession for %s...", pluginConfig_.name().c_str());
128     CHECK_TRUE(state_ == CREATED, false, "plugin state %d invalid!", state_);
129 
130     auto pluginService = pluginService_.lock();
131     CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
132 
133     bool retval = pluginService->StartPluginSession(pluginConfig_);
134     PROFILER_LOG_INFO(LOG_CORE, "StartPluginSession for %s %s!", pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
135     CHECK_TRUE(retval, false, "call PluginService::StartPluginSession failed!");
136 
137     state_ = STARTED;
138     return retval;
139 }
140 
Refresh()141 bool PluginSession::Refresh()
142 {
143     std::unique_lock<std::mutex> lock(mutex_);
144     PROFILER_LOG_INFO(LOG_CORE, "Refresh for %s...", pluginConfig_.name().c_str());
145     CHECK_TRUE(state_ == STARTED, false, "plugin state %d invalid!", state_);
146 
147     auto pluginService = pluginService_.lock();
148     CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
149 
150     bool retval = pluginService->RefreshPluginSession(pluginConfig_.name());
151     PROFILER_LOG_INFO(LOG_CORE, "RefreshPluginSession for %s %s!",
152                       pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
153     CHECK_TRUE(retval, false, "call PluginService::RefreshPluginSession failed!");
154 
155     return retval;
156 }
157 
Stop()158 bool PluginSession::Stop()
159 {
160     std::unique_lock<std::mutex> lock(mutex_);
161     return StopLocked();
162 }
163 
StopLocked()164 bool PluginSession::StopLocked()
165 {
166     PROFILER_LOG_INFO(LOG_CORE, "StopPluginSession for %s...", pluginConfig_.name().c_str());
167     CHECK_TRUE(state_ == STARTED, false, "plugin state %d invalid!", state_);
168 
169     auto pluginService = pluginService_.lock();
170     CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
171 
172     bool retval = pluginService->StopPluginSession(pluginConfig_.name());
173     PROFILER_LOG_INFO(LOG_CORE, "StopPluginSession for %s %s!", pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
174     CHECK_TRUE(retval, false, "call PluginService::StopPluginSession failed!");
175 
176     state_ = CREATED;
177     return retval;
178 }