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