成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

OpenHarmony 源碼解析之DFX子系統-Hiview(上)

開發 開發工具
DFX(Design for X)子系統是為了提升軟件質量設計的工具集,目前包含的內容主要有:DFR(Design for Reliability,可靠性)和DFT(Design for Testability,可測試性)特性。

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

1 簡介

DFX(Design for X)子系統是為了提升軟件質量設計的工具集,目前包含的內容主要有:DFR(Design for Reliability,可靠性)和DFT(Design for Testability,可測試性)特性。

已實現以下功能:

  • HiLog:流水日志。
  • HiSysEvent:系統事件記錄接口。
  • HiView:插件平臺。
  • FaultLoggerd:應用故障訂閱和收集。
  • HiAppEvent: js應用事件記錄接口。

1.1 OpenHarmony架構圖

OpenHarmony 源碼解析之DFX子系統-Hiview(上)-鴻蒙HarmonyOS技術社區

1.2 Hiview簡介

Hiview是一個跨平臺的終端設備維測服務集。目前開源部分僅包含插件管理平臺和系統事件源。

Hiview架構圖如下:

OpenHarmony 源碼解析之DFX子系統-Hiview(上)-鴻蒙HarmonyOS技術社區

Hiview由框架和插件組成,分別為:

  • 操作系統適配層(adapter),對使用的系統服務的接口進行適配。
  • Hiview基礎定義(hiview base),包括插件基類、管道的定義,事件、事件隊列定義以及一些工具類。
  • Hiview的核心模塊(hiview core),包括插件配置,插件管理以及事件源。
  • Hiview服務(hiview services),目前僅包括hiview運行信息dump功能。
  • Hiview插件(plugins),為獨立功能的業務模塊。
  • Hiview維測服務是由事件驅動的,其核心為分布在系統各處的HiSysEvent樁點。

格式化的事件通過HiSysEvent API上報至hiview進行處理,如下圖:

OpenHarmony 源碼解析之DFX子系統-Hiview(上)-鴻蒙HarmonyOS技術社區
  • 應用框架、系統服務使用HiSysEvent組件上報系統事件。
  • Hiview中SysEventSource獲取消息,解析并組裝成管道事件分發給插件處理。

1.3 Hiview代碼目錄

  1. /base/hiviewdfx/hiview. 
  2. ├── adapter             #平臺適配 
  3. │   ├── service        #服務適配 
  4. │   └── system_service #系統接口適配 
  5. ├── base                #模塊定義,工具類 
  6. │   └── utility 
  7. ├── build               #編譯腳本 
  8. ├── core                #插件管理核心代碼 
  9. ├── include             #公共定義 
  10. ├── plugins             #插件 
  11. ├── service             #HiviewService服務 
  12. └── utility             #工具類 

2 源碼分析

本文主要分析hiview插件管理平臺的初始化,和事件處理流程。

hiview是個常駐服務,在開機階段啟動。

base/hiviewdfx/hiview/service/config/hiview.cfg定義如下

  1.     "jobs" : [{ 
  2.             "name" : "post-fs-data"
  3.             "cmds" : [ 
  4.                 "mkdir /data/log/ 0770 system log"
  5.                 "mkdir /data/log/faultlog/ 0770 system system"
  6.                 "mkdir /data/log/faultlog/temp/ 0770 system system"
  7.                 "mkdir /data/log/faultlog/faultlogger/ 0770 system system"
  8.                 "start hiview" 
  9.             ] 
  10.         } 
  11.     ], 
  12.     "services" : [{ 
  13.             "name" : "hiview"
  14.             "path" : ["/system/bin/hiview"], 
  15.             "uid" : "system"
  16.             "gid" : ["system""log"], 
  17.             "writepid" : [ 
  18.                 "/dev/cpuset/system-background/tasks" 
  19.             ], 
  20.             "socket" : [ 
  21.                 "hisysevent dgram 0666 root system passcred" 
  22.             ] 
  23.         } 
  24.     ] 

2.1 初始化

