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 "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 DEFINE_LOG_TAG("FreezeDetector");
Init()30 bool FreezeResolver::Init()
31 {
32 if (freezeCommon_ == nullptr) {
33 return false;
34 }
35 freezeRuleCluster_ = freezeCommon_->GetFreezeRuleCluster();
36 if (freezeRuleCluster_ == nullptr) {
37 return false;
38 }
39 dBHelper_ = std::make_unique<DBHelper>(freezeCommon_);
40 vendor_ = std::make_unique<Vendor>(freezeCommon_);
41 return vendor_->Init();
42 }
43
MatchEvent(const WatchPoint & watchPoint,const std::list<WatchPoint> & wpList,std::vector<WatchPoint> & list,const FreezeResult & result) const44 void FreezeResolver::MatchEvent(const WatchPoint& watchPoint,
45 const std::list<WatchPoint>& wpList, std::vector<WatchPoint>& list, const FreezeResult& result) const
46 {
47 std::string domain = watchPoint.GetDomain();
48 std::string stringId = watchPoint.GetStringId();
49 std::string package = watchPoint.GetPackageName();
50 for (auto &item : wpList) {
51 if ((result.GetDomain() == item.GetDomain()) && (result.GetStringId() == item.GetStringId())) {
52 if (result.GetSamePackage() == "true" && package != item.GetPackageName()) {
53 HIVIEW_LOGE("failed to match the same package,"
54 " domain:%{public}s stringid:%{public}s pacakgeName:%{public}s"
55 " and domain:%{public}s stringid:%{public}s pacakgeName:%{public}s.",
56 domain.c_str(), stringId.c_str(), package.c_str(),
57 item.GetDomain().c_str(), item.GetStringId().c_str(), item.GetPackageName().c_str());
58 continue;
59 }
60 list.push_back(item); // take watchpoint back
61 break;
62 }
63 }
64 }
65
ResolveEvent(const WatchPoint & watchPoint,std::vector<WatchPoint> & list,std::vector<FreezeResult> & result) const66 bool FreezeResolver::ResolveEvent(const WatchPoint& watchPoint,
67 std::vector<WatchPoint>& list, std::vector<FreezeResult>& result) const
68 {
69 if (freezeRuleCluster_ == nullptr) {
70 return false;
71 }
72 if (!freezeRuleCluster_->GetResult(watchPoint, result)) {
73 return false;
74 }
75 unsigned long long timestamp = watchPoint.GetTimestamp();
76 for (auto& i : result) {
77 int window = i.GetWindow();
78 std::list<WatchPoint> wpList;
79 if (window == 0) {
80 list.push_back(watchPoint);
81 } else if (window > 0) {
82 unsigned long long start = timestamp;
83 unsigned long long end = timestamp + (window * MILLISECOND);
84 if (dBHelper_ != nullptr) {
85 dBHelper_->SelectEventFromDB(false, start, end, wpList);
86 }
87 } else {
88 unsigned long long start = timestamp + (window * MILLISECOND);
89 unsigned long long end = timestamp;
90 if (dBHelper_ != nullptr) {
91 dBHelper_->SelectEventFromDB(false, start, end, wpList);
92 }
93 }
94 MatchEvent(watchPoint, wpList, list, i);
95 }
96
97 HIVIEW_LOGI("list size %{public}zu", list.size());
98 return true;
99 }
100
JudgmentResult(const WatchPoint & watchPoint,const std::vector<WatchPoint> & list,const std::vector<FreezeResult> & result) const101 bool FreezeResolver::JudgmentResult(const WatchPoint& watchPoint,
102 const std::vector<WatchPoint>& list, const std::vector<FreezeResult>& result) const
103 {
104 if (watchPoint.GetDomain() == "ACE" && watchPoint.GetStringId() == "UI_BLOCK_6S") {
105 if (list.size() == result.size()) {
106 HIVIEW_LOGI("ACE UI_BLOCK has UI_BLOCK_3S UI_BLOCK_6S UI_BLOCK_RECOVERED as UI_JANK");
107 return false;
108 }
109
110 if (list.size() != (result.size() - 1)) {
111 return false;
112 }
113
114 for (auto& i : list) {
115 if (i.GetStringId() == "UI_BLOCK_RECOVERED") {
116 return false;
117 }
118 }
119 return true;
120 }
121
122 if (list.size() == result.size()) {
123 return true;
124 }
125 return false;
126 }
127
ProcessEvent(const WatchPoint & watchPoint) const128 int FreezeResolver::ProcessEvent(const WatchPoint &watchPoint) const
129 {
130 HIVIEW_LOGI("process event [%{public}s, %{public}s]",
131 watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
132 if (vendor_ == nullptr) {
133 return -1;
134 }
135 std::vector<WatchPoint> list;
136 std::vector<FreezeResult> result;
137 if (!ResolveEvent(watchPoint, list, result)) {
138 HIVIEW_LOGW("no rule for event [%{public}s, %{public}s]",
139 watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
140 return -1;
141 }
142
143 if (!JudgmentResult(watchPoint, list, result)) {
144 HIVIEW_LOGW("no match event for event [%{public}s, %{public}s]",
145 watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
146 return -1;
147 }
148
149 std::string logPath = vendor_->MergeEventLog(watchPoint, list, result);
150
151 for (auto node : list) {
152 if (dBHelper_ != nullptr) {
153 dBHelper_->UpdateEventIntoDB(node, result[0].GetId());
154 }
155 }
156 return 0;
157 }
158
GetTimeZone() const159 std::string FreezeResolver::GetTimeZone() const
160 {
161 std::string timeZone = "";
162 struct timeval tv;
163 struct timezone tz;
164 if (gettimeofday(&tv, &tz) != 0) {
165 HIVIEW_LOGE("failed to gettimeofday");
166 return timeZone;
167 }
168
169 int hour = (-tz.tz_minuteswest) / MINUTES_IN_HOUR;
170 timeZone = (hour >= 0) ? "+" : "-";
171
172 int absHour = std::abs(hour);
173 if (absHour < 10) {
174 timeZone.append("0");
175 }
176 timeZone.append(std::to_string(absHour));
177
178 int minute = (-tz.tz_minuteswest) % MINUTES_IN_HOUR;
179 int absMinute = std::abs(minute);
180 if (absMinute < 10) {
181 timeZone.append("0");
182 }
183 timeZone.append(std::to_string(absMinute));
184
185 return timeZone;
186 }
187 } // namespace HiviewDFX
188 } // namespace OHOS
189