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 DEFINE_LOG_LABEL(0xD002D01, "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
ResolveEvent(const WatchPoint & watchPoint,std::vector<WatchPoint> & list,std::vector<FreezeResult> & result) const44 bool FreezeResolver::ResolveEvent(const WatchPoint& watchPoint,
45 std::vector<WatchPoint>& list, std::vector<FreezeResult>& result) const
46 {
47 if (freezeRuleCluster_ == nullptr) {
48 return false;
49 }
50 if (!freezeRuleCluster_->GetResult(watchPoint, result)) {
51 return false;
52 }
53 unsigned long long timestamp = watchPoint.GetTimestamp();
54 std::string packageName = watchPoint.GetPackageName().empty() ?
55 watchPoint.GetProcessName() : watchPoint.GetPackageName();
56 for (auto& i : result) {
57 long window = i.GetWindow();
58 if (window == 0) {
59 list.push_back(watchPoint);
60 } else if (dBHelper_ != nullptr) {
61 unsigned long long timeInterval = static_cast<unsigned long long>(std::abs(window) * MILLISECOND);
62 unsigned long long start = window > 0 ? timestamp : timestamp - timeInterval;
63 unsigned long long end = window > 0 ? timestamp + timeInterval : timestamp;
64 dBHelper_->SelectEventFromDB(start, end, list, packageName, i);
65 }
66 }
67
68 HIVIEW_LOGI("list size %{public}zu", list.size());
69 return true;
70 }
71
JudgmentResult(const WatchPoint & watchPoint,const std::vector<WatchPoint> & list,const std::vector<FreezeResult> & result) const72 bool FreezeResolver::JudgmentResult(const WatchPoint& watchPoint,
73 const std::vector<WatchPoint>& list, const std::vector<FreezeResult>& result) const
74 {
75 if (watchPoint.GetDomain() == "ACE" && watchPoint.GetStringId() == "UI_BLOCK_6S") {
76 if (list.size() == result.size()) {
77 HIVIEW_LOGI("ACE UI_BLOCK has UI_BLOCK_3S UI_BLOCK_6S UI_BLOCK_RECOVERED as UI_JANK");
78 return false;
79 }
80
81 if (list.size() != (result.size() - 1)) {
82 return false;
83 }
84
85 for (auto& i : list) {
86 if (i.GetStringId() == "UI_BLOCK_RECOVERED") {
87 return false;
88 }
89 }
90 return true;
91 }
92
93 if (std::any_of(result.begin(), result.end(), [&list](auto& res) {
94 return res.GetAction() == "or";
95 })) {
96 return list.size() >= minMatchNum;
97 }
98
99 if (list.size() == result.size()) {
100 return true;
101 }
102 return false;
103 }
104
ProcessEvent(const WatchPoint & watchPoint) const105 int FreezeResolver::ProcessEvent(const WatchPoint &watchPoint) const
106 {
107 HIVIEW_LOGI("process event [%{public}s, %{public}s]",
108 watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
109 if (vendor_ == nullptr) {
110 return -1;
111 }
112 std::vector<WatchPoint> list;
113 std::vector<FreezeResult> result;
114 if (!ResolveEvent(watchPoint, list, result)) {
115 HIVIEW_LOGW("no rule for event [%{public}s, %{public}s]",
116 watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
117 return -1;
118 }
119
120 if (!JudgmentResult(watchPoint, list, result)) {
121 HIVIEW_LOGW("no match event for event [%{public}s, %{public}s]",
122 watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
123 return -1;
124 }
125
126 vendor_->MergeEventLog(watchPoint, list, result);
127 return 0;
128 }
129
GetTimeZone() const130 std::string FreezeResolver::GetTimeZone() const
131 {
132 std::string timeZone = "";
133 struct timeval tv;
134 struct timezone tz;
135 if (gettimeofday(&tv, &tz) != 0) {
136 HIVIEW_LOGE("failed to gettimeofday");
137 return timeZone;
138 }
139
140 int hour = (-tz.tz_minuteswest) / MINUTES_IN_HOUR;
141 timeZone = (hour >= 0) ? "+" : "-";
142
143 int absHour = std::abs(hour);
144 if (absHour < defaultHours) {
145 timeZone.append("0");
146 }
147 timeZone.append(std::to_string(absHour));
148
149 int minute = (-tz.tz_minuteswest) % MINUTES_IN_HOUR;
150 int absMinute = std::abs(minute);
151 if (absMinute < defaultHours) {
152 timeZone.append("0");
153 }
154 timeZone.append(std::to_string(absMinute));
155
156 return timeZone;
157 }
158 } // namespace HiviewDFX
159 } // namespace OHOS
160