1 /*
2 * Copyright (c) 2021-2025 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 #include "sysevent_source.h"
17
18 #include "event_json_parser.h"
19 #include "hiview_logger.h"
20 #include "hiview_platform.h"
21 #include "plugin_factory.h"
22 #include "sys_event.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 REGISTER(SysEventSource);
27 DEFINE_LOG_TAG("SysEventSource");
28
HandlerEvent(std::shared_ptr<EventRaw::RawData> rawData)29 void SysEventReceiver::HandlerEvent(std::shared_ptr<EventRaw::RawData> rawData)
30 {
31 if (rawData == nullptr || rawData->GetData() == nullptr) {
32 HIVIEW_LOGW("raw data of sys event is null");
33 return;
34 }
35 std::shared_ptr<PipelineEvent> event = std::make_shared<SysEvent>("SysEventSource",
36 static_cast<PipelineEventProducer*>(&eventSource), rawData);
37 eventSource.PublishPipelineEvent(event);
38 }
39
OnLoad()40 void SysEventSource::OnLoad()
41 {
42 HIVIEW_LOGI("SysEventSource load");
43 std::shared_ptr<EventLoop> looper = GetHiviewContext()->GetSharedWorkLoop();
44 platformMonitor_.StartMonitor(looper);
45
46 // start sys event service
47 SysEventServiceAdapter::StartService(this);
48 SysEventServiceAdapter::SetWorkLoop(looper);
49 }
50
OnUnload()51 void SysEventSource::OnUnload()
52 {
53 HIVIEW_LOGI("SysEventSource unload");
54 eventServer_.Stop();
55 }
56
StartEventSource()57 void SysEventSource::StartEventSource()
58 {
59 HIVIEW_LOGI("SysEventSource start");
60 EventJsonParser::GetInstance()->ReadDefFile();
61 std::shared_ptr<EventReceiver> sysEventReceiver = std::make_shared<SysEventReceiver>(*this);
62 eventServer_.AddReceiver(sysEventReceiver);
63 eventServer_.Start();
64 }
65
Recycle(PipelineEvent * event)66 void SysEventSource::Recycle(PipelineEvent *event)
67 {
68 platformMonitor_.CollectCostTime(event);
69 }
70
PauseDispatch(std::weak_ptr<Plugin> plugin)71 void SysEventSource::PauseDispatch(std::weak_ptr<Plugin> plugin)
72 {
73 auto requester = plugin.lock();
74 if (requester != nullptr) {
75 HIVIEW_LOGI("process pause dispatch event from plugin:%s.\n", requester->GetName().c_str());
76 }
77 }
78
PublishPipelineEvent(std::shared_ptr<PipelineEvent> event)79 bool SysEventSource::PublishPipelineEvent(std::shared_ptr<PipelineEvent> event)
80 {
81 platformMonitor_.CollectEvent(event);
82 platformMonitor_.Breaking();
83
84 auto context = GetHiviewContext();
85 HiviewPlatform* hiviewPlatform = static_cast<HiviewPlatform*>(context);
86 if (hiviewPlatform == nullptr) {
87 HIVIEW_LOGW("hiviewPlatform is null");
88 return false;
89 }
90 auto const &pipelineRules = hiviewPlatform->GetPipelineConfigMap();
91 auto const &pipelineMap = hiviewPlatform->GetPipelineMap();
92 for (auto it = pipelineRules.begin(); it != pipelineRules.end(); it++) {
93 std::string pipelineName = it->first;
94 auto dispathRule = it->second;
95 if (dispathRule->FindEvent(event->domain_, event->eventName_)) {
96 pipelineMap.at(pipelineName)->ProcessEvent(event);
97 return true;
98 }
99 }
100
101 constexpr char defaultPipeline[] = "SysEventPipeline";
102 if (pipelineMap.find(defaultPipeline) == pipelineMap.end()) {
103 HIVIEW_LOGW("default %{public}s does not exist", defaultPipeline);
104 return false;
105 }
106 pipelineMap.at(defaultPipeline)->ProcessEvent(event);
107 return true;
108 }
109 } // namespace HiviewDFX
110 } // namespace OHOS
111