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_MANAGER_H
17 #define PLUGIN_MANAGER_H
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 #include <unordered_map>
24 
25 #include "manager_interface.h"
26 #include "plugin_module.h"
27 #include "schedule_task_manager.h"
28 
29 class ProfilerPluginConfig;
30 class PluginResult;
31 class CommandPoller;
32 
33 using CommandPollerPtr = STD_PTR(shared, CommandPoller);
34 using PluginModulePtr = STD_PTR(shared, PluginModule);
35 
36 class PluginManager : public ManagerInterface {
37 public:
38     virtual ~PluginManager();
39     bool AddPlugin(const std::string& pluginPath);
40     bool RemovePlugin(const std::string& pluginPath);
41 
42     bool LoadPlugin(const std::string& pluginName);
43     bool UnloadPlugin(const std::string& pluginName);
44     bool UnloadPlugin(const uint32_t pluginId);
45 
46     // CommandPoller will call the following four interfaces after receiving the command
47     bool CreatePluginSession(const std::vector<ProfilerPluginConfig>& config);
48     bool DestroyPluginSession(const std::vector<uint32_t>& pluginIds);
49     bool StartPluginSession(const std::vector<uint32_t>& pluginIds, const std::vector<ProfilerPluginConfig>& config,
50                                 PluginResult& result);
51     bool StopPluginSession(const std::vector<uint32_t>& pluginIds);
52     bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds);
53 
54     // call the 'PluginModule::ReportResult' and 'PluginManager::SubmitResult' according to 'pluginId'
55     // creat PluginResult for  current plug-in inside
56     bool PullResult(uint32_t pluginId, bool isProtobufSerialize = true);
57     bool StopAllPluginSession();
58 
59     // for test
60     virtual bool SubmitResult(const PluginResult& pluginResult);
61     bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd,
62                       bool isProtobufSerialize = true);
63     bool ResetWriter(uint32_t pluginId);
64     void SetCommandPoller(const CommandPollerPtr& p);
65     bool RegisterPlugin(const PluginModulePtr& plugin, const std::string& pluginPath,
66                         const PluginModuleInfo& pluginInfo);
67 
68 private:
69     std::map<uint32_t, std::shared_ptr<PluginModule>> pluginModules_;
70     std::map<std::string, uint32_t> pluginIds_; // pluginName maps to pluginId
71     CommandPollerPtr commandPoller_;
72     ScheduleTaskManager scheduleTaskManager_;
73     std::map<std::string, std::string> pluginPathAndNameMap_;
74     std::unordered_map<std::string, int32_t> scheduleTask_; // key is plugin name, value is timerfd.
75 };
76 
77 #endif // PLUGIN_MANAGER_H
78