1 /*
2 * Copyright (c) 2022 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 "freeze_common.h"
17
18 #include "hiview_logger.h"
19 #include "file_util.h"
20 #include "time_util.h"
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace {
24 static const int SYSTEM_WARNING_RESULT_ID = 2;
25 }
26 DEFINE_LOG_LABEL(0xD002D01, "FreezeDetector");
FreezeCommon()27 FreezeCommon::FreezeCommon()
28 {
29 freezeRuleCluster_ = nullptr;
30 }
31
~FreezeCommon()32 FreezeCommon::~FreezeCommon()
33 {
34 freezeRuleCluster_ = nullptr;
35 }
36
Init()37 bool FreezeCommon::Init()
38 {
39 freezeRuleCluster_ = std::make_shared<FreezeRuleCluster>();
40 return freezeRuleCluster_->Init();
41 }
42
IsFreezeEvent(const std::string & domain,const std::string & stringId) const43 bool FreezeCommon::IsFreezeEvent(const std::string& domain, const std::string& stringId) const
44 {
45 return IsApplicationEvent(domain, stringId) || IsSystemEvent(domain, stringId) ||
46 IsSysWarningEvent(domain, stringId);
47 }
48
IsApplicationEvent(const std::string & domain,const std::string & stringId) const49 bool FreezeCommon::IsApplicationEvent(const std::string& domain, const std::string& stringId) const
50 {
51 return IsAssignedEvent(domain, stringId, APPLICATION_RESULT_ID);
52 }
53
IsSystemEvent(const std::string & domain,const std::string & stringId) const54 bool FreezeCommon::IsSystemEvent(const std::string& domain, const std::string& stringId) const
55 {
56 return IsAssignedEvent(domain, stringId, SYSTEM_RESULT_ID);
57 }
58
IsSysWarningEvent(const std::string & domain,const std::string & stringId) const59 bool FreezeCommon::IsSysWarningEvent(const std::string& domain, const std::string& stringId) const
60 {
61 return IsAssignedEvent(domain, stringId, SYSTEM_WARNING_RESULT_ID);
62 }
63
IsAssignedEvent(const std::string & domain,const std::string & stringId,int freezeId) const64 bool FreezeCommon::IsAssignedEvent(const std::string& domain, const std::string& stringId, int freezeId) const
65 {
66 if (freezeRuleCluster_ == nullptr) {
67 HIVIEW_LOGW("freezeRuleCluster_ == nullptr.");
68 return false;
69 }
70
71 std::map<std::string, std::pair<std::string, bool>> pairs;
72 switch (freezeId) {
73 case APPLICATION_RESULT_ID:
74 pairs = freezeRuleCluster_->GetApplicationPairs();
75 break;
76 case SYSTEM_RESULT_ID:
77 pairs = freezeRuleCluster_->GetSystemPairs();
78 break;
79 case SYSTEM_WARNING_RESULT_ID:
80 pairs = freezeRuleCluster_->GetSysWarningPairs();
81 break;
82 default:
83 return false;
84 }
85 for (auto const &pair : pairs) {
86 if (stringId == pair.first && domain == pair.second.first) {
87 return true;
88 }
89 }
90 return false;
91 }
92
IsSystemResult(const FreezeResult & result) const93 bool FreezeCommon::IsSystemResult(const FreezeResult& result) const
94 {
95 return result.GetId() == SYSTEM_RESULT_ID;
96 }
97
IsApplicationResult(const FreezeResult & result) const98 bool FreezeCommon::IsApplicationResult(const FreezeResult& result) const
99 {
100 return result.GetId() == APPLICATION_RESULT_ID;
101 }
102
IsSysWarningResult(const FreezeResult & result) const103 bool FreezeCommon::IsSysWarningResult(const FreezeResult& result) const
104 {
105 return result.GetId() == SYSTEM_WARNING_RESULT_ID;
106 }
107
IsBetaVersion() const108 bool FreezeCommon::IsBetaVersion() const
109 {
110 return true;
111 }
112
GetPrincipalStringIds() const113 std::set<std::string> FreezeCommon::GetPrincipalStringIds() const
114 {
115 std::set<std::string> set;
116 if (freezeRuleCluster_ == nullptr) {
117 HIVIEW_LOGW("freezeRuleCluster_ == nullptr.");
118 return set;
119 }
120 auto applicationPairs = freezeRuleCluster_->GetApplicationPairs();
121 auto systemPairs = freezeRuleCluster_->GetSystemPairs();
122 auto sysWarningPairs = freezeRuleCluster_->GetSysWarningPairs();
123 for (auto const &pair : applicationPairs) {
124 if (pair.second.second) {
125 set.insert(pair.first);
126 }
127 }
128 for (auto const &pair : systemPairs) {
129 if (pair.second.second) {
130 set.insert(pair.first);
131 }
132 }
133 for (auto const &pair : sysWarningPairs) {
134 if (pair.second.second) {
135 set.insert(pair.first);
136 }
137 }
138
139 return set;
140 }
141
WriteStartInfoToFd(int fd,const std::string & msg)142 void FreezeCommon::WriteStartInfoToFd(int fd, const std::string& msg)
143 {
144 uint64_t logTime = TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC;
145 std::string formatTime = TimeUtil::TimestampFormatToDate(logTime, "%Y/%m/%d-%H:%M:%S");
146 FileUtil::SaveStringToFd(fd, "\n---------------------------------------------------\n");
147 std::string description = msg + formatTime + "\n";
148 FileUtil::SaveStringToFd(fd, description);
149 }
150
WriteEndInfoToFd(int fd,const std::string & msg)151 void FreezeCommon::WriteEndInfoToFd(int fd, const std::string& msg)
152 {
153 uint64_t logTime = TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC;
154 std::string formatTime = TimeUtil::TimestampFormatToDate(logTime, "%Y/%m/%d-%H:%M:%S");
155 std::string description = msg + formatTime + "\n";
156 FileUtil::SaveStringToFd(fd, description);
157 FileUtil::SaveStringToFd(fd, "---------------------------------------------------\n");
158 }
159 } // namespace HiviewDFX
160 } // namespace OHOS