1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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 HILOG_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 HILOG_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 HILOG_INFO(LOG_CORE, "CreatePluginSession for %s %s!", pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
70 }
71 CHECK_TRUE(retval, false, "call PluginService::CreatePluginSession failed!");
72
73 state_ = CREATED;
74 return retval;
75 }
76
Destroy()77 bool PluginSession::Destroy()
78 {
79 std::unique_lock<std::mutex> lock(mutex_);
80 HILOG_INFO(LOG_CORE, "DestroyPluginSession for %s...", pluginConfig_.name().c_str());
81 RETURN_IF(state_ == INITIAL, false, "plugin state %d, no need to destroy!", state_);
82 CHECK_TRUE(state_ == CREATED || state_ == STARTED, false, "plugin state %d invalid!", state_);
83
84 auto pluginService = pluginService_.lock();
85 CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
86
87 if (state_ == STARTED) {
88 HILOG_INFO(LOG_CORE, "PluginSession::Destroy state is STARED, need stop!");
89 StopLocked();
90 }
91
92 bool retval = pluginService->DestroyPluginSession(pluginConfig_.name());
93 HILOG_INFO(LOG_CORE, "DestroyPluginSession for %s %s!", pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
94 CHECK_TRUE(retval, false, "call PluginService::DestroyPluginSession failed!");
95
96 state_ = INITIAL;
97 return true;
98 }
99
IsAvailable() const100 bool PluginSession::IsAvailable() const
101 {
102 std::unique_lock<std::mutex> lock(mutex_);
103 return state_ != INITIAL;
104 }
105
GetState() const106 PluginSession::State PluginSession::GetState() const
107 {
108 std::unique_lock<std::mutex> lock(mutex_);
109 return state_;
110 }
111
Invalidate()112 void PluginSession::Invalidate()
113 {
114 std::unique_lock<std::mutex> lock(mutex_);
115 state_ = INVALID;
116 }
117
Start()118 bool PluginSession::Start()
119 {
120 std::unique_lock<std::mutex> lock(mutex_);
121 HILOG_INFO(LOG_CORE, "StartPluginSession for %s...", pluginConfig_.name().c_str());
122 CHECK_TRUE(state_ == CREATED, false, "plugin state %d invalid!", state_);
123
124 auto pluginService = pluginService_.lock();
125 CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
126
127 bool retval = pluginService->StartPluginSession(pluginConfig_);
128 HILOG_INFO(LOG_CORE, "StartPluginSession for %s %s!", pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
129 CHECK_TRUE(retval, false, "call PluginService::StartPluginSession failed!");
130
131 state_ = STARTED;
132 return retval;
133 }
134
Refresh()135 bool PluginSession::Refresh()
136 {
137 std::unique_lock<std::mutex> lock(mutex_);
138 HILOG_INFO(LOG_CORE, "Refresh for %s...", pluginConfig_.name().c_str());
139 CHECK_TRUE(state_ == STARTED, false, "plugin state %d invalid!", state_);
140
141 auto pluginService = pluginService_.lock();
142 CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
143
144 bool retval = pluginService->RefreshPluginSession(pluginConfig_.name());
145 HILOG_INFO(LOG_CORE, "RefreshPluginSession for %s %s!", pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
146 CHECK_TRUE(retval, false, "call PluginService::RefreshPluginSession failed!");
147
148 return retval;
149 }
150
Stop()151 bool PluginSession::Stop()
152 {
153 std::unique_lock<std::mutex> lock(mutex_);
154 return StopLocked();
155 }
156
StopLocked()157 bool PluginSession::StopLocked()
158 {
159 HILOG_INFO(LOG_CORE, "StopPluginSession for %s...", pluginConfig_.name().c_str());
160 CHECK_TRUE(state_ == STARTED, false, "plugin state %d invalid!", state_);
161
162 auto pluginService = pluginService_.lock();
163 CHECK_NOTNULL(pluginService, false, "PluginSession::%s pluginService promote failed!", __func__);
164
165 bool retval = pluginService->StopPluginSession(pluginConfig_.name());
166 HILOG_INFO(LOG_CORE, "StopPluginSession for %s %s!", pluginConfig_.name().c_str(), retval ? "OK" : "FAIL");
167 CHECK_TRUE(retval, false, "call PluginService::StopPluginSession failed!");
168
169 state_ = CREATED;
170 return retval;
171 }