• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     const int APPLICATION_RESULT_ID = 0;
26     const int SYSTEM_RESULT_ID = 1;
27     const int SYSTEM_WARNING_RESULT_ID = 2;
28     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 
GetPrincipalStringIds() const97 std::set<std::string> FreezeCommon::GetPrincipalStringIds() const
98 {
99     std::set<std::string> set;
100     if (freezeRuleCluster_ == nullptr) {
101         HIVIEW_LOGW("freezeRuleCluster_ == nullptr.");
102         return set;
103     }
104     auto applicationPairs = freezeRuleCluster_->GetApplicationPairs();
105     auto systemPairs = freezeRuleCluster_->GetSystemPairs();
106     auto sysWarningPairs = freezeRuleCluster_->GetSysWarningPairs();
107     for (auto const &pair : applicationPairs) {
108         if (pair.second.second) {
109             set.insert(pair.first);
110         }
111     }
112     for (auto const &pair : systemPairs) {
113         if (pair.second.second) {
114             set.insert(pair.first);
115         }
116     }
117     for (auto const &pair : sysWarningPairs) {
118         if (pair.second.second) {
119             set.insert(pair.first);
120         }
121     }
122 
123     return set;
124 }
125 
GetFreezeRuleCluster() const126 std::shared_ptr<FreezeRuleCluster> FreezeCommon::GetFreezeRuleCluster() const
127 {
128     return freezeRuleCluster_;
129 }
130 
WriteTimeInfoToFd(int fd,const std::string & msg,bool isStart)131 void FreezeCommon::WriteTimeInfoToFd(int fd, const std::string& msg, bool isStart)
132 {
133     if (isStart) {
134         FileUtil::SaveStringToFd(fd, "\n---------------------------------------------------\n");
135     }
136     uint64_t ms = TimeUtil::GetMilliseconds();
137     std::ostringstream timeStr;
138     timeStr << msg << TimeUtil::TimestampFormatToDate(ms / TimeUtil::SEC_TO_MILLISEC, "%Y/%m/%d-%H:%M:%S")
139             << ":" << std::setw(PLACEHOLDER) << std::setfill('0')
140             << std::to_string(ms % TimeUtil::SEC_TO_MILLISEC) << std::endl;
141     FileUtil::SaveStringToFd(fd, timeStr.str());
142     if (!isStart) {
143         FileUtil::SaveStringToFd(fd, "---------------------------------------------------\n");
144     }
145 }
146 }  // namespace HiviewDFX
147 }  // namespace OHOS