Input ANR处理流程

ANR时间区别即是指当前此次的事件dispatch过程当中执行findFocusedWindowTargetsLocked()方法到下一次执行resetANRTimeoutsLocked()的时间区间. 如下5个时机会reset. 都位于InputDispatcher.cpp文件:shell

  • resetAndDropEverythingLocked
  • releasePendingEventLocked
  • setFocusedApplication
  • dispatchOnceInnerLocked
  • setInputDispatchMode

简单来讲, 上面方法主要是对应如下4个场景,会有机会执行resetANRTimeoutsLocked重置timeout时间:app

  • 解冻屏幕, 系统开/关机的时刻点 (thawInputDispatchingLw, setEventDispatchingLw)
  • wms聚焦app的改变 (WMS.setFocusedApp, WMS.removeAppToken)
  • 设置input filter的过程 (IMS.setInputFilter)
  • 再次分发事件的过程(dispatchOnceInnerLocked)

当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表明的当下正在处理的事件.队列

ANR分类

由小节[3.5] InputMonitor.notifyANR完成, 当发生ANR时system log中会出现如下信息, 而且TAG=WindowManager:事件

Input event dispatching timed out xxx. Reason: + reason, 其中xxx取值:rem

  • 窗口类型: sending to windowState.mAttrs.getTitle()
  • 应用类型: sending to application appWindowToken.stringName
  • 其余类型: 则为空.

至于Reason主要有如下类型:get

5.1.1 reason类型

由checkWindowReadyForMoreInputLocked完成, ANR reason主要有如下几类:input

  1. 无窗口, 有应用:Waiting because no window has focus but there is a focused application that may eventually add a window when it finishes starting up.
  2. 窗口暂停: Waiting because the [targetType] window is paused.
  3. 窗口未链接: Waiting because the [targetType] window’s input channel is not registered with the input dispatcher. The window may be in the process of being removed.
  4. 窗口链接已死亡:Waiting because the [targetType] window’s input connection is [Connection.Status]. The window may be in the process of being removed.
  5. 窗口链接已满:Waiting because the [targetType] window’s input channel is full. Outbound queue length: [outboundQueue长度]. Wait queue length: [waitQueue长度].
  6. 按键事件,输出队列或事件等待队列不为空:Waiting to send key event because the [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长度].
  7. 非按键事件,事件等待队列不为空且头事件分发超时500ms:Waiting to send non-key event because the [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: [等待时长].

其中

  • targetType: 取值为”focused”或者”touched”
  • Connection.Status: 取值为”NORMAL”,”BROKEN”,”ZOMBIE”

另外, findFocusedWindowTargetsLocked, findTouchedWindowTargetsLocked这两个方法中能够经过实现 updateDispatchStatisticsLocked()来分析anr问题.

drop事件分类

由dropInboundEventLocked完成,输出事件丢弃的缘由:

  1. DROP_REASON_POLICY: “inbound event was dropped because the policy consumed it”;
  2. DROP_REASON_DISABLED: “inbound event was dropped because input dispatch is disabled”;
  3. DROP_REASON_APP_SWITCH: “inbound event was dropped because of pending overdue app switch”;
  4. DROP_REASON_BLOCKED: “inbound event was dropped because the current application is not responding and the user has started interacting with a different application””;
  5. DROP_REASON_STALE: “inbound event was dropped because it is stale”;

其余:

  • doDispatchCycleFinishedLockedInterruptible的过程, 会记录分发时间超过2s的事件,
  • findFocusedWindowTargetsLocked的过程, 能够统计等待时长信息.
相关文章
相关标签/搜索