1 /* 2 * Copyright (c) 2021-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 #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 "param_event_manager.h" 27 #include "pipeline.h" 28 #include "plugin.h" 29 #include "plugin_bundle.h" 30 #include "plugin_config.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 bool IsReady() override; 61 void AppendPluginToPipeline(const std::string& pluginName, const std::string& pipelineName) override; 62 void RequestLoadBundle(const std::string& bundleName __UNUSED) override; 63 void RequestUnloadBundle(const std::string& bundleName, uint64_t delay = 0) override; 64 std::shared_ptr<Plugin> InstancePluginByProxy(std::shared_ptr<Plugin> proxy) override; 65 std::shared_ptr<Plugin> GetPluginByName(const std::string& name) override; 66 void AddDispatchInfo(std::weak_ptr<Plugin> plugin, const std::unordered_set<uint8_t>& types, 67 const std::unordered_set<std::string>& eventNames, const std::unordered_set<std::string>& tags, 68 const std::unordered_map<std::string, DomainRule>& domainRulesMap) override; 69 std::vector<std::weak_ptr<Plugin>> GetDisPatcherInfo(uint32_t type, 70 const std::string& eventName, const std::string& tag, const std::string& domain) override; 71 void AddListenerInfo(uint32_t type, const std::string& name, const std::set<std::string>& eventNames, 72 const std::map<std::string, DomainRule>& domainRulesMap) override; 73 void AddListenerInfo(uint32_t type, const std::string& name) override; 74 std::vector<std::weak_ptr<EventListener>> GetListenerInfo(uint32_t type, 75 const std::string& eventName, const std::string& domain) override; 76 GetEventJsonParser()77 std::shared_ptr<EventJsonParser> GetEventJsonParser() 78 { 79 return sysEventParser_; 80 } 81 GetPipelineConfigMap()82 PipelineConfigMap& GetPipelineConfigMap() 83 { 84 return pipelineRules_; 85 } 86 GetPluginMap()87 const std::map<std::string, std::shared_ptr<Plugin>>& GetPluginMap() 88 { 89 return pluginMap_; 90 } 91 GetPipelineMap()92 const std::map<std::string, std::shared_ptr<Pipeline>>& GetPipelineMap() 93 { 94 return pipelines_; 95 } 96 GetWorkLoopMap()97 const std::map<std::string, std::shared_ptr<EventLoop>>& GetWorkLoopMap() 98 { 99 return privateWorkLoopMap_; 100 } 101 GetPluginBundleInfoMap()102 const std::map<std::string, std::shared_ptr<PluginBundle>>& GetPluginBundleInfoMap() 103 { 104 return pluginBundleInfos_; 105 } 106 107 private: 108 struct ListenerInfo { 109 std::weak_ptr<EventListener> listener_; 110 std::vector<uint32_t> messageTypes_; 111 std::map<uint32_t, std::set<std::string>> eventsInfo_; 112 std::map<uint32_t, std::map<std::string, DomainRule>> domainsInfo_; MatchListenerInfo113 bool Match(uint32_t type, const std::string& eventName, const std::string& domain) 114 { 115 auto it = std::find(messageTypes_.begin(), messageTypes_.end(), type); 116 if (it != messageTypes_.end()) { 117 return true; 118 } 119 auto itEventList = eventsInfo_.find(type); 120 if (itEventList != eventsInfo_.end()) { 121 auto eventList = itEventList->second; 122 if (eventList.find(eventName) != eventList.end()) { 123 return true; 124 } 125 } 126 auto itDomainsInfo = domainsInfo_.find(type); 127 if (itDomainsInfo != domainsInfo_.end()) { 128 auto itDomainRule = itDomainsInfo->second.find(domain); 129 if (itDomainRule != itDomainsInfo->second.end()) { 130 return itDomainRule->second.FindEvent(eventName); 131 } 132 } 133 return false; 134 } 135 }; 136 137 struct DispatchInfo { 138 std::weak_ptr<Plugin> plugin_; 139 std::unordered_set<uint8_t> typesInfo_; 140 std::unordered_set<std::string> eventsInfo_; 141 std::unordered_set<std::string> tagsInfo_; 142 std::unordered_map<std::string, DomainRule> domainsInfo_; MatchDispatchInfo143 bool Match(uint8_t type, const std::string& eventName, const std::string& tag, 144 const std::string& domain) 145 { 146 if (typesInfo_.find(type) != typesInfo_.end()) { 147 return true; 148 } 149 if (tagsInfo_.find(tag) != tagsInfo_.end()) { 150 return true; 151 } 152 if (eventsInfo_.find(eventName) != eventsInfo_.end()) { 153 return true; 154 } 155 auto itDomainRule = domainsInfo_.find(domain); 156 if (itDomainRule != domainsInfo_.end()) { 157 return itDomainRule->second.FindEvent(eventName); 158 } 159 return false; 160 } 161 }; 162 163 void CreateWorkingDirectories(const std::string& platformConfigDir); 164 void InitSysEventParser(); 165 void StartPlatformDispatchQueue(); 166 void CreatePlugin(const PluginConfig::PluginInfo& pluginInfo); 167 void CreatePipeline(const PluginConfig::PipelineInfo& pipelineInfo); 168 void InitPlugin(const PluginConfig& config __UNUSED, const PluginConfig::PluginInfo& pluginInfo); 169 void NotifyPluginReady(); 170 void ScheduleCreateAndInitPlugin(const PluginConfig::PluginInfo& pluginInfo); 171 DynamicModule LoadDynamicPlugin(const std::string& name) const; 172 std::string GetDynamicLibName(const std::string& name, bool hasOhosSuffix) const; 173 std::shared_ptr<EventLoop> GetAvaliableWorkLoop(const std::string& name); 174 void CleanupUnusedResources(); 175 void UnloadPlugin(const std::string& name); 176 void StartEventSource(std::shared_ptr<EventSource> source); 177 void ValidateAndCreateDirectory(std::string& defaultPath, const std::string& realPath); 178 void ValidateAndCreateDirectories(const std::string& localPath, const std::string& workPath, 179 const std::string& persistPath); 180 void LoadBusinessPlugin(const PluginConfig& config); 181 void ExitHiviewIfNeed(); 182 std::string GetPluginConfigPath(); 183 std::string SplitBundleNameFromPath(const std::string& filePath); 184 void UpdateBetaConfigIfNeed(); 185 void LoadPluginBundles(); 186 void LoadPluginBundle(const std::string& bundleName, const std::string& filePath); 187 void ScheduleCheckUnloadablePlugins(); 188 void CheckUnloadablePlugins(); 189 std::string SearchPluginBundle(const std::string& name) const; 190 191 bool isReady_; 192 std::string defaultConfigDir_; 193 std::string defaultWorkDir_; 194 std::string defaultCommercialWorkDir_; 195 std::string defaultPersistDir_; 196 std::string defaultConfigName_; 197 std::vector<std::string> dynamicLibSearchDir_; 198 std::unique_ptr<EventDispatchQueue> unorderQueue_; 199 std::shared_ptr<EventLoop> sharedWorkLoop_; 200 std::shared_ptr<EventJsonParser> sysEventParser_ = nullptr; 201 std::map<std::string, std::shared_ptr<Plugin>> pluginMap_; 202 std::map<std::string, std::shared_ptr<Pipeline>> pipelines_; 203 std::map<std::string, std::shared_ptr<EventLoop>> privateWorkLoopMap_; 204 std::map<std::string, std::string> hiviewProperty_; 205 std::map<std::string, std::shared_ptr<PluginBundle>> pluginBundleInfos_; 206 207 // Listener data structure:<pluginName, <domain_eventName, Plugin>> 208 std::unordered_map<std::string, std::shared_ptr<ListenerInfo>> listeners_; 209 std::unordered_map<std::string, std::shared_ptr<DispatchInfo>> dispatchers_; 210 PipelineConfigMap pipelineRules_; 211 std::vector<std::shared_ptr<Plugin>> eventSourceList_; 212 213 // the max waited time before destroy plugin instance 214 const time_t DEFAULT_IDLE_TIME = 300; // 300 seconds 215 time_t maxIdleTime_ = DEFAULT_IDLE_TIME; 216 time_t checkIdlePeriod_ = DEFAULT_IDLE_TIME / 2; // 2 : half idle time 217 ParamEventManager paramEventManager; 218 }; 219 } // namespace HiviewDFX 220 } // namespace OHOS 221 #endif // HIVIEW_BASE_PLUGIN_PLATFORM_H 222