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