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
16 #include "sysevent_source.h"
17
18 #include <functional>
19 #include <memory>
20
21 #include "decoded/decoded_event.h"
22 #include "defines.h"
23 #include "logger.h"
24 #include "plugin_factory.h"
25 #include "time_util.h"
26 #include "sys_event.h"
27 #include "hiview_platform.h"
28 #include "dispatch_config.h"
29
30 namespace OHOS {
31 namespace HiviewDFX {
32 REGISTER(SysEventSource);
33 DEFINE_LOG_TAG("HiView-SysEventSource");
34
Parser(std::shared_ptr<EventRaw::RawData> rawData) const35 std::shared_ptr<PipelineEvent> SysEventParser::Parser(std::shared_ptr<EventRaw::RawData> rawData) const
36 {
37 if (rawData == nullptr) {
38 HIVIEW_LOGI("raw data of sys event is null");
39 return nullptr;
40 }
41 return std::make_shared<SysEvent>("SysEventSource", pipeProducer, rawData);
42 }
43
HandlerEvent(std::shared_ptr<EventRaw::RawData> rawData)44 void SysEventReceiver::HandlerEvent(std::shared_ptr<EventRaw::RawData> rawData)
45 {
46 SysEventParser sysEventParser(static_cast<PipelineEventProducer *>(&eventSource));
47 std::shared_ptr<PipelineEvent> event = sysEventParser.Parser(rawData);
48 if (event != nullptr) {
49 event->realtime_ += TimeUtil::GenerateTimestamp() - event->createTime_;
50 }
51 if (eventSource.CheckValidSysEvent(event)) {
52 eventSource.PublishPipelineEvent(event);
53 }
54 }
55
OnLoad()56 void SysEventSource::OnLoad()
57 {
58 HIVIEW_LOGI("SysEventSource load ");
59 std::shared_ptr<EventLoop> looper = GetHiviewContext()->GetSharedWorkLoop();
60 platformMonitor_.StartMonitor(looper);
61 sysEventParser_ = HiviewPlatform::GetInstance().GetEventJsonParser();
62 sysEventStat_ = std::make_unique<SysEventStat>();
63 }
64
OnUnload()65 void SysEventSource::OnUnload()
66 {
67 eventServer.Stop();
68 HIVIEW_LOGI("SysEventSource unload");
69 }
70
StartEventSource()71 void SysEventSource::StartEventSource()
72 {
73 HIVIEW_LOGI("SysEventSource start");
74 std::shared_ptr<EventReceiver> sysEventReceiver = std::make_shared<SysEventReceiver>(*this);
75 eventServer.AddReceiver(sysEventReceiver);
76 eventServer.Start();
77 }
78
Recycle(PipelineEvent * event)79 void SysEventSource::Recycle(PipelineEvent *event)
80 {
81 platformMonitor_.CollectCostTime(event);
82 }
83
PauseDispatch(std::weak_ptr<Plugin> plugin)84 void SysEventSource::PauseDispatch(std::weak_ptr<Plugin> plugin)
85 {
86 auto requester = plugin.lock();
87 if (requester != nullptr) {
88 HIVIEW_LOGI("process pause dispatch event from plugin:%s.\n", requester->GetName().c_str());
89 }
90 }
91
PublishPipelineEvent(std::shared_ptr<PipelineEvent> event)92 bool SysEventSource::PublishPipelineEvent(std::shared_ptr<PipelineEvent> event)
93 {
94 platformMonitor_.CollectEvent(event);
95 platformMonitor_.Breaking();
96 auto &platform = HiviewPlatform::GetInstance();
97 auto const &pipelineRules = platform.GetPipelineConfigMap();
98 auto const &pipelineMap = platform.GetPipelineMap();
99 for (auto it = pipelineRules.begin(); it != pipelineRules.end(); it++) {
100 std::string pipelineName = it->first;
101 auto dispathRule = it->second;
102 if (dispathRule->FindEvent(event->domain_, event->eventName_)) {
103 pipelineMap.at(pipelineName)->ProcessEvent(event);
104 return true;
105 }
106 }
107 pipelineMap.at("SysEventPipeline")->ProcessEvent(event);
108 return true;
109 }
110
CheckValidSysEvent(std::shared_ptr<Event> event)111 bool SysEventSource::CheckValidSysEvent(std::shared_ptr<Event> event)
112 {
113 std::shared_ptr<SysEvent> sysEvent = Convert2SysEvent(event);
114 if (sysEvent == nullptr || sysEventParser_ == nullptr) {
115 HIVIEW_LOGE("event or event parser is null.");
116 sysEventStat_->AccumulateEvent(false);
117 return false;
118 }
119 if (!sysEventParser_->HandleEventJson(sysEvent)) {
120 sysEventStat_->AccumulateEvent(sysEvent->domain_, sysEvent->eventName_, false);
121 return false;
122 }
123 HIVIEW_LOGI("event[%{public}s|%{public}s|%{public}" PRId64 "] is valid.",
124 sysEvent->domain_.c_str(), sysEvent->eventName_.c_str(), sysEvent->GetEventSeq());
125 sysEvent->SetTag(sysEventParser_->GetTagByDomainAndName(sysEvent->domain_, sysEvent->eventName_));
126 sysEvent->eventType_ = sysEventParser_->GetTypeByDomainAndName(sysEvent->domain_, sysEvent->eventName_);
127 sysEvent->preserve_ = sysEventParser_->GetPreserveByDomainAndName(sysEvent->domain_, sysEvent->eventName_);
128 sysEventStat_->AccumulateEvent();
129 return true;
130 }
131
Convert2SysEvent(std::shared_ptr<Event> & event)132 std::shared_ptr<SysEvent> SysEventSource::Convert2SysEvent(std::shared_ptr<Event>& event)
133 {
134 if (event == nullptr) {
135 HIVIEW_LOGE("event is null");
136 return nullptr;
137 }
138 if (event->messageType_ != Event::MessageType::SYS_EVENT) {
139 HIVIEW_LOGE("receive out of sys event type");
140 return nullptr;
141 }
142 std::shared_ptr<SysEvent> sysEvent = Event::DownCastTo<SysEvent>(event);
143 if (sysEvent == nullptr) {
144 HIVIEW_LOGE("sysevent is null");
145 }
146 return sysEvent;
147 }
148
ShowUsage(int fd,const std::vector<std::string> & cmds)149 static void ShowUsage(int fd, const std::vector<std::string>& cmds)
150 {
151 dprintf(fd, "invalid cmd:");
152 for (auto it = cmds.begin(); it != cmds.end(); it++) {
153 dprintf(fd, "%s ", it->c_str());
154 }
155 dprintf(fd, "\n");
156 dprintf(fd, "usage: SysEventService [sum|detail|invalid|clear]\n");
157 }
158
Dump(int fd,const std::vector<std::string> & cmds)159 void SysEventSource::Dump(int fd, const std::vector<std::string>& cmds)
160 {
161 if (cmds.size() >= 2) { // 2:args from the second item
162 std::string arg1 = cmds[1];
163 if (arg1 == "sum") {
164 sysEventStat_->StatSummary(fd);
165 } else if (arg1 == "detail") {
166 sysEventStat_->StatDetail(fd);
167 } else if (arg1 == "invalid") {
168 sysEventStat_->StatInvalidDetail(fd);
169 } else if (arg1 == "clear") {
170 sysEventStat_->Clear(fd);
171 } else {
172 ShowUsage(fd, cmds);
173 }
174 } else {
175 sysEventStat_->StatSummary(fd);
176 }
177 }
178 } // namespace HiviewDFX
179 } // namespace OHOS
180