System startup process diagram:
All service in the framework layer is run in the systemserver process; The systemserver process is created by the zygote process.
The systemserver process starts in two processes init1 creating service and process state objects, init2 creating the service for the framework layer, adding it to the ServiceManager, and finally starting launcher;
Android provides a watchdog class to monitor whether a service is working properly and is started in Systemserver.
Let's look at the watchdog process in Systemserver.
Systemserver.java:
Copy Code code as follows:
public void Run () {
Initialize watchdog incoming service as parameters
Watchdog.getinstance (). Init (context, battery, power, alarm,
Activitymanagerservice.self ());
Start watchdog
Watchdog.getinstance (). Start ();
}
Watchdog class implementation
Class inheritance structure:
See watchdog is a thread that runs in the Systemserver process, single case mode;
The Heartbeathandler processes the object (Service) that receives the monitor, runs in the main thread;
Monitor provides monitoring interface, accept monitoring object to implement this interface;
Xxxservice the specific implementation of the detection object.
Execution process:
External interface
Class
Copy Code code as follows:
public void init (context, batteryservice battery,
Powermanagerservice Power, Alarmmanagerservice alarm,
Activitymanagerservice activity) {
Store service objects, running in the same process
Mresolver = Context.getcontentresolver ();
Mbattery = battery; MPower = power;
Malarm = alarm; Mactivity = activity;
Register for Broadcast
Context.registerreceiver (New Rebootreceiver (),
New Intentfilter (reboot_action));
Mrebootintent = Pendingintent.getbroadcast (Context,
, New Intent (Reboot_action), 0);
......
Boot time
Mboottime = System.currenttimemillis ();
}
To register a monitoring object:
Copy Code code as follows:
public void Addmonitor (monitor monitor) {
Synchronized (this) {
To add a monitor object to the list
Mmonitors.add (monitor);
}
}
Search for a call to this function to indicate that it is being monitored; see the Monitor interface for implementing watchdog in the following service:
Activitymanagerservice
Inputmanagerservice
Networkmanagementservice
Powermanagerservice
Windowmanagerservice
All have calls: Watchdog.getinstance (). Addmonitor (this);
Watchdog thread execution function:
Copy Code code as follows:
public void Run () {
Boolean waitedhalf = false;
while (true) {
Monitoring completion Mark
mcompleted = false;
Send a monitoring message
Mhandler.sendemptymessage (MONITOR);
Synchronized (this) {
Long timeout = time_to_wait;
Long start = Systemclock.uptimemillis ();
while (Timeout > 0 &&!mforcekillsystem) {
Hibernate wait for check results
Wait (timeout); Notifyall () is called when Mforcekillsystem is set
Timeout = time_to_wait-(Systemclock.uptimemillis ()-start);
}
if (mcompleted &&!mforcekillsystem) {
Check results OK
Waitedhalf = false;
Continue
}
I'm checking it out once.
if (!waitedhalf) {
Activitymanagerservice.dumpstacktraces (True, PIDs, NULL, NULL,
Native_stacks_of_interest);
Waitedhalf = true;
Continue
}
}
Indicates a problem with the monitor object
If we got here, that's means that's system is most likely hung.
The collect stack traces from all threads to the system process.
Then kill this process so the system would restart.
Saving 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, a circular message is sent to determine whether the flag is normal and whether the object is working properly.
If the monitor object is not working properly, collect the important stack information and save it, then restart the systemserver.
Monitoring the processing of messages:
In Heartbeathandler, look at the message handler function.
Copy Code code as follows:
public void Handlemessage (msg) {
Switch (msg.what) {
Case MONITOR: {
If we should force a reboot.
Monitor whether the object is working properly ...
final int size = Mmonitors.size ();
for (int i = 0; i < size; i++) {
Monitor interface for call monitoring object
Mcurrentmonitor = Mmonitors.get (i);
Mcurrentmonitor.monitor ();
}
Walking here indicates that the monitoring object is normal.
Synchronized (watchdog.this) {
Mcompleted = true;
Mcurrentmonitor = null;
}
} break;
}
}
To determine whether the monitoring object is working correctly, monitor the interface implemented by calling the monitoring object to see how this interface should be implemented.
In Powermanagerservice:
public void Monitor () {
To determine whether the service has a deadlock, if a deadlock occurs, the program will wait here//mainly because of synchronization between threads cause deadlock
Synchronized (mlocks) {}
}
The above is watchdog monitoring service is normal work process, we can also use watchdog to monitor other resources such as memory and other uses.
This watchdog provides us with a way of thinking, a framework, a monitoring mechanism for the normal operation of the program or the normal use of resources.