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 "resolver.h"
17
18 #include <sys/time.h>
19
20 #include "file_util.h"
21 #include "hiview_event_report.h"
22 #include "hiview_logger.h"
23 #include "string_util.h"
24 #include "sys_event.h"
25 #include "sys_event_dao.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 static const int DEFAULT_TIME_WINDOW = 30;
31 static const int MINUTES_IN_HOUR = 60;
32 static const int MIN_MATCH_NUM = 2;
33 static const int DEFAULT_HOURS = 10;
34 static const int SYS_MATCH_NUM = 1;
35 }
36
37 DEFINE_LOG_LABEL(0xD002D01, "FreezeDetector");
Init()38 bool FreezeResolver::Init()
39 {
40 if (freezeCommon_ == nullptr) {
41 return false;
42 }
43 freezeRuleCluster_ = freezeCommon_->GetFreezeRuleCluster();
44 if (freezeRuleCluster_ == nullptr) {
45 return false;
46 }
47 dBHelper_ = std::make_unique<DBHelper>(freezeCommon_);
48 vendor_ = std::make_unique<Vendor>(freezeCommon_);
49 return vendor_->Init();
50 }
51
ResolveEvent(const WatchPoint & watchPoint,std::vector<WatchPoint> & list,std::vector<FreezeResult> & result) const52 bool FreezeResolver::ResolveEvent(const WatchPoint& watchPoint,
53 std::vector<WatchPoint>& list, std::vector<FreezeResult>& result) const
54 {
55 if (freezeRuleCluster_ == nullptr || !freezeRuleCluster_->GetResult(watchPoint, result)) {
56 return false;
57 }
58 unsigned long long timestamp = watchPoint.GetTimestamp();
59 long pid = watchPoint.GetPid();
60 long tid = watchPoint.GetTid();
61 std::string packageName = watchPoint.GetPackageName().empty() ?
62 watchPoint.GetProcessName() : watchPoint.GetPackageName();
63 DBHelper::WatchParams params = {pid, tid, timestamp, packageName};
64 for (auto& i : result) {
65 long window = i.GetWindow();
66 if (window == 0) {
67 list.push_back(watchPoint);
68 } else if (dBHelper_ != nullptr) {
69 unsigned long long timeInterval = static_cast<unsigned long long>(std::abs(window) * MILLISECOND);
70 unsigned long long start = window > 0 ? timestamp : timestamp - timeInterval;
71 unsigned long long end = window > 0 ? timestamp + timeInterval : timestamp;
72 unsigned long long delay = static_cast<unsigned long long>(std::max(i.GetDelay(), 0L) * MILLISECOND);
73 end += delay;
74 dBHelper_->SelectEventFromDB(start, end, list, params, i);
75 }
76 }
77
78 HIVIEW_LOGI("list size %{public}zu", list.size());
79 return true;
80 }
81
JudgmentResult(const WatchPoint & watchPoint,const std::vector<WatchPoint> & list,const std::vector<FreezeResult> & result) const82 bool FreezeResolver::JudgmentResult(const WatchPoint& watchPoint,
83 const std::vector<WatchPoint>& list, const std::vector<FreezeResult>& result) const
84 {
85 if (watchPoint.GetDomain() == "ACE" && watchPoint.GetStringId() == "UI_BLOCK_6S") {
86 if (list.size() == result.size()) {
87 HIVIEW_LOGI("ACE UI_BLOCK has UI_BLOCK_3S UI_BLOCK_6S UI_BLOCK_RECOVERED as UI_JANK");
88 return false;
89 }
90
91 if (list.size() != (result.size() - 1)) {
92 return false;
93 }
94
95 for (auto& i : list) {
96 if (i.GetStringId() == "UI_BLOCK_RECOVERED") {
97 return false;
98 }
99 }
100 return true;
101 }
102
103 if (std::any_of(result.begin(), result.end(), [&list](auto& res) {
104 return res.GetAction() == "or";
105 })) {
106 return list.size() >= MIN_MATCH_NUM;
107 }
108
109 if ((watchPoint.GetStringId() == "SERVICE_WARNING" || watchPoint.GetStringId() ==
110 "THREAD_BLOCK_3S") && (list.size() == result.size() - SYS_MATCH_NUM)) {
111 return true;
112 }
113
114 if (list.size() == result.size()) {
115 return true;
116 }
117 return false;
118 }
119
ProcessEvent(const WatchPoint & watchPoint) const120 int FreezeResolver::ProcessEvent(const WatchPoint &watchPoint) const
121 {
122 HIVIEW_LOGI("process event [%{public}s, %{public}s]",
123 watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
124 if (vendor_ == nullptr) {
125 return -1;
126 }
127 std::vector<WatchPoint> list;
128 std::vector<FreezeResult> result;
129 if (!ResolveEvent(watchPoint, list, result)) {
130 HIVIEW_LOGW("no rule for event [%{public}s, %{public}s]",
131 watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
132 return -1;
133 }
134
135 if (!JudgmentResult(watchPoint, list, result)) {
136 HIVIEW_LOGW("no match event for event [%{public}s, %{public}s]",
137 watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
138 return -1;
139 }
140
141 WatchPoint watchPointCopy = watchPoint;
142 vendor_->MergeEventLog(watchPointCopy, list, result);
143 return 0;
144 }
145
GetTimeZone() const146 std::string FreezeResolver::GetTimeZone() const
147 {
148 std::string timeZone = "";
149 struct timeval tv;
150 struct timezone tz;
151 if (gettimeofday(&tv, &tz) != 0) {
152 HIVIEW_LOGE("failed to gettimeofday");
153 return timeZone;
154 }
155
156 int hour = (-tz.tz_minuteswest) / MINUTES_IN_HOUR;
157 timeZone = (hour >= 0) ? "+" : "-";
158
159 int absHour = std::abs(hour);
160 if (absHour < DEFAULT_HOURS) {
161 timeZone.append("0");
162 }
163 timeZone.append(std::to_string(absHour));
164
165 int minute = (-tz.tz_minuteswest) % MINUTES_IN_HOUR;
166 int absMinute = std::abs(minute);
167 if (absMinute < DEFAULT_HOURS) {
168 timeZone.append("0");
169 }
170 timeZone.append(std::to_string(absMinute));
171
172 return timeZone;
173 }
174 } // namespace HiviewDFX
175 } // namespace OHOS
176