hiview的入口函數定義在base/hiviewdfx/hiview/main.cpp中

  1. int main(int argc __UNUSED, char* argv[] __UNUSED) 
  2.     auto& hiview = OHOS::HiviewDFX::HiviewPlatform::GetInstance(); 
  3.     // process cmdline 
  4.     hiview.ProcessArgsRequest(argc, argv); 
  5.  
  6.     // 初始化環境,主要解析配置文件,加載插件 
  7.     if (!hiview.InitEnvironment()) { 
  8.         HIVIEW_LOGW("Fail to init plugin environment. exit"); 
  9.         return -1; 
  10.     } 
  11.  
  12.     // 啟動HiviewService服務,該服務提供了hiview運行信息dump功能 
  13.     auto hiviewService = std::make_unique<OHOS::HiviewDFX::HiviewService>(); 
  14.     hiviewService->StartService(); 
  15.     return 0; 

 HiviewPlatform::InitEnvironment()函數實現如下:

  1. bool HiviewPlatform::InitEnvironment(const std::string& defaultDir, const std::string& cloudUpdateDir, 
  2.                                      const std::string& workDir, const std::string& persistDir) 
  3.     // 創建工作目錄,目前目錄名使用了默認的空字符串,所以并未實際創建工作目錄 
  4.     ValidateAndCreateDirectories(defaultDir, cloudUpdateDir, workDir, persistDir); 
  5.  
  6.     // update beta config 
  7.     UpdateBetaConfigIfNeed(); 
  8.  
  9.     // check whether hiview is already started 
  10.     ExitHiviewIfNeed(); 
  11.  
  12.     // 解析"/system/etc/hiview/plugin_config"插件配置文件 
  13.     std::string cfgPath = GetPluginConfigPath(); 
  14.     PluginConfig config(cfgPath); 
  15.     if (!config.StartParse()) { //...........注[1] 
  16.         HIVIEW_LOGE("Fail to parse plugin config. exit!"); 
  17.         return false
  18.     } 
  19.     // 啟動插件管理平臺消息隊列 
  20.     StartPlatformDispatchQueue(); 
  21.  
  22.     // init global context helper, remove in the future 
  23.     HiviewGlobal::CreateInstance(static_cast<HiviewContext&>(*this)); 
  24.     //加載插件 
  25.     LoadBusinessPlugin(config); 
  26.  
  27.     isReady_ = true
  28.     NotifyPluginReady(); 
  29.     return true

注[1]處PluginConfig::StartParse()函數會按照特定規則去解析插件配置文件:

  1. if (field == "plugins") { 
  2.        ParsePlugin(strTmp); 
  3.    } else if (field == "pipelines") { 
  4.        ParsePipeline(strTmp); 
  5.    } else if (field == "pipelinegroups") { 
  6.        ParsePipelineGroup(strTmp); 
  7.    } 

 目前使用的插件配置文件/system/etc/hiview/plugin_config內容如下:

  1. plugins:3 
  2. SysEventSource[thread:sysevent_source]:0 static 
  3. Faultlogger[]:0 static 
  4. SysEventService[thread:sysevent_service]:0 static 
  5. pipelines:1 
  6. SysEventPipeline:SysEventService Faultlogger 
  7. pipelinegroups:1 
  8. SysEventSource:SysEventPipeline 

 注:base/hiviewdfx/hiview/plugins 下有eventlogger eventservice faultlogger freeze_detector hicollie_collector五個插件目錄,而目前插件配置文件里實際只用到了Faultlogger和SysEventService。

PluginConfig::StartParse()解析完之后會把相關信息保存到列表中。類圖如下:

OpenHarmony 源碼解析之DFX子系統-Hiview(上)-鴻蒙HarmonyOS技術社區

