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