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:

Public VoidRun (){//Initialize watchdog and input each service as a parameterWatchdog. getinstance (). INIT (context, battery, power, alarm, activitymanagerservice. Self ());//Start WatchdogWatchdog. getinstance (). Start ();}

 

WatchdogClass 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:

 Public   Void  Init (context, batteryservice battery, powermanagerservice power, alarmmanagerservice alarm, activitymanagerservice activity ){  // Stores service objects and runs 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,  0 , New Intent (reboot_action ), 0  );......  //  Start Time Mboottime = System. currenttimemillis ();} 

 

Register the Monitored object:

Public VoidAddmonitor (monitor) {synchronized (This){//Add a Monitored object to the listMmonitors. 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 );

WatchdogThread execution function:

 Public   Void  Run () {Boolean waitedhalf = False  ;  While ( True  ){  //  Monitoring completion mark Mcompleted =False  ;  //  Send monitoring messages  Mhandler. sendemptymessage (MONITOR); synchronized (  This  ){  Long Timeout = Time_to_wait;  Long Start = Systemclock. uptimemillis ();  While (Timeout> 0 &&!Mforcekillsystem ){  //  Sleep wait check result 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.

 Public   Void Handlemessage (Message MSG ){  Switch  (Msg. What ){  Case  Monitor :{  //  See if we shoshould force a reboot.  //  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 ();}  //  It indicates that 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 VoidMonitor (){//Determine whether a service is deadlocked. If a deadlock occurs,ProgramWill be waiting here
// Deadlock caused by inter-Thread SynchronizationSynchronized (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.

 

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.