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 "defines.h"
22 #include "logger.h"
23 #include "plugin_factory.h"
24 #include "time_util.h"
25 #include "sys_event.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 REGISTER(SysEventSource);
30 DEFINE_LOG_TAG("HiView-SysEventSource");
31
Parser(const std::string & rawMsg) const32 std::shared_ptr<PipelineEvent> SysEventParser::Parser(const std::string& rawMsg) const
33 {
34 HIVIEW_LOGD("parser raw message size=%{public}d, %{public}s", rawMsg.length(), rawMsg.c_str());
35 auto baseEvent = std::make_shared<SysEvent>("SysEventSource", pipeProducer, rawMsg);
36 if (baseEvent->ParseJson() != 0) {
37 HIVIEW_LOGI("parser sys event error");
38 return nullptr;
39 }
40 HIVIEW_LOGI("parser result domain_=%{public}s eventName_=%{public}s",
41 baseEvent->domain_.c_str(), baseEvent->eventName_.c_str());
42 return baseEvent;
43 }
44
HandlerEvent(const std::string & rawMsg)45 void SysEventReceiver::HandlerEvent(const std::string& rawMsg)
46 {
47 SysEventParser sysEventParser(static_cast<PipelineEventProducer *>(&eventSource));
48 std::shared_ptr<PipelineEvent> event = sysEventParser.Parser(rawMsg);
49 if (event != nullptr) {
50 event->realtime_ += TimeUtil::GenerateTimestamp() - event->createTime_;
51 }
52 eventSource.PublishPipelineEvent(event);
53 }
54
OnLoad()55 void SysEventSource::OnLoad()
56 {
57 HIVIEW_LOGI("SysEventSource load ");
58 std::shared_ptr<EventLoop> looper = GetHiviewContext()->GetSharedWorkLoop();
59 platformMonitor_.StartMonitor(looper);
60 }
61
OnUnload()62 void SysEventSource::OnUnload()
63 {
64 eventServer.Stop();
65 HIVIEW_LOGI("SysEventSource unload");
66 }
67
StartEventSource()68 void SysEventSource::StartEventSource()
69 {
70 HIVIEW_LOGI("SysEventSource start");
71 std::shared_ptr<EventReceiver> sysEventReceiver = std::make_shared<SysEventReceiver>(*this);
72 eventServer.AddReceiver(sysEventReceiver);
73 eventServer.Start();
74 }
75
Recycle(PipelineEvent * event)76 void SysEventSource::Recycle(PipelineEvent *event)
77 {
78 platformMonitor_.CollectCostTime(event);
79 }
80
PauseDispatch(std::weak_ptr<Plugin> plugin)81 void SysEventSource::PauseDispatch(std::weak_ptr<Plugin> plugin)
82 {
83 auto requester = plugin.lock();
84 if (requester != nullptr) {
85 HIVIEW_LOGI("process pause dispatch event from plugin:%s.\n", requester->GetName().c_str());
86 }
87 }
88
PublishPipelineEvent(std::shared_ptr<PipelineEvent> event)89 bool SysEventSource::PublishPipelineEvent(std::shared_ptr<PipelineEvent> event)
90 {
91 platformMonitor_.CollectEvent(event);
92 platformMonitor_.Breaking();
93 return EventSource::PublishPipelineEvent(event);
94 }
95 } // namespace HiviewDFX
96 } // namespace OHOS