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