PluginConfig::ParsePipelineGroup()代碼如下

  1. void PluginConfig::ParsePipelineGroup(const std::string& pipelineGroup) 
  2.     std::smatch result; 
  3.     // EventSourceExample:FromTwo2Three FromThree2Two 
  4.     if (!regex_search(pipelineGroup, result, std::regex("(\\S+)\\s*:(.+)"))) { 
  5.         HIVIEW_LOGW("Fail to match pipeline group expression."); 
  6.         return
  7.     } 
  8.  
  9.     const int pipelineGroupNameField = 1; 
  10.     const int pipelineNameListField = 2; 
  11.     std::string eventSourceName = StringUtil::TrimStr(result.str(pipelineGroupNameField)); 
  12.     auto ret = std::find_if(pluginInfoList_.begin(), pluginInfoList_.end(), [&](PluginInfo& info) { 
  13.         if (info.name == eventSourceName) { 
  14.             info.isEventSource = true
  15.             info.pipelineNameList = StringUtil::SplitStr(result.str(pipelineNameListField)); 
  16.             return true
  17.         } 
  18.         return false
  19.     }); 
  20.     if (ret != std::end(pluginInfoList_)) { 
  21.         HIVIEW_LOGD("%s is an event source.", eventSourceName.c_str()); 
  22.     } 

說明:

  • 在解析pipelinegroups時,如果發現pipelineGroupName和pluginInfoList中某個插件的name一致,則把該插件PluginInfo.isEventSource置為true并且把pipelineNameList賦值給PluginInfo.pipelineNameList。
  • 結合/system/etc/hiview/plugin_config配置文件,可以看到SysEventSource插件是帶管道(SysEventPipeline)的,插件SysEventService和Faultlogger隸屬于管道SysEventPipeline,用于處理SysEventSource扔給管道的事件。

解析完插件配置信息之后會調用HiviewPlatform::LoadBusinessPlugin(const PluginConfig& config)去裝載插件和管道。

代碼如下

  1. void HiviewPlatform::LoadBusinessPlugin(const PluginConfig& config) 
  2.     // start to load plugin 
  3.     // 1. 遍歷pluginInfoList,根據插件名創建插件。因為目前配置的插件加載延時(loadDelay)為0,所以直接走[2],調用CreatePlugin()創建插件并添加到pluginMap_中。 
  4.     auto const& pluginInfoList = config.GetPluginInfoList(); 
  5.     for (auto const& pluginInfo : pluginInfoList) { 
  6.         HIVIEW_LOGI("Start to create plugin %{public}s delay:%{public}d", pluginInfo.name.c_str(), 
  7.                     pluginInfo.loadDelay); 
  8.         if (pluginInfo.loadDelay > 0) { //.............[1] 
  9.             auto task = std::bind(&HiviewPlatform::ScheduleCreateAndInitPlugin, this, pluginInfo); 
  10.             sharedWorkLoop_->AddTimerEvent(nullptr, nullptr, task, pluginInfo.loadDelay, false); 
  11.         } else {       //...............[2] 
  12.             CreatePlugin(pluginInfo); 
  13.         } 
  14.     } 
  15.     // 2. 遍歷pipelineInfoList,調用CreatePipeline()創建管道并添加到pipelines_中。 
  16.     auto const& pipelineInfoList = config.GetPipelineInfoList(); 
  17.     for (auto const& pipelineInfo : pipelineInfoList) { 
  18.         HIVIEW_LOGI("Start to create pipeline %{public}s", pipelineInfo.name.c_str()); 
  19.         CreatePipeline(pipelineInfo); 
  20.     } 
  21.  
  22.     // 3. 遍歷pluginInfoList,調用InitPlugin()初始化插件 
  23.     for (auto const& pluginInfo : pluginInfoList) { 
  24.         HIVIEW_LOGI("Start to Load plugin %{public}s", pluginInfo.name.c_str()); 
  25.         InitPlugin(config, pluginInfo); //............注[1] 
  26.     } 
  27.  
  28.     CleanupUnusedResources(); 

說明:

