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 #include <iomanip>
16
17 #include "freeze_common.h"
18
19 #include "hiview_logger.h"
20 #include "file_util.h"
21 #include "time_util.h"
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace {
25 static const int APPLICATION_RESULT_ID = 0;
26 static const int SYSTEM_RESULT_ID = 1;
27 static const int SYSTEM_WARNING_RESULT_ID = 2;
28 static const uint32_t PLACEHOLDER = 3;
29 }
30 DEFINE_LOG_LABEL(0xD002D01, "FreezeDetector");
FreezeCommon()31 FreezeCommon::FreezeCommon()
32 {
33 freezeRuleCluster_ = nullptr;
34 }
35
~FreezeCommon()36 FreezeCommon::~FreezeCommon()
37 {
38 freezeRuleCluster_ = nullptr;
39 }
40
Init()41 bool FreezeCommon::Init()
42 {
43 freezeRuleCluster_ = std::make_shared<FreezeRuleCluster>();
44 return freezeRuleCluster_->Init();
45 }
46
IsFreezeEvent(const std::string & domain,const std::string & stringId) const47 bool FreezeCommon::IsFreezeEvent(const std::string& domain, const std::string& stringId) const
48 {
49 return IsApplicationEvent(domain, stringId) || IsSystemEvent(domain, stringId) ||
50 IsSysWarningEvent(domain, stringId);
51 }
52
IsApplicationEvent(const std::string & domain,const std::string & stringId) const53 bool FreezeCommon::IsApplicationEvent(const std::string& domain, const std::string& stringId) const
54 {
55 return IsAssignedEvent(domain, stringId, APPLICATION_RESULT_ID);
56 }
57
IsSystemEvent(const std::string & domain,const std::string & stringId) const58 bool FreezeCommon::IsSystemEvent(const std::string& domain, const std::string& stringId) const
59 {
60 return IsAssignedEvent(domain, stringId, SYSTEM_RESULT_ID);
61 }
62
IsSysWarningEvent(const std::string & domain,const std::string & stringId) const63 bool FreezeCommon::IsSysWarningEvent(const std::string& domain, const std::string& stringId) const
64 {
65 return IsAssignedEvent(domain, stringId, SYSTEM_WARNING_RESULT_ID);
66 }
67
IsAssignedEvent(const std::string & domain,const std::string & stringId,int freezeId) const68 bool FreezeCommon::IsAssignedEvent(const std::string& domain, const std::string& stringId, int freezeId) const
69 {
70 if (freezeRuleCluster_ == nullptr) {
71 HIVIEW_LOGW("freezeRuleCluster_ == nullptr.");
72 return false;
73 }
74
75 std::map<std::string, std::pair<std::string, bool>> pairs;
76 switch (freezeId) {
77 case APPLICATION_RESULT_ID:
78 pairs = freezeRuleCluster_->GetApplicationPairs();
79 break;
80 case SYSTEM_RESULT_ID:
81 pairs = freezeRuleCluster_->GetSystemPairs();
82 break;
83 case SYSTEM_WARNING_RESULT_ID:
84 pairs = freezeRuleCluster_->GetSysWarningPairs();
85 break;
86 default:
87 return false;
88 }
89 for (auto const &pair : pairs) {
90 if (stringId == pair.first && domain == pair.second.first) {
91 return true;
92 }
93 }
94 return false;
95 }
96
IsSystemResult(const FreezeResult & result) const97 bool FreezeCommon::IsSystemResult(const FreezeResult& result) const
98 {
99 return result.GetId() == SYSTEM_RESULT_ID;
100 }
101
IsApplicationResult(const FreezeResult & result) const102 bool FreezeCommon::IsApplicationResult(const FreezeResult& result) const
103 {
104 return result.GetId() == APPLICATION_RESULT_ID;
105 }
106
IsSysWarningResult(const FreezeResult & result) const107 bool FreezeCommon::IsSysWarningResult(const FreezeResult& result) const
108 {
109 return result.GetId() == SYSTEM_WARNING_RESULT_ID;
110 }
111
IsBetaVersion() const112 bool FreezeCommon::IsBetaVersion() const
113 {
114 return true;
115 }
116
GetPrincipalStringIds() const117 std::set<std::string> FreezeCommon::GetPrincipalStringIds() const
118 {
119 std::set<std::string> set;
120 if (freezeRuleCluster_ == nullptr) {
121 HIVIEW_LOGW("freezeRuleCluster_ == nullptr.");
122 return set;
123 }
124 auto applicationPairs = freezeRuleCluster_->GetApplicationPairs();
125 auto systemPairs = freezeRuleCluster_->GetSystemPairs();
126 auto sysWarningPairs = freezeRuleCluster_->GetSysWarningPairs();
127 for (auto const &pair : applicationPairs) {
128 if (pair.second.second) {
129 set.insert(pair.first);
130 }
131 }
132 for (auto const &pair : systemPairs) {
133 if (pair.second.second) {
134 set.insert(pair.first);
135 }
136 }
137 for (auto const &pair : sysWarningPairs) {
138 if (pair.second.second) {
139 set.insert(pair.first);
140 }
141 }
142
143 return set;
144 }
145
GetFreezeRuleCluster() const146 std::shared_ptr<FreezeRuleCluster> FreezeCommon::GetFreezeRuleCluster() const
147 {
148 return freezeRuleCluster_;
149 }
150
WriteStartInfoToFd(int fd,const std::string & msg)151 void FreezeCommon::WriteStartInfoToFd(int fd, const std::string& msg)
152 {
153 FileUtil::SaveStringToFd(fd, "\n---------------------------------------------------\n");
154 auto start = TimeUtil::GetMilliseconds();
155 uint64_t startTime = start / TimeUtil::SEC_TO_MILLISEC;
156 std::ostringstream startTimeStr;
157 startTimeStr << msg << TimeUtil::TimestampFormatToDate(startTime, "%Y/%m/%d-%H:%M:%S");
158 startTimeStr << ":" << std::setw(PLACEHOLDER) << std::setfill('0') <<
159 std::to_string(start % TimeUtil::SEC_TO_MILLISEC) << std::endl;
160 FileUtil::SaveStringToFd(fd, startTimeStr.str());
161 }
162
WriteEndInfoToFd(int fd,const std::string & msg)163 void FreezeCommon::WriteEndInfoToFd(int fd, const std::string& msg)
164 {
165 auto start = TimeUtil::GetMilliseconds();
166 uint64_t startTime = start / TimeUtil::SEC_TO_MILLISEC;
167 std::ostringstream startTimeStr;
168 startTimeStr << msg << TimeUtil::TimestampFormatToDate(startTime, "%Y/%m/%d-%H:%M:%S");
169 startTimeStr << ":" << std::setw(PLACEHOLDER) << std::setfill('0') <<
170 std::to_string(start % TimeUtil::SEC_TO_MILLISEC) << std::endl;
171 FileUtil::SaveStringToFd(fd, startTimeStr.str());
172 FileUtil::SaveStringToFd(fd, "---------------------------------------------------\n");
173 }
174 } // namespace HiviewDFX
175 } // namespace OHOS