From ebfd0c82eb71e6aaeeaa002bd00d678e573ae3ef Mon Sep 17 00:00:00 2001 From: meng-xinhai Date: Thu, 1 Sep 2022 07:49:57 +0000 Subject: [PATCH 1/3] anr optimization Signed-off-by: meng-xinhai Change-Id: I56ddd1a198dc739349176c9f32f09ee94d86b57e --- service/event_dispatch/src/event_dispatch.cpp | 4 +- service/event_handler/include/anr_manager.h | 5 +- service/event_handler/src/anr_manager.cpp | 104 ++++++++++++------ .../message_handle/src/server_msg_handler.cpp | 3 +- service/monitor/src/event_monitor_handler.cpp | 2 +- util/socket/include/uds_session.h | 7 +- util/socket/src/uds_session.cpp | 37 +++++-- 7 files changed, 110 insertions(+), 52 deletions(-) mode change 100644 => 100755 service/event_dispatch/src/event_dispatch.cpp mode change 100644 => 100755 service/event_handler/include/anr_manager.h mode change 100644 => 100755 service/event_handler/src/anr_manager.cpp mode change 100644 => 100755 service/message_handle/src/server_msg_handler.cpp mode change 100644 => 100755 service/monitor/src/event_monitor_handler.cpp mode change 100644 => 100755 util/socket/include/uds_session.h mode change 100644 => 100755 util/socket/src/uds_session.cpp diff --git a/service/event_dispatch/src/event_dispatch.cpp b/service/event_dispatch/src/event_dispatch.cpp old mode 100644 new mode 100755 index ca7dda7793..5c92aa62a2 --- a/service/event_dispatch/src/event_dispatch.cpp +++ b/service/event_dispatch/src/event_dispatch.cpp @@ -112,7 +112,7 @@ void EventDispatch::HandlePointerEventInner(const std::shared_ptr MMI_HILOGE("Sending structure of EventTouch failed! errCode:%{public}d", MSG_SEND_FAIL); return; } - session->SaveANREvent(ANR_DISPATCH, point->GetId(), currentTime); + ANRMgr->AddTimer(ANR_DISPATCH, point->GetId(), currentTime, session); } #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_POINTER @@ -150,7 +150,7 @@ int32_t EventDispatch::DispatchKeyEventPid(UDSServer& udsServer, std::shared_ptr MMI_HILOGE("Sending structure of EventKeyboard failed! errCode:%{public}d", MSG_SEND_FAIL); return MSG_SEND_FAIL; } - session->SaveANREvent(ANR_DISPATCH, key->GetId(), currentTime); + ANRMgr->AddTimer(ANR_DISPATCH, key->GetId(), currentTime, session); return RET_OK; } #endif // OHOS_BUILD_ENABLE_KEYBOARD diff --git a/service/event_handler/include/anr_manager.h b/service/event_handler/include/anr_manager.h old mode 100644 new mode 100755 index 845b45e786..48def75385 --- a/service/event_handler/include/anr_manager.h +++ b/service/event_handler/include/anr_manager.h @@ -33,10 +33,13 @@ public: bool TriggerANR(int32_t type, int64_t time, SessionPtr sess); int32_t SetANRNoticedPid(int32_t anrPid); void OnSessionLost(SessionPtr session); + void AddTimer(int32_t type, int32_t id, int64_t currentTime, SessionPtr sess); + void MarkProcessed(int32_t eventType, int32_t eventId, SessionPtr sess); + void RemoveTimers(SessionPtr sess); private: int32_t anrNoticedPid_ { -1 }; - UDSServer *udsServer_ = nullptr;; + UDSServer *udsServer_ { nullptr }; }; } // namespace MMI } // namespace OHOS diff --git a/service/event_handler/src/anr_manager.cpp b/service/event_handler/src/anr_manager.cpp old mode 100644 new mode 100755 index a04ba19f05..2f33ea8a74 --- a/service/event_handler/src/anr_manager.cpp +++ b/service/event_handler/src/anr_manager.cpp @@ -13,12 +13,15 @@ * limitations under the License. */ -#include "ability_manager_client.h" #include "anr_manager.h" + +#include "ability_manager_client.h" #include "dfx_hisysevent.h" + #include "input_event_handler.h" #include "mmi_log.h" #include "proto.h" +#include "timer_manager.h" namespace OHOS { namespace MMI { @@ -26,6 +29,8 @@ namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "ANRManager" }; constexpr int64_t INPUT_UI_TIMEOUT_TIME = 5 * 1000000; const std::string FOUNDATION = "foundation"; +constexpr int32_t ANR_DISPATCH = 0; +constexpr int32_t ANR_MONITOR = 1; } // namespace void ANRManager::Init(UDSServer& udsServer) @@ -36,51 +41,82 @@ void ANRManager::Init(UDSServer& udsServer) udsServer_->AddSessionDeletedCallback(std::bind(&ANRManager::OnSessionLost, this, std::placeholders::_1)); } +void ANRManager::MarkProcessed(int32_t eventType, int32_t eventId, SessionPtr sess) +{ + CHKPV(sess); + std::list timerIds = sess->DelEvents(eventType, eventId); + for (int32_t item : timerIds) { + if (item != -1) { + TimerMgr->RemoveTimer(item); + } + } +} + +void ANRManager::RemoveTimers(SessionPtr sess) +{ + CHKPV(sess); + std::vector DispatchTimerIds = sess->GetTimerIds(ANR_DISPATCH); + for (int32_t item : DispatchTimerIds) { + if (item != -1) { + TimerMgr->RemoveTimer(item); + } + } + std::vector MonitorTimerIds = sess->GetTimerIds(ANR_MONITOR); + for (int32_t item : MonitorTimerIds) { + if (item != -1) { + TimerMgr->RemoveTimer(item); + } + } +} + +void ANRManager::AddTimer(int32_t type, int32_t id, int64_t currentTime, SessionPtr sess) +{ + CHKPV(sess); + if (sess->GetTokenType() != TokenType::TOKEN_HAP || sess->GetProgramName() == FOUNDATION) { + MMI_HILOGD("Ignore native event"); + return; + } + int32_t timerId = TimerMgr->AddTimer(INPUT_UI_TIMEOUT_TIME, 1, [this, type, sess]() { + CHKPV(sess); + sess->SetAnrStatus(type, true); + DfxHisysevent::ApplicationBlockInput(sess); + if (anrNoticedPid_ < 0) { + MMI_HILOGE("anrNoticedPid_ is invalid"); + return; + } + NetPacket pkt(MmiMessageId::NOTICE_ANR); + pkt << sess->GetPid(); + if (pkt.ChkRWError()) { + MMI_HILOGE("Packet write failed"); + return; + } + auto fd = udsServer_->GetClientFd(anrNoticedPid_); + if (!udsServer_->SendMsg(fd, pkt)) { + MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL); + return; + } + MMI_HILOGI("ANR remove all timers"); + RemoveTimers(sess); + }); + sess->SaveANREvent(type, id, currentTime, timerId); +} + bool ANRManager::TriggerANR(int32_t type, int64_t time, SessionPtr sess) { CALL_DEBUG_ENTER; CHKPF(udsServer_); CHKPF(sess); - MMI_HILOGD("Current time: %{public}" PRId64 "", time); + MMI_HILOGD("Current time:%{public}" PRId64 "", time); if (sess->GetTokenType() != TokenType::TOKEN_HAP || sess->GetProgramName() == FOUNDATION) { MMI_HILOGD("Native event"); return false; } - if (sess->CheckAnrStatus(type)) { MMI_HILOGW("application not responding"); return true; } - int64_t earliest; - if (sess->IsEventQueueEmpty(type)) { - earliest = time; - } else { - earliest = sess->GetEarliestEventTime(type); - } - if (time < (earliest + INPUT_UI_TIMEOUT_TIME)) { - sess->SetAnrStatus(type, false); - MMI_HILOGD("the event reports normally"); - return false; - } - sess->SetAnrStatus(type, true); - DfxHisysevent::ApplicationBlockInput(sess); - if (anrNoticedPid_ < 0) { - MMI_HILOGE("NoticedPid_ is invalid"); - return true; - } - NetPacket pkt(MmiMessageId::NOTICE_ANR); - pkt << sess->GetPid(); - if (pkt.ChkRWError()) { - MMI_HILOGE("Packet write key event failed"); - return true; - } - auto fd = udsServer_->GetClientFd(anrNoticedPid_); - if (!udsServer_->SendMsg(fd, pkt)) { - MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL); - return true; - } MMI_HILOGI("AAFwk send ANR process id succeeded"); - return true; + return false; } void ANRManager::OnSessionLost(SessionPtr session) @@ -88,9 +124,11 @@ void ANRManager::OnSessionLost(SessionPtr session) CALL_DEBUG_ENTER; CHKPV(session); if (anrNoticedPid_ == session->GetPid()) { - MMI_HILOGD("NoticedPid_ is invalid"); + MMI_HILOGD("anrNoticedPid_ is invalid"); anrNoticedPid_ = -1; } + MMI_HILOGI("SessionLost remove all Timers"); + RemoveTimers(session); } int32_t ANRManager::SetANRNoticedPid(int32_t pid) diff --git a/service/message_handle/src/server_msg_handler.cpp b/service/message_handle/src/server_msg_handler.cpp old mode 100644 new mode 100755 index e4def7005a..5b2d19f172 --- a/service/message_handle/src/server_msg_handler.cpp +++ b/service/message_handle/src/server_msg_handler.cpp @@ -17,6 +17,7 @@ #include +#include "anr_manager.h" #include "event_dump.h" #include "event_interceptor_handler.h" #include "event_monitor_handler.h" @@ -121,7 +122,7 @@ int32_t ServerMsgHandler::MarkProcessed(SessionPtr sess, NetPacket& pkt) MMI_HILOGE("Packet read data failed"); return PACKET_READ_FAIL; } - sess->DelEvents(eventType, eventId); + ANRMgr->MarkProcessed(eventType, eventId, sess); return RET_OK; } diff --git a/service/monitor/src/event_monitor_handler.cpp b/service/monitor/src/event_monitor_handler.cpp old mode 100644 new mode 100755 index fd53693bb7..83f3c2292e --- a/service/monitor/src/event_monitor_handler.cpp +++ b/service/monitor/src/event_monitor_handler.cpp @@ -198,7 +198,7 @@ void EventMonitorHandler::SessionHandler::SendToClient(std::shared_ptrGetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) { - session_->SaveANREvent(ANR_MONITOR, pointerEvent->GetId(), currentTime); + ANRMgr->AddTimer(ANR_MONITOR, pointerEvent->GetId(), currentTime, session_); } } diff --git a/util/socket/include/uds_session.h b/util/socket/include/uds_session.h old mode 100644 new mode 100755 index 5e387ca82a..56b2d6dfad --- a/util/socket/include/uds_session.h +++ b/util/socket/include/uds_session.h @@ -15,6 +15,7 @@ #ifndef UDS_SESSION_H #define UDS_SESSION_H +#include #include #include @@ -96,8 +97,9 @@ public: } void UpdateDescript(); - void SaveANREvent(int32_t type, int32_t id, int64_t time); - void DelEvents(int32_t type, int32_t id); + void SaveANREvent(int32_t type, int32_t id, int64_t time, int32_t timerId); + std::vector GetTimerIds(int32_t type); + std::list DelEvents(int32_t type, int32_t id); int64_t GetEarliestEventTime(int32_t type = 0) const; bool IsEventQueueEmpty(int32_t type = 0); @@ -113,6 +115,7 @@ protected: struct EventTime { int32_t id { 0 }; int64_t eventTime { 0 }; + int32_t timerId { -1 }; }; std::map> events_; std::map isAnrProcess_; diff --git a/util/socket/src/uds_session.cpp b/util/socket/src/uds_session.cpp old mode 100644 new mode 100755 index c3c625eba1..3c85128981 --- a/util/socket/src/uds_session.cpp +++ b/util/socket/src/uds_session.cpp @@ -131,33 +131,45 @@ bool UDSSession::SendMsg(NetPacket& pkt) const return SendMsg(buf.Data(), buf.Size()); } -void UDSSession::SaveANREvent(int32_t type, int32_t id, int64_t time) +void UDSSession::SaveANREvent(int32_t type, int32_t id, int64_t time, int32_t timerId) { CALL_DEBUG_ENTER; - if (GetTokenType() != TokenType::TOKEN_HAP || GetProgramName() == FOUNDATION) { - MMI_HILOGD("Is native event"); - return; - } - EventTime eventTime = {id, time}; + EventTime eventTime = { id, time, timerId }; auto iter = events_.find(type); if (iter != events_.end()) { iter->second.push_back(eventTime); } } -void UDSSession::DelEvents(int32_t type, int32_t id) +std::vector UDSSession::GetTimerIds(int32_t type) +{ + auto iter = events_.find(type); + if (iter == events_.end()) { + MMI_HILOGE("events_ have no event type:%{public}d", type); + return {}; + } + std::vector Timers; + for (auto &item : iter->second) { + Timers.push_back(item.timerId); + } + return Timers; +} + +std::list UDSSession::DelEvents(int32_t type, int32_t id) { CALL_DEBUG_ENTER; - MMI_HILOGE("del events id:%{public}d type:%{public}d", id, type); + MMI_HILOGE("Delete events id:%{public}d type:%{public}d", id, type); auto iter = events_.find(type); if (iter == events_.end()) { - MMI_HILOGE("events_ not contain event type:%{public}d", type); - return; + MMI_HILOGE("events_ have no event type:%{public}d", type); + return {}; } auto &events = iter->second; int32_t count = 0; + std::list timerIds; for (auto &item : events) { ++count; + timerIds.push_back(item.timerId); if (item.id == id) { events.erase(events.begin(), events.begin() + count); MMI_HILOGD("Delete events"); @@ -166,17 +178,18 @@ void UDSSession::DelEvents(int32_t type, int32_t id) } if (events.empty()) { isAnrProcess_[type] = false; - return; + return timerIds; } int64_t endTime = 0; if (!AddInt64(events.begin()->eventTime, INPUT_UI_TIMEOUT_TIME, endTime)) { MMI_HILOGE("The addition of endTime overflows"); - return; + return timerIds; } auto currentTime = GetSysClockTime(); if (currentTime < endTime) { isAnrProcess_[type] = false; } + return timerIds; } int64_t UDSSession::GetEarliestEventTime(int32_t type) const -- Gitee From 50bfce999448f8af6df8d28ab1ceefa07ebda9d9 Mon Sep 17 00:00:00 2001 From: meng-xinhai Date: Thu, 1 Sep 2022 08:01:57 +0000 Subject: [PATCH 2/3] anr optimization Signed-off-by: meng-xinhai Change-Id: Ic71f6cba57603bdb0ad72e1fa45ce088b805d91f --- service/event_handler/src/anr_manager.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/service/event_handler/src/anr_manager.cpp b/service/event_handler/src/anr_manager.cpp index 2f33ea8a74..5b41a33e51 100755 --- a/service/event_handler/src/anr_manager.cpp +++ b/service/event_handler/src/anr_manager.cpp @@ -96,7 +96,12 @@ void ANRManager::AddTimer(int32_t type, int32_t id, int64_t currentTime, Session return; } MMI_HILOGI("ANR remove all timers"); - RemoveTimers(sess); + std::vector TimerIds = sess->GetTimerIds(type); + for (int32_t item : TimerIds) { + if (item != -1) { + TimerMgr->RemoveTimer(item); + } + } }); sess->SaveANREvent(type, id, currentTime, timerId); } @@ -112,7 +117,7 @@ bool ANRManager::TriggerANR(int32_t type, int64_t time, SessionPtr sess) return false; } if (sess->CheckAnrStatus(type)) { - MMI_HILOGW("application not responding"); + MMI_HILOGW("Application not responding"); return true; } MMI_HILOGI("AAFwk send ANR process id succeeded"); -- Gitee From 1b57b33604fe93cec81902ba38ce7fd8650cecc9 Mon Sep 17 00:00:00 2001 From: meng-xinhai Date: Thu, 1 Sep 2022 08:15:04 +0000 Subject: [PATCH 3/3] anr optimization Signed-off-by: meng-xinhai Change-Id: I511c017fda1c903be36e2522344da35fb0f6a09c --- service/event_dispatch/src/event_dispatch.cpp | 0 service/event_handler/include/anr_manager.h | 0 service/event_handler/src/anr_manager.cpp | 0 service/message_handle/src/server_msg_handler.cpp | 0 service/monitor/src/event_monitor_handler.cpp | 0 util/socket/include/uds_session.h | 0 util/socket/src/uds_session.cpp | 0 7 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 service/event_dispatch/src/event_dispatch.cpp mode change 100755 => 100644 service/event_handler/include/anr_manager.h mode change 100755 => 100644 service/event_handler/src/anr_manager.cpp mode change 100755 => 100644 service/message_handle/src/server_msg_handler.cpp mode change 100755 => 100644 service/monitor/src/event_monitor_handler.cpp mode change 100755 => 100644 util/socket/include/uds_session.h mode change 100755 => 100644 util/socket/src/uds_session.cpp diff --git a/service/event_dispatch/src/event_dispatch.cpp b/service/event_dispatch/src/event_dispatch.cpp old mode 100755 new mode 100644 diff --git a/service/event_handler/include/anr_manager.h b/service/event_handler/include/anr_manager.h old mode 100755 new mode 100644 diff --git a/service/event_handler/src/anr_manager.cpp b/service/event_handler/src/anr_manager.cpp old mode 100755 new mode 100644 diff --git a/service/message_handle/src/server_msg_handler.cpp b/service/message_handle/src/server_msg_handler.cpp old mode 100755 new mode 100644 diff --git a/service/monitor/src/event_monitor_handler.cpp b/service/monitor/src/event_monitor_handler.cpp old mode 100755 new mode 100644 diff --git a/util/socket/include/uds_session.h b/util/socket/include/uds_session.h old mode 100755 new mode 100644 diff --git a/util/socket/src/uds_session.cpp b/util/socket/src/uds_session.cpp old mode 100755 new mode 100644 -- Gitee