• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_PLUGIN_MGR_H
17 #define RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_PLUGIN_MGR_H
18 
19 #include <functional>
20 #include <list>
21 #include <string>
22 #include <memory>
23 #include <map>
24 #include <vector>
25 #include "datetime_ex.h"
26 #include "event_handler.h"
27 #include "config_reader.h"
28 #include "plugin_switch.h"
29 #include "plugin.h"
30 #include "nocopyable.h"
31 #include "res_data.h"
32 #include "res_type.h"
33 #include "single_instance.h"
34 #include "config_info.h"
35 
36 namespace OHOS {
37 namespace ResourceSchedule {
38 using Clock = std::chrono::high_resolution_clock;
39 using TimePoint = std::chrono::time_point<Clock>;
40 using OnPluginInitFunc = bool (*)(const std::string);
41 using OnDispatchResourceFunc = void (*)(const std::shared_ptr<ResData>&);
42 using OnDumpFunc = void (*)(const std::vector<std::string>&, std::string&);
43 using OnPluginDisableFunc = void (*)();
44 
45 constexpr int32_t DISPATCH_TIME_OUT = 50; // ms
46 constexpr int32_t DISPATCH_TIME_OUT_US = DISPATCH_TIME_OUT * 1000; // us
47 constexpr int32_t PLUGIN_STAT_MAX_USE_COUNT = 1000;
48 
49 struct PluginStat {
50     uint64_t totalTime;
51     uint64_t useCount;
52     std::list<TimePoint> timeOutTime;
53 
UpdatePluginStat54     inline void Update(int32_t costTime)
55     {
56         if (costTime > 0 && costTime < DISPATCH_TIME_OUT_US) {
57             if (totalTime + (uint32_t)costTime < totalTime) {
58                 totalTime = (uint32_t)costTime;
59                 useCount = 1;
60             } else {
61                 totalTime += (uint32_t)costTime;
62                 useCount += 1;
63             }
64         }
65     }
66 
AverageTimePluginStat67     inline uint64_t AverageTime()
68     {
69         return (useCount > 0) ? (totalTime / useCount) : 0;
70     }
71 };
72 
73 struct PluginLib {
74     std::shared_ptr<void> handle = nullptr;
75     OnPluginInitFunc onPluginInitFunc_;
76     OnDispatchResourceFunc onDispatchResourceFunc_;
77     OnDumpFunc onDumpFunc_;
78     OnPluginDisableFunc onPluginDisableFunc_;
79 };
80 
81 class PluginMgr {
82     DECLARE_SINGLE_INSTANCE_BASE(PluginMgr);
83 
84 public:
85     ~PluginMgr();
86 
87     /**
88      * Init pluginmanager, load xml config file, construct plugin instances.
89      */
90     void Init();
91 
92     /**
93      * Disable all plugins, maybe service exception happens or stopped.
94      */
95     void Stop();
96 
97     /**
98      * receive all reported resource data, then dispatch all plugins.
99      *
100      * @param resData Reported resource data.
101      */
102     void DispatchResource(const std::shared_ptr<ResData>& resData);
103 
104     /**
105      * Subscribe resource type from plugin.
106      *
107      * @param pluginLib The lib name of plugin.
108      * @param resType interested in resource type.
109      */
110     void SubscribeResource(const std::string& pluginLib, uint32_t resType);
111 
112     /**
113      * Unsubscribe resource type from plugin.
114      *
115      * @param pluginLib The lib name of plugin.
116      * @param resType interested in resource type.
117      */
118     void UnSubscribeResource(const std::string& pluginLib, uint32_t resType);
119 
120     /**
121      * Kill process by pid.
122      *
123      * @param payload process message.
124      */
125     void KillProcessByPid(const nlohmann::json& payload, std::string killClientInitiator);
126 
127     void DumpAllPlugin(std::string &result);
128 
129     void DumpOnePlugin(std::string &result, std::string pluginName, std::vector<std::string>& args);
130 
131     std::string DumpInfoFromPlugin(std::string& result, std::string libPath, std::vector<std::string>& args);
132 
133     void DumpHelpFromPlugin(std::string& result);
134 
135     PluginConfig GetConfig(const std::string& pluginName, const std::string& configName);
136 
137     const std::shared_ptr<AppExecFwk::EventRunner>& GetRunner();
138 
139 private:
140     PluginMgr() = default;
141     std::string GetRealConfigPath(const char* configName);
142     void OnDestroy();
143     void LoadPlugin();
144     std::shared_ptr<PluginLib> LoadOnePlugin(const PluginInfo& info);
145     void UnLoadPlugin();
146     void ClearResource();
147     void DeliverResourceToPlugin(const std::list<std::string>& pluginList, const std::shared_ptr<ResData>& resData);
148     void RepairPlugin(TimePoint endTime, const std::string& pluginLib, PluginLib libInfo);
149     void RemoveDisablePluginHandler();
150     void DumpPluginInfoAppend(std::string &result, PluginInfo info);
151     bool GetPluginListByResType(uint32_t resType, std::list<std::string>& pluginList);
152 
153     // plugin crash 3 times in 60s, will be disable forever
154     const int32_t MAX_PLUGIN_TIMEOUT_TIMES = 3;
155     const int32_t DISABLE_PLUGIN_TIME = 60000;
156     const int32_t DUMP_ONE_STRING_SIZE = 32;
157     std::unique_ptr<ConfigReader> configReader_ = nullptr;
158     std::unique_ptr<PluginSwitch> pluginSwitch_ = nullptr;
159 
160     std::mutex pluginMutex_;
161     std::map<std::string, PluginLib> pluginLibMap_;
162 
163     // mutex for resTypeMap_
164     std::mutex resTypeMutex_;
165     std::map<uint32_t, std::list<std::string>> resTypeLibMap_;
166 
167     std::mutex dispatcherHandlerMutex_;
168     std::shared_ptr<AppExecFwk::EventHandler> dispatcher_ = nullptr;
169     std::shared_ptr<AppExecFwk::EventRunner> asyncRunner_ = nullptr;
170     std::map<std::string, PluginStat> pluginStat_;
171 };
172 } // namespace ResourceSchedule
173 } // namespace OHOS
174 
175 #endif // RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_PLUGIN_MGR_H
176