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