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 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 36 class PluginServiceImpl; 37 class ProfilerDataRepeater; 38 class SocketContext; 39 class ShareMemoryBlock; 40 class PluginCommandBuilder; 41 class PluginSessionManager; 42 43 using ProfilerDataRepeaterPtr = STD_PTR(shared, ProfilerDataRepeater); 44 using ProfilerPluginStatePtr = STD_PTR(shared, ProfilerPluginState); 45 46 struct PluginInfo { 47 uint32_t id = 0; 48 std::string name; 49 std::string path; 50 std::string sha256; 51 uint32_t bufferSizeHint; 52 bool isStandaloneFileData = false; 53 std::string outFileName = ""; 54 std::string pluginVersion = ""; 55 SocketContext* context = nullptr; 56 }; 57 58 struct PluginContext { 59 std::string name; 60 std::string path; 61 std::string sha256; 62 uint32_t bufferSizeHint; 63 bool isStandaloneFileData = false; 64 std::string outFileName = ""; 65 std::string pluginVersion = ""; 66 SocketContext* context = nullptr; 67 ProfilerPluginConfig config; 68 ProfilerDataRepeaterPtr profilerDataRepeater; 69 std::shared_ptr<ShareMemoryBlock> shareMemoryBlock; 70 EventNotifierPtr eventNotifier; 71 ProfilerPluginState profilerPluginState; 72 }; 73 74 using PluginContextPtr = std::shared_ptr<PluginContext>; 75 76 using PluginSessionManagerPtr = std::shared_ptr<PluginSessionManager>; 77 78 class PluginService { 79 public: 80 PluginService(); 81 ~PluginService(); 82 83 bool CreatePluginSession(const ProfilerPluginConfig& pluginConfig, 84 const ProfilerSessionConfig::BufferConfig& bufferConfig, 85 const ProfilerDataRepeaterPtr& dataRepeater); 86 bool CreatePluginSession(const ProfilerPluginConfig& pluginConfig, const ProfilerDataRepeaterPtr& dataRepeater); 87 bool StartPluginSession(const ProfilerPluginConfig& config); 88 bool StopPluginSession(const std::string& pluginName); 89 bool DestroyPluginSession(const std::string& pluginName); 90 bool RefreshPluginSession(const std::string& pluginName); 91 92 bool AddPluginInfo(const PluginInfo& pluginInfo); 93 bool GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo); 94 bool RemovePluginInfo(const PluginInfo& pluginInfo); 95 96 bool AppendResult(NotifyResultRequest& request); 97 98 std::vector<ProfilerPluginState> GetPluginStatus(); 99 uint32_t GetPluginIdByName(std::string name); 100 101 void SetPluginSessionManager(const PluginSessionManagerPtr& pluginSessionManager); 102 void SetProfilerSessionConfig(const ProfilerSessionConfig& profilerSessionConfig); 103 104 std::pair<uint32_t, PluginContextPtr> GetPluginContext(const std::string& pluginName); 105 void SetTraceWriter(const TraceFileWriterPtr& traceWriter); 106 107 private: 108 bool StartService(const std::string& unixSocketName); 109 110 SemaphorePtr GetSemaphore(uint32_t) const; 111 void ReadShareMemoryOffline(PluginContext&); 112 void ReadShareMemoryOnline(PluginContext&); 113 PluginContextPtr GetPluginContextById(uint32_t id); 114 115 bool RemovePluginSessionCtx(const std::string& pluginName); 116 117 mutable std::mutex mutex_; 118 std::map<uint32_t, PluginContextPtr> pluginContext_; 119 std::map<uint32_t, SemaphorePtr> waitSemphores_; 120 std::map<std::string, uint32_t> nameIndex_; 121 122 std::atomic<uint32_t> pluginIdCounter_; 123 std::shared_ptr<ServiceEntry> serviceEntry_; 124 std::shared_ptr<PluginServiceImpl> pluginServiceImpl_; 125 std::shared_ptr<PluginCommandBuilder> pluginCommandBuilder_; 126 std::unique_ptr<EpollEventPoller> eventPoller_; 127 PluginSessionManagerPtr pluginSessionManager_; 128 ProfilerSessionConfig profilerSessionConfig_; 129 TraceFileWriterPtr traceWriter_ = nullptr; 130 uint32_t dataFlushSize_ = 0; 131 }; 132 133 #endif // PLUGIN_SERVICE_H