• 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 "db_helper.h"
21 #include "file_util.h"
22 #include "logger.h"
23 #include "plugin.h"
24 #include "string_util.h"
25 #include "sys_event.h"
26 #include "sys_event_dao.h"
27 #include "vendor.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 DEFINE_LOG_TAG("FreezeDetector");
32 
FreezeResolver()33 FreezeResolver::FreezeResolver() : startTime_(time(nullptr) * MILLISECOND)
34 {
35 }
36 
~FreezeResolver()37 FreezeResolver::~FreezeResolver()
38 {
39 }
40 
Init()41 bool FreezeResolver::Init()
42 {
43     // freeze_rules.xml
44     if (FreezeRuleCluster::GetInstance().Init() == false) {
45         HIVIEW_LOGE("failed to init rule.");
46         return false;
47     }
48 
49     return true;
50 }
51 
ResolveEvent(WatchPoint & watchPoint,WatchPoint & matchedWatchPoint,std::list<WatchPoint> & list,FreezeResult & result) const52 bool FreezeResolver::ResolveEvent(WatchPoint& watchPoint, WatchPoint& matchedWatchPoint,
53     std::list<WatchPoint>& list, FreezeResult& result) const
54 {
55     unsigned long window = 0;
56     if (FreezeRuleCluster::GetInstance().GetTimeWindow(watchPoint, window) == false) {
57         return false;
58     }
59 
60     if (Vendor::GetInstance().IsApplicationEvent(watchPoint.GetDomain(), watchPoint.GetStringId())) {
61         if (window > DEFAULT_TIME_WINDOW) {
62             window = DEFAULT_TIME_WINDOW;
63         }
64     }
65 
66     unsigned long long start = watchPoint.GetTimestamp() - (window * MILLISECOND);
67     unsigned long long end = watchPoint.GetTimestamp();
68     if (window == 0) {
69         list.push_back(watchPoint);
70     } else {
71         DBHelper::SelectEventFromDB(false, start, end, list);
72     }
73 
74     HIVIEW_LOGI("list size %{public}zu", list.size());
75     return FreezeRuleCluster::GetInstance().GetResult(watchPoint, matchedWatchPoint, list, result);
76 }
77 
ProcessEvent(WatchPoint & watchPoint) const78 std::shared_ptr<PipelineEvent> FreezeResolver::ProcessEvent(WatchPoint &watchPoint) const
79 {
80     HIVIEW_LOGI("process event [%{public}s, %{public}s]",
81         watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
82 
83     FreezeResult result;
84     std::list<WatchPoint> list;
85     WatchPoint matchedWatchPoint;
86     if (ResolveEvent(watchPoint, matchedWatchPoint, list, result) == false) {
87         HIVIEW_LOGI("no rule for event [%{public}s, %{public}s]",
88             watchPoint.GetDomain().c_str(), watchPoint.GetStringId().c_str());
89         return nullptr;
90     }
91 
92     if (watchPoint.GetDomain() == matchedWatchPoint.GetDomain() &&
93         watchPoint.GetStringId() == matchedWatchPoint.GetStringId()) {
94         // self rule with non-zero time window
95         if (list.size() > 1) {
96             std::list<WatchPoint>::iterator it;
97             for (it = list.begin(); it != list.end();) {
98                 if (it->GetDomain() == watchPoint.GetDomain() && it->GetStringId() == watchPoint.GetStringId()) {
99                     it++;
100                 } else {
101                     it = list.erase(it);
102                 }
103             }
104         }
105         // self rule with zero time window
106     }
107     else {
108         list.clear();
109         list.push_back(matchedWatchPoint);
110         list.push_back(watchPoint);
111     }
112 
113     std::string digest;
114     std::string logPath = Vendor::GetInstance().MergeEventLog(watchPoint, list, result, digest);
115 
116     return Vendor::GetInstance().MakeEvent(watchPoint, matchedWatchPoint, list, result, logPath, digest);
117 }
118 
GetTimeZone() const119 std::string FreezeResolver::GetTimeZone() const
120 {
121     std::string timeZone = "";
122     struct timeval tv;
123     struct timezone tz;
124     if (gettimeofday(&tv, &tz) != 0) {
125         HIVIEW_LOGE("failed to gettimeofday");
126         return timeZone;
127     }
128 
129     int hour = (-tz.tz_minuteswest) / MINUTES_IN_HOUR;
130     timeZone = (hour >= 0) ? "+" : "-";
131 
132     int absHour = std::abs(hour);
133     if (absHour < 10) {
134         timeZone.append("0");
135     }
136     timeZone.append(std::to_string(absHour));
137 
138     int minute = (-tz.tz_minuteswest) % MINUTES_IN_HOUR;
139     int absMinute = std::abs(minute);
140     if (absMinute < 10) {
141         timeZone.append("0");
142     }
143     timeZone.append(std::to_string(absMinute));
144 
145     return timeZone;
146 }
147 } // namespace HiviewDFX
148 } // namespace OHOS
149