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