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 "anr_manager.h"
17
18 #include "dfx_hisysevent.h"
19 #include "i_input_windows_manager.h"
20 #include "timer_manager.h"
21
22 #undef MMI_LOG_DOMAIN
23 #define MMI_LOG_DOMAIN MMI_LOG_ANRDETECT
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "ANRManager"
26
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 const char* FOUNDATION { "foundation" };
31 constexpr int32_t MAX_TIMER_COUNT { 50 };
32 constexpr int32_t TIME_CONVERT_RATIO { 1000 };
33 } // namespace
34
ANRManager()35 ANRManager::ANRManager() {}
~ANRManager()36 ANRManager::~ANRManager() {}
37
Init(UDSServer & udsServer)38 void ANRManager::Init(UDSServer &udsServer)
39 {
40 CALL_DEBUG_ENTER;
41 udsServer_ = &udsServer;
42 CHKPV(udsServer_);
43 udsServer_->AddSessionDeletedCallback([this] (SessionPtr session) {
44 return this->OnSessionLost(session);
45 }
46 );
47 }
48
MarkProcessed(int32_t pid,int32_t eventType,int32_t eventId)49 int32_t ANRManager::MarkProcessed(int32_t pid, int32_t eventType, int32_t eventId)
50 {
51 CALL_DEBUG_ENTER;
52 MMI_HILOGD("pid:%{public}d, eventType:%{public}d, eventId:%{public}d", pid, eventType, eventId);
53 SessionPtr sess = udsServer_->GetSessionByPid(pid);
54 if (sess == nullptr) {
55 if (pid_ != pid) {
56 pid_ = pid;
57 MMI_HILOGE("The sess is null, return value is %{public}d", RET_ERR);
58 }
59 return RET_ERR;
60 }
61 std::list<int32_t> timerIds = sess->DelEvents(eventType, eventId);
62 for (int32_t item : timerIds) {
63 if (item != -1) {
64 TimerMgr->RemoveTimer(item);
65 anrTimerCount_--;
66 MMI_HILOGD("Remove anr timer, anr type:%{public}d, eventId:%{public}d, timer id:%{public}d,"
67 "count:%{public}d", eventType, eventId, item, anrTimerCount_);
68 }
69 }
70 return RET_OK;
71 }
72
RemoveTimers(SessionPtr sess)73 void ANRManager::RemoveTimers(SessionPtr sess)
74 {
75 CHKPV(sess);
76 std::vector<int32_t> DispatchTimerIds = sess->GetTimerIds(ANR_DISPATCH);
77 for (int32_t item : DispatchTimerIds) {
78 if (item != -1) {
79 TimerMgr->RemoveTimer(item);
80 anrTimerCount_--;
81 }
82 }
83 std::vector<int32_t> MonitorTimerIds = sess->GetTimerIds(ANR_MONITOR);
84 for (int32_t item : MonitorTimerIds) {
85 if (item != -1) {
86 TimerMgr->RemoveTimer(item);
87 anrTimerCount_--;
88 }
89 }
90 }
91
RemoveTimersByType(SessionPtr sess,int32_t type)92 void ANRManager::RemoveTimersByType(SessionPtr sess, int32_t type)
93 {
94 CHKPV(sess);
95 if (type != ANR_DISPATCH && type != ANR_MONITOR) {
96 MMI_HILOGE("Remove times failed, your input parm is %{public}d, which is not legal", type);
97 return;
98 }
99 std::vector<int32_t> timerIds = sess->GetTimerIds(ANR_MONITOR);
100 for (int32_t item : timerIds) {
101 if (item != -1) {
102 TimerMgr->RemoveTimer(item);
103 anrTimerCount_--;
104 }
105 }
106 }
107
AddTimer(int32_t type,int32_t id,int64_t currentTime,SessionPtr sess)108 void ANRManager::AddTimer(int32_t type, int32_t id, int64_t currentTime, SessionPtr sess)
109 {
110 CHKPV(sess);
111 if (sess->GetTokenType() != TokenType::TOKEN_HAP || sess->GetProgramName() == FOUNDATION) {
112 MMI_HILOGD("Not application event, skip. pid:%{public}d, anr type:%{public}d", sess->GetPid(), type);
113 return;
114 }
115 if (anrTimerCount_ >= MAX_TIMER_COUNT) {
116 MMI_HILOGD("Add timer failed, timer count reached the maximum number:%{public}d", MAX_TIMER_COUNT);
117 return;
118 }
119 int32_t timerId = TimerMgr->AddTimer(INPUT_UI_TIMEOUT_TIME / TIME_CONVERT_RATIO, 1, [this, id, type, sess]() {
120 CHKPV(sess);
121 if (type == ANR_MONITOR || WIN_MGR->IsWindowVisible(sess->GetPid())) {
122 sess->SetAnrStatus(type, true);
123 DfxHisysevent::ApplicationBlockInput(sess);
124 MMI_HILOG_FREEZEE("Application not responding. pid:%{public}d, anr type:%{public}d, eventId:%{public}d",
125 sess->GetPid(), type, id);
126 CHK_INVALID_RV(anrNoticedPid_, "Add anr timer failed, timer count reached the maximum number");
127 NetPacket pkt(MmiMessageId::NOTICE_ANR);
128 pkt << sess->GetPid();
129 pkt << id;
130 if (pkt.ChkRWError()) {
131 MMI_HILOGE("Packet write failed");
132 return;
133 }
134 auto fd = udsServer_->GetClientFd(anrNoticedPid_);
135 if (!udsServer_->SendMsg(fd, pkt)) {
136 MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
137 return;
138 }
139 }
140 std::vector<int32_t> timerIds = sess->GetTimerIds(type);
141 for (int32_t item : timerIds) {
142 if (item != -1) {
143 TimerMgr->RemoveTimer(item);
144 anrTimerCount_--;
145 MMI_HILOGD("Clear anr timer, type:%{public}d, timer id:%{public}d, count:%{public}d",
146 type, item, anrTimerCount_);
147 }
148 }
149 });
150 CHK_INVALID_RV(timerId, "Add anr timer failed, timer count reached the maximum number");
151 anrTimerCount_++;
152 MMI_HILOGD("Add anr timer success, anr type:%{public}d, eventId:%{public}d, timer id:%{public}d, count:%{public}d",
153 type, id, timerId, anrTimerCount_);
154 sess->SaveANREvent(type, id, currentTime, timerId);
155 }
156
TriggerANR(int32_t type,int64_t time,SessionPtr sess)157 bool ANRManager::TriggerANR(int32_t type, int64_t time, SessionPtr sess)
158 {
159 CALL_DEBUG_ENTER;
160 CHKPF(udsServer_);
161 CHKPF(sess);
162 if (sess->GetTokenType() != TokenType::TOKEN_HAP || sess->GetProgramName() == FOUNDATION) {
163 MMI_HILOGD("Not application event, skip. pid:%{public}d, anr type:%{public}d", sess->GetPid(), type);
164 return false;
165 }
166 if (sess->CheckAnrStatus(type)) {
167 MMI_HILOGD("Application not responding. pid:%{public}d, anr type:%{public}d", sess->GetPid(), type);
168 return true;
169 }
170 MMI_HILOGD("Event dispatch normal");
171 return false;
172 }
173
OnSessionLost(SessionPtr session)174 void ANRManager::OnSessionLost(SessionPtr session)
175 {
176 CALL_DEBUG_ENTER;
177 CHKPV(session);
178 if (anrNoticedPid_ == session->GetPid()) {
179 MMI_HILOGI("The anrNoticedPid_ changes to invalid");
180 anrNoticedPid_ = -1;
181 }
182 MMI_HILOGI("SessionLost remove all Timers");
183 RemoveTimers(session);
184 }
185
SetANRNoticedPid(int32_t pid)186 int32_t ANRManager::SetANRNoticedPid(int32_t pid)
187 {
188 CALL_INFO_TRACE;
189 anrNoticedPid_ = pid;
190 return RET_OK;
191 }
192 } // namespace MMI
193 } // namespace OHOS
194