当前位置:网站首页>Anr principle and Practice

Anr principle and Practice

2022-07-07 06:51:00 Little xian

Broadcast ANR Design principle

  1. Only in an orderly broadcast ANR Monitoring of
  2. AMS Responsible for distributing orderly broadcasts , And set timeout monitoring alarm during distribution , If the broadcast receiver processes the message within the timeout period and AMS If the communication is cancelled, this timeout monitoring will not happen ANR, Otherwise, it will be judged ANR.
  3. AMS It's used Binder The mechanism and APP Process of communication , When receiving broadcast data , Will encapsulate the data and sendMessage To the main thread message queue , Main thread processing Msg, The final call receiver.onReceive Method .( When there are time-consuming tasks in front of the message queue of the main thread , Or other processes have time-consuming tasks , Affect the scheduling of current events , It's not that the event itself takes too long , Will also trigger ANR)

Service ANR Design principle

Input ANR Design principle

ANR Dump technological process

  1. Filter out special scenes ( is dump, Shutting down , is crash, Being used by the system kill)
  2. Find what you need Dump The process of , It mainly includes ANR The parent of the process ,SystemServer process , high CPU Of ( most 5 individual ) process
  3. send out SIGQUIT Signal the need Dump The process of
     Insert picture description here
  4. Processes SignalCatcher Thread collects current virtual machine information , Memory information ,class Information ,GC Information , Then suspend all threads , Get the stack information of the thread
  5. Finally, it's piped to SystemServer, Append write to Trace In file

Application detection ANR

  1. watchdog
  2. monitor SIGNALQUIT The signal , Determine your progress ANR When ,Java Get stack information at the end , Or reflection calls inside the virtual machine Dump Stack interface of thread , Get the data and output it to your own directory .
    Implementation reference Xcrash:https://github.com/iqiyi/xCrash/blob/master/README.zh-CN.md

obtain ANR Info

  1. adopt ActivityManagerService.getProcessesInErrorState, Traverse mLruProcesses, And according to the current abnormal state of the process, such as crash perhaps anr type , Return to concrete ProcessErrorStateInfo

ANR The time-consuming situation in the message queue before the occurrence

  1. At present Trace The business where the stack is located takes a lot of time ;
  2. At present Trace The business where the stack is located is not time consuming , But historical scheduling has a serious time-consuming ;
  3. At present Trace The business where the stack is located is not time consuming , But historical scheduling has many messages that take time ;
  4. At present Trace The business where the stack is located is not time consuming , But there are a large number of repeated messages in historical scheduling ( Business sends messages frequently );
  5. At present Trace Stack business logic is not time consuming , But other threads have serious resource preemption , Such as IO,Mem,CPU;
  6. At present Trace Stack business logic is not time consuming , But other processes have serious resource preemption , Such as IO,Mem,CPU;

ANR Log semantic analysis

  1. Load: 2.62 / 2.55 / 2.25
    Express :1、5、15 In use and waiting for use within minutes CPU The average number of active processes
  2. CPU usage from 0ms to 1987ms later (2020-03-10 08:31:55.169 to 2020-03-10 08:32:17.156)
    indicate : Load information is captured in ANR After that 0~1987ms. It also points out that ANR The timing of the :2020-03-10 08:31:55.169
    If it is CPU usage xxx awake Said is ANR Previous system load information
  3. 41% 2080/system_server: 28% user + 12% kernel / faults: 76445 minor 180 major 26% 9378/com.xiaomi.store: 20% user + 6.8% kernel / faults: 68408 minor 68 major
    Express : Each process takes up CPU Details of
  4. 66% TOTAL: 20% user + 15% kernel + 28% iowait + 0.7% irq + 0.7% softirq
    Express : All processes occupy CPU Information about

ANR keyword

a. user: User mode ,kernel: Kernel mode 
b. faults: Memory page missing ,minor—— Slight ,major—— severe , You need to take data from disk 
​c. iowait:IO Use ( wait for ) Proportion 
​d. irq: Hard interrupt ,softirq: Soft interrupt 
e. utm: user space time,stm:  kernel space time

Reference resources :

  1. Today's headline ANR Optimization Practice Series - Design principles and influencing factors
  2. Today's headline ANR Optimization Practice Series - Monitoring tools and analysis ideas
  3. Today's headline ANR Optimization Practice Series sharing - A collection of case studies
  4. Android Source code analysis four component series ( 7、 ... and )— Broadcast sending process
  5. ANR Analyze comprehensively
  6. How to analyze ANR problem
原网站

版权声明
本文为[Little xian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070227549754.html