PrefaceLinux introduces Watchdog. In the Linux kernel, when Watchdog is started, a timer is set. If no write operation is performed to /dev/Watchdog within the timeout period, the system will restart. Watchdog implemented by timer belongs to the software level; Android has designed a software-level Watchdog to protect some important system services. When a failure occurs, the Android system will usually restart. Due to the existence of this mechanism, some system_server processes are often killed by Watchdog, causing the phone to restart. Today we will analyze the principle; 1. Detailed explanation of WatchDog startup mechanismThe ANR mechanism is for applications. For system processes, if they are "unresponsive" for a long time, the Android system has designed a WatchDog mechanism to control them. If the "unresponsive" delay is exceeded, the system WatchDog will trigger the suicide mechanism; Watchdog is a thread that inherits from Thread. In SystemServer.java, get the watchdog object through getInstance; 1. Start in SystemServer.java
Because it is a thread, all you need to do is start it; 2. View the construction method of WatchDog
Focus on two objects: mMonitorChecker and mHandlerCheckers The source of the mHandlerCheckers list elements: Import of construction objects: UiThread, IoThread, DisplatyThread, FgThread added External import: Watchdog.getInstance().addThread(handler); Source of mMonitorChecker list elements: External import: Watchdog.getInstance().addMonitor(monitor); Special note: addMonitor(new BinderThreadMonitor()); 3. Check the run method of WatchDog
Check the mHandlerCheckers list elements; 4. Check scheduleCheckLocked of HandlerChecker
When mMonitors.size() == 0: Mainly to check whether the elements in mHandlerCheckers have timed out, the method used is: mHandler.getLooper().getQueue().isPolling(); The list elements of the mMonitorChecker object must be greater than 0. At this time, the focus is on mHandler.postAtFrontOfQueue(this);
Listen to the monitor method, here is to monitor mMonitors, and the only one that can meet the conditions is: mMonitorChecker, for example: various services are added to the list through addMonitor;
The monitor method executed is very simple, for example ActivityManagerService:
Here we just check whether the system service is locked; Watchdog's inner class;
Here we just check that the number of executable threads contained in the process does not exceed mMaxThreads. If it exceeds the maximum value (31), we need to wait;
5. Exit after timeout
Kill the process you are in (system_server) and exit; 2. Principle Explanation1. All services that need to be monitored in the system call Watchdog's addMonitor to add Monitor Checker to the mMonitors List or addThread method to add Looper Checker to the mHandlerCheckers List; 2. When the Watchdog thread is started, it begins an infinite loop and its run method begins to execute;
3. If the timeout is reached and the HandlerChecker is still in an unfinished state (OVERDUE), get the blocked HandlerChecker through the getBlockedCheckersLocked() method, generate some descriptive information, save the log, including some runtime stack information. 4. Finally, kill the SystemServer process; SummarizeWatchdog is a thread used to monitor whether the system services are running normally and no deadlock occurs; HandlerChecker is used to check Handler and monitor; Monitor uses locks to determine whether there is a deadlock; If the timeout is 30 seconds, the log will be output, and if the timeout is 60 seconds, the system will restart. Watchdog will kill its own process, which means that the system_server process id will change at this time; This article is reproduced from the WeChat public account "Android Development Programming" |
You may have heard of many methods to identify re...
Having more children means more fighting, and thi...
[[147539]] If you are already a very good program...
Produced by: Science Popularization China Author:...
Every team is pursuing growth, but how is growth ...
"Dual SIM dual standby" may not be a com...
Last year, Qualcomm released the flagship SoC Sna...
1. Activity Background 1. Market conditions Pay a...
Recently, Guangzhou issued the latest revised &qu...
Follow the steps below to publish your own cocoap...
Fuxing Intelligent EMU On the Beijing-Zhangjiakou...
Recently, Chinese scientists have made a major br...
When Lucid unveiled the prototype of its new Air e...
1. Recent Algorithm Adjustments and Response Stra...
Li Xiaoyue's Xiaohongshu full-level short vid...