Input ANR处理流程

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

  • resetAndDropEverythingLocked
  • releasePendingEventLocked
  • setFocusedApplication
  • dispatchOnceInnerLocked
  • setInputDispatchMode

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

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

当InputDispatcher线程 findFocusedWindowTargetsLocked()过程调用到handleTargetsNotReadyLocked,且满足超时5s的情况则会调用onANRLocked().

通过将InputManagerService加入到Watchdog的monitor队列,定时监测是否发生死锁. 整个监测过涉及EventHub, InputReader, InputDispatcher, InputManagerService的死锁监测. 监测的原理很简单,通过尝试获取锁并释放锁的方式.

可通过adb shell dumpsys input来查看手机当前的input状态, 输出内容分别为EventHub.dump(), InputReader.dump(),InputDispatcher.dump()这3类,另外如果发生过input ANR,那么也会输出上一个ANR的状态.

其中mPendingEvent代表的当下正在处理的事件.

ANR分类

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

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

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

至于Reason主要有以下类型:

5.1.1 reason类型

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

  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的过程, 可以统计等待时长信息.

原文地址:https://www.cnblogs.com/krislight1105/p/10175306.html

时间: 2024-08-30 15:21:18

Input ANR处理流程的相关文章

Android4.0 input事件输入流程详解(中间层到应用层)

在Android系统中,类似于键盘按键.触摸屏等事件是由WindowManagerService服务来管理的,然后再以消息的形式来分发给应用程序进行处理.系统启动时,窗口管理服务也会启动,该服务启动过程中,会通过系统输入管理器InputManager来负责监控键盘消息.当某一个Activity激活时,会在该Service下注册一个接收消息的通道,表明可以处理具体的消息,然后当有消息时,InputManager就会分发给当前处于激活状态下的Activity进行处理. InputManager的启动

linux input输入子系统分析《四》:input子系统整体流程全面分析

1      input输入子系统整体流程 本节分析input子系统在内核中的实现,包括输入子系统(Input Core),事件处理层(Event Handler)和设备驱动层.由于上节代码讲解了设备驱动层的写法,因此在开头部分会从设备驱动层做为线索,分析输入子系统和事件处理层是如何配合的,最后从用户角度出发,从"/dev/input/*"接口如何使用输入子系统提供的服务. 既然需要详细分析,有一个这样的流程图能够帮助我们在被绕进代码的过程中,找到出口,你能够知道你现在位于代码框架的什

input 输入子系统分析

//input 输入子系统分析//刘术河2016.08.22 linux-2.6.39-at91-2016.08.11-lsh\drivers\input\Input.c该文件下有input_register_device 和 input_register_handler 函数接口0.先分析input.c的核心层架构 input_init(void) class_register(&input_class); //注册一个类 register_chrdev(INPUT_MAJOR, "i

Android异常分析(转)

关于异常 异常? 异常就是一种程序中没有预料到的问题,既然是没有预料到的,就可能不在原有逻辑处理范围内,脱离了代码控制,软件可能会出现各种奇怪的现象.比如:android系统常见异常现象有应用无响应.应用停止运行.冻屏.重启.死机等,这些异常系统有统一的异常处理机制,出现异常系统就会执行相应的操作,最终有相应的现象体现出来.另外,一些不在预料之中的界面显示问题,操作问题,运行卡顿问题等也可以归于异常,只不过这种异常是人为逻辑缺陷,对系统来说是正常的,但这些缺陷在异常现象中占比却相当大,直接体现出

Linux 输入子系统

Technorati 标签: Kernel 输入子系统 Input      在Linux中,输入设备(如按键.键盘.触摸屏.鼠标等)是典型的字符设备,其一般的工作机理,是底层在按键.触摸时,触发一个中断,或者驱动通过定时器定时查询,通过这两种方式通知CPU,CPU然后通过SPI.I2C或I/O接口读取键值.坐标等数据,放入缓冲区,字符设备驱动管理该缓冲区,向上提供read接口供应用程序使用.      在上述的工作流程中,只有终端.读取数值是根具体硬件设备相关,而输入事件的缓冲区管理以及字符设

awk(1)-简述

1.概述 AWK is a programming language designed for text processing and typically used as a data extraction and reporting tool. AWK是一种设计用于文本处理的编程语言,通常用作数据提取和报告工具. awk程序是有一个或多个模式-动作语句构成的. 如:awk 'pattern {action} pattern {action}..'input 在一些语句中,pattern可以被省

Python学习day01

首先和大家说说为什么选择了Python 目前的工作内容是Linux系统运维和业务技术支持,自动化运维尤其这一两年吵的火热,本人shell水平也很一般了,所以还是非常想学一门语言来帮助自己的工作.自动化运维也是一个趋势,我也相信以后只会系统技术的运维路肯定会越走越窄.Python又被叫作"胶水语言". Python的优势 非常丰富的类库直接使用 Linux系统自带python 以上两点难道还不够吗? Python的安装 windows安装 Linux 安装 变量 变量是对象的引用,变量只

《Spring实战》学习笔记-第八章:使用Spring Web Flow

第四版的第八章内容与第三版基本一致. 本章内容: 创建会话式web应用程序 定义流程状态和行为 保护web流程 互联网的一个奇特之处就在于它很容易让人迷失.有如此多的内容可以查看和阅读,而超链接是其强大魔力的核心所在. 有时候,web应用程序需要控制web冲浪者的导向,引导他们一步步地访问应用.比如电子商务网站的付款流程,从购物车开始,应用程序会引导你依次经过配送详情.账单信息以及最终的订单确认. Spring Web Flow是一个web框架,它适用于元素规定流程运行的程序.本章中,我们将会探

2018年ElasticSearch6.2.2教程ELK搭建日志采集分析系统(目录)

章节一  2018年 ELK课程计划和效果演示 1.课程安排和效果演示 简介:课程介绍和主要知识点说明,ES搜索接口演示,部署的ELK项目演示 章节二 elasticSearch 6.2版本基础讲解到阿里云部署实战 2.搜索引擎知识介绍和相关框架 简介:介绍搜索的基本概念,市面上主流的搜索框架elasticSearch和solr等对比 什么是搜索:在海量信息中获取我们想要的信息 3.新版本 elasticSearch 6.1.2介绍 简介:介绍ES的主要特点和使用场景,新特性讲解 4.windo