注[1]InitPlugin()這一步中,如果插件的workHandlerType為thread,則綁定工作線程EventLoop。如果插件是EventSource類型,則綁定管道Pipeline,并且調用StartEventSource開啟消息監聽。代碼如下:

  1. void HiviewPlatform::InitPlugin(const PluginConfig& config __UNUSED, const PluginConfig::PluginInfo& pluginInfo) 
  2.     auto& plugin = pluginMap_[pluginInfo.name]; 
  3.     if (plugin == nullptr) { 
  4.         return
  5.     } 
  6.     // 如果插件的workHandlerType為thread,則綁定工作線程EventLoop。 
  7.     if (pluginInfo.workHandlerType == "thread") { 
  8.         auto workLoop = GetAvaliableWorkLoop(pluginInfo.workHandlerName); 
  9.         plugin->BindWorkLoop(workLoop); 
  10.     } 
  11.  
  12.     auto begin = TimeUtil::GenerateTimestamp(); 
  13.     plugin->OnLoad(); 
  14.     // 如果插件是EventSource類型,則添加管道Pipeline,并且調用StartEventSource開啟消息監聽 
  15.     if (pluginInfo.isEventSource) { 
  16.         auto sharedSource = std::static_pointer_cast<EventSource>(plugin); 
  17.         if (sharedSource == nullptr) { 
  18.             HIVIEW_LOGE("Fail to cast plugin to event source!"); 
  19.             return
  20.         } 
  21.         for (auto& pipelineName : pluginInfo.pipelineNameList) { 
  22.             sharedSource->AddPipeline(pipelines_[pipelineName]); 
  23.         } 
  24.         StartEventSource(sharedSource); 
  25.     } 
  26.     auto end = TimeUtil::GenerateTimestamp(); 
  27.     HIVIEW_LOGI("Plugin %{public}s loadtime:%{public}" PRIu64 ".", pluginInfo.name.c_str(), end - begin); 

至此,插件管理平臺初始化工作已完成。

2.2 事件處理流程

結合插件配置文件,目前實際使用的插件類圖如下:

OpenHarmony 源碼解析之DFX子系統-Hiview(上)-鴻蒙HarmonyOS技術社區

說明:

  • 前文最后的代碼段提到,如果插件是EventSource類型,則綁定管道,并且調用StartEventSource開啟消息監聽。結合類圖,只有SysEventSource是EventSource類型的插件,所以只有SysEventSource持有管道。
  • EventSource類型的插件會監聽HiSysEvent接口發來的消息,解析并組裝成管道事件,分發給管道中的插件處理。

SysEventSource::StartEventSource()函數實現如下。

  1. void SysEventSource::StartEventSource() 
  2.     HIVIEW_LOGI("SysEventSource start"); 
  3.     std::shared_ptr<EventReceiver> sysEventReceiver = std::make_shared<SysEventReceiver>(*this); 
  4.     eventServer.AddReceiver(sysEventReceiver); 
  5.  
  6.     eventServer.Start(); 

SysEventSource有個成員變量eventServer,EventServer會開啟socketserver端用于接收HiSysEvent接口發來的socket消息。

類圖如下:

OpenHarmony 源碼解析之DFX子系統-Hiview(上)-鴻蒙HarmonyOS技術社區
OpenHarmony 源碼解析之DFX子系統-Hiview(上)-鴻蒙HarmonyOS技術社區

說明:

1.SysEventSource::StartEventSource()中把SysEventReceiver對象加入到EventServer的receivers_容器中。

2.EventServer收到socket消息之后,調用SysEventReceiver::HandlerEvent(const std::string& rawMsg)方法處理接收到的消息。

3.SysEventReceiver::HandlerEvent()方法中會調用SysEventParser::Parser(const std::string& rawMsg)方法解析消息并組裝成SysEvent對象,然后調用EventSource::PublishPipelineEvent(std::shared_ptr event)方法把管道事件發布出去。

EventSource::PublishPipelineEvent()代碼如下:

  1. bool EventSource::PublishPipelineEvent(std::shared_ptr<PipelineEvent> event) 
  2.     HIVIEW_LOGD("EventSource PublishPipelineEvent"); 
  3.     if (event == nullptr) { 
  4.         return false
  5.     } 
  6.  
  7.     if (Audit::IsEnabled()) { 
  8.         auto digest = GetPluginInfo() + Audit::DOMAIN_DELIMITER + event->GetEventInfo(); 
  9.         Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_CREATE, event->createTime_, digest); 
  10.     } 
  11.  
  12.     for (auto& pipeline : listeners_) { 
  13.         if ((pipeline != nullptr) && (pipeline->CanProcessEvent(event))) { 
  14.             // one event can only be processed by one pipeline 
  15.             pipeline->ProcessEvent(event); 
  16.             return true
  17.         } 
  18.     } 
  19.     return false

