• 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 
24 namespace OHOS {
25 namespace HiviewDFX {
~Plugin()26 Plugin::~Plugin()
27 {
28     if (handle_ != DynamicModuleDefault) {
29         UnloadModule(handle_);
30         handle_ = DynamicModuleDefault;
31     }
32 }
33 
OnEvent(std::shared_ptr<Event> & event __UNUSED)34 bool Plugin::OnEvent(std::shared_ptr<Event>& event __UNUSED)
35 {
36     return true;
37 }
38 
CanProcessEvent(std::shared_ptr<Event> event __UNUSED)39 bool Plugin::CanProcessEvent(std::shared_ptr<Event> event __UNUSED)
40 {
41     return true;
42 }
43 
IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)44 bool Plugin::IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)
45 {
46     return true;
47 }
48 
CanProcessMoreEvents()49 bool Plugin::CanProcessMoreEvents()
50 {
51     return true;
52 }
53 
OnEventProxy(std::shared_ptr<Event> event)54 bool Plugin::OnEventProxy(std::shared_ptr<Event> event)
55 {
56     if (event == nullptr) {
57         return false;
58     }
59 
60     std::shared_ptr<Event> dupEvent = event;
61     auto processorSize = dupEvent->GetPendingProcessorSize();
62     dupEvent->ResetPendingStatus();
63     bool ret = false;
64     auto timePtr = std::make_shared<uint64_t>(0);
65     {
66         TimeUtil::TimeCalculator tc(timePtr);
67         ret = OnEvent(dupEvent);
68     }
69     HiviewEventReport::UpdatePluginStats(this->name_, event->eventName_, *timePtr);
70     event->realtime_ +=  *timePtr;
71 
72     if (!dupEvent->IsPipelineEvent()) {
73         if (Audit::IsEnabled()) {
74             Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_OUT, dupEvent->createTime_,
75                 std::to_string(Thread::GetTid()));
76         }
77     } else {
78         if ((!dupEvent->HasFinish() && !dupEvent->HasPending()) &&
79             (processorSize == dupEvent->GetPendingProcessorSize())) {
80             dupEvent->OnContinue();
81         }
82     }
83     return ret;
84 }
85 
DelayProcessEvent(std::shared_ptr<Event> event,uint64_t delay)86 void Plugin::DelayProcessEvent(std::shared_ptr<Event> event, uint64_t delay)
87 {
88     if (workLoop_ == nullptr || event == nullptr) {
89         return;
90     }
91 
92     UpdateTimeByDelay(delay);
93     event->OnPending();
94     auto task = std::bind(&Plugin::OnEventProxy, this, event);
95     workLoop_->AddTimerEvent(nullptr, nullptr, task, delay, false);
96     return;
97 }
98 
AddDispatchInfo(const std::unordered_set<uint8_t> & types,const std::unordered_set<std::string> & eventNames,const std::unordered_set<std::string> & tags,const std::unordered_map<std::string,DomainRule> & domainRulesMap)99 void Plugin::AddDispatchInfo(const std::unordered_set<uint8_t>& types,
100     const std::unordered_set<std::string> &eventNames, const std::unordered_set<std::string> &tags,
101     const std::unordered_map<std::string, DomainRule>& domainRulesMap)
102 {
103     if (context_ == nullptr) {
104         return;
105     }
106     context_->AddDispatchInfo(shared_from_this(), types, eventNames, tags, domainRulesMap);
107 }
108 
GetPluginInfo()109 std::string Plugin::GetPluginInfo()
110 {
111     return GetName();
112 }
113 
GetHandlerInfo()114 std::string Plugin::GetHandlerInfo()
115 {
116     return GetName();
117 }
118 
GetName()119 const std::string& Plugin::GetName()
120 {
121     return name_;
122 }
123 
GetVersion()124 const std::string& Plugin::GetVersion()
125 {
126     return version_;
127 }
128 
SetName(const std::string & name)129 void Plugin::SetName(const std::string& name)
130 {
131     name_ = name;
132 }
133 
SetVersion(const std::string & version)134 void Plugin::SetVersion(const std::string& version)
135 {
136     version_ = version;
137 }
138 
BindWorkLoop(std::shared_ptr<EventLoop> loop)139 void Plugin::BindWorkLoop(std::shared_ptr<EventLoop> loop)
140 {
141     workLoop_ = loop;
142 }
143 
GetWorkLoop()144 std::shared_ptr<EventLoop> Plugin::GetWorkLoop()
145 {
146     return workLoop_;
147 }
148 
UpdateActiveTime()149 void Plugin::UpdateActiveTime()
150 {
151     lastActiveTime_ = time(nullptr);
152 }
153 
UpdateTimeByDelay(time_t delay)154 void Plugin::UpdateTimeByDelay(time_t delay)
155 {
156     lastActiveTime_ = time(nullptr) + delay;
157 }
158 } // namespace HiviewDFX
159 } // namespace OHOS
160