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 16 #ifndef PLUGIN_SESSION_H 17 #define PLUGIN_SESSION_H 18 19 #include <mutex> 20 #include "logging.h" 21 #include "nocopyable.h" 22 #include "plugin_service_types.pb.h" 23 #include "profiler_service_types.pb.h" 24 #include "profiler_data_repeater.h" 25 26 class PluginService; 27 28 using PluginServiceWeakPtr = STD_PTR(weak, PluginService); 29 using ProfilerDataRepeaterPtr = std::shared_ptr<ProfilerDataRepeater<ProfilerPluginData>>; 30 using ProfilerStateRepeaterPtr = std::shared_ptr<ProfilerDataRepeater<ProfilerPluginState>>; 31 32 class PluginSession { 33 public: 34 PluginSession(const ProfilerPluginConfig& pluginConfig, 35 const PluginServiceWeakPtr& pluginService, 36 const ProfilerDataRepeaterPtr& dataRepeater, 37 const ProfilerStateRepeaterPtr& stateRepeater); 38 PluginSession(const ProfilerPluginConfig& pluginConfig, 39 const ProfilerSessionConfig::BufferConfig& bufferConfig, 40 const PluginServiceWeakPtr& pluginService, 41 const ProfilerDataRepeaterPtr& dataRepeater, 42 const ProfilerStateRepeaterPtr& stateRepeater); 43 ~PluginSession(); 44 45 bool Start(); 46 bool Stop(); 47 bool Refresh(); 48 49 bool IsAvailable() const; 50 51 enum State { 52 INVALID = -1, 53 INITIAL = 0, 54 CREATED = 1, 55 STARTED = 2, 56 }; 57 58 void Invalidate(); 59 60 State GetState() const; 61 62 private: 63 bool Create(); 64 bool StopLocked(); 65 bool Destroy(); 66 67 private: 68 mutable std::mutex mutex_; 69 State state_; 70 bool withBufferConfig_; 71 ProfilerPluginConfig pluginConfig_; 72 ProfilerSessionConfig::BufferConfig bufferConfig_; 73 PluginServiceWeakPtr pluginService_; 74 ProfilerDataRepeaterPtr dataRepeater_; 75 ProfilerStateRepeaterPtr stateRepeater_; 76 77 DISALLOW_COPY_AND_MOVE(PluginSession); 78 }; 79 80 #endif // !PLUGIN_SESSION_H 81