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