結合上面的類圖,EventSource持有管道列表對象listeners_,EventSource::PublishPipelineEvent()函數中會遍歷該列表,調用Pipeline::ProcessEvent(std::shared_ptr event)函數去處理管道事件。(Pipeline對象是在HiviewPlatform::InitPlugin()函數中被加入到EventSource.listeners_中的。)

Pipeline類型有個成員變量std::list

在插件平臺初始化的時候,HiviewPlatform::CreatePipeline()函數中已經把Plugin插件對象加入到了processors_列表中。

接下來分析Pipeline::ProcessEvent()函數做了什么。

  1. void Pipeline::ProcessEvent(std::shared_ptr<PipelineEvent> event) 
  2.     event->SetPipelineInfo(name_, processors_); 
  3.     event->OnContinue(); 

 把Pipeline的processors_對象賦給了PipelineEvent的processors_對象,然后調用

PipelineEvent::OnContinue()函數。

接下來分析PipelineEvent::OnContinue()函數:

  1. bool PipelineEvent::OnContinue() 
  2.     // 判斷hasFinish_標志位和processors_列表是否已經為空,如已為空,結束本次事件傳遞 
  3.     if ((!hasFinish_) && processors_.empty()) { 
  4.         return OnFinish(); 
  5.     } 
  6.  
  7.     // once the event start delivering 
  8.     // the call OnContinue means one has done the processing of the event 
  9.     // this may be called by upstream event processor or the framework 
  10.     if (Audit::IsEnabled() && startDeliver_) { 
  11.         Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_OUT, 
  12.             createTime_, std::to_string(Thread::GetTid())); 
  13.     } 
  14.  
  15.     // the framework will call OnContinue when the event is assigned to a pipeline 
  16.     if (!startDeliver_) { 
  17.         startDeliver_ = true
  18.     } 
  19.  
  20.     // 取出processors_列表中第一個Plugin元素,并從列表彈出 
  21.     std::weak_ptr<Plugin> plugin = processors_.front(); 
  22.     processors_.pop_front(); 
  23.     if (auto pluginPtr = plugin.lock()) { 
  24.         if (!pluginPtr->CanProcessMoreEvents()) { 
  25.             handler_->PauseDispatch(plugin); 
  26.         } 
  27.  
  28.         if (Audit::IsEnabled()) { 
  29.             Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_IN, createTime_, 
  30.                                    pluginPtr->GetHandlerInfo()); 
  31.         } 
  32.         // 判斷當前Plugin是否開啟了事件工作隊列,如有則加入事件隊列處理,如沒有直接調用OnEventProxy 
  33.         if (auto workLoop = pluginPtr->GetWorkLoop()) { 
  34.             workLoop->AddEvent(pluginPtr, shared_from_this()); //............[1] 
  35.         } else { 
  36.             pluginPtr->OnEventProxy(shared_from_this()); //..........[2] 
  37.         } 
  38.     } else { 
  39.         return OnContinue(); 
  40.     } 
  41.     return true

不管插件是否使用了事件隊列,最終都會調用到Plugin::OnEventProxy()函數來處理管道事件。

Plugin::OnEventProxy()函數實現如下:

  1. bool Plugin::OnEventProxy(std::shared_ptr<Event> event) 
  2.     if (event == nullptr) { 
  3.  
  4.         return false
  5.     } 
  6.  
  7.     std::shared_ptr<Event> dupEvent = event; 
  8.     auto processorSize = dupEvent->GetPendingProcessorSize(); 
  9.     dupEvent->ResetPendingStatus(); 
  10.     bool ret = OnEvent(dupEvent); //..............注[1] 
  11.  
  12.     if (!dupEvent->IsPipelineEvent()) { 
  13.         if (Audit::IsEnabled()) { 
  14.             Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_OUT, dupEvent->createTime_, 
  15.                 std::to_string(Thread::GetTid())); 
  16.         } 
  17.     } else { 
  18.         if ((!dupEvent->HasFinish() && !dupEvent->HasPending()) && 
  19.             (processorSize == dupEvent->GetPendingProcessorSize())) { 
  20.             dupEvent->OnContinue();//.............注[2] 
  21.         } 
  22.     } 
  23.     return ret; 

