ANR時間區別即是指當前此次的事件dispatch過程當中執行findFocusedWindowTargetsLocked()方法到下一次執行resetANRTimeoutsLocked()的時間區間. 如下5個時機會reset. 都位於InputDispatcher.cpp文件:shell
簡單來講, 上面方法主要是對應如下4個場景,會有機會執行resetANRTimeoutsLocked重置timeout時間:app
當InputDispatcher線程 findFocusedWindowTargetsLocked()過程調用到handleTargetsNotReadyLocked,且知足超時5s的狀況則會調用onANRLocked().spa
經過將InputManagerService加入到Watchdog的monitor隊列,定時監測是否發生死鎖. 整個監測過涉及EventHub, InputReader, InputDispatcher, InputManagerService的死鎖監測. 監測的原理很簡單,經過嘗試獲取鎖並釋放鎖的方式.線程
可經過adb shell dumpsys input
來查看手機當前的input狀態, 輸出內容分別爲EventHub.dump(), InputReader.dump(),InputDispatcher.dump()這3類,另外若是發生過input ANR,那麼也會輸出上一個ANR的狀態.code
其中mPendingEvent表明的當下正在處理的事件.隊列
由小節[3.5] InputMonitor.notifyANR完成, 當發生ANR時system log中會出現如下信息, 而且TAG=WindowManager:事件
Input event dispatching timed out xxx. Reason: + reason
, 其中xxx取值:rem
windowState.mAttrs.getTitle()
appWindowToken.stringName
至於Reason主要有如下類型:get
由checkWindowReadyForMoreInputLocked完成, ANR reason主要有如下幾類:input
[targetType]
window is paused.[targetType]
window’s input channel is not registered with the input dispatcher. The window may be in the process of being removed.[targetType]
window’s input connection is [Connection.Status]
. The window may be in the process of being removed.[targetType]
window’s input channel is full. Outbound queue length: [outboundQueue長度]
. Wait queue length: [waitQueue長度]
.[targetType]
window has not finished processing all of the input events that were previously delivered to it. Outbound queue length: [outboundQueue長度]
. Wait queue length: [waitQueue長度]
.[targetType]
window has not finished processing certain input events that were delivered to it over 500ms ago. Wait queue length: [waitQueue長度]
. Wait queue head age: [等待時長]
.其中
另外, findFocusedWindowTargetsLocked, findTouchedWindowTargetsLocked這兩個方法中能夠經過實現 updateDispatchStatisticsLocked()來分析anr問題.
由dropInboundEventLocked完成,輸出事件丟棄的緣由:
其餘: