• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "plugin.h"
16 #include <functional>
17 #include "audit.h"
18 #include "defines.h"
19 #include "file_util.h"
20 #include "hiview_event_report.h"
21 #include "thread_util.h"
22 #include "time_util.h"
23 namespace OHOS {
24 namespace HiviewDFX {
~Plugin()25 Plugin::~Plugin()
26 {
27     if (handle_ != DynamicModuleDefault) {
28         UnloadModule(handle_);
29         handle_ = DynamicModuleDefault;
30     }
31 }
32 
OnEvent(std::shared_ptr<Event> & event __UNUSED)33 bool Plugin::OnEvent(std::shared_ptr<Event>& event __UNUSED)
34 {
35     return true;
36 }
37 
CanProcessEvent(std::shared_ptr<Event> event __UNUSED)38 bool Plugin::CanProcessEvent(std::shared_ptr<Event> event __UNUSED)
39 {
40     return true;
41 }
42 
IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)43 bool Plugin::IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)
44 {
45     return true;
46 }
47 
CanProcessMoreEvents()48 bool Plugin::CanProcessMoreEvents()
49 {
50     return true;
51 }
52 
OnEventProxy(std::shared_ptr<Event> event)53 bool Plugin::OnEventProxy(std::shared_ptr<Event> event)
54 {
55     if (event == nullptr) {
56         return false;
57     }
58 
59     std::shared_ptr<Event> dupEvent = event;
60     auto processorSize = dupEvent->GetPendingProcessorSize();
61     dupEvent->ResetPendingStatus();
62     bool ret = false;
63     auto timePtr = std::make_shared<uint64_t>(0);
64     {
65         TimeUtil::TimeCalculator tc(timePtr);
66         ret = OnEvent(dupEvent);
67     }
68     HiviewEventReport::UpdatePluginStats(this->name_, event->eventName_, *timePtr);
69     event->realtime_ +=  *timePtr;
70 
71     if (!dupEvent->IsPipelineEvent()) {
72         if (Audit::IsEnabled()) {
73             Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_OUT, dupEvent->createTime_,
74                 std::to_string(Thread::GetTid()));
75         }
76     } else {
77         if ((!dupEvent->HasFinish() && !dupEvent->HasPending()) &&
78             (processorSize == dupEvent->GetPendingProcessorSize())) {
79             dupEvent->OnContinue();
80         }
81     }
82     return ret;
83 }
84 
DelayProcessEvent(std::shared_ptr<Event> event,uint64_t delay)85 void Plugin::DelayProcessEvent(std::shared_ptr<Event> event, uint64_t delay)
86 {
87     if (workLoop_ == nullptr || event == nullptr) {
88         return;
89     }
90 
91     UpdateTimeByDelay(delay);
92     event->OnPending();
93     auto task = std::bind(&Plugin::OnEventProxy, this, event);
94     workLoop_->AddTimerEvent(nullptr, nullptr, task, delay, false);
95     return;
96 }
97 
AddEventListenerInfo(uint32_t type,const EventListener::EventIdRange & range)98 void Plugin::AddEventListenerInfo(uint32_t type, const EventListener::EventIdRange& range)
99 {
100     if (context_ == nullptr) {
101         return;
102     }
103     std::set<std::string> eventNames;
104     std::set<EventListener::EventIdRange> listenerInfo;
105     listenerInfo.insert(range);
106     context_->AddListenerInfo(type, shared_from_this(), eventNames, listenerInfo);
107 }
108 
AddEventListenerInfo(uint32_t type,const std::set<EventListener::EventIdRange> & listenerInfo)109 void Plugin::AddEventListenerInfo(uint32_t type, const std::set<EventListener::EventIdRange> &listenerInfo)
110 {
111     if (context_ == nullptr) {
112         return;
113     }
114     std::set<std::string> eventNames;
115     context_->AddListenerInfo(type, shared_from_this(), eventNames, listenerInfo);
116 }
117 
GetEventListenerInfo(uint32_t type,std::set<EventListener::EventIdRange> & listenerInfo)118 bool Plugin::GetEventListenerInfo(uint32_t type, std::set<EventListener::EventIdRange> &listenerInfo)
119 {
120     if (context_ == nullptr) {
121         return false;
122     }
123     return context_->GetListenerInfo(type, name_, listenerInfo);
124 }
125 
AddEventListenerInfo(uint32_t type,const std::string & eventName)126 void Plugin::AddEventListenerInfo(uint32_t type, const std::string& eventName)
127 {
128     if (context_ == nullptr) {
129         return;
130     }
131     std::set<std::string> eventNames;
132     eventNames.insert(eventName);
133     std::set<EventListener::EventIdRange> listenerInfo;
134     context_->AddListenerInfo(type, shared_from_this(), eventNames, listenerInfo);
135 }
136 
AddEventListenerInfo(uint32_t type,const std::set<std::string> & eventNames)137 void Plugin::AddEventListenerInfo(uint32_t type, const std::set<std::string> &eventNames)
138 {
139     if (context_ == nullptr) {
140         return;
141     }
142     std::set<EventListener::EventIdRange> listenerInfo;
143     context_->AddListenerInfo(type, shared_from_this(), eventNames, listenerInfo);
144 }
145 
GetEventListenerInfo(uint32_t type,std::set<std::string> & eventNames)146 bool Plugin::GetEventListenerInfo(uint32_t type, std::set<std::string> &eventNames)
147 {
148     if (context_ == nullptr) {
149         return false;
150     }
151     return context_->GetListenerInfo(type, name_, eventNames);
152 }
153 
GetPluginInfo()154 std::string Plugin::GetPluginInfo()
155 {
156     return GetName();
157 }
158 
GetHandlerInfo()159 std::string Plugin::GetHandlerInfo()
160 {
161     return GetName();
162 }
163 
GetName()164 const std::string& Plugin::GetName()
165 {
166     return name_;
167 }
168 
GetVersion()169 const std::string& Plugin::GetVersion()
170 {
171     return version_;
172 }
173 
SetName(const std::string & name)174 void Plugin::SetName(const std::string& name)
175 {
176     name_ = name;
177 }
178 
SetVersion(const std::string & version)179 void Plugin::SetVersion(const std::string& version)
180 {
181     version_ = version;
182 }
183 
BindWorkLoop(std::shared_ptr<EventLoop> loop)184 void Plugin::BindWorkLoop(std::shared_ptr<EventLoop> loop)
185 {
186     workLoop_ = loop;
187 }
188 
GetWorkLoop()189 std::shared_ptr<EventLoop> Plugin::GetWorkLoop()
190 {
191     return workLoop_;
192 }
193 
UpdateActiveTime()194 void Plugin::UpdateActiveTime()
195 {
196     lastActiveTime_ = time(nullptr);
197 }
198 
UpdateTimeByDelay(time_t delay)199 void Plugin::UpdateTimeByDelay(time_t delay)
200 {
201     lastActiveTime_ = time(nullptr) + delay;
202 }
203 } // namespace HiviewDFX
204 } // namespace OHOS
205