• 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 
16 #include "freeze_detector_plugin.h"
17 
18 #include <algorithm>
19 
20 #include "ffrt.h"
21 #include "hiview_logger.h"
22 #include "plugin_factory.h"
23 #include "process_status.h"
24 #include "string_util.h"
25 #include "sys_event_dao.h"
26 
27 #include "decoded/decoded_event.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 namespace {
32     const static uint64_t TO_NANO_SECOND_MULTPLE = 1000000;
33     const static int MIN_APP_UID = 10000;
34 }
35 REGISTER_PROXY(FreezeDetectorPlugin);
36 DEFINE_LOG_LABEL(0xD002D01, "FreezeDetector");
FreezeDetectorPlugin()37 FreezeDetectorPlugin::FreezeDetectorPlugin()
38 {
39 }
40 
~FreezeDetectorPlugin()41 FreezeDetectorPlugin::~FreezeDetectorPlugin()
42 {
43 }
44 
ReadyToLoad()45 bool FreezeDetectorPlugin::ReadyToLoad()
46 {
47     freezeCommon_ = std::make_shared<FreezeCommon>();
48     bool ret1 = freezeCommon_->Init();
49     freezeResolver_ = std::make_unique<FreezeResolver>(freezeCommon_);
50     bool ret2 = freezeResolver_->Init();
51     return ret1 && ret2;
52 }
53 
OnLoad()54 void FreezeDetectorPlugin::OnLoad()
55 {
56     HIVIEW_LOGD("OnLoad.");
57     SetName(FREEZE_DETECTOR_PLUGIN_NAME);
58     SetVersion(FREEZE_DETECTOR_PLUGIN_VERSION);
59 
60     freezeCommon_ = std::make_shared<FreezeCommon>();
61     bool ret = freezeCommon_->Init();
62     if (!ret) {
63         HIVIEW_LOGW("freezeCommon_->Init false.");
64         freezeCommon_ = nullptr;
65         return;
66     }
67     freezeResolver_ = std::make_unique<FreezeResolver>(freezeCommon_);
68     ret = freezeResolver_->Init();
69     if (!ret) {
70         HIVIEW_LOGW("freezeResolver_->Init false.");
71         freezeCommon_ = nullptr;
72         freezeResolver_ = nullptr;
73     }
74 }
75 
OnUnload()76 void FreezeDetectorPlugin::OnUnload()
77 {
78     HIVIEW_LOGD("OnUnload.");
79 }
80 
OnEvent(std::shared_ptr<Event> & event)81 bool FreezeDetectorPlugin::OnEvent(std::shared_ptr<Event> &event)
82 {
83     return false;
84 }
85 
CanProcessEvent(std::shared_ptr<Event> event)86 bool FreezeDetectorPlugin::CanProcessEvent(std::shared_ptr<Event> event)
87 {
88     return false;
89 }
90 
RemoveRedundantNewline(const std::string & content) const91 std::string FreezeDetectorPlugin::RemoveRedundantNewline(const std::string& content) const
92 {
93     std::vector<std::string> lines;
94     StringUtil::SplitStr(content, "\\n", lines, false, false);
95 
96     std::string outContent;
97     for (const auto& line : lines) {
98         outContent.append(line).append("\n");
99     }
100     return outContent;
101 }
102 
MakeWatchPoint(const Event & event)103 WatchPoint FreezeDetectorPlugin::MakeWatchPoint(const Event& event)
104 {
105     Event& eventRef = const_cast<Event&>(event);
106     SysEvent& sysEvent = static_cast<SysEvent&>(eventRef);
107 
108     long seq = sysEvent.GetSeq();
109     long tid = sysEvent.GetTid();
110     long pid = sysEvent.GetEventIntValue(FreezeCommon::EVENT_PID);
111     pid = pid ? pid : sysEvent.GetPid();
112     long uid = sysEvent.GetEventIntValue(FreezeCommon::EVENT_UID);
113     uid = uid ? uid : sysEvent.GetUid();
114     std::string packageName = sysEvent.GetEventValue(FreezeCommon::EVENT_PACKAGE_NAME);
115     std::string processName = sysEvent.GetEventValue(FreezeCommon::EVENT_PROCESS_NAME);
116     std::string hiteaceTime = sysEvent.GetEventValue(FreezeCommon::HIREACE_TIME);
117     std::string sysrqTime = sysEvent.GetEventValue(FreezeCommon::SYSRQ_TIME);
118     std::string terminalThreadStack = sysEvent.GetEventValue(FreezeCommon::TERMINAL_THREAD_STACK);
119     std::string info = sysEvent.GetEventValue(EventStore::EventCol::INFO);
120     std::regex reg("logPath:([^,]+)");
121     std::smatch result;
122     std::string logPath = std::regex_search(info, result, reg) ? result[1].str() : info;
123     std::string foreGround = "";
124     CheckForeGround(uid, pid, event.happenTime_, foreGround);
125     WatchPoint watchPoint = OHOS::HiviewDFX::WatchPoint::Builder()
126         .InitSeq(seq)
127         .InitDomain(event.domain_)
128         .InitStringId(event.eventName_)
129         .InitTimestamp(event.happenTime_)
130         .InitPid(pid)
131         .InitTid(tid)
132         .InitUid(uid)
133         .InitTerminalThreadStack(terminalThreadStack)
134         .InitPackageName(packageName)
135         .InitProcessName(processName)
136         .InitForeGround(foreGround)
137         .InitMsg("")
138         .InitLogPath(logPath)
139         .InitHitraceTime(hiteaceTime)
140         .InitSysrqTime(sysrqTime)
141         .Build();
142     HIVIEW_LOGI("watchpoint domain=%{public}s, stringid=%{public}s, pid=%{public}ld, uid=%{public}ld, seq=%{public}ld,"
143         " packageName=%{public}s, processName=%{public}s, logPath=%{public}s.", event.domain_.c_str(),
144         event.eventName_.c_str(), pid, uid, seq, packageName.c_str(), processName.c_str(), logPath.c_str());
145 
146     return watchPoint;
147 }
148 
CheckForeGround(long uid,long pid,unsigned long long eventTime,std::string & foreGround)149 void FreezeDetectorPlugin::CheckForeGround(long uid, long pid, unsigned long long eventTime, std::string& foreGround)
150 {
151     if (uid < MIN_APP_UID) {
152         return;
153     }
154 
155     UCollectUtil::ProcessState state = UCollectUtil::ProcessStatus::GetInstance().GetProcessState(pid);
156     if (state == UCollectUtil::FOREGROUND) {
157         foreGround = "Yes";
158     }
159     if (state == UCollectUtil::BACKGROUND) {
160         uint64_t lastFgTime = static_cast<uint64_t>(UCollectUtil::ProcessStatus::GetInstance()
161             .GetProcessLastForegroundTime(pid));
162         foreGround = (lastFgTime > eventTime) ? "Yes" : "No";
163     }
164 }
165 
OnEventListeningCallback(const Event & event)166 void FreezeDetectorPlugin::OnEventListeningCallback(const Event& event)
167 {
168     if (event.rawData_ == nullptr) {
169         HIVIEW_LOGE("raw data of event is null.");
170         return;
171     }
172     EventRaw::DecodedEvent decodedEvent(event.rawData_->GetData());
173     if (!decodedEvent.IsValid()) {
174         HIVIEW_LOGE("failed to decode the raw data of event.");
175         return;
176     }
177     HIVIEW_LOGD("received event id=%{public}u, domain=%{public}s, stringid=%{public}s, extraInfo=%{public}s.",
178         event.eventId_, event.domain_.c_str(), event.eventName_.c_str(), decodedEvent.AsJsonStr().c_str());
179     if (freezeCommon_ == nullptr) {
180         return;
181     }
182 
183     if (!freezeCommon_->IsFreezeEvent(event.domain_, event.eventName_)) {
184         HIVIEW_LOGE("not freeze event.");
185         return;
186     }
187 
188     HIVIEW_LOGD("received event domain=%{public}s, stringid=%{public}s",
189         event.domain_.c_str(), event.eventName_.c_str());
190     this->AddUseCount();
191     // dispatcher context, send task to our thread
192     WatchPoint watchPoint = MakeWatchPoint(event);
193     if (watchPoint.GetLogPath().empty()) {
194         HIVIEW_LOGW("log path is empty.");
195         return;
196     }
197 
198     std::shared_ptr<FreezeRuleCluster> freezeRuleCluster = freezeCommon_->GetFreezeRuleCluster();
199     std::vector<FreezeResult> freezeResultList;
200     bool ruleRet = freezeRuleCluster->GetResult(watchPoint, freezeResultList);
201     if (!ruleRet) {
202         HIVIEW_LOGW("get rule failed.");
203         return;
204     }
205     long delayTime = 0;
206     if (freezeResultList.size() > 1) {
207         for (auto& i : freezeResultList) {
208             long window = i.GetWindow();
209             delayTime = std::max(delayTime, window);
210         }
211         if (delayTime == 0) {
212             delayTime = 10; // delay: 10s
213         }
214     }
215     ffrt::submit([this, watchPoint] { this->ProcessEvent(watchPoint); }, {}, {},
216         ffrt::task_attr().name("dfr_fre_detec").qos(ffrt::qos_default)
217         .delay(static_cast<unsigned long long>(delayTime) * TO_NANO_SECOND_MULTPLE));
218 }
219 
ProcessEvent(WatchPoint watchPoint)220 void FreezeDetectorPlugin::ProcessEvent(WatchPoint watchPoint)
221 {
222     HIVIEW_LOGD("received event domain=%{public}s, stringid=%{public}s",
223         watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
224     if (freezeResolver_ == nullptr) {
225         this->SubUseCount();
226         return;
227     }
228 
229     auto ret = freezeResolver_->ProcessEvent(watchPoint);
230     if (ret < 0) {
231         HIVIEW_LOGE("FreezeResolver ProcessEvent filled.");
232     }
233     this->SubUseCount();
234 }
235 } // namespace HiviewDFX
236 } // namespace OHOS
237