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