先調用注[1]OnEvent()函數處理事件,再判斷管道事件傳遞是否已結束,如未結束則調用注[2]PipelineEvent::OnContinue()函數繼續把管道事件傳遞給后面的

插件處理。

結合目前的插件配置文件,整個插件管理平臺的消息處理流程大致如下:

EventServer接收到HiSysEvent組件接口發來的消息 --> SysEventReceiver::HandlerEvent() --> SysEventParser::Parser() --> EventSource::PublishPipelineEvent() --> Pipeline::ProcessEvent() --> PipelineEvent::OnContinue() --> SysEventService::OnEventProxy --> SysEventService::OnEvent --> PipelineEvent::OnContinue() --> Faultlogger::OnEventProxy --> Faultlogger::OnEvent --> 結束

至此,事件處理流程已分析完。

3 總結

以上內容首先分析了hiview插件管理平臺的初始化,然后對事件的處理流程做了分析。后續會詳細講解base/hiviewdfx/hiview/plugins目錄下每個插件的源碼。

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2021-11-08 15:04:47

鴻蒙HarmonyOS應用

2022-01-13 10:11:59

鴻蒙HarmonyOS應用

2022-02-17 20:57:07

OpenHarmon操作系統鴻蒙

2022-01-06 16:17:58

鴻蒙HarmonyOS應用

2021-12-17 16:42:09

鴻蒙HarmonyOS應用

2021-09-18 14:40:37

鴻蒙HarmonyOS應用

2023-04-12 15:31:11

系統服務管理鴻蒙

2022-05-10 11:17:27

電話子系統數據服務模塊

2021-11-18 10:28:03

鴻蒙HarmonyOS應用

2022-05-24 15:46:51

Wi-FiSTA模式

2021-09-13 15:15:18

鴻蒙HarmonyOS應用

2023-04-06 09:14:11

多模輸入子系統鴻蒙

2023-06-28 15:00:02

開源鴻蒙輸入系統架構

2021-09-17 14:38:58

鴻蒙HarmonyOS應用

2022-01-20 14:33:29

openharmonwayland協議鴻蒙

2022-02-14 14:47:11

SystemUIOpenHarmon鴻蒙

2022-03-18 16:07:04

Graphic子系統鴻蒙

2022-05-17 10:42:36

reboot源碼解析

2022-06-13 14:18:39

電源管理子系統耗電量服務

2021-09-16 15:08:08

鴻蒙HarmonyOS應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲三级国产 | 久久久久久免费毛片精品 | 亚洲视频www| 亚洲 欧美 另类 日韩 | 久久一 | 午夜性视频 | 成人一区二区三区在线观看 | 精品视频999 | 日韩高清在线 | 欧美精品一区二区在线观看 | 狠狠操狠狠操 | 欧美一区二区在线观看视频 | 亚洲激精日韩激精欧美精品 | 在线观看免费黄色片 | 亚洲欧美视频 | 亚洲精品欧美 | 欧美成人一级视频 | 神马久久av | 久久久久久黄 | 亚洲精品一二三区 | 国产午夜精品久久久久免费视高清 | 天天天堂| 日操操夜操操 | 久久免费精品 | 免费观看黄 | 在线观看免费av网 | 欧美精品一区二区在线观看 | 亚洲精品一区二区三区四区高清 | 国产最新精品视频 | 国产成人精品一区二区 | 日本天天操| 中文字幕一区在线 | 久久99蜜桃综合影院免费观看 | 在线成人免费视频 | 精品国产一级片 | 日本免费黄色一级片 | 欧美日韩精品一区二区三区视频 | 91在线精品视频 | 国产精品中文字幕在线 | 国产精品视频网站 | 国产美女在线播放 |