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 36class PluginServiceImpl; 37class ProfilerDataRepeater; 38class SocketContext; 39class ShareMemoryBlock; 40class PluginCommandBuilder; 41class PluginSessionManager; 42 43using ProfilerDataRepeaterPtr = STD_PTR(shared, ProfilerDataRepeater); 44using ProfilerPluginStatePtr = STD_PTR(shared, ProfilerPluginState); 45 46struct 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 58struct 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 74using PluginContextPtr = std::shared_ptr<PluginContext>; 75 76using PluginSessionManagerPtr = std::shared_ptr<PluginSessionManager>; 77 78class PluginService { 79public: 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 std::shared_ptr<ProfilerSessionConfig> profilerSessionConfig, 103 const std::vector<std::string>& pluginNames); 104 105 std::pair<uint32_t, PluginContextPtr> GetPluginContext(const std::string& pluginName); 106 void SetTraceWriter(const TraceFileWriterPtr& traceWriter); 107 void FlushAllData(const std::string& pluginName); 108 bool StartEpollThread(); 109 bool StopEpollThread(); 110 111private: 112 bool StartService(const std::string& unixSocketName); 113 114 SemaphorePtr GetSemaphore(uint32_t) const; 115 void ReadShareMemoryOffline(PluginContext&); 116 void ReadShareMemoryOnline(PluginContext&); 117 void FlushShareMemory(PluginContext&); 118 PluginContextPtr GetPluginContextById(uint32_t id); 119 120 bool RemovePluginSessionCtx(const std::string& pluginName); 121 122 mutable std::mutex mutex_; 123 std::map<uint32_t, PluginContextPtr> pluginContext_; 124 std::map<uint32_t, SemaphorePtr> waitSemphores_; 125 std::map<std::string, uint32_t> nameIndex_; 126 127 std::atomic<uint32_t> pluginIdCounter_; 128 std::shared_ptr<ServiceEntry> serviceEntry_; 129 std::shared_ptr<PluginServiceImpl> pluginServiceImpl_; 130 std::shared_ptr<PluginCommandBuilder> pluginCommandBuilder_; 131 std::unique_ptr<EpollEventPoller> eventPoller_; 132 PluginSessionManagerPtr pluginSessionManager_; 133 std::map<std::string, std::shared_ptr<ProfilerSessionConfig>> profilerSessionConfigs_; 134 TraceFileWriterPtr traceWriter_ = nullptr; 135 uint32_t dataFlushSize_ = 0; 136 bool isProtobufSerialize_ = true; 137}; 138 139#endif // PLUGIN_SERVICE_H