106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#ifndef PLUGIN_MANAGER_H
1706f6ba60Sopenharmony_ci#define PLUGIN_MANAGER_H
1806f6ba60Sopenharmony_ci
1906f6ba60Sopenharmony_ci#include <map>
2006f6ba60Sopenharmony_ci#include <memory>
2106f6ba60Sopenharmony_ci#include <string>
2206f6ba60Sopenharmony_ci#include <vector>
2306f6ba60Sopenharmony_ci#include <unordered_map>
2406f6ba60Sopenharmony_ci
2506f6ba60Sopenharmony_ci#include "manager_interface.h"
2606f6ba60Sopenharmony_ci#include "plugin_module.h"
2706f6ba60Sopenharmony_ci#include "schedule_task_manager.h"
2806f6ba60Sopenharmony_ci
2906f6ba60Sopenharmony_ciclass ProfilerPluginConfig;
3006f6ba60Sopenharmony_ciclass PluginResult;
3106f6ba60Sopenharmony_ciclass CommandPoller;
3206f6ba60Sopenharmony_ci
3306f6ba60Sopenharmony_ciusing CommandPollerPtr = STD_PTR(shared, CommandPoller);
3406f6ba60Sopenharmony_ciusing PluginModulePtr = STD_PTR(shared, PluginModule);
3506f6ba60Sopenharmony_ci
3606f6ba60Sopenharmony_ciclass PluginManager : public ManagerInterface {
3706f6ba60Sopenharmony_cipublic:
3806f6ba60Sopenharmony_ci    virtual ~PluginManager();
3906f6ba60Sopenharmony_ci    bool AddPlugin(const std::string& pluginPath);
4006f6ba60Sopenharmony_ci    bool RemovePlugin(const std::string& pluginPath);
4106f6ba60Sopenharmony_ci
4206f6ba60Sopenharmony_ci    bool LoadPlugin(const std::string& pluginName);
4306f6ba60Sopenharmony_ci    bool UnloadPlugin(const std::string& pluginName);
4406f6ba60Sopenharmony_ci    bool UnloadPlugin(const uint32_t pluginId);
4506f6ba60Sopenharmony_ci
4606f6ba60Sopenharmony_ci    // CommandPoller will call the following four interfaces after receiving the command
4706f6ba60Sopenharmony_ci    bool CreatePluginSession(const std::vector<ProfilerPluginConfig>& config);
4806f6ba60Sopenharmony_ci    bool DestroyPluginSession(const std::vector<uint32_t>& pluginIds);
4906f6ba60Sopenharmony_ci    bool StartPluginSession(const std::vector<uint32_t>& pluginIds, const std::vector<ProfilerPluginConfig>& config,
5006f6ba60Sopenharmony_ci                                PluginResult& result);
5106f6ba60Sopenharmony_ci    bool StopPluginSession(const std::vector<uint32_t>& pluginIds);
5206f6ba60Sopenharmony_ci    bool ReportPluginBasicData(const std::vector<uint32_t>& pluginIds);
5306f6ba60Sopenharmony_ci
5406f6ba60Sopenharmony_ci    // call the 'PluginModule::ReportResult' and 'PluginManager::SubmitResult' according to 'pluginId'
5506f6ba60Sopenharmony_ci    // creat PluginResult for  current plug-in inside
5606f6ba60Sopenharmony_ci    bool PullResult(uint32_t pluginId, bool isProtobufSerialize = true);
5706f6ba60Sopenharmony_ci    bool StopAllPluginSession();
5806f6ba60Sopenharmony_ci
5906f6ba60Sopenharmony_ci    // for test
6006f6ba60Sopenharmony_ci    virtual bool SubmitResult(const PluginResult& pluginResult);
6106f6ba60Sopenharmony_ci    bool CreateWriter(std::string pluginName, uint32_t bufferSize, int smbFd, int eventFd,
6206f6ba60Sopenharmony_ci                      bool isProtobufSerialize = true);
6306f6ba60Sopenharmony_ci    bool ResetWriter(uint32_t pluginId);
6406f6ba60Sopenharmony_ci    void SetCommandPoller(const CommandPollerPtr& p);
6506f6ba60Sopenharmony_ci    bool RegisterPlugin(const PluginModulePtr& plugin, const std::string& pluginPath,
6606f6ba60Sopenharmony_ci                        const PluginModuleInfo& pluginInfo);
6706f6ba60Sopenharmony_ci
6806f6ba60Sopenharmony_ciprivate:
6906f6ba60Sopenharmony_ci    std::map<uint32_t, std::shared_ptr<PluginModule>> pluginModules_;
7006f6ba60Sopenharmony_ci    std::map<std::string, uint32_t> pluginIds_; // pluginName maps to pluginId
7106f6ba60Sopenharmony_ci    CommandPollerPtr commandPoller_;
7206f6ba60Sopenharmony_ci    ScheduleTaskManager scheduleTaskManager_;
7306f6ba60Sopenharmony_ci    std::map<std::string, std::string> pluginPathAndNameMap_;
7406f6ba60Sopenharmony_ci    std::unordered_map<std::string, int32_t> scheduleTask_; // key is plugin name, value is timerfd.
7506f6ba60Sopenharmony_ci};
7606f6ba60Sopenharmony_ci
7706f6ba60Sopenharmony_ci#endif // PLUGIN_MANAGER_H
78