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