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_SERVICE_H 17 #define PLUGIN_SERVICE_H 18 19 #include <atomic> 20 #include <map> 21 #include <memory> 22 #include <string> 23 #include <thread> 24 #include <vector> 25 26 #include "common_types.pb.h" 27 #include "epoll_event_poller.h" 28 #include "event_notifier.h" 29 #include "i_semaphore.h" 30 #include "logging.h" 31 #include "plugin_service_types.pb.h" 32 #include "profiler_service_types.pb.h" 33 #include "service_entry.h" 34 #include "trace_file_writer.h" 35 #include "profiler_data_repeater.h" 36 37 class PluginServiceImpl; 38 class SocketContext; 39 class ShareMemoryBlock; 40 class PluginCommandBuilder; 41 class PluginSessionManager; 42 43 using ProfilerDataRepeaterPtr = std::shared_ptr<ProfilerDataRepeater<ProfilerPluginData>>; 44 using ProfilerStateRepeaterPtr = std::shared_ptr<ProfilerDataRepeater<ProfilerPluginState>>; 45 using ProfilerPluginStatePtr = STD_PTR(shared, ProfilerPluginState); 46 47 struct PluginInfo { 48 uint32_t id = 0; 49 std::string name; 50 std::string path; 51 std::string sha256; 52 uint32_t bufferSizeHint; 53 bool isStandaloneFileData = false; 54 std::string outFileName = ""; 55 std::string pluginVersion = ""; 56 SocketContext* context = nullptr; 57 }; 58 59 struct PluginContext { 60 std::string name; 61 std::string path; 62 std::string sha256; 63 uint32_t bufferSizeHint; 64 bool isStandaloneFileData = false; 65 std::string outFileName = ""; 66 std::string pluginVersion = ""; 67 SocketContext* context = nullptr; 68 ProfilerPluginConfig config; 69 ProfilerDataRepeaterPtr profilerDataRepeater; 70 ProfilerStateRepeaterPtr profilerStateRepeater; 71 std::shared_ptr<ShareMemoryBlock> shareMemoryBlock; 72 EventNotifierPtr eventNotifier; 73 ProfilerPluginState profilerPluginState; 74 }; 75 76 using PluginContextPtr = std::shared_ptr<PluginContext>; 77 78 using PluginSessionManagerPtr = std::shared_ptr<PluginSessionManager>; 79 80 class PluginService { 81 public: 82 PluginService(); 83 ~PluginService(); 84 85 bool CreatePluginSession(const ProfilerPluginConfig& pluginConfig, 86 const ProfilerSessionConfig::BufferConfig& bufferConfig, 87 const ProfilerDataRepeaterPtr& dataRepeater, 88 const ProfilerStateRepeaterPtr& stateRepeater); 89 bool CreatePluginSession(const ProfilerPluginConfig& pluginConfig, const ProfilerDataRepeaterPtr& dataRepeater, 90 const ProfilerStateRepeaterPtr& stateRepeater); 91 bool StartPluginSession(const ProfilerPluginConfig& config); 92 bool StopPluginSession(const std::string& pluginName); 93 bool DestroyPluginSession(const std::string& pluginName); 94 bool RefreshPluginSession(const std::string& pluginName); 95 96 bool AddPluginInfo(const PluginInfo& pluginInfo); 97 bool GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo); 98 bool RemovePluginInfo(const PluginInfo& pluginInfo); 99 100 bool AppendResult(NotifyResultRequest& request); 101 102 std::vector<ProfilerPluginState> GetPluginStatus(); 103 uint32_t GetPluginIdByName(std::string name); 104 105 void SetPluginSessionManager(const PluginSessionManagerPtr& pluginSessionManager); 106 void SetProfilerSessionConfig(const std::shared_ptr<ProfilerSessionConfig> profilerSessionConfig, 107 const std::vector<std::string>& pluginNames); 108 109 std::pair<uint32_t, PluginContextPtr> GetPluginContext(const std::string& pluginName); 110 void SetTraceWriter(const TraceFileWriterPtr& traceWriter); 111 void FlushAllData(const std::string& pluginName); 112 bool StartEpollThread(); 113 bool StopEpollThread(); 114 void SendErrorMsg(std::shared_ptr<ProfilerDataRepeater<ProfilerPluginState>> stateRepeater); 115 116 private: 117 bool StartService(const std::string& unixSocketName); 118 119 SemaphorePtr GetSemaphore(uint32_t) const; 120 void ReadShareMemoryOffline(PluginContext&); 121 void ReadShareMemoryOnline(PluginContext&); 122 void FlushShareMemory(PluginContext&); 123 PluginContextPtr GetPluginContextById(uint32_t id); 124 125 bool RemovePluginSessionCtx(const std::string& pluginName); 126 127 mutable std::mutex mutex_; 128 std::map<uint32_t, PluginContextPtr> pluginContext_; 129 std::map<uint32_t, SemaphorePtr> waitSemphores_; 130 std::map<std::string, uint32_t> nameIndex_; 131 132 std::atomic<uint32_t> pluginIdCounter_; 133 std::shared_ptr<ServiceEntry> serviceEntry_; 134 std::shared_ptr<PluginServiceImpl> pluginServiceImpl_; 135 std::shared_ptr<PluginCommandBuilder> pluginCommandBuilder_; 136 std::unique_ptr<EpollEventPoller> eventPoller_; 137 PluginSessionManagerPtr pluginSessionManager_; 138 std::map<std::string, std::shared_ptr<ProfilerSessionConfig>> profilerSessionConfigs_; 139 TraceFileWriterPtr traceWriter_ = nullptr; 140 uint32_t dataFlushSize_ = 0; 141 bool isProtobufSerialize_ = true; 142 }; 143 144 #endif // PLUGIN_SERVICE_H