• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef HIVIEW_BASE_PLUGIN_PLATFORM_H
16 #define HIVIEW_BASE_PLUGIN_PLATFORM_H
17 #include <memory>
18 #include <mutex>
19 #include <string>
20 
21 #include "defines.h"
22 #include "dynamic_module.h"
23 #include "event_dispatch_queue.h"
24 #include "event_loop.h"
25 #include "event_source.h"
26 #include "pipeline.h"
27 #include "plugin.h"
28 #include "plugin_bundle.h"
29 #include "plugin_config.h"
30 #include "plugin_extra_info.h"
31 #include "dispatch_config.h"
32 #include "event_json_parser.h"
33 
34 #include "singleton.h"
35 namespace OHOS {
36 namespace HiviewDFX {
37 using PipelineConfigMap = std::map<std::string, std::shared_ptr<DispatchRule>>;
38 class HiviewPlatform : public HiviewContext, public Singleton<HiviewPlatform> {
39 public:
40     HiviewPlatform();
41     ~HiviewPlatform();
42     bool InitEnvironment(const std::string& platformConfigDir = "");
43     void ProcessArgsRequest(int argc, char* argv[]);
44     void StartLoop();
45     void SetMaxProxyIdleTime(time_t idleTime);
46     void SetCheckProxyIdlePeriod(time_t period);
47 
48     void PostUnorderedEvent(std::shared_ptr<Plugin> plugin, std::shared_ptr<Event> event) override;
49     void RegisterUnorderedEventListener(std::weak_ptr<EventListener> listener) override;
50     bool PostSyncEventToTarget(std::shared_ptr<Plugin> caller, const std::string& calleeName,
51         std::shared_ptr<Event> event) override;
52     void PostAsyncEventToTarget(std::shared_ptr<Plugin> caller, const std::string& calleeName,
53         std::shared_ptr<Event> event) override;
54     void RequestUnloadPlugin(std::shared_ptr<Plugin> caller) override;
55     std::list<std::weak_ptr<Plugin>> GetPipelineSequenceByName(const std::string& name) override;
56     std::shared_ptr<EventLoop> GetSharedWorkLoop() override;
57     std::string GetHiViewDirectory(DirectoryType type) override;
58     std::string GetHiviewProperty(const std::string& key, const std::string& defaultValue) override;
59     bool SetHiviewProperty(const std::string& key, const std::string& value, bool forceUpdate) override;
60     void PublishPluginCapacity(PluginCapacityInfo &pluginCapacityInfo) override;
61     void GetRemoteByCapacity(const std::string& plugin, const std::string& capacity,
62         std::list<std::string> &deviceIdList) override;
63     int32_t PostEventToRemote(std::shared_ptr<Plugin> caller, const std::string& deviceId,
64         const std::string& targetPlugin, std::shared_ptr<Event> event) override;
65     bool IsReady() override;
66     void AppendPluginToPipeline(const std::string& pluginName, const std::string& pipelineName) override;
67     void RequestLoadBundle(const std::string& bundleName __UNUSED) override;
68     void RequestUnloadBundle(const std::string& bundleName, uint64_t delay = 0) override;
69     std::shared_ptr<Plugin> InstancePluginByProxy(std::shared_ptr<Plugin> proxy) override;
70     std::shared_ptr<Plugin> GetPluginByName(const std::string& name) override;
71     void AddDispatchInfo(std::weak_ptr<Plugin> plugin, const std::unordered_set<uint8_t>& types,
72         const std::unordered_set<std::string>& eventNames, const std::unordered_set<std::string>& tags,
73         const std::unordered_map<std::string, DomainRule>& domainRulesMap) override;
74     std::vector<std::weak_ptr<Plugin>> GetDisPatcherInfo(uint32_t type,
75         const std::string& eventName, const std::string& tag, const std::string& domain) override;
76     void AddListenerInfo(uint32_t type, const std::string& name, const std::set<std::string>& eventNames,
77         const std::map<std::string, DomainRule>& domainRulesMap) override;
78     void AddListenerInfo(uint32_t type, const std::string& name) override;
79     std::vector<std::weak_ptr<EventListener>> GetListenerInfo(uint32_t type,
80         const std::string& eventName, const std::string& domain) override;
81 
GetEventJsonParser()82     std::shared_ptr<EventJsonParser> GetEventJsonParser()
83     {
84         return sysEventParser_;
85     }
86 
GetPipelineConfigMap()87     PipelineConfigMap& GetPipelineConfigMap()
88     {
89         return pipelineRules_;
90     }
91 
GetPluginMap()92     const std::map<std::string, std::shared_ptr<Plugin>>& GetPluginMap()
93     {
94         return pluginMap_;
95     }
96 
GetPipelineMap()97     const std::map<std::string, std::shared_ptr<Pipeline>>& GetPipelineMap()
98     {
99         return pipelines_;
100     }
101 
GetWorkLoopMap()102     const std::map<std::string, std::shared_ptr<EventLoop>>& GetWorkLoopMap()
103     {
104         return privateWorkLoopMap_;
105     }
106 
GetPluginBundleInfoMap()107     const std::map<std::string, std::shared_ptr<PluginBundle>>& GetPluginBundleInfoMap()
108     {
109         return pluginBundleInfos_;
110     }
111 
112 private:
113     struct ListenerInfo {
114         std::weak_ptr<EventListener> listener_;
115         std::vector<uint32_t> messageTypes_;
116         std::map<uint32_t, std::set<std::string>> eventsInfo_;
117         std::map<uint32_t, std::map<std::string, DomainRule>> domainsInfo_;
MatchListenerInfo118         bool Match(uint32_t type, const std::string& eventName, const std::string& domain)
119         {
120             auto it = std::find(messageTypes_.begin(), messageTypes_.end(), type);
121             if (it != messageTypes_.end()) {
122                 return true;
123             }
124             auto itEventList = eventsInfo_.find(type);
125             if (itEventList != eventsInfo_.end()) {
126                 auto eventList = itEventList->second;
127                 if (eventList.find(eventName) != eventList.end()) {
128                     return true;
129                 }
130             }
131             auto itDomainsInfo = domainsInfo_.find(type);
132             if (itDomainsInfo != domainsInfo_.end()) {
133                 auto itDomainRule = itDomainsInfo->second.find(domain);
134                 if (itDomainRule != itDomainsInfo->second.end()) {
135                     return itDomainRule->second.FindEvent(eventName);
136                 }
137             }
138             return false;
139         }
140     };
141 
142     struct DispatchInfo {
143         std::weak_ptr<Plugin> plugin_;
144         std::unordered_set<uint8_t> typesInfo_;
145         std::unordered_set<std::string> eventsInfo_;
146         std::unordered_set<std::string> tagsInfo_;
147         std::unordered_map<std::string, DomainRule> domainsInfo_;
MatchDispatchInfo148         bool Match(uint8_t type, const std::string& eventName, const std::string& tag,
149             const std::string& domain)
150         {
151             if (typesInfo_.find(type) != typesInfo_.end()) {
152                 return true;
153             }
154             if (tagsInfo_.find(tag) != tagsInfo_.end()) {
155                 return true;
156             }
157             if (eventsInfo_.find(eventName) != eventsInfo_.end()) {
158                 return true;
159             }
160             auto itDomainRule = domainsInfo_.find(domain);
161             if (itDomainRule != domainsInfo_.end()) {
162                 return itDomainRule->second.FindEvent(eventName);
163             }
164             return false;
165         }
166     };
167 
168     void CreateWorkingDirectories(const std::string& platformConfigDir);
169     void InitSysEventParser();
170     void StartPlatformDispatchQueue();
171     void CreatePlugin(const PluginConfig::PluginInfo& pluginInfo);
172     void CreatePipeline(const PluginConfig::PipelineInfo& pipelineInfo);
173     void InitPlugin(const PluginConfig& config __UNUSED, const PluginConfig::PluginInfo& pluginInfo);
174     void NotifyPluginReady();
175     void ScheduleCreateAndInitPlugin(const PluginConfig::PluginInfo& pluginInfo);
176     DynamicModule LoadDynamicPlugin(const std::string& name) const;
177     std::string GetDynamicLibName(const std::string& name, bool hasOhosSuffix) const;
178     std::shared_ptr<EventLoop> GetAvaliableWorkLoop(const std::string& name);
179     void CleanupUnusedResources();
180     void UnloadPlugin(const std::string& name);
181     void StartEventSource(std::shared_ptr<EventSource> source);
182     void ValidateAndCreateDirectory(std::string& defaultPath, const std::string& realPath);
183     void ValidateAndCreateDirectories(const std::string& localPath, const std::string& workPath,
184         const std::string& persistPath);
185     void LoadBusinessPlugin(const PluginConfig& config);
186     void ExitHiviewIfNeed();
187     std::string GetPluginConfigPath();
188     std::string SplitBundleNameFromPath(const std::string& filePath);
189     void UpdateBetaConfigIfNeed();
190     void LoadPluginBundles();
191     void LoadPluginBundle(const std::string& bundleName, const std::string& filePath);
192     void ScheduleCheckUnloadablePlugins();
193     void CheckUnloadablePlugins();
194     std::string SearchPluginBundle(const std::string& name) const;
195 
196     bool isReady_;
197     std::string defaultConfigDir_;
198     std::string defaultWorkDir_;
199     std::string defaultCommercialWorkDir_;
200     std::string defaultPersistDir_;
201     std::string defaultConfigName_;
202     std::vector<std::string> dynamicLibSearchDir_;
203     std::unique_ptr<EventDispatchQueue> unorderQueue_;
204     std::shared_ptr<EventLoop> sharedWorkLoop_;
205     std::shared_ptr<EventJsonParser> sysEventParser_ = nullptr;
206     std::map<std::string, std::shared_ptr<Plugin>> pluginMap_;
207     std::map<std::string, std::shared_ptr<Pipeline>> pipelines_;
208     std::map<std::string, std::shared_ptr<EventLoop>> privateWorkLoopMap_;
209     std::map<std::string, std::string> hiviewProperty_;
210     std::map<std::string, std::shared_ptr<PluginBundle>> pluginBundleInfos_;
211 
212     // Listener data structure:<pluginName, <domain_eventName, Plugin>>
213     std::unordered_map<std::string, std::shared_ptr<ListenerInfo>> listeners_;
214     std::unordered_map<std::string, std::shared_ptr<DispatchInfo>> dispatchers_;
215     PipelineConfigMap pipelineRules_;
216     std::vector<std::shared_ptr<Plugin>> eventSourceList_;
217 
218     // the max waited time before destroy plugin instance
219     const time_t DEFAULT_IDLE_TIME = 300; // 300 seconds
220     time_t maxIdleTime_ = DEFAULT_IDLE_TIME;
221     time_t checkIdlePeriod_ = DEFAULT_IDLE_TIME / 2; // 2 : half idle time
222 };
223 } // namespace HiviewDFX
224 } // namespace OHOS
225 #endif // HIVIEW_BASE_PLUGIN_PLATFORM_H
226