/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PLUGIN_SERVICE_H #define PLUGIN_SERVICE_H #include #include #include #include #include #include #include "common_types.pb.h" #include "epoll_event_poller.h" #include "event_notifier.h" #include "i_semaphore.h" #include "logging.h" #include "plugin_service_types.pb.h" #include "profiler_service_types.pb.h" #include "service_entry.h" class PluginServiceImpl; class ProfilerDataRepeater; class SocketContext; class ShareMemoryBlock; class PluginCommandBuilder; class PluginSessionManager; using ProfilerDataRepeaterPtr = STD_PTR(shared, ProfilerDataRepeater); using ProfilerPluginStatePtr = STD_PTR(shared, ProfilerPluginState); struct PluginInfo { uint32_t id = 0; std::string name; std::string path; std::string sha256; uint32_t bufferSizeHint; SocketContext* context = nullptr; }; struct PluginContext { std::string name; std::string path; std::string sha256; uint32_t bufferSizeHint; SocketContext* context = nullptr; ProfilerPluginConfig config; ProfilerDataRepeaterPtr profilerDataRepeater; std::shared_ptr shareMemoryBlock; EventNotifierPtr eventNotifier; ProfilerPluginState profilerPluginState; }; using PluginContextPtr = std::shared_ptr; using PluginSessionManagerPtr = std::shared_ptr; class PluginService { public: PluginService(); ~PluginService(); bool CreatePluginSession(const ProfilerPluginConfig& pluginConfig, const ProfilerSessionConfig::BufferConfig& bufferConfig, const ProfilerDataRepeaterPtr& dataRepeater); bool CreatePluginSession(const ProfilerPluginConfig& pluginConfig, const ProfilerDataRepeaterPtr& dataRepeater); bool StartPluginSession(const ProfilerPluginConfig& config); bool StopPluginSession(const std::string& pluginName); bool DestroyPluginSession(const std::string& pluginName); bool AddPluginInfo(const PluginInfo& pluginInfo); bool GetPluginInfo(const std::string& pluginName, PluginInfo& pluginInfo); bool RemovePluginInfo(const PluginInfo& pluginInfo); bool AppendResult(NotifyResultRequest& request); std::vector GetPluginStatus(); uint32_t GetPluginIdByName(std::string name); void SetPluginSessionManager(const PluginSessionManagerPtr& pluginSessionManager); private: bool StartService(const std::string& unixSocketName); SemaphorePtr GetSemaphore(uint32_t) const; void ReadShareMemory(PluginContext&); std::pair GetPluginContext(const std::string& pluginName); PluginContextPtr GetPluginContextById(uint32_t id); bool RemovePluginSessionCtx(const std::string& pluginName); mutable std::mutex mutex_; std::map pluginContext_; std::map waitSemphores_; std::map nameIndex_; std::atomic pluginIdCounter_; std::shared_ptr serviceEntry_; std::shared_ptr pluginServiceImpl_; std::shared_ptr pluginCommandBuilder_; std::unique_ptr eventPoller_; PluginSessionManagerPtr pluginSessionManager_; }; #endif // PLUGIN_SERVICE_H