Android Watchdog Implementation Analysis

Source: Internet
Author: User

System Startup Process diagram:

All services in the Framework layer are run in the SystemServer process, and the SystemServer process is created by the Zygote process.
SystemServer process initiation involves two processes: init1 creates a Service and a process state object; init2 creates a Service at the Framework layer, adds it to ServiceManager, and finally starts launcher;
Android provides the Watchdog class, which is used to monitor whether the Service is running normally and started in SystemServer.
Let's take a look at the Watchdog process in SystemServer.
SystemServer. java: Copy codeThe Code is as follows: public void run (){
// Initialize Watchdog and input each Service as a parameter
Watchdog. getInstance (). init (context, battery, power, alarm,
ActivityManagerService. self ());
// Start Watchdog
Watchdog. getInstance (). start ();
}

Watchdog class implementation
Class inheritance structure:

Watchdog is a Thread running in the SystemServer process, Singleton mode;
HeartbeatHandler processes Monitored Objects (services) and runs in the main thread;
Monitor provides a monitoring interface, which is implemented by monitoring objects;
XXXService.
Execution Process:

External Interface
Initialization:Copy codeThe Code is as follows: public void init (Context context, BatteryService battery,
PowerManagerService power, AlarmManagerService alarm,
ActivityManagerService activity ){
// Store the Service object and run in the same process
MResolver = context. getContentResolver ();
MBattery = battery; mPower = power;
MAlarm = alarm; mActivity = activity;
// Register Broadcast
Context. registerReceiver (new RebootReceiver (),
New IntentFilter (REBOOT_ACTION ));
MRebootIntent = PendingIntent. getBroadcast (context,
, New Intent (REBOOT_ACTION), 0 );
......
// Start time
MBootTime = System. currentTimeMillis ();
}

Register the Monitored object:Copy codeThe Code is as follows: public void addMonitor (Monitor monitor ){
Synchronized (this ){
// Add the Monitored object to the list
MMonitors. add (monitor );
}
}

Search for a call to this function, indicating that the function is monitored. You can see the Monitor interface that implements Watchdog in the following Service:
ActivityManagerService
InputManagerService
NetworkManagementService
PowerManagerService
WindowManagerService
All are called: Watchdog. getInstance (). addMonitor (this );
Watchdog thread execution function:Copy codeThe Code is as follows: public void run (){
Boolean waitedHalf = false;
While (true ){
// Indicator of monitoring completion
MCompleted = false;
// Send monitoring messages
MHandler. sendEmptyMessage (MONITOR );
Synchronized (this ){
Long timeout = TIME_TO_WAIT;
Long start = SystemClock. uptimeMillis ();
While (timeout> 0 &&! MForceKillSystem ){
// Wait for the result of sleep
Wait (timeout); // policyall () is called when mForceKillSystem is set
Timeout = TIME_TO_WAIT-(SystemClock. uptimeMillis ()-start );
}
If (mCompleted &&! MForceKillSystem ){
// Check result OK
WaitedHalf = false;
Continue;
}
// Check once
If (! WaitedHalf ){
ActivityManagerService. dumpStackTraces (true, pids, null, null,
NATIVE_STACKS_OF_INTEREST );
WaitedHalf = true;
Continue;
}
}
// Indicates that the Monitored object is faulty.
// If we got here, that means that the system is most likely hung.
// First collect stack traces from all threads of the system process.
// Then kill this process so that the system will restart.
// Save stack information
......
// Only kill the process if the debugger is not attached.
If (! Debug. isDebuggerConnected ()){
If (SystemProperties. getInt ("sys. watchdog. disabled", 0) = 0 ){
// Kill the current process SystemServer
Process. killProcess (Process. myPid ());
System. exit (10 );
}
}
WaitedHalf = false;
}
}

In this run function, messages are sent cyclically to determine whether the flag is normal and whether the object is normal.
If the Monitored object does not work properly, collect important stack information and save the information, and restart SystemServer.
Processing of monitoring messages:
It is done in HeartbeatHandler to see the message processing function.Copy codeThe Code is as follows: public void handleMessage (Message msg ){
Switch (msg. what ){
Case MONITOR :{
// See if we shoshould force a reboot.
// Check whether the Monitored object is working properly ......
Final int size = mMonitors. size ();
For (int I = 0; I <size; I ++ ){
// Call the monitor interface of the monitoring object
MCurrentMonitor = mMonitors. get (I );
MCurrentMonitor. monitor ();
}
// The Monitored object is normal.
Synchronized (Watchdog. this ){
MCompleted = true;
MCurrentMonitor = null;
}
} Break;
}
}

Determine whether the monitoring object is working normally. Call the monitor interface implemented by the monitoring object to see how this interface is executed.
In PowerManagerService:
Public void monitor (){
// Determine whether the Service has a deadlock. If a deadlock occurs, the program will wait here. // It is mainly caused by the synchronization problem between threads.
Synchronized (mLocks ){}
}
The above is the process for Watchdog to monitor whether the Service works normally. We can also use Watchdog to monitor other resources, such as memory usage.
This Watchdog provides us with a way of thinking, a framework, a monitoring mechanism for normal program running or normal resource